MfaRegisterQRGenerator { public static String getQRCode(String issuer, String accountName, String secretKey) throws WriterException, IOException { if(!StringUtils.hasText(issuer) || issuer.contains(":")) { throw new IllegalArgumentException("invalid issuer"); } if(!StringUtils.hasText(accountName) || accountName.contains(":")) { throw new IllegalArgumentException("invalid account name"); } QRCodeWriter writer = new QRCodeWriter(); BitMatrix qrBitMatrix = writer.encode(String.format( OTPAUTH_TOTP_URI, UriUtils.encode(issuer, STRING_ENCODING), UriUtils.encode(accountName, STRING_ENCODING), secretKey, UriUtils.encode(issuer, STRING_ENCODING)), BarcodeFormat.QR_CODE, 200, 200); BufferedImage qrImage = MatrixToImageWriter.toBufferedImage(qrBitMatrix); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(qrImage, "png", os); return Base64.getEncoder().encodeToString(os.toByteArray()); }  static String getQRCode(String issuer,
                                   String accountName,
                                   String secretKey); static String getQRCodePngDataUri(String issuer,
                                      String accountName,
                                      String secretKey);  }
@Test public void testQRCode() throws Exception { GoogleAuthenticator authenticator = new GoogleAuthenticator(); GoogleAuthenticatorKey key = authenticator.createCredentials(); String encodedQrCode = MfaRegisterQRGenerator.getQRCode("testIssuer", "accountName", key.getKey()); String result = decodeQrPng(encodedQrCode); String[] split = result.split("\\?"); assertEquals("otpauth: List<String> list = Arrays.asList(split[1].split("&")); assertTrue(list.contains("issuer=testIssuer")); assertTrue(list.contains("secret=" + key.getKey())); assertEquals(2, list.size()); }
@Test public void testQrWithSpecialChars() throws Exception { GoogleAuthenticator authenticator = new GoogleAuthenticator(); GoogleAuthenticatorKey key = authenticator.createCredentials(); String encodedQrCode = MfaRegisterQRGenerator.getQRCode("test=Issuer","account?&#Name", key.getKey()); String result = decodeQrPng(encodedQrCode); String[] split = result.split("\\?"); assertEquals("otpauth: List<String> list = Arrays.asList(split[1].split("&")); assertTrue(list.contains("issuer=test%3DIssuer")); assertTrue(list.contains("secret=" + key.getKey())); assertEquals(2, list.size()); }
@Test(expected = IllegalArgumentException.class) public void testQrWithColonIssuer() throws Exception { GoogleAuthenticator authenticator = new GoogleAuthenticator(); GoogleAuthenticatorKey key = authenticator.createCredentials(); String encodedQrCode = MfaRegisterQRGenerator.getQRCode("test:Issuer", "accountName", key.getKey()); }
@Test(expected = IllegalArgumentException.class) public void testQrWithColonAccountName() throws Exception { GoogleAuthenticator authenticator = new GoogleAuthenticator(); GoogleAuthenticatorKey key = authenticator.createCredentials(); String encodedQrCode = MfaRegisterQRGenerator.getQRCode("testIssuer", "accountName:", key.getKey()); }
@Test(expected = IllegalArgumentException.class) public void testEmptyIssuer() throws Exception { GoogleAuthenticator authenticator = new GoogleAuthenticator(); GoogleAuthenticatorKey key = authenticator.createCredentials(); String encodedQrCode = MfaRegisterQRGenerator.getQRCode("", "accountName", key.getKey()); }