Search in sources :

Example 6 with AuthenticationHandler

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;
}
Also used : PrincipalException(org.apereo.cas.authentication.PrincipalException) Logger(org.slf4j.Logger) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) PrincipalResolver(org.apereo.cas.authentication.principal.PrincipalResolver) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) IPersonAttributeDao(org.apereo.services.persondir.IPersonAttributeDao) ArrayList(java.util.ArrayList) List(java.util.List) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) PrincipalFactory(org.apereo.cas.authentication.principal.PrincipalFactory) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) Map(java.util.Map) Principal(org.apereo.cas.authentication.principal.Principal) Credential(org.apereo.cas.authentication.Credential) Collections(java.util.Collections) DefaultPrincipalFactory(org.apereo.cas.authentication.principal.DefaultPrincipalFactory) MergingPersonAttributeDaoImpl(org.apereo.services.persondir.support.MergingPersonAttributeDaoImpl) HashMap(java.util.HashMap) PrincipalException(org.apereo.cas.authentication.PrincipalException) ArrayList(java.util.ArrayList) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) Principal(org.apereo.cas.authentication.principal.Principal)

Example 7 with AuthenticationHandler

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()));
}
Also used : SpnegoCredential(org.apereo.cas.support.spnego.authentication.principal.SpnegoCredential) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) MockJcifsAuthentication(org.apereo.cas.support.spnego.MockJcifsAuthentication) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) Test(org.junit.Test)

Example 8 with AuthenticationHandler

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());
}
Also used : SpnegoCredential(org.apereo.cas.support.spnego.authentication.principal.SpnegoCredential) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) MockJcifsAuthentication(org.apereo.cas.support.spnego.MockJcifsAuthentication) Test(org.junit.Test)

Example 9 with AuthenticationHandler

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());
    }
}
Also used : SpnegoCredential(org.apereo.cas.support.spnego.authentication.principal.SpnegoCredential) GeneralSecurityException(java.security.GeneralSecurityException) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) MockJcifsAuthentication(org.apereo.cas.support.spnego.MockJcifsAuthentication) Test(org.junit.Test)

Example 10 with AuthenticationHandler

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;
}
Also used : DefaultAuthenticationBuilder(org.apereo.cas.authentication.DefaultAuthenticationBuilder) DefaultAuthenticationBuilder(org.apereo.cas.authentication.DefaultAuthenticationBuilder) AuthenticationBuilder(org.apereo.cas.authentication.AuthenticationBuilder) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) BasicCredentialMetaData(org.apereo.cas.authentication.BasicCredentialMetaData) CredentialMetaData(org.apereo.cas.authentication.CredentialMetaData) DefaultHandlerResult(org.apereo.cas.authentication.DefaultHandlerResult) RememberMeUsernamePasswordCredential(org.apereo.cas.authentication.RememberMeUsernamePasswordCredential) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) BasicCredentialMetaData(org.apereo.cas.authentication.BasicCredentialMetaData)

Aggregations

AuthenticationHandler (org.apereo.cas.authentication.AuthenticationHandler)11 Test (org.junit.Test)6 MockJcifsAuthentication (org.apereo.cas.support.spnego.MockJcifsAuthentication)4 SpnegoCredential (org.apereo.cas.support.spnego.authentication.principal.SpnegoCredential)4 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)3 HashSet (java.util.HashSet)2 AcceptUsersAuthenticationHandler (org.apereo.cas.authentication.AcceptUsersAuthenticationHandler)2 AuthenticationTransaction (org.apereo.cas.authentication.AuthenticationTransaction)2 BasicCredentialMetaData (org.apereo.cas.authentication.BasicCredentialMetaData)2 CredentialMetaData (org.apereo.cas.authentication.CredentialMetaData)2 DefaultAuthenticationBuilder (org.apereo.cas.authentication.DefaultAuthenticationBuilder)2 DefaultHandlerResult (org.apereo.cas.authentication.DefaultHandlerResult)2 RegisteredServiceAuthenticationHandlerResolver (org.apereo.cas.authentication.RegisteredServiceAuthenticationHandlerResolver)2 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 Bean (org.springframework.context.annotation.Bean)2 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1