Search in sources :

Example 1 with UnresolvedPrincipalException

use of org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException in project cas by apereo.

the class AbstractAuthenticationManager method authenticate.

@Override
@Audit(action = "AUTHENTICATION", actionResolverName = "AUTHENTICATION_RESOLVER", resourceResolverName = "AUTHENTICATION_RESOURCE_RESOLVER")
@Timed(name = "AUTHENTICATE_TIMER")
@Metered(name = "AUTHENTICATE_METER")
@Counted(name = "AUTHENTICATE_COUNT", monotonic = true)
public Authentication authenticate(final AuthenticationTransaction transaction) throws AuthenticationException {
    AuthenticationCredentialsLocalBinder.bindCurrent(transaction.getCredentials());
    final AuthenticationBuilder builder = authenticateInternal(transaction);
    authenticationEventExecutionPlan.getAuthenticationPostProcessors().forEach(p -> {
        LOGGER.info("Invoking authentication post processor [{}]", p);
        p.process(transaction, builder);
    });
    final Authentication authentication = builder.build();
    final Principal principal = authentication.getPrincipal();
    if (principal instanceof NullPrincipal) {
        throw new UnresolvedPrincipalException(authentication);
    }
    addAuthenticationMethodAttribute(builder, authentication);
    LOGGER.info("Authenticated principal [{}] with attributes [{}] via credentials [{}].", principal.getId(), principal.getAttributes(), transaction.getCredentials());
    populateAuthenticationMetadataAttributes(builder, transaction.getCredentials());
    final Authentication a = builder.build();
    AuthenticationCredentialsLocalBinder.bindCurrent(a);
    return a;
}
Also used : NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) UnresolvedPrincipalException(org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) Principal(org.apereo.cas.authentication.principal.Principal) Audit(org.apereo.inspektr.audit.annotation.Audit) Counted(com.codahale.metrics.annotation.Counted) Metered(com.codahale.metrics.annotation.Metered) Timed(com.codahale.metrics.annotation.Timed)

Example 2 with UnresolvedPrincipalException

use of org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException in project cas by apereo.

the class AbstractAuthenticationManager method authenticateAndResolvePrincipal.

/**
     * Authenticate and resolve principal.
     *
     * @param builder    the builder
     * @param credential the credential
     * @param resolver   the resolver
     * @param handler    the handler
     * @throws GeneralSecurityException the general security exception
     * @throws PreventedException       the prevented exception
     */
protected void authenticateAndResolvePrincipal(final AuthenticationBuilder builder, final Credential credential, final PrincipalResolver resolver, final AuthenticationHandler handler) throws GeneralSecurityException, PreventedException {
    Principal principal;
    publishEvent(new CasAuthenticationTransactionStartedEvent(this, credential));
    final HandlerResult result = handler.authenticate(credential);
    builder.addSuccess(handler.getName(), result);
    LOGGER.debug("Authentication handler [{}] successfully authenticated [{}]", handler.getName(), credential);
    publishEvent(new CasAuthenticationTransactionSuccessfulEvent(this, credential));
    principal = result.getPrincipal();
    if (resolver == null) {
        LOGGER.debug("No principal resolution is configured for [{}]. Falling back to handler principal [{}]", handler.getName(), principal);
    } else {
        principal = resolvePrincipal(handler, resolver, credential, principal);
        if (principal == null) {
            if (this.principalResolutionFailureFatal) {
                LOGGER.warn("Principal resolution handled by [{}] produced a null principal for: [{}]" + "CAS is configured to treat principal resolution failures as fatal.", resolver.getClass().getSimpleName(), credential);
                throw new UnresolvedPrincipalException();
            }
            LOGGER.warn("Principal resolution handled by [{}] produced a null principal. " + "This is likely due to misconfiguration or missing attributes; CAS will attempt to use the principal " + "produced by the authentication handler, if any.", resolver.getClass().getSimpleName());
        }
    }
    if (principal != null) {
        builder.setPrincipal(principal);
    }
    LOGGER.debug("Final principal resolved for this authentication event is [{}]", principal);
    publishEvent(new CasAuthenticationPrincipalResolvedEvent(this, principal));
}
Also used : CasAuthenticationPrincipalResolvedEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPrincipalResolvedEvent) CasAuthenticationTransactionStartedEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionStartedEvent) UnresolvedPrincipalException(org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException) CasAuthenticationTransactionSuccessfulEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionSuccessfulEvent) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) Principal(org.apereo.cas.authentication.principal.Principal)

