Skip to content

Commit

Permalink
Show qrcode img in case of client login - Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
devgurung committed Apr 11, 2019
1 parent 35fa4b8 commit 27875e7
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,20 @@ protected void createCommonAttributes(Theme theme, Locale locale, Properties mes
* @return Response object to be returned to the browser, never null
*/
protected Response processTemplate(Theme theme, String templateName, Locale locale) {
QRCodeGenerator qrCodeGenerator= new QRCodeGenerator();
String imgURL= Urls.themeRoot(uriInfo.getBaseUri()).getPath() + "/login/" + theme.getName();
String result = "";
try {
String result = freeMarker.processTemplate(attributes, templateName, theme);

if(!client.getClientId().equals("security-admin-console")) {
qrCodeGenerator.createQRImage(realm.getName() , client.getClientId() ,imgURL);
result= qrCodeGenerator.generateQRCodeHtml(imgURL);
}
else {
result = freeMarker.processTemplate(attributes, templateName, theme);
}


javax.ws.rs.core.MediaType mediaType = contentType == null ? MediaType.TEXT_HTML_UTF_8_TYPE : contentType;
Response.ResponseBuilder builder = Response.status(status == null ? Response.Status.OK : status).type(mediaType).language(locale).entity(result);
BrowserSecurityHeaderSetup.headers(builder, realm);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.forms.login.freemarker;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import org.keycloak.theme.FreeMarkerException;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeGenerator {

public static void createQRImage(String relamName, String clientId , String imgPath) throws FreeMarkerException {
String qrCodeText = relamName +":"+clientId;
String filePath = "themes/src/main/resources/theme/keycloak/login/resources/img"+"/JD.png";
int size = 500;
String fileType = "png";
File qrFile = new File(filePath);

// Create the ByteMatrix for the QR-Code that encodes the given String
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = null;
try {
byteMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size, hintMap);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Make the BufferedImage that are to hold the QRCode
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();

Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// Paint and save the image using the ByteMatrix
graphics.setColor(Color.BLACK);

for (int i = 0; i < matrixWidth; i++) {
for (int j = 0; j < matrixWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
try {
ImageIO.write(image, fileType, qrFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static String generateQRCodeHtml(String imgURL) throws FreeMarkerException {
String responseQRHtml ="<!DOCTYPE html><html><body><img src=\"" + imgURL +"/img/JD.png\" alt=\"Smiley face\" width=\"500\" height=500\"></body></html>\r\n";
return responseQRHtml;

}





}

0 comments on commit 27875e7

Please sign in to comment.