Search in sources :

Example 1 with PasswordHashProviderFactory

use of org.keycloak.credential.hash.PasswordHashProviderFactory 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)

Aggregations

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 PasswordHashProvider (org.keycloak.credential.hash.PasswordHashProvider)1 PasswordHashProviderFactory (org.keycloak.credential.hash.PasswordHashProviderFactory)1 PasswordCredentialModel (org.keycloak.models.credential.PasswordCredentialModel)1 CredentialRepresentation (org.keycloak.representations.idm.CredentialRepresentation)1 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)1 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)1