Search in sources :

Example 1 with AuthenticationHandler

use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.

the class SamlAuthenticationMetaDataPopulatorTests method newAuthenticationBuilder.

private static AuthenticationBuilder newAuthenticationBuilder(final Principal principal) {
    final CredentialMetaData meta = new BasicCredentialMetaData(new UsernamePasswordCredential());
    final AuthenticationHandler handler = new SimpleTestUsernamePasswordAuthenticationHandler();
    return new DefaultAuthenticationBuilder(principal).addCredential(meta).addSuccess("test", new DefaultHandlerResult(handler, meta));
}
Also used : DefaultAuthenticationBuilder(org.apereo.cas.authentication.DefaultAuthenticationBuilder) 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) UsernamePasswordCredential(org.apereo.cas.authentication.UsernamePasswordCredential) BasicCredentialMetaData(org.apereo.cas.authentication.BasicCredentialMetaData)

Example 2 with AuthenticationHandler

use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.

the class CasJdbcAuthenticationConfiguration method jdbcAuthenticationHandlers.

@ConditionalOnMissingBean(name = "jdbcAuthenticationHandlers")
@Bean
@RefreshScope
public Collection<AuthenticationHandler> jdbcAuthenticationHandlers() {
    final Collection<AuthenticationHandler> handlers = new HashSet<>();
    final JdbcAuthenticationProperties jdbc = casProperties.getAuthn().getJdbc();
    jdbc.getBind().forEach(b -> handlers.add(bindModeSearchDatabaseAuthenticationHandler(b)));
    jdbc.getEncode().forEach(b -> handlers.add(queryAndEncodeDatabaseAuthenticationHandler(b)));
    jdbc.getQuery().forEach(b -> handlers.add(queryDatabaseAuthenticationHandler(b)));
    jdbc.getSearch().forEach(b -> handlers.add(searchModeSearchDatabaseAuthenticationHandler(b)));
    return handlers;
}
Also used : BindModeSearchDatabaseAuthenticationHandler(org.apereo.cas.adaptors.jdbc.BindModeSearchDatabaseAuthenticationHandler) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) QueryDatabaseAuthenticationHandler(org.apereo.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler) QueryAndEncodeDatabaseAuthenticationHandler(org.apereo.cas.adaptors.jdbc.QueryAndEncodeDatabaseAuthenticationHandler) SearchModeSearchDatabaseAuthenticationHandler(org.apereo.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler) BindJdbcAuthenticationProperties(org.apereo.cas.configuration.model.support.jdbc.BindJdbcAuthenticationProperties) QueryEncodeJdbcAuthenticationProperties(org.apereo.cas.configuration.model.support.jdbc.QueryEncodeJdbcAuthenticationProperties) QueryJdbcAuthenticationProperties(org.apereo.cas.configuration.model.support.jdbc.QueryJdbcAuthenticationProperties) SearchJdbcAuthenticationProperties(org.apereo.cas.configuration.model.support.jdbc.SearchJdbcAuthenticationProperties) JdbcAuthenticationProperties(org.apereo.cas.configuration.model.support.jdbc.JdbcAuthenticationProperties) HashSet(java.util.HashSet) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with AuthenticationHandler

use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.

the class ChainingPrincipalResolver method resolve.

