Search in sources :

Example 6 with AuthenticationResultBuilder

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

the class RankedAuthenticationProviderWebflowEventResolver method resolveInternal.

@Override
public Set<Event> resolveInternal(final RequestContext context) {
    final String tgt = WebUtils.getTicketGrantingTicketId(context);
    final RegisteredService service = WebUtils.getRegisteredService(context);
    if (service == null) {
        LOGGER.debug("No service is available to determine event for principal");
        return resumeFlow();
    }
    if (StringUtils.isBlank(tgt)) {
        LOGGER.trace("TGT is blank; proceed with flow normally.");
        return resumeFlow();
    }
    final Authentication authentication = this.ticketRegistrySupport.getAuthenticationFrom(tgt);
    if (authentication == null) {
        LOGGER.trace("TGT has no authentication and is blank; proceed with flow normally.");
        return resumeFlow();
    }
    final Credential credential = WebUtils.getCredential(context);
    final AuthenticationResultBuilder builder = this.authenticationSystemSupport.establishAuthenticationContextFromInitial(authentication, credential);
    WebUtils.putAuthenticationResultBuilder(builder, context);
    WebUtils.putAuthentication(authentication, context);
    final Event event = this.initialAuthenticationAttemptWebflowEventResolver.resolveSingle(context);
    if (event == null) {
        LOGGER.trace("Request does not indicate a requirement for authentication policy; proceed with flow normally.");
        return resumeFlow();
    }
    final String id = event.getId();
    if (id.equals(CasWebflowConstants.TRANSITION_ID_ERROR) || id.equals(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE) || id.equals(CasWebflowConstants.TRANSITION_ID_SUCCESS)) {
        LOGGER.debug("Returning webflow event as [{}]", id);
        return Collections.singleton(event);
    }
    final Pair<Boolean, Optional<MultifactorAuthenticationProvider>> result = this.authenticationContextValidator.validate(authentication, id, service);
    if (result.getKey()) {
        LOGGER.debug("Authentication context is successfully validated by [{}] for service [{}]", id, service);
        return resumeFlow();
    }
    if (result.getValue().isPresent()) {
        return Collections.singleton(validateEventIdForMatchingTransitionInContext(id, context, buildEventAttributeMap(authentication.getPrincipal(), service, result.getValue().get())));
    }
    LOGGER.warn("The authentication context cannot be satisfied and the requested event [{}] is unrecognized", id);
    return Collections.singleton(new Event(this, CasWebflowConstants.TRANSITION_ID_ERROR));
}
Also used : Credential(org.apereo.cas.authentication.Credential) RegisteredService(org.apereo.cas.services.RegisteredService) Optional(java.util.Optional) Authentication(org.apereo.cas.authentication.Authentication) Event(org.springframework.webflow.execution.Event) AuthenticationResultBuilder(org.apereo.cas.authentication.AuthenticationResultBuilder)

Aggregations

AuthenticationResultBuilder (org.apereo.cas.authentication.AuthenticationResultBuilder)6 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)5 AuthenticationException (org.apereo.cas.authentication.AuthenticationException)5 Credential (org.apereo.cas.authentication.Credential)5 Service (org.apereo.cas.authentication.principal.Service)5 RegisteredService (org.apereo.cas.services.RegisteredService)4 Authentication (org.apereo.cas.authentication.Authentication)3 AuthenticationResult (org.apereo.cas.authentication.AuthenticationResult)3 InvalidTicketException (org.apereo.cas.ticket.InvalidTicketException)3 ServiceTicket (org.apereo.cas.ticket.ServiceTicket)3 Event (org.springframework.webflow.execution.Event)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AbstractTicketException (org.apereo.cas.ticket.AbstractTicketException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Optional (java.util.Optional)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 DefaultAuthenticationResultBuilder (org.apereo.cas.authentication.DefaultAuthenticationResultBuilder)1 MessageBuilder (org.springframework.binding.message.MessageBuilder)1 MessageContext (org.springframework.binding.message.MessageContext)1 ResponseEntity (org.springframework.http.ResponseEntity)1