From c00c712c3996e1f0c8cc3533063755f23ed22573 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 9 Oct 2014 02:46:17 -0300 Subject: [PATCH] KEYCLOAK-745 Display the realm name for OTP QR code scan --- .../freemarker/FreeMarkerAccountProvider.java | 2 +- .../account/freemarker/model/TotpBean.java | 19 +++++++++++-------- .../login/freemarker/model/TotpBean.java | 16 ++++++++-------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/FreeMarkerAccountProvider.java b/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/FreeMarkerAccountProvider.java index b42090f3e9..6d777478dd 100755 --- a/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/FreeMarkerAccountProvider.java +++ b/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/FreeMarkerAccountProvider.java @@ -131,7 +131,7 @@ public class FreeMarkerAccountProvider implements AccountProvider { attributes.put("account", new AccountBean(user, profileFormData)); break; case TOTP: - attributes.put("totp", new TotpBean(user, baseUri)); + attributes.put("totp", new TotpBean(realm, user, baseUri)); break; case SOCIAL: attributes.put("social", new AccountSocialBean(session, realm, user, uriInfo.getBaseUri(), stateChecker)); diff --git a/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/model/TotpBean.java b/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/model/TotpBean.java index 75c9fea725..33542e07c8 100755 --- a/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/model/TotpBean.java +++ b/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/model/TotpBean.java @@ -21,6 +21,7 @@ */ package org.keycloak.account.freemarker.model; +import org.keycloak.models.RealmModel; import org.keycloak.models.UserModel; import org.keycloak.models.utils.Base32; @@ -35,17 +36,19 @@ import java.security.SecureRandom; */ public class TotpBean { - private String totpSecret; - private String totpSecretEncoded; - private boolean enabled; - private String contextUrl; + private final String totpSecret; + private final String totpSecretEncoded; + private final boolean enabled; + private final String contextUrl; + private final String realmName; - public TotpBean(UserModel user, URI baseUri) { + public TotpBean(RealmModel realm, UserModel user, URI baseUri) { + this.realmName = realm.getName(); this.enabled = user.isTotp(); this.contextUrl = baseUri.getPath(); - totpSecret = randomString(20); - totpSecretEncoded = Base32.encode(totpSecret.getBytes()); + this.totpSecret = randomString(20); + this.totpSecretEncoded = Base32.encode(totpSecret.getBytes()); } private static String randomString(int length) { @@ -86,7 +89,7 @@ public class TotpBean { } public String getTotpSecretQrCodeUrl() throws UnsupportedEncodingException { - String contents = URLEncoder.encode("otpauth://totp/keycloak?secret=" + totpSecretEncoded, "utf-8"); + String contents = URLEncoder.encode("otpauth://totp/" + realmName + "?secret=" + totpSecretEncoded, "utf-8"); return contextUrl + "qrcode" + "?size=246x246&contents=" + contents; } diff --git a/forms/login-freemarker/src/main/java/org/keycloak/login/freemarker/model/TotpBean.java b/forms/login-freemarker/src/main/java/org/keycloak/login/freemarker/model/TotpBean.java index 497e75a7cf..7ac58644a9 100755 --- a/forms/login-freemarker/src/main/java/org/keycloak/login/freemarker/model/TotpBean.java +++ b/forms/login-freemarker/src/main/java/org/keycloak/login/freemarker/model/TotpBean.java @@ -35,19 +35,19 @@ import java.util.Random; */ public class TotpBean { - private String totpSecret; - private String totpSecretEncoded; - private boolean enabled; - private String contextUrl; - private String realmName; + private final String totpSecret; + private final String totpSecretEncoded; + private final boolean enabled; + private final String contextUrl; + private final String realmName; public TotpBean(RealmModel realm, UserModel user, URI baseUri) { this.realmName = realm.getName(); this.enabled = user.isTotp(); this.contextUrl = baseUri.getPath(); - - totpSecret = randomString(20); - totpSecretEncoded = Base32.encode(totpSecret.getBytes()); + + this.totpSecret = randomString(20); + this.totpSecretEncoded = Base32.encode(totpSecret.getBytes()); } private static String randomString(int length) {