Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.keycloak.authentication.model;
 
 import java.util.Map;
 
Authentication provider, which delegates calling of all methods to specified realm

Author(s):
Marek Posolda
public abstract class AbstractModelAuthenticationProvider implements AuthenticationProvider {
    private static final Logger logger = Logger.getLogger(AbstractModelAuthenticationProvider.class);
    @Override
    public AuthUser getUser(RealmModel currentRealmMap<StringStringconfigString usernamethrows AuthenticationProviderException {
        RealmModel realm = getRealm(currentRealmconfig);
        UserModel user = KeycloakModelUtils.findUserByNameOrEmail(realmusername);
        return user == null ? null : createAuthenticatedUserInstance(user);
    }
    @Override
    public String registerUser(RealmModel currentRealmMap<StringStringconfigString usernamethrows AuthenticationProviderException {
        RealmModel realm = getRealm(currentRealmconfig);
        UserModel user = currentRealm.addUser(username);
        user.setEnabled(true);
        return user.getId();
    }
    @Override
    public AuthProviderStatus validatePassword(RealmModel currentRealmMap<StringStringconfigString usernameString passwordthrows AuthenticationProviderException {
        RealmModel realm = getRealm(currentRealmconfig);
        UserModel user = KeycloakModelUtils.findUserByNameOrEmail(realmusername);
        boolean result = realm.validatePassword(userpassword);
        return result ? . : .;
    }
    @Override
    public boolean updateCredential(RealmModel currentRealmMap<StringStringconfigString usernameString passwordthrows AuthenticationProviderException {
        RealmModel realm = getRealm(currentRealmconfig);
        // Validate password policy
        String error = realm.getPasswordPolicy().validate(password);
        if (error != null) {
            throw new AuthenticationProviderException(error);
        }
        UserModel user = realm.getUser(username);
        if (user == null) {
            .warnf("User '%s' doesn't exists. Skip password update"username);
            return false;
        }
        UserCredentialModel cred = new UserCredentialModel();
        cred.setValue(password);
        realm.updateCredential(usercred);
        return true;
    }
    @Override
    public void close() {
    }
    protected abstract RealmModel getRealm(RealmModel currentRealmMap<StringStringconfigthrows AuthenticationProviderException;
        return new AuthUser(user.getId(), user.getLoginName(), getName())
                .setName(user.getFirstName(), user.getLastName())
                .setEmail(user.getEmail());
    }
New to GrepCode? Check out our FAQ X