Search in sources :

Example 1 with SamlAttributeQueryTicket

use of org.apereo.cas.ticket.query.SamlAttributeQueryTicket in project cas by apereo.

the class Saml2AttributeQueryProfileHandlerController method handlePostRequest.

/**
 * Handle post request.
 *
 * @param response the response
 * @param request  the request
 */
@PostMapping(path = SamlIdPConstants.ENDPOINT_SAML2_SOAP_ATTRIBUTE_QUERY)
protected void handlePostRequest(final HttpServletResponse response, final HttpServletRequest request) {
    final MessageContext ctx = decodeSoapRequest(request);
    final AttributeQuery query = (AttributeQuery) ctx.getMessage();
    try {
        final String issuer = query.getIssuer().getValue();
        final SamlRegisteredService service = verifySamlRegisteredService(issuer);
        final Optional<SamlRegisteredServiceServiceProviderMetadataFacade> adaptor = getSamlMetadataFacadeFor(service, query);
        if (!adaptor.isPresent()) {
            throw new UnauthorizedServiceException(UnauthorizedServiceException.CODE_UNAUTHZ_SERVICE, "Cannot find metadata linked to " + issuer);
        }
        final SamlRegisteredServiceServiceProviderMetadataFacade facade = adaptor.get();
        verifyAuthenticationContextSignature(ctx, request, query, facade);
        final Map<String, Object> attrs = new LinkedHashMap<>();
        if (query.getAttributes().isEmpty()) {
            final String id = this.samlAttributeQueryTicketFactory.createTicketIdFor(query.getSubject().getNameID().getValue());
            final SamlAttributeQueryTicket ticket = this.ticketRegistry.getTicket(id, SamlAttributeQueryTicket.class);
            final Authentication authentication = ticket.getTicketGrantingTicket().getAuthentication();
            final Principal principal = authentication.getPrincipal();
            final Map<String, Object> authnAttrs = authentication.getAttributes();
            final Map<String, Object> principalAttrs = principal.getAttributes();
            query.getAttributes().forEach(a -> {
                if (authnAttrs.containsKey(a.getName())) {
                    attrs.put(a.getName(), authnAttrs.get(a.getName()));
                } else if (principalAttrs.containsKey(a.getName())) {
                    attrs.put(a.getName(), principalAttrs.get(a.getName()));
                }
            });
        }
        final Assertion casAssertion = buildCasAssertion(issuer, service, attrs);
        this.responseBuilder.build(query, request, response, casAssertion, service, facade, SAMLConstants.SAML2_SOAP11_BINDING_URI);
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
        request.setAttribute(SamlIdPConstants.REQUEST_ATTRIBUTE_ERROR, e.getMessage());
        samlFaultResponseBuilder.build(query, request, response, null, null, null, SAMLConstants.SAML2_SOAP11_BINDING_URI);
    }
}
Also used : SamlAttributeQueryTicket(org.apereo.cas.ticket.query.SamlAttributeQueryTicket) SamlRegisteredServiceServiceProviderMetadataFacade(org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade) Assertion(org.jasig.cas.client.validation.Assertion) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) UnauthorizedServiceException(org.apereo.cas.services.UnauthorizedServiceException) LinkedHashMap(java.util.LinkedHashMap) AttributeQuery(org.opensaml.saml.saml2.core.AttributeQuery) Authentication(org.apereo.cas.authentication.Authentication) SamlRegisteredService(org.apereo.cas.support.saml.services.SamlRegisteredService) SAMLObject(org.opensaml.saml.common.SAMLObject) MessageContext(org.opensaml.messaging.context.MessageContext) Principal(org.apereo.cas.authentication.principal.Principal) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with SamlAttributeQueryTicket

use of org.apereo.cas.ticket.query.SamlAttributeQueryTicket in project cas by apereo.

the class SamlProfileSaml2ResponseBuilder method storeAttributeQueryTicketInRegistry.

private void storeAttributeQueryTicketInRegistry(final Assertion assertion, final HttpServletRequest request, final SamlRegisteredServiceServiceProviderMetadataFacade adaptor) {
    final String value = assertion.getSubject().getNameID().getValue();
    final TicketGrantingTicket ticketGrantingTicket = CookieUtils.getTicketGrantingTicketFromRequest(ticketGrantingTicketCookieGenerator, this.ticketRegistry, request);
    final SamlAttributeQueryTicket ticket = samlAttributeQueryTicketFactory.create(value, assertion, adaptor.getEntityId(), ticketGrantingTicket);
    this.ticketRegistry.addTicket(ticket);
}
Also used : SamlAttributeQueryTicket(org.apereo.cas.ticket.query.SamlAttributeQueryTicket) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket)

Aggregations

SamlAttributeQueryTicket (org.apereo.cas.ticket.query.SamlAttributeQueryTicket)2 LinkedHashMap (java.util.LinkedHashMap)1 Authentication (org.apereo.cas.authentication.Authentication)1 Principal (org.apereo.cas.authentication.principal.Principal)1 UnauthorizedServiceException (org.apereo.cas.services.UnauthorizedServiceException)1 SamlRegisteredService (org.apereo.cas.support.saml.services.SamlRegisteredService)1 SamlRegisteredServiceServiceProviderMetadataFacade (org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade)1 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)1 Assertion (org.jasig.cas.client.validation.Assertion)1 MessageContext (org.opensaml.messaging.context.MessageContext)1 SAMLObject (org.opensaml.saml.common.SAMLObject)1 AttributeQuery (org.opensaml.saml.saml2.core.AttributeQuery)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1