use of org.apereo.cas.authentication.principal.PrincipalResolver in project cas by apereo.
the class PolicyBasedAuthenticationManagerTests method verifyAuthenticateAllSuccess.
@Test
public void verifyAuthenticateAllSuccess() throws Exception {
final Map<AuthenticationHandler, PrincipalResolver> map = new LinkedHashMap<>();
map.put(newMockHandler(true), null);
map.put(newMockHandler(true), null);
final AuthenticationEventExecutionPlan authenticationExecutionPlan = getAuthenticationExecutionPlan(map);
authenticationExecutionPlan.registerAuthenticationPolicy(new AllAuthenticationPolicy());
final PolicyBasedAuthenticationManager manager = new PolicyBasedAuthenticationManager(authenticationExecutionPlan, false, mock(ApplicationEventPublisher.class));
final Authentication auth = manager.authenticate(transaction);
assertEquals(2, auth.getSuccesses().size());
assertEquals(0, auth.getFailures().size());
assertEquals(2, auth.getCredentials().size());
}
use of org.apereo.cas.authentication.principal.PrincipalResolver in project cas by apereo.
the class PolicyBasedAuthenticationManager method authenticateInternal.
/**
* Authenticate internal authentication builder.
*
* @param transaction the transaction
* @return the authentication builder
* @throws AuthenticationException the authentication exception
*/
protected AuthenticationBuilder authenticateInternal(final AuthenticationTransaction transaction) throws AuthenticationException {
final Collection<Credential> credentials = transaction.getCredentials();
LOGGER.debug("Authentication credentials provided for this transaction are [{}]", credentials);
if (credentials.isEmpty()) {
LOGGER.error("Resolved authentication handlers for this transaction are empty");
throw new AuthenticationException("Resolved credentials for this transaction are empty");
}
final AuthenticationBuilder builder = new DefaultAuthenticationBuilder(NullPrincipal.getInstance());
credentials.stream().forEach(cred -> builder.addCredential(new BasicCredentialMetaData(cred)));
@NonNull final Set<AuthenticationHandler> handlerSet = getAuthenticationHandlersForThisTransaction(transaction);
LOGGER.debug("Candidate resolved authentication handlers for this transaction are [{}]", handlerSet);
if (handlerSet.isEmpty()) {
LOGGER.error("Resolved authentication handlers for this transaction are empty");
throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
}
try {
final Iterator<Credential> it = credentials.iterator();
AuthenticationCredentialsThreadLocalBinder.clearInProgressAuthentication();
while (it.hasNext()) {
final Credential credential = it.next();
LOGGER.debug("Attempting to authenticate credential [{}]", credential);
final Iterator<AuthenticationHandler> itHandlers = handlerSet.iterator();
boolean proceedWithNextHandler = true;
while (proceedWithNextHandler && itHandlers.hasNext()) {
final AuthenticationHandler handler = itHandlers.next();
if (handler.supports(credential)) {
try {
final PrincipalResolver resolver = getPrincipalResolverLinkedToHandlerIfAny(handler, transaction);
LOGGER.debug("Attempting authentication of [{}] using [{}]", credential.getId(), handler.getName());
authenticateAndResolvePrincipal(builder, credential, resolver, handler);
AuthenticationCredentialsThreadLocalBinder.bindInProgress(builder.build());
final Pair<Boolean, Set<Throwable>> failures = evaluateAuthenticationPolicies(builder.build(), transaction);
proceedWithNextHandler = !failures.getKey();
} catch (final Exception e) {
LOGGER.error("Authentication has failed. Credentials may be incorrect or CAS cannot " + "find authentication handler that supports [{}] of type [{}]. Examine the configuration to " + "ensure a method of authentication is defined and analyze CAS logs at DEBUG level to trace " + "the authentication event.", credential, credential.getClass().getSimpleName());
handleAuthenticationException(e, handler.getName(), builder);
proceedWithNextHandler = true;
}
} else {
LOGGER.debug("Authentication handler [{}] does not support the credential type [{}]. Trying next...", handler.getName(), credential);
}
}
}
evaluateFinalAuthentication(builder, transaction);
return builder;
} finally {
AuthenticationCredentialsThreadLocalBinder.clearInProgressAuthentication();
}
}
use of org.apereo.cas.authentication.principal.PrincipalResolver in project cas by apereo.
the class ChainingPrincipalResolver method getAttributeRepository.
@Override
public IPersonAttributeDao getAttributeRepository() {
val dao = new MergingPersonAttributeDaoImpl();
dao.setPersonAttributeDaos(this.chain.stream().map(PrincipalResolver::getAttributeRepository).collect(Collectors.toList()));
return dao;
}
use of org.apereo.cas.authentication.principal.PrincipalResolver in project cas by apereo.
the class PolicyBasedAuthenticationManagerTests method verifyAuthenticateAnyButTryAllSuccess.
@Test
public void verifyAuthenticateAnyButTryAllSuccess() throws Exception {
final Map<AuthenticationHandler, PrincipalResolver> map = new HashMap<>();
map.put(newMockHandler(true), null);
map.put(newMockHandler(false), null);
final AuthenticationEventExecutionPlan authenticationExecutionPlan = getAuthenticationExecutionPlan(map);
authenticationExecutionPlan.registerAuthenticationPolicy(new AnyAuthenticationPolicy(true));
final PolicyBasedAuthenticationManager manager = new PolicyBasedAuthenticationManager(authenticationExecutionPlan, false, mock(ApplicationEventPublisher.class));
final Authentication auth = manager.authenticate(transaction);
assertEquals(1, auth.getSuccesses().size());
assertEquals(1, auth.getFailures().size());
assertEquals(2, auth.getCredentials().size());
}
use of org.apereo.cas.authentication.principal.PrincipalResolver in project cas by apereo.
the class PolicyBasedAuthenticationManagerTests method verifyAuthenticateAnyFailure.
@Test
public void verifyAuthenticateAnyFailure() throws Exception {
final Map<AuthenticationHandler, PrincipalResolver> map = new LinkedHashMap<>();
map.put(newMockHandler(false), null);
map.put(newMockHandler(false), null);
final AuthenticationEventExecutionPlan authenticationExecutionPlan = getAuthenticationExecutionPlan(map);
authenticationExecutionPlan.registerAuthenticationPolicy(new AnyAuthenticationPolicy());
final PolicyBasedAuthenticationManager manager = new PolicyBasedAuthenticationManager(authenticationExecutionPlan, false, mock(ApplicationEventPublisher.class));
this.thrown.expect(AuthenticationException.class);
manager.authenticate(transaction);
throw new AssertionError("Should have thrown authentication exception");
}
Aggregations