Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate QR code image locally (for seurity reasons) #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<javadoc.plugin.version>3.2.0</javadoc.plugin.version>
<junit.version>4.13</junit.version>
<source.plugin.version>3.2.1</source.plugin.version>
<zxing.version>3.4.0</zxing.version>
</properties>

<dependencies>
Expand All @@ -102,6 +103,16 @@
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>${zxing.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>${zxing.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@

package com.warrenstrange.googleauth;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.apache.http.client.utils.URIBuilder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

Expand Down Expand Up @@ -132,7 +141,11 @@ private static String formatLabel(String issuer, String accountName)
* @return the Google Chart API call URL to generate a QR code containing
* the provided information.
* @see <a href="https://code.google.com/p/google-authenticator/wiki/KeyUriFormat">Google Authenticator - KeyUriFormat</a>
*
* @deprecated this method is deprecated in favor of {@link GoogleAuthenticatorQRGenerator#getOtpAuthQrByteArrayOutputStream}
* which does make user send secret over wire (generated URL by this method does that)
*/
@Deprecated
public static String getOtpAuthURL(String issuer,
String accountName,
GoogleAuthenticatorKey credentials)
Expand All @@ -143,6 +156,44 @@ public static String getOtpAuthURL(String issuer,
internalURLEncode(getOtpAuthTotpURL(issuer, accountName, credentials)));
}

/**
* Returns a {@link ByteArrayOutputStream} that contains a QR code that was generated locally
* and which can be loaded into the Google Authenticator application. The user scans this
* bar code with the application on their smart phones.
* <p>
* The current implementation supports the following features:
* <ul>
* <li>Label, made up of an optional issuer and an account name.</li>
* <li>Secret parameter.</li>
* <li>Issuer parameter.</li>
* </ul>
*
* @param issuer The issuer name. This parameter cannot contain the colon
* (:) character. This parameter can be null.
* @param accountName The account name. This parameter shall not be null.
* @param credentials The generated credentials. This parameter shall not be null.
* @return a byte array stream that contains a QR code image
* @throws IOException in case when buffered image could not be written into byte array stream
* @throws WriterException in case when QR bit matrix could not be prepared
*/
public static ByteArrayOutputStream getOtpAuthQrByteArrayOutputStream(String issuer,
String accountName,
GoogleAuthenticatorKey credentials)
throws IOException, WriterException
{
String otpAuthUri = getOtpAuthTotpURL(issuer, accountName, credentials);

QRCodeWriter qrWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrWriter.encode(otpAuthUri, BarcodeFormat.QR_CODE, 200, 200);

BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", outputStream);

return outputStream;
}

/**
* Returns the basic otpauth TOTP URI. This URI might be sent to the user via email, QR code or some other method.
* Use a secure transport since this URI contains the secret.
Expand Down