Example 3 with UnresolvedPrincipalException

use of org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException in project cas by apereo.

the class DefaultAuthenticationManager method authenticateAndResolvePrincipal.

/**
 * Authenticate and resolve principal.
 *
 * @param builder    the builder
 * @param credential the credential
 * @param resolver   the resolver
 * @param handler    the handler
 * @throws GeneralSecurityException the general security exception
 * @throws PreventedException       the prevented exception
 */
protected void authenticateAndResolvePrincipal(final AuthenticationBuilder builder, final Credential credential, final PrincipalResolver resolver, final AuthenticationHandler handler) throws GeneralSecurityException, PreventedException {
    publishEvent(new CasAuthenticationTransactionStartedEvent(this, credential));
    val result = handler.authenticate(credential);
    val authenticationHandlerName = handler.getName();
    builder.addSuccess(authenticationHandlerName, result);
    LOGGER.debug("Authentication handler [{}] successfully authenticated [{}]", authenticationHandlerName, credential);
    publishEvent(new CasAuthenticationTransactionSuccessfulEvent(this, credential));
    var principal = result.getPrincipal();
    if (resolver != null) {
        principal = resolvePrincipal(handler, resolver, credential, principal);
    }
    if (principal == null) {
        val resolverName = resolver == null ? authenticationHandlerName : resolver.getName();
        if (this.principalResolutionFailureFatal) {
            LOGGER.warn("Principal resolution handled by [{}] produced a null principal for: [{}]" + "CAS is configured to treat principal resolution failures as fatal.", resolverName, credential);
            throw new UnresolvedPrincipalException();
        }
        LOGGER.warn("Principal resolution handled by [{}] produced a null principal. " + "This is likely due to misconfiguration or missing attributes; CAS will attempt to use the principal " + "produced by the authentication handler, if any.", resolverName);
    } else {
        builder.setPrincipal(principal);
    }
    LOGGER.debug("Final principal resolved for this authentication event is [{}]", principal);
    publishEvent(new CasAuthenticationPrincipalResolvedEvent(this, principal));
}
Also used : lombok.val(lombok.val) CasAuthenticationPrincipalResolvedEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPrincipalResolvedEvent) CasAuthenticationTransactionStartedEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionStartedEvent) UnresolvedPrincipalException(org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException) CasAuthenticationTransactionSuccessfulEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionSuccessfulEvent)

Example 4 with UnresolvedPrincipalException

use of org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException in project cas by apereo.

the class PolicyBasedAuthenticationManager method authenticate.

@Override
@Audit(action = "AUTHENTICATION", actionResolverName = "AUTHENTICATION_RESOLVER", resourceResolverName = "AUTHENTICATION_RESOURCE_RESOLVER")
@Timed(name = "AUTHENTICATE_TIMER")
@Metered(name = "AUTHENTICATE_METER")
@Counted(name = "AUTHENTICATE_COUNT", monotonic = true)
public Authentication authenticate(final AuthenticationTransaction transaction) throws AuthenticationException {
    AuthenticationCredentialsThreadLocalBinder.bindCurrent(transaction.getCredentials());
    final AuthenticationBuilder builder = authenticateInternal(transaction);
    AuthenticationCredentialsThreadLocalBinder.bindCurrent(builder);
    final Authentication authentication = builder.build();
    addAuthenticationMethodAttribute(builder, authentication);
    populateAuthenticationMetadataAttributes(builder, transaction);
    invokeAuthenticationPostProcessors(builder, transaction);
    final Authentication auth = builder.build();
    final Principal principal = auth.getPrincipal();
    if (principal instanceof NullPrincipal) {
        throw new UnresolvedPrincipalException(auth);
    }
    LOGGER.info("Authenticated principal [{}] with attributes [{}] via credentials [{}].", principal.getId(), principal.getAttributes(), transaction.getCredentials());
    AuthenticationCredentialsThreadLocalBinder.bindCurrent(auth);
    return auth;
}
Also used : NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) UnresolvedPrincipalException(org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) Principal(org.apereo.cas.authentication.principal.Principal) Audit(org.apereo.inspektr.audit.annotation.Audit) Counted(com.codahale.metrics.annotation.Counted) Metered(com.codahale.metrics.annotation.Metered) Timed(com.codahale.metrics.annotation.Timed)

