Skip to content

Commit

Permalink
Added QR code generator for device flow
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Aug 13, 2024
1 parent f18d9b0 commit 0cf8655
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/oidc/manage/ServiceProviderTranslation.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/
public class ServiceProviderTranslation {

private ServiceProviderTranslation() {
}

public static String translateServiceProviderEntityId(String entityId) {
return entityId.replace("@", "@@").replaceAll(":", "@");
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/oidc/qr/QRCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package oidc.qr;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class QRCode {

private String url;
private String image;
}
28 changes: 28 additions & 0 deletions src/main/java/oidc/qr/QRGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package oidc.qr;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import lombok.SneakyThrows;

import java.io.ByteArrayOutputStream;
import java.util.Base64;

public class QRGenerator {

private static final int IMAGE_SIZE = 400;

private QRGenerator() {
}

@SneakyThrows
public static QRCode qrCode(String url) {
BitMatrix matrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, IMAGE_SIZE, IMAGE_SIZE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(matrix, "png", bos);
//<img src="data:image/png;base64,iVBORw0KGgoA....>
String image = Base64.getEncoder().encodeToString(bos.toByteArray()); // base64 encode
return new QRCode(url, image);
}
}
4 changes: 2 additions & 2 deletions src/test/java/oidc/manage/ServiceProviderTranslationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void translateServiceProviderEntityId() {
s = ServiceProviderTranslation.translateServiceProviderEntityId(originalSPEntityID);
assertEquals(s, "https@//test@@@@@@test", s);

s = new ServiceProviderTranslation().translateClientId(s);
s = ServiceProviderTranslation.translateClientId(s);
assertEquals(originalSPEntityID, s);
}

Expand All @@ -27,7 +27,7 @@ public void translateClientId() {
String s = ServiceProviderTranslation.translateClientId("https@//test");
assertEquals("https://test", s);

s = new ServiceProviderTranslation().translateClientId("https@//test@@test");
s = ServiceProviderTranslation.translateClientId("https@//test@@test");
assertEquals(s, "https://test@test", s);
}
}
37 changes: 37 additions & 0 deletions src/test/java/oidc/qr/QRGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package oidc.qr;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;
import java.util.Base64;

import static org.junit.jupiter.api.Assertions.assertEquals;

class QRGeneratorTest {

@SneakyThrows
@Test
void qrCode() {
String url = "https://surf.nl";
QRCode qrCode = QRGenerator.qrCode(url);
assertEquals(url, qrCode.getUrl());
//Because we can
byte[] decoded = Base64.getDecoder().decode(qrCode.getImage());
BinaryBitmap binaryBitmap = new BinaryBitmap(
new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new ByteArrayInputStream(decoded)))));
int height = binaryBitmap.getHeight();
int width = binaryBitmap.getWidth();
assertEquals(400, width);
assertEquals(400, height);

Result result = new MultiFormatReader().decode(binaryBitmap);
assertEquals(url, result.getText());
}
}

0 comments on commit 0cf8655

Please sign in to comment.