Search in sources :

Example 1 with SAMLSelfEntityContext

use of org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext in project cas by apereo.

the class BaseSamlResponseEncoder method getEncoderMessageContext.

/**
 * Build encoder message context.
 *
 * @param samlResponse the saml response
 * @param relayState   the relay state
 * @return the message context
 */
protected MessageContext getEncoderMessageContext(final Response samlResponse, final String relayState) {
    final MessageContext ctx = new MessageContext<>();
    ctx.setMessage(samlResponse);
    SAMLBindingSupport.setRelayState(ctx, relayState);
    SamlIdPUtils.preparePeerEntitySamlEndpointContext(ctx, adaptor, getBinding());
    final SAMLSelfEntityContext self = ctx.getSubcontext(SAMLSelfEntityContext.class, true);
    self.setEntityId(samlResponse.getIssuer().getValue());
    return ctx;
}
Also used : SAMLSelfEntityContext(org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext) MessageContext(org.opensaml.messaging.context.MessageContext)

Example 2 with SAMLSelfEntityContext

use of org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext in project pac4j by pac4j.

the class SAML2LogoutRequestBuilder method buildLogoutRequest.

@SuppressWarnings("unchecked")
protected final LogoutRequest buildLogoutRequest(final SAML2MessageContext context, final AssertionConsumerService assertionConsumerService, final SingleLogoutService ssoService) {
    final SAMLObjectBuilder<LogoutRequest> builder = (SAMLObjectBuilder<LogoutRequest>) this.builderFactory.getBuilder(LogoutRequest.DEFAULT_ELEMENT_NAME);
    final LogoutRequest request = builder.buildObject();
    final SAMLSelfEntityContext selfContext = context.getSAMLSelfEntityContext();
    request.setID(generateID());
    request.setIssuer(getIssuer(selfContext.getEntityId()));
    request.setIssueInstant(DateTime.now(DateTimeZone.UTC).plusSeconds(this.issueInstantSkewSeconds));
    request.setVersion(SAMLVersion.VERSION_20);
    request.setDestination(ssoService.getLocation());
    // very very bad...
    ProfileManager manager = new ProfileManager(context.getWebContext());
    Optional<UserProfile> p = manager.get(true);
    if (p.isPresent() && p.get() instanceof SAML2Profile) {
        final SAML2Profile samlP = (SAML2Profile) p.get();
        // name id added (id of profile)
        final SAMLObjectBuilder<NameID> nameIdBuilder = (SAMLObjectBuilder<NameID>) this.builderFactory.getBuilder(NameID.DEFAULT_ELEMENT_NAME);
        final NameID nameId = nameIdBuilder.buildObject();
        nameId.setValue(samlP.getId());
        nameId.setFormat(samlP.getSamlNameIdFormat());
        nameId.setNameQualifier(samlP.getSamlNameIdNameQualifier());
        nameId.setSPNameQualifier(samlP.getSamlNameIdSpNameQualifier());
        nameId.setSPProvidedID(samlP.getSamlNameIdSpProviderId());
        request.setNameID(nameId);
        // session index added
        final String sessIdx = (String) samlP.getAttribute("sessionindex");
        final SAMLObjectBuilder<SessionIndex> sessionIndexBuilder = (SAMLObjectBuilder<SessionIndex>) this.builderFactory.getBuilder(SessionIndex.DEFAULT_ELEMENT_NAME);
        final SessionIndex sessionIdx = sessionIndexBuilder.buildObject();
        sessionIdx.setSessionIndex(sessIdx);
        request.getSessionIndexes().add(sessionIdx);
    }
    return request;
}
Also used : ProfileManager(org.pac4j.core.profile.ProfileManager) SAMLSelfEntityContext(org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext) UserProfile(org.pac4j.core.profile.UserProfile) SAMLObjectBuilder(org.opensaml.saml.common.SAMLObjectBuilder) NameID(org.opensaml.saml.saml2.core.NameID) SAML2Profile(org.pac4j.saml.profile.SAML2Profile) SessionIndex(org.opensaml.saml.saml2.core.SessionIndex) LogoutRequest(org.opensaml.saml.saml2.core.LogoutRequest)

Example 3 with SAMLSelfEntityContext

use of org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext in project pac4j by pac4j.

the class SAML2AuthnRequestBuilder method buildAuthnRequest.

