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));
}
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;
}
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);
}
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;
}
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;
}
Aggregations