Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-2683 Remove QRCodeResource and embed QR code in image
- Loading branch information
Showing
10 changed files
with
100 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 0 additions & 106 deletions
106
services/src/main/java/org/keycloak/services/resources/QRCodeResource.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,69 @@ | |||
/* | |||
* 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://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.utils; | |||
|
|||
import com.google.zxing.BarcodeFormat; | |||
import com.google.zxing.client.j2se.MatrixToImageWriter; | |||
import com.google.zxing.common.BitMatrix; | |||
import com.google.zxing.qrcode.QRCodeWriter; | |||
import org.keycloak.common.util.Base64; | |||
import org.keycloak.models.RealmModel; | |||
import org.keycloak.models.UserModel; | |||
import org.keycloak.models.utils.Base32; | |||
|
|||
import java.io.ByteArrayOutputStream; | |||
import java.net.URLEncoder; | |||
|
|||
/** | |||
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> | |||
*/ | |||
public class TotpUtils { | |||
|
|||
public static String encode(String totpSecret) { | |||
String encoded = Base32.encode(totpSecret.getBytes()); | |||
StringBuilder sb = new StringBuilder(); | |||
for (int i = 0; i < encoded.length(); i += 4) { | |||
sb.append(encoded.substring(i, i + 4 < encoded.length() ? i + 4 : encoded.length())); | |||
if (i + 4 < encoded.length()) { | |||
sb.append(" "); | |||
} | |||
} | |||
return sb.toString(); | |||
} | |||
|
|||
public static String qrCode(String totpSecret, RealmModel realm, UserModel user) { | |||
try { | |||
String keyUri = realm.getOTPPolicy().getKeyURI(realm, user, totpSecret); | |||
|
|||
int width = 246; | |||
int height = 246; | |||
|
|||
QRCodeWriter writer = new QRCodeWriter(); | |||
final BitMatrix bitMatrix = writer.encode(keyUri, BarcodeFormat.QR_CODE, width, height); | |||
|
|||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |||
MatrixToImageWriter.writeToStream(bitMatrix, "png", bos); | |||
bos.close(); | |||
|
|||
return Base64.encodeBytes(bos.toByteArray()); | |||
} catch (Exception e) { | |||
throw new RuntimeException(e); | |||
} | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters