KEYCLOAK-745 Display the realm name for OTP QR code scan

This commit is contained in:
Bruno Oliveira 2014-10-09 02:46:17 -03:00 committed by Stian Thorgersen
parent 2445575a04
commit c00c712c39
3 changed files with 20 additions and 17 deletions

View file

@ -131,7 +131,7 @@ public class FreeMarkerAccountProvider implements AccountProvider {
attributes.put("account", new AccountBean(user, profileFormData)); attributes.put("account", new AccountBean(user, profileFormData));
break; break;
case TOTP: case TOTP:
attributes.put("totp", new TotpBean(user, baseUri)); attributes.put("totp", new TotpBean(realm, user, baseUri));
break; break;
case SOCIAL: case SOCIAL:
attributes.put("social", new AccountSocialBean(session, realm, user, uriInfo.getBaseUri(), stateChecker)); attributes.put("social", new AccountSocialBean(session, realm, user, uriInfo.getBaseUri(), stateChecker));

View file

@ -21,6 +21,7 @@
*/ */
package org.keycloak.account.freemarker.model; package org.keycloak.account.freemarker.model;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.utils.Base32; import org.keycloak.models.utils.Base32;
@ -35,17 +36,19 @@ import java.security.SecureRandom;
*/ */
public class TotpBean { public class TotpBean {
private String totpSecret; private final String totpSecret;
private String totpSecretEncoded; private final String totpSecretEncoded;
private boolean enabled; private final boolean enabled;
private String contextUrl; 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.enabled = user.isTotp();
this.contextUrl = baseUri.getPath(); this.contextUrl = baseUri.getPath();
totpSecret = randomString(20); this.totpSecret = randomString(20);
totpSecretEncoded = Base32.encode(totpSecret.getBytes()); this.totpSecretEncoded = Base32.encode(totpSecret.getBytes());
} }
private static String randomString(int length) { private static String randomString(int length) {
@ -86,7 +89,7 @@ public class TotpBean {
} }
public String getTotpSecretQrCodeUrl() throws UnsupportedEncodingException { 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; return contextUrl + "qrcode" + "?size=246x246&contents=" + contents;
} }

View file

@ -35,19 +35,19 @@ import java.util.Random;
*/ */
public class TotpBean { public class TotpBean {
private String totpSecret; private final String totpSecret;
private String totpSecretEncoded; private final String totpSecretEncoded;
private boolean enabled; private final boolean enabled;
private String contextUrl; private final String contextUrl;
private String realmName; private final String realmName;
public TotpBean(RealmModel realm, UserModel user, URI baseUri) { public TotpBean(RealmModel realm, UserModel user, URI baseUri) {
this.realmName = realm.getName(); this.realmName = realm.getName();
this.enabled = user.isTotp(); this.enabled = user.isTotp();
this.contextUrl = baseUri.getPath(); this.contextUrl = baseUri.getPath();
totpSecret = randomString(20); this.totpSecret = randomString(20);
totpSecretEncoded = Base32.encode(totpSecret.getBytes()); this.totpSecretEncoded = Base32.encode(totpSecret.getBytes());
} }
private static String randomString(int length) { private static String randomString(int length) {