Search in sources :

Example 1 with InternalAttributeHandler

use of org.pac4j.core.profile.InternalAttributeHandler in project pac4j by pac4j.

the class CasAuthenticator method validate.

@Override
public void validate(final TokenCredentials credentials, final WebContext context) {
    init();
    final String ticket = credentials.getToken();
    try {
        final String finalCallbackUrl = callbackUrlResolver.compute(urlResolver, callbackUrl, clientName, context);
        final Assertion assertion = configuration.retrieveTicketValidator(context).validate(ticket, finalCallbackUrl);
        final AttributePrincipal principal = assertion.getPrincipal();
        logger.debug("principal: {}", principal);
        final String id = principal.getName();
        final Map<String, Object> newPrincipalAttributes = new HashMap<>();
        final Map<String, Object> newAuthenticationAttributes = new HashMap<>();
        // restore both sets of attributes
        final Map<String, Object> oldPrincipalAttributes = principal.getAttributes();
        final Map<String, Object> oldAuthenticationAttributes = assertion.getAttributes();
        final InternalAttributeHandler attrHandler = ProfileHelper.getInternalAttributeHandler();
        if (oldPrincipalAttributes != null) {
            oldPrincipalAttributes.entrySet().stream().forEach(e -> newPrincipalAttributes.put(e.getKey(), attrHandler.restore(e.getValue())));
        }
        if (oldAuthenticationAttributes != null) {
            oldAuthenticationAttributes.entrySet().stream().forEach(e -> newAuthenticationAttributes.put(e.getKey(), attrHandler.restore(e.getValue())));
        }
        final CommonProfile profile;
        // in case of CAS proxy, don't restore the profile, just build a CAS one
        if (configuration.getProxyReceptor() != null) {
            profile = getProfileDefinition().newProfile(principal, configuration.getProxyReceptor());
            profile.setId(ProfileHelper.sanitizeIdentifier(profile, id));
            getProfileDefinition().convertAndAdd(profile, newPrincipalAttributes, newAuthenticationAttributes);
        } else {
            profile = ProfileHelper.restoreOrBuildProfile(getProfileDefinition(), id, newPrincipalAttributes, newAuthenticationAttributes, principal, configuration.getProxyReceptor());
        }
        logger.debug("profile returned by CAS: {}", profile);
        credentials.setUserProfile(profile);
    } catch (final TicketValidationException e) {
        String message = "cannot validate CAS ticket: " + ticket;
        throw new TechnicalException(message, e);
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) InternalAttributeHandler(org.pac4j.core.profile.InternalAttributeHandler) HashMap(java.util.HashMap) CommonProfile(org.pac4j.core.profile.CommonProfile) Assertion(org.jasig.cas.client.validation.Assertion) AttributePrincipal(org.jasig.cas.client.authentication.AttributePrincipal) TicketValidationException(org.jasig.cas.client.validation.TicketValidationException)

Aggregations

HashMap (java.util.HashMap)1 AttributePrincipal (org.jasig.cas.client.authentication.AttributePrincipal)1 Assertion (org.jasig.cas.client.validation.Assertion)1 TicketValidationException (org.jasig.cas.client.validation.TicketValidationException)1 TechnicalException (org.pac4j.core.exception.TechnicalException)1 CommonProfile (org.pac4j.core.profile.CommonProfile)1 InternalAttributeHandler (org.pac4j.core.profile.InternalAttributeHandler)1