use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.
the class ChainingPrincipalResolver method resolve.
/**
* {@inheritDoc}
* Resolves a credential by delegating to each of the configured resolvers in sequence. Note that the
* final principal is taken from the first resolved principal in the chain, yet attributes are merged.
*
* @param credential Authenticated credential.
* @param principal Authenticated principal, if any.
* @return The principal from the last configured resolver in the chain.
*/
@Override
public Principal resolve(final Credential credential, final Principal principal, final AuthenticationHandler handler) {
final List<Principal> principals = new ArrayList<>();
chain.stream().filter(resolver -> resolver.supports(credential)).forEach(resolver -> {
LOGGER.debug("Invoking principal resolver [{}]", resolver);
final Principal p = resolver.resolve(credential, principal, handler);
if (p != null) {
principals.add(p);
}
});
if (principals.isEmpty()) {
LOGGER.warn("None of the principal resolvers in the chain were able to produce a principal");
return NullPrincipal.getInstance();
}
final Map<String, Object> attributes = new HashMap<>();
principals.forEach(p -> {
if (p != null) {
LOGGER.debug("Resolved principal [{}]", p);
if (p.getAttributes() != null && !p.getAttributes().isEmpty()) {
LOGGER.debug("Adding attributes [{}] for the final principal", p.getAttributes());
attributes.putAll(p.getAttributes());
}
}
});
final long count = principals.stream().map(p -> p.getId().trim().toLowerCase()).distinct().collect(Collectors.toSet()).size();
if (count > 1) {
throw new PrincipalException("Resolved principals by the chain are not unique because principal resolvers have produced CAS principals " + "with different identifiers which typically is the result of a configuration issue.", Collections.emptyMap(), Collections.emptyMap());
}
final String principalId = principal != null ? principal.getId() : principals.iterator().next().getId();
final Principal finalPrincipal = this.principalFactory.createPrincipal(principalId, attributes);
LOGGER.debug("Final principal constructed by the chain of resolvers is [{}]", finalPrincipal);
return finalPrincipal;
}
use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.
the class JcifsSpnegoAuthenticationHandlerTests method verifySupports.
@Test
public void verifySupports() {
final AuthenticationHandler authenticationHandler = new JcifsSpnegoAuthenticationHandler("", null, null, new MockJcifsAuthentication(true), true, true);
assertFalse(authenticationHandler.supports(null));
assertTrue(authenticationHandler.supports(new SpnegoCredential(new byte[] { 0, 1, 2 })));
assertFalse(authenticationHandler.supports(new UsernamePasswordCredential()));
}
use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.
the class JcifsSpnegoAuthenticationHandlerTests method verifySuccessfulAuthenticationWithDomainName.
@Test
public void verifySuccessfulAuthenticationWithDomainName() throws Exception {
final SpnegoCredential credentials = new SpnegoCredential(new byte[] { 0, 1, 2 });
final AuthenticationHandler authenticationHandler = new JcifsSpnegoAuthenticationHandler("", null, null, new MockJcifsAuthentication(true), true, true);
assertNotNull(authenticationHandler.authenticate(credentials));
assertEquals("test", credentials.getPrincipal().getId());
assertNotNull(credentials.getNextToken());
}
use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.
the class JcifsSpnegoAuthenticationHandlerTests method verifyUnsuccessfulAuthentication.
@Test
public void verifyUnsuccessfulAuthentication() throws Exception {
final SpnegoCredential credentials = new SpnegoCredential(new byte[] { 0, 1, 2 });
final AuthenticationHandler authenticationHandler = new JcifsSpnegoAuthenticationHandler("", null, null, new MockJcifsAuthentication(false), true, true);
try {
authenticationHandler.authenticate(credentials);
fail("An AbstractAuthenticationException should have been thrown");
} catch (final GeneralSecurityException e) {
assertNull(credentials.getNextToken());
assertNull(credentials.getPrincipal());
}
}
use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.
the class RememberMeAuthenticationMetaDataPopulatorTests method newBuilder.
private AuthenticationBuilder newBuilder(final Credential credential) {
final CredentialMetaData meta = new BasicCredentialMetaData(new UsernamePasswordCredential());
final AuthenticationHandler handler = new SimpleTestUsernamePasswordAuthenticationHandler();
final AuthenticationBuilder builder = new DefaultAuthenticationBuilder(CoreAuthenticationTestUtils.getPrincipal()).addCredential(meta).addSuccess("test", new DefaultHandlerResult(handler, meta));
if (this.p.supports(credential)) {
this.p.populateAttributes(builder, credential);
}
return builder;
}
Aggregations