From 0cf8655153a7fcbabef914bb4171c35f13605a80 Mon Sep 17 00:00:00 2001 From: Okke Harsta Date: Tue, 13 Aug 2024 12:39:09 +0200 Subject: [PATCH] Added QR code generator for device flow --- pom.xml | 6 +++ .../manage/ServiceProviderTranslation.java | 3 ++ src/main/java/oidc/qr/QRCode.java | 12 ++++++ src/main/java/oidc/qr/QRGenerator.java | 28 ++++++++++++++ .../ServiceProviderTranslationTest.java | 4 +- src/test/java/oidc/qr/QRGeneratorTest.java | 37 +++++++++++++++++++ 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/main/java/oidc/qr/QRCode.java create mode 100644 src/main/java/oidc/qr/QRGenerator.java create mode 100644 src/test/java/oidc/qr/QRGeneratorTest.java diff --git a/pom.xml b/pom.xml index 39b5eafc..199d222c 100644 --- a/pom.xml +++ b/pom.xml @@ -118,6 +118,12 @@ bcprov-jdk18on 1.78.1 + + + com.google.zxing + javase + 3.5.3 + org.projectlombok lombok diff --git a/src/main/java/oidc/manage/ServiceProviderTranslation.java b/src/main/java/oidc/manage/ServiceProviderTranslation.java index 071eadea..b43b8c05 100644 --- a/src/main/java/oidc/manage/ServiceProviderTranslation.java +++ b/src/main/java/oidc/manage/ServiceProviderTranslation.java @@ -22,6 +22,9 @@ */ public class ServiceProviderTranslation { + private ServiceProviderTranslation() { + } + public static String translateServiceProviderEntityId(String entityId) { return entityId.replace("@", "@@").replaceAll(":", "@"); } diff --git a/src/main/java/oidc/qr/QRCode.java b/src/main/java/oidc/qr/QRCode.java new file mode 100644 index 00000000..3fa2707a --- /dev/null +++ b/src/main/java/oidc/qr/QRCode.java @@ -0,0 +1,12 @@ +package oidc.qr; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class QRCode { + + private String url; + private String image; +} diff --git a/src/main/java/oidc/qr/QRGenerator.java b/src/main/java/oidc/qr/QRGenerator.java new file mode 100644 index 00000000..e15cca7b --- /dev/null +++ b/src/main/java/oidc/qr/QRGenerator.java @@ -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); + //