Search in sources :

Example 1 with CasAuthenticationPolicyFailureEvent

use of org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent in project cas by apereo.

the class PolicyBasedAuthenticationManager method evaluateFinalAuthentication.

/**
 * Evaluate produced authentication context.
 * We apply an implicit security policy of at least one successful authentication.
 * Then, we apply the configured security policy.
 *
 * @param builder     the builder
 * @param transaction the transaction
 * @throws AuthenticationException the authentication exception
 */
protected void evaluateFinalAuthentication(final AuthenticationBuilder builder, final AuthenticationTransaction transaction) throws AuthenticationException {
    if (builder.getSuccesses().isEmpty()) {
        publishEvent(new CasAuthenticationTransactionFailureEvent(this, builder.getFailures(), transaction.getCredentials()));
        throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
    }
    final Authentication authentication = builder.build();
    final Pair<Boolean, Set<Throwable>> failures = evaluateAuthenticationPolicies(authentication, transaction);
    if (!failures.getKey()) {
        publishEvent(new CasAuthenticationPolicyFailureEvent(this, builder.getFailures(), transaction, authentication));
        failures.getValue().forEach(e -> handleAuthenticationException(e, e.getClass().getSimpleName(), builder));
        throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
    }
}
Also used : CasAuthenticationTransactionFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionFailureEvent) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) CasAuthenticationPolicyFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent)

Example 2 with CasAuthenticationPolicyFailureEvent

use of org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent in project cas by apereo.

the class CasAuthenticationEventListenerTests method verifyCasAuthenticationPolicyFailureEvent.

@Test
public void verifyCasAuthenticationPolicyFailureEvent() {
    val event = new CasAuthenticationPolicyFailureEvent(this, CollectionUtils.wrap("error", new FailedLoginException()), new DefaultAuthenticationTransaction(CoreAuthenticationTestUtils.getService(), CollectionUtils.wrap(CoreAuthenticationTestUtils.getCredentialsWithSameUsernameAndPassword())), CoreAuthenticationTestUtils.getAuthentication());
    applicationContext.publishEvent(event);
    assertFalse(casEventRepository.load().findAny().isEmpty());
}
Also used : lombok.val(lombok.val) FailedLoginException(javax.security.auth.login.FailedLoginException) DefaultAuthenticationTransaction(org.apereo.cas.authentication.DefaultAuthenticationTransaction) CasAuthenticationPolicyFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with CasAuthenticationPolicyFailureEvent

use of org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent in project cas by apereo.

the class DefaultAuthenticationManager method evaluateFinalAuthentication.

/**
 * Evaluate produced authentication context.
 * We apply an implicit security policy of at least one successful authentication.
 * Then, we apply the configured security policy.
 *
 * @param builder                the builder
 * @param transaction            the transaction
 * @param authenticationHandlers the authentication handlers
 * @throws AuthenticationException the authentication exception
 */
protected void evaluateFinalAuthentication(final AuthenticationBuilder builder, final AuthenticationTransaction transaction, final Set<AuthenticationHandler> authenticationHandlers) throws AuthenticationException {
    if (builder.getSuccesses().isEmpty()) {
        publishEvent(new CasAuthenticationTransactionFailureEvent(this, builder.getFailures(), transaction.getCredentials()));
        throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
    }
    val authentication = builder.build();
    val executionResult = evaluateAuthenticationPolicies(authentication, transaction, authenticationHandlers);
    if (!executionResult.isSuccess()) {
        publishEvent(new CasAuthenticationPolicyFailureEvent(this, builder.getFailures(), transaction, authentication));
        executionResult.getFailures().forEach(e -> handleAuthenticationException(e, e.getClass().getSimpleName(), builder));
        throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
    }
}
Also used : lombok.val(lombok.val) CasAuthenticationTransactionFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionFailureEvent) CasAuthenticationPolicyFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent)

Example 4 with CasAuthenticationPolicyFailureEvent

use of org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent in project cas by apereo.

the class PolicyBasedAuthenticationManager method evaluateProducedAuthenticationContext.

/**
     * Evaluate produced authentication context.
     * We apply an implicit security policy of at least one successful authentication.
     * Then, we apply the configured security policy.
     *
     * @param builder     the builder
     * @param transaction the transaction
     * @throws AuthenticationException the authentication exception
     */
protected void evaluateProducedAuthenticationContext(final AuthenticationBuilder builder, final AuthenticationTransaction transaction) throws AuthenticationException {
    if (builder.getSuccesses().isEmpty()) {
        publishEvent(new CasAuthenticationTransactionFailureEvent(this, builder.getFailures(), transaction.getCredentials()));
        throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
    }
    LOGGER.debug("Executing authentication policy [{}]", this.authenticationPolicy);
    final Authentication authentication = builder.build();
    if (!this.authenticationPolicy.isSatisfiedBy(authentication)) {
        publishEvent(new CasAuthenticationPolicyFailureEvent(this, builder.getFailures(), transaction, authentication));
        throw new AuthenticationException(builder.getFailures(), builder.getSuccesses());
    }
}
Also used : CasAuthenticationTransactionFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationTransactionFailureEvent) CasAuthenticationPolicyFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent)

Example 5 with CasAuthenticationPolicyFailureEvent

use of org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent in project cas by apereo.

the class DefaultCasEventListener method handleCasAuthenticationPolicyFailureEvent.

/**
 * Handle cas authentication policy failure event.
 *
 * @param event the event
 */
@EventListener
public void handleCasAuthenticationPolicyFailureEvent(final CasAuthenticationPolicyFailureEvent event) {
    if (this.casEventRepository != null) {
        final CasEvent dto = prepareCasEvent(event);
        dto.setPrincipalId(event.getAuthentication().getPrincipal().getId());
        dto.putId(CasAuthenticationPolicyFailureEvent.class.getSimpleName());
        this.casEventRepository.save(dto);
    }
}
Also used : AbstractCasEvent(org.apereo.cas.support.events.AbstractCasEvent) CasEvent(org.apereo.cas.support.events.dao.CasEvent) CasAuthenticationPolicyFailureEvent(org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent) EventListener(org.springframework.context.event.EventListener)

Aggregations

CasAuthenticationPolicyFailureEvent (org.apereo.cas.support.events.authentication.CasAuthenticationPolicyFailureEvent)6 lombok.val (lombok.val)3 CasAuthenticationTransactionFailureEvent (org.apereo.cas.support.events.authentication.CasAuthenticationTransactionFailureEvent)3 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 DefaultAuthenticationTransaction (org.apereo.cas.authentication.DefaultAuthenticationTransaction)1 AbstractCasEvent (org.apereo.cas.support.events.AbstractCasEvent)1 CasEvent (org.apereo.cas.support.events.dao.CasEvent)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 EventListener (org.springframework.context.event.EventListener)1