@Override
public Principal resolve(final Credential credential, final Optional<Principal> principal, final Optional<AuthenticationHandler> handler) {
    val principals = new ArrayList<Principal>(chain.size());
    chain.stream().filter(resolver -> resolver.supports(credential)).forEach(resolver -> {
        LOGGER.debug("Invoking principal resolver [{}]", resolver.getName());
        val p = resolver.resolve(credential, principal, handler);
        if (p != null) {
            LOGGER.debug("Resolved principal [{}]", p);
            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();
    }
    val attributes = new HashMap<String, List<Object>>();
    val merger = CoreAuthenticationUtils.getAttributeMerger(casProperties.getAuthn().getAttributeRepository().getCore().getMerger());
    principals.forEach(p -> {
        if (p != null) {
            LOGGER.debug("Resolved principal [{}]", p);
            val principalAttributes = p.getAttributes();
            if (principalAttributes != null && !principalAttributes.isEmpty()) {
                LOGGER.debug("Adding attributes [{}] for the final principal", principalAttributes);
                attributes.putAll(CoreAuthenticationUtils.mergeAttributes(attributes, principalAttributes, merger));
            }
        }
    });
    return principalElectionStrategy.nominate(principals, attributes);
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Setter(lombok.Setter) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) PrincipalFactoryUtils(org.apereo.cas.authentication.principal.PrincipalFactoryUtils) PrincipalResolver(org.apereo.cas.authentication.principal.PrincipalResolver) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) IPersonAttributeDao(org.apereo.services.persondir.IPersonAttributeDao) ArrayList(java.util.ArrayList) PrincipalElectionStrategy(org.apereo.cas.authentication.PrincipalElectionStrategy) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) PrincipalFactory(org.apereo.cas.authentication.principal.PrincipalFactory) ToString(lombok.ToString) Optional(java.util.Optional) Principal(org.apereo.cas.authentication.principal.Principal) CoreAuthenticationUtils(org.apereo.cas.authentication.CoreAuthenticationUtils) Credential(org.apereo.cas.authentication.Credential) MergingPersonAttributeDaoImpl(org.apereo.services.persondir.support.MergingPersonAttributeDaoImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 4 with AuthenticationHandler

use of org.apereo.cas.authentication.AuthenticationHandler in project cas by apereo.

the class AuthenticationPolicyAwareServiceTicketValidationAuthorizerTests method getAssertion.

private static Assertion getAssertion(final Map<Credential, ? extends AuthenticationHandler> handlers) {
    val assertion = mock(Assertion.class);
    val principal = CoreAuthenticationTestUtils.getPrincipal("casuser");
    val authentication = CoreAuthenticationTestUtils.getAuthenticationBuilder(principal, handlers, Map.of(AuthenticationHandler.SUCCESSFUL_AUTHENTICATION_HANDLERS, handlers.values().stream().map(AuthenticationHandler::getName).collect(Collectors.toList()))).build();
    when(assertion.getPrimaryAuthentication()).thenReturn(authentication);
    return assertion;
}
Also used : lombok.val(lombok.val) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) TestOneTimePasswordAuthenticationHandler(org.apereo.cas.TestOneTimePasswordAuthenticationHandler) AcceptUsersAuthenticationHandler(org.apereo.cas.authentication.AcceptUsersAuthenticationHandler) SimpleTestUsernamePasswordAuthenticationHandler(org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)

Example 5 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.", new HashMap<>(0), new HashMap<>(0));
    }
    final String principalId = principal != null ? principal.getId() : principals.get(0).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) Setter(lombok.Setter) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) PrincipalResolver(org.apereo.cas.authentication.principal.PrincipalResolver) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) IPersonAttributeDao(org.apereo.services.persondir.IPersonAttributeDao) ArrayList(java.util.ArrayList) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AuthenticationHandler(org.apereo.cas.authentication.AuthenticationHandler) PrincipalFactory(org.apereo.cas.authentication.principal.PrincipalFactory) Map(java.util.Map) ToString(lombok.ToString) Principal(org.apereo.cas.authentication.principal.Principal) Credential(org.apereo.cas.authentication.Credential) 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) ToString(lombok.ToString) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) Principal(org.apereo.cas.authentication.principal.Principal)

Aggregations

AuthenticationHandler (org.apereo.cas.authentication.AuthenticationHandler)10 Slf4j (lombok.extern.slf4j.Slf4j)4 SimpleTestUsernamePasswordAuthenticationHandler (org.apereo.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler)4 HashSet (java.util.HashSet)3 lombok.val (lombok.val)3 BasicCredentialMetaData (org.apereo.cas.authentication.BasicCredentialMetaData)3 CredentialMetaData (org.apereo.cas.authentication.CredentialMetaData)3 DefaultAuthenticationBuilder (org.apereo.cas.authentication.DefaultAuthenticationBuilder)3 UsernamePasswordCredential (org.apereo.cas.authentication.UsernamePasswordCredential)3 PrincipalFactory (org.apereo.cas.authentication.principal.PrincipalFactory)3 IPersonAttributeDao (org.apereo.services.persondir.IPersonAttributeDao)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 RequiredArgsConstructor (lombok.RequiredArgsConstructor)2 Setter (lombok.Setter)2 ToString (lombok.ToString)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Credential (org.apereo.cas.authentication.Credential)2