@SuppressWarnings("unchecked")
protected final AuthnRequest buildAuthnRequest(final SAML2MessageContext context, final AssertionConsumerService assertionConsumerService, final SingleSignOnService ssoService) {
    final SAMLObjectBuilder<AuthnRequest> builder = (SAMLObjectBuilder<AuthnRequest>) this.builderFactory.getBuilder(AuthnRequest.DEFAULT_ELEMENT_NAME);
    final AuthnRequest request = builder.buildObject();
    if (comparisonType != null) {
        final RequestedAuthnContext authnContext = new RequestedAuthnContextBuilder().buildObject();
        authnContext.setComparison(comparisonType);
        if (authnContextClassRef != null) {
            final AuthnContextClassRef classRef = new AuthnContextClassRefBuilder().buildObject();
            classRef.setAuthnContextClassRef(authnContextClassRef);
            authnContext.getAuthnContextClassRefs().add(classRef);
        }
        request.setRequestedAuthnContext(authnContext);
    }
    final SAMLSelfEntityContext selfContext = context.getSAMLSelfEntityContext();
    request.setID(generateID());
    request.setIssuer(getIssuer(selfContext.getEntityId()));
    request.setIssueInstant(DateTime.now(DateTimeZone.UTC).plusSeconds(this.issueInstantSkewSeconds));
    request.setVersion(SAMLVersion.VERSION_20);
    request.setIsPassive(this.passive);
    request.setForceAuthn(this.forceAuth);
    request.setProviderName("pac4j-saml");
    if (nameIdPolicyFormat != null) {
        final NameIDPolicy nameIdPolicy = new NameIDPolicyBuilder().buildObject();
        nameIdPolicy.setAllowCreate(true);
        nameIdPolicy.setFormat(nameIdPolicyFormat);
        request.setNameIDPolicy(nameIdPolicy);
    }
    request.setDestination(ssoService.getLocation());
    if (assertionConsumerServiceIndex >= 0) {
        request.setAssertionConsumerServiceIndex(assertionConsumerServiceIndex);
    } else {
        request.setAssertionConsumerServiceURL(assertionConsumerService.getLocation());
    }
    request.setProtocolBinding(assertionConsumerService.getBinding());
    if (attributeConsumingServiceIndex >= 0) {
        request.setAttributeConsumingServiceIndex(attributeConsumingServiceIndex);
    }
    return request;
}
Also used : RequestedAuthnContextBuilder(org.opensaml.saml.saml2.core.impl.RequestedAuthnContextBuilder) RequestedAuthnContext(org.opensaml.saml.saml2.core.RequestedAuthnContext) SAMLSelfEntityContext(org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext) NameIDPolicyBuilder(org.opensaml.saml.saml2.core.impl.NameIDPolicyBuilder) AuthnRequest(org.opensaml.saml.saml2.core.AuthnRequest) SAMLObjectBuilder(org.opensaml.saml.common.SAMLObjectBuilder) NameIDPolicy(org.opensaml.saml.saml2.core.NameIDPolicy) AuthnContextClassRef(org.opensaml.saml.saml2.core.AuthnContextClassRef) AuthnContextClassRefBuilder(org.opensaml.saml.saml2.core.impl.AuthnContextClassRefBuilder)

Example 4 with SAMLSelfEntityContext

use of org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext in project pac4j by pac4j.

the class SAML2ContextProvider method addSPContext.

protected final void addSPContext(final SAML2MessageContext context) {
    final SAMLSelfEntityContext selfContext = context.getSAMLSelfEntityContext();
    selfContext.setEntityId(this.spEntityId.getEntityId());
    selfContext.setRole(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
    addContext(this.spEntityId, selfContext, SPSSODescriptor.DEFAULT_ELEMENT_NAME);
}
Also used : SAMLSelfEntityContext(org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext)

Aggregations

SAMLSelfEntityContext (org.opensaml.saml.common.messaging.context.SAMLSelfEntityContext)4 SAMLObjectBuilder (org.opensaml.saml.common.SAMLObjectBuilder)2 MessageContext (org.opensaml.messaging.context.MessageContext)1 AuthnContextClassRef (org.opensaml.saml.saml2.core.AuthnContextClassRef)1 AuthnRequest (org.opensaml.saml.saml2.core.AuthnRequest)1 LogoutRequest (org.opensaml.saml.saml2.core.LogoutRequest)1 NameID (org.opensaml.saml.saml2.core.NameID)1 NameIDPolicy (org.opensaml.saml.saml2.core.NameIDPolicy)1 RequestedAuthnContext (org.opensaml.saml.saml2.core.RequestedAuthnContext)1 SessionIndex (org.opensaml.saml.saml2.core.SessionIndex)1 AuthnContextClassRefBuilder (org.opensaml.saml.saml2.core.impl.AuthnContextClassRefBuilder)1 NameIDPolicyBuilder (org.opensaml.saml.saml2.core.impl.NameIDPolicyBuilder)1 RequestedAuthnContextBuilder (org.opensaml.saml.saml2.core.impl.RequestedAuthnContextBuilder)1 ProfileManager (org.pac4j.core.profile.ProfileManager)1 UserProfile (org.pac4j.core.profile.UserProfile)1 SAML2Profile (org.pac4j.saml.profile.SAML2Profile)1