Example 5 with UnresolvedPrincipalException

use of org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException in project cas by apereo.

the class PolicyBasedAuthenticationManager method authenticateAndResolvePrincipal.

/**
 * Authenticate and resolve principal.
 *
 * @param builder    the builder
 * @param credential the credential
 * @param resolver   the resolver
 * @param handler    the handler
 * @throws GeneralSecurityException the general security exception
 * @throws PreventedException       the prevented exception
 */
protected void authenticateAndResolvePrincipal(final AuthenticationBuilder builder, final Credential credential, final PrincipalResolver resolver, final AuthenticationHandler handler) throws GeneralSecurityException, PreventedException {
    publishEvent(new CasAuthenticationTransactionStartedEvent(this, credential));
    final AuthenticationHandlerExecutionResult result = handler.authenticate(credential);
    builder.addSuccess(handler.getName(), result);
    LOGGER.debug("Authentication handler [{}] successfully authenticated [{}]", handler.getName(), credential);
    publishEvent(new CasAuthenticationTransactionSuccessfulEvent(this, credential));
    Principal principal = result.getPrincipal();
    final String resolverName = resolver != null ? resolver.getClass().getSimpleName() : "N/A";
    if (resolver == null) {
        LOGGER.debug("No principal resolution is configured for [{}]. Falling back to handler principal [{}]", handler.getName(), principal);
    } else {
        principal = resolvePrincipal(handler, resolver, credential, principal);
        if (principal == null) {
            if (this.principalResolutionFailureFatal) {
                LOGGER.warn("Principal resolution handled by [{}] produced a null principal for: [{}]" + "CAS is configured to treat principal resolution failures as fatal.", resolverName, credential);
                throw new UnresolvedPrincipalException();
            }
            LOGGER.warn("Principal resolution handled by [{}] produced a null principal. " + "This is likely due to misconfiguration or missing attributes; CAS will attempt to use the principal " + "produced by the authentication handler, if any.", resolver.getClass().getSimpleName());
        }
    }
    if (principal == null) {
        LOGGER.warn("Principal resolution for authentication by [{}] produced a null principal.", handler.getName());
    } else {
        builder.setPrincipal(principal);
    }
    LOGGER.debug("Final principal resolved for this authentication event is [{}]", principal);
    publishEvent(new CasAuthenticationPrincipalResolvedEvent(this, principal));
}
Also used : CasAuthenticationPrincipalResolvedEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPrincipalResolvedEvent) CasAuthenticationTransactionStartedEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionStartedEvent) UnresolvedPrincipalException(org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException) CasAuthenticationTransactionSuccessfulEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionSuccessfulEvent) NullPrincipal(org.apereo.cas.authentication.principal.NullPrincipal) Principal(org.apereo.cas.authentication.principal.Principal)

Aggregations

UnresolvedPrincipalException (org.apereo.cas.authentication.exceptions.UnresolvedPrincipalException)6 NullPrincipal (org.apereo.cas.authentication.principal.NullPrincipal)5 Principal (org.apereo.cas.authentication.principal.Principal)4 CasAuthenticationPrincipalResolvedEvent (org.apereo.cas.support.events.authentication.CasAuthenticationPrincipalResolvedEvent)3 CasAuthenticationTransactionStartedEvent (org.apereo.cas.support.events.authentication.CasAuthenticationTransactionStartedEvent)3 CasAuthenticationTransactionSuccessfulEvent (org.apereo.cas.support.events.authentication.CasAuthenticationTransactionSuccessfulEvent)3 Audit (org.apereo.inspektr.audit.annotation.Audit)3 Counted (com.codahale.metrics.annotation.Counted)2 Metered (com.codahale.metrics.annotation.Metered)2 Timed (com.codahale.metrics.annotation.Timed)2 lombok.val (lombok.val)2