Search in sources :

Example 1 with Authenticator

use of eu.bcvsolutions.idm.core.security.api.authentication.Authenticator in project CzechIdMng by bcvsolutions.

the class DefaultAuthenticationManager method authenticateOverAuthenticator.

/**
 * Authenticate {@link LoginDto} over all found {@link Authenticator}
 *
 * @param loginDto
 */
private LoginDto authenticateOverAuthenticator(LoginDto loginDto) {
    Assert.notNull(authenticators);
    // 
    List<LoginDto> resultsList = new LinkedList<>();
    RuntimeException firstFailture = null;
    // 
    for (Authenticator authenticator : getEnabledAuthenticators()) {
        LOG.debug("AuthenticationManager call authenticate by [{}].", authenticator.getName());
        try {
            LoginDto result = authenticator.authenticate(cloneLoginDto(loginDto));
            if (result == null) {
                // continue, authenticator is not implemented or etc.
                continue;
            }
            if (authenticator.getExceptedResult() == AuthenticationResponseEnum.SUFFICIENT) {
                passwordService.setLastSuccessfulLogin(loginDto.getUsername());
                return result;
            }
            // if otherwise add result too list and continue
            resultsList.add(result);
        } catch (RuntimeException e) {
            // if excepted response is REQUISITE exit immediately with error
            if (authenticator.getExceptedResult() == AuthenticationResponseEnum.REQUISITE) {
                throw e;
            }
            // if otherwise save first failure into exception
            if (firstFailture == null) {
                firstFailture = e;
            }
        }
    }
    // authenticator is sorted by implement ordered, return first success authenticate authenticator, if don't exist any otherwise throw first failure
    if (resultsList.isEmpty()) {
        passwordService.increaseUnsuccessfulAttempts(loginDto.getUsername());
        throw firstFailture;
    }
    passwordService.setLastSuccessfulLogin(loginDto.getUsername());
    return resultsList.get(0);
}
Also used : LoginDto(eu.bcvsolutions.idm.core.security.api.dto.LoginDto) LinkedList(java.util.LinkedList) Authenticator(eu.bcvsolutions.idm.core.security.api.authentication.Authenticator)

Aggregations

Authenticator (eu.bcvsolutions.idm.core.security.api.authentication.Authenticator)1 LoginDto (eu.bcvsolutions.idm.core.security.api.dto.LoginDto)1 LinkedList (java.util.LinkedList)1