Search in sources :

Example 6 with PasswordHashProvider

use of org.keycloak.credential.hash.PasswordHashProvider in project keycloak by keycloak.

the class PasswordCredentialProvider method createCredential.

public boolean createCredential(RealmModel realm, UserModel user, String password) {
    PasswordPolicy policy = realm.getPasswordPolicy();
    PolicyError error = session.getProvider(PasswordPolicyManagerProvider.class).validate(realm, user, password);
    if (error != null)
        throw new ModelException(error.getMessage(), error.getParameters());
    PasswordHashProvider hash = getHashProvider(policy);
    if (hash == null) {
        return false;
    }
    PasswordCredentialModel credentialModel = hash.encodedCredential(password, policy.getHashIterations());
    credentialModel.setCreatedDate(Time.currentTimeMillis());
    createCredential(realm, user, credentialModel);
    return true;
}
Also used : ModelException(org.keycloak.models.ModelException) PasswordPolicyManagerProvider(org.keycloak.policy.PasswordPolicyManagerProvider) PasswordPolicy(org.keycloak.models.PasswordPolicy) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) PolicyError(org.keycloak.policy.PolicyError) PasswordHashProvider(org.keycloak.credential.hash.PasswordHashProvider)

Example 7 with PasswordHashProvider

use of org.keycloak.credential.hash.PasswordHashProvider in project keycloak by keycloak.

the class AddUser method createUser.

private static void createUser(File addUserFile, String realmName, String userName, String password, String rolesString, int iterations) throws Exception {
    List<RealmRepresentation> realms;
    if (addUserFile.isFile()) {
        realms = JsonSerialization.readValue(new FileInputStream(addUserFile), new TypeReference<List<RealmRepresentation>>() {
        });
    } else {
        realms = new LinkedList<>();
    }
    if (realmName == null) {
        realmName = "master";
    }
    RealmRepresentation realm = null;
    for (RealmRepresentation r : realms) {
        if (r.getRealm().equals(realmName)) {
            realm = r;
        }
    }
    if (realm == null) {
        realm = new RealmRepresentation();
        realm.setRealm(realmName);
        realms.add(realm);
        realm.setUsers(new LinkedList<>());
    }
    for (UserRepresentation u : realm.getUsers()) {
        if (u.getUsername().equals(userName)) {
            throw new Exception("User with username '" + userName + "' already added to '" + addUserFile + "'");
        }
    }
    UserRepresentation user = new UserRepresentation();
    user.setEnabled(true);
    user.setUsername(userName);
    user.setCredentials(new LinkedList<>());
    PasswordHashProviderFactory hashProviderFactory = getHashProviderFactory(DEFAULT_HASH_ALGORITH);
    PasswordHashProvider hashProvider = hashProviderFactory.create(null);
    PasswordCredentialModel credentialModel = hashProvider.encodedCredential(password, iterations > 0 ? iterations : DEFAULT_HASH_ITERATIONS);
    CredentialRepresentation credentials = ModelToRepresentation.toRepresentation(credentialModel);
    user.getCredentials().add(credentials);
    String[] roles;
    if (rolesString != null) {
        roles = rolesString.split(",");
    } else {
        if (realmName.equals("master")) {
            roles = new String[] { "admin" };
        } else {
            roles = new String[] { "realm-management/realm-admin" };
        }
    }
    for (String r : roles) {
        if (r.indexOf('/') != -1) {
            String[] cr = r.split("/");
            String client = cr[0];
            String clientRole = cr[1];
            if (user.getClientRoles() == null) {
                user.setClientRoles(new HashMap<>());
            }
            if (user.getClientRoles().get(client) == null) {
                user.getClientRoles().put(client, new LinkedList<>());
            }
            user.getClientRoles().get(client).add(clientRole);
        } else {
            if (user.getRealmRoles() == null) {
                user.setRealmRoles(new LinkedList<>());
            }
            user.getRealmRoles().add(r);
        }
    }
    realm.getUsers().add(user);
    JsonSerialization.writeValuePrettyToStream(new FileOutputStream(addUserFile), realms);
    System.out.println("Added '" + userName + "' to '" + addUserFile + "', restart server to load user");
}
Also used : RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) FileInputStream(java.io.FileInputStream) CommandNotFoundException(org.aesh.command.CommandNotFoundException) CommandRegistryException(org.aesh.command.registry.CommandRegistryException) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) PasswordHashProviderFactory(org.keycloak.credential.hash.PasswordHashProviderFactory) FileOutputStream(java.io.FileOutputStream) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PasswordHashProvider(org.keycloak.credential.hash.PasswordHashProvider) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 8 with PasswordHashProvider

use of org.keycloak.credential.hash.PasswordHashProvider in project keycloak by keycloak.

the class AbstractUsernameFormAuthenticator method runDefaultDummyHash.

protected void runDefaultDummyHash(AuthenticationFlowContext context) {
    PasswordHashProvider hash = context.getSession().getProvider(PasswordHashProvider.class, PasswordPolicy.HASH_ALGORITHM_DEFAULT);
    hash.encode("SlightlyLongerDummyPassword", PasswordPolicy.HASH_ITERATIONS_DEFAULT);
}
Also used : PasswordHashProvider(org.keycloak.credential.hash.PasswordHashProvider)

Aggregations

PasswordHashProvider (org.keycloak.credential.hash.PasswordHashProvider)8 PasswordPolicy (org.keycloak.models.PasswordPolicy)5 UserCredentialModel (org.keycloak.models.UserCredentialModel)3 PasswordCredentialModel (org.keycloak.models.credential.PasswordCredentialModel)3 CredentialModel (org.keycloak.credential.CredentialModel)2 UserCache (org.keycloak.models.cache.UserCache)2 PasswordUserCredentialModel (org.keycloak.models.credential.PasswordUserCredentialModel)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 CommandNotFoundException (org.aesh.command.CommandNotFoundException)1 CommandRegistryException (org.aesh.command.registry.CommandRegistryException)1 PasswordHashProviderFactory (org.keycloak.credential.hash.PasswordHashProviderFactory)1 ModelException (org.keycloak.models.ModelException)1 OTPPolicy (org.keycloak.models.OTPPolicy)1 OnUserCache (org.keycloak.models.cache.OnUserCache)1 TimeBasedOTP (org.keycloak.models.utils.TimeBasedOTP)1 PasswordPolicyManagerProvider (org.keycloak.policy.PasswordPolicyManagerProvider)1 PolicyError (org.keycloak.policy.PolicyError)1 CredentialRepresentation (org.keycloak.representations.idm.CredentialRepresentation)1