Search in sources :

Example 1 with CredentialProvider

use of org.keycloak.credential.CredentialProvider in project keycloak by keycloak.

the class CredentialHelper method createOTPCredential.

/**
 * Create OTP credential either in userStorage or local storage (Keycloak DB)
 *
 * @return true if credential was successfully created either in the user storage or Keycloak DB. False if error happened (EG. during HOTP validation)
 */
public static boolean createOTPCredential(KeycloakSession session, RealmModel realm, UserModel user, String totpCode, OTPCredentialModel credentialModel) {
    CredentialProvider otpCredentialProvider = session.getProvider(CredentialProvider.class, "keycloak-otp");
    String totpSecret = credentialModel.getOTPSecretData().getValue();
    UserCredentialModel otpUserCredential = new UserCredentialModel("", realm.getOTPPolicy().getType(), totpSecret);
    boolean userStorageCreated = session.userCredentialManager().updateCredential(realm, user, otpUserCredential);
    String credentialId = null;
    if (userStorageCreated) {
        logger.debugf("Created OTP credential for user '%s' in the user storage", user.getUsername());
    } else {
        CredentialModel createdCredential = otpCredentialProvider.createCredential(realm, user, credentialModel);
        credentialId = createdCredential.getId();
    }
    // If the type is HOTP, call verify once to consume the OTP used for registration and increase the counter.
    UserCredentialModel credential = new UserCredentialModel(credentialId, otpCredentialProvider.getType(), totpCode);
    return session.userCredentialManager().isValid(realm, user, credential);
}
Also used : UserCredentialModel(org.keycloak.models.UserCredentialModel) CredentialModel(org.keycloak.credential.CredentialModel) OTPCredentialModel(org.keycloak.models.credential.OTPCredentialModel) CredentialProvider(org.keycloak.credential.CredentialProvider) UserCredentialModel(org.keycloak.models.UserCredentialModel)

Example 2 with CredentialProvider

use of org.keycloak.credential.CredentialProvider in project keycloak by keycloak.

the class CredentialHelper method deleteOTPCredential.

public static void deleteOTPCredential(KeycloakSession session, RealmModel realm, UserModel user, String credentialId) {
    CredentialProvider otpCredentialProvider = session.getProvider(CredentialProvider.class, "keycloak-otp");
    boolean removed = otpCredentialProvider.deleteCredential(realm, user, credentialId);
    // This can usually happened when credential is stored in the userStorage. Propagate to "disable" credential in the userStorage
    if (!removed) {
        logger.debug("Removing OTP credential from userStorage");
        session.userCredentialManager().disableCredentialType(realm, user, OTPCredentialModel.TYPE);
    }
}
Also used : CredentialProvider(org.keycloak.credential.CredentialProvider)

Aggregations

CredentialProvider (org.keycloak.credential.CredentialProvider)2 CredentialModel (org.keycloak.credential.CredentialModel)1 UserCredentialModel (org.keycloak.models.UserCredentialModel)1 OTPCredentialModel (org.keycloak.models.credential.OTPCredentialModel)1