Search in sources :

Example 1 with NameIDType

use of org.opensaml.saml.saml2.core.NameIDType in project cas by apereo.

the class SamlProfileSamlAttributeStatementBuilderTests method verifyAttributeAsNameIDSameAsSubject.

@Test
public void verifyAttributeAsNameIDSameAsSubject() throws Exception {
    val service = getSamlRegisteredServiceForTestShib();
    service.getAttributeValueTypes().put("customNameId", NameIDType.class.getSimpleName());
    val adaptor = SamlRegisteredServiceServiceProviderMetadataFacade.get(samlRegisteredServiceCachingMetadataResolver, service, service.getServiceId()).get();
    val buildContext = SamlProfileBuilderContext.builder().samlRequest(getAuthnRequestFor(service)).httpRequest(new MockHttpServletRequest()).httpResponse(new MockHttpServletResponse()).authenticatedAssertion(getAssertion(Map.of("customNameId", List.of(UUID.randomUUID().toString())))).registeredService(service).adaptor(adaptor).binding(SAMLConstants.SAML2_POST_BINDING_URI).build();
    val statement = samlProfileSamlAttributeStatementBuilder.build(buildContext);
    val attributes = statement.getAttributes();
    assertFalse(attributes.isEmpty());
    val result = attributes.stream().filter(a -> a.getName().equals("customNameId")).findFirst();
    assertTrue(result.isPresent());
    assertTrue(result.get().getAttributeValues().get(0) instanceof NameIDType);
}
Also used : lombok.val(lombok.val) lombok.val(lombok.val) Autowired(org.springframework.beans.factory.annotation.Autowired) TestPropertySource(org.springframework.test.context.TestPropertySource) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UUID(java.util.UUID) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) BaseSamlIdPConfigurationTests(org.apereo.cas.support.saml.BaseSamlIdPConfigurationTests) List(java.util.List) SamlProfileBuilderContext(org.apereo.cas.support.saml.web.idp.profile.builders.SamlProfileBuilderContext) SamlProfileObjectBuilder(org.apereo.cas.support.saml.web.idp.profile.builders.SamlProfileObjectBuilder) AttributeStatement(org.opensaml.saml.saml2.core.AttributeStatement) Map(java.util.Map) NameIDType(org.opensaml.saml.saml2.core.NameIDType) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Assertions(org.junit.jupiter.api.Assertions) SamlRegisteredServiceServiceProviderMetadataFacade(org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade) Tag(org.junit.jupiter.api.Tag) SAMLConstants(org.opensaml.saml.common.xml.SAMLConstants) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) NameIDType(org.opensaml.saml.saml2.core.NameIDType) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 2 with NameIDType

use of org.opensaml.saml.saml2.core.NameIDType in project cas by apereo.

the class SamlProfileSamlAttributeStatementBuilderTests method verifyAttributeAsNameIDPersistent.

@Test
public void verifyAttributeAsNameIDPersistent() throws Exception {
    val service = getSamlRegisteredServiceForTestShib();
    service.getAttributeValueTypes().put("customNameId", NameIDType.PERSISTENT);
    val adaptor = SamlRegisteredServiceServiceProviderMetadataFacade.get(samlRegisteredServiceCachingMetadataResolver, service, service.getServiceId()).get();
    val buildContext = SamlProfileBuilderContext.builder().samlRequest(getAuthnRequestFor(service)).httpRequest(new MockHttpServletRequest()).httpResponse(new MockHttpServletResponse()).authenticatedAssertion(getAssertion(Map.of("customNameId", List.of(UUID.randomUUID().toString())))).registeredService(service).adaptor(adaptor).binding(SAMLConstants.SAML2_POST_BINDING_URI).build();
    val statement = samlProfileSamlAttributeStatementBuilder.build(buildContext);
    val attributes = statement.getAttributes();
    assertFalse(attributes.isEmpty());
    val result = attributes.stream().filter(a -> a.getName().equals("customNameId")).findFirst();
    assertTrue(result.isPresent());
    assertTrue(result.get().getAttributeValues().get(0) instanceof NameIDType);
}
Also used : lombok.val(lombok.val) lombok.val(lombok.val) Autowired(org.springframework.beans.factory.annotation.Autowired) TestPropertySource(org.springframework.test.context.TestPropertySource) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UUID(java.util.UUID) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) BaseSamlIdPConfigurationTests(org.apereo.cas.support.saml.BaseSamlIdPConfigurationTests) List(java.util.List) SamlProfileBuilderContext(org.apereo.cas.support.saml.web.idp.profile.builders.SamlProfileBuilderContext) SamlProfileObjectBuilder(org.apereo.cas.support.saml.web.idp.profile.builders.SamlProfileObjectBuilder) AttributeStatement(org.opensaml.saml.saml2.core.AttributeStatement) Map(java.util.Map) NameIDType(org.opensaml.saml.saml2.core.NameIDType) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Assertions(org.junit.jupiter.api.Assertions) SamlRegisteredServiceServiceProviderMetadataFacade(org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade) Tag(org.junit.jupiter.api.Tag) SAMLConstants(org.opensaml.saml.common.xml.SAMLConstants) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) NameIDType(org.opensaml.saml.saml2.core.NameIDType) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with NameIDType

use of org.opensaml.saml.saml2.core.NameIDType in project cas by apereo.

the class AbstractSamlObjectBuilder method newAttributeValue.

/**
 * New attribute value.
 *
 * @param value       the value
 * @param valueType   the value type
 * @param elementName the element name
 * @return the xS string
 */
protected XMLObject newAttributeValue(final Object value, final String valueType, final QName elementName) {
    LOGGER.trace("Creating new attribute value XMLObject for value: [{}], value type: [{}], QName: [{}]", value, valueType, elementName);
    if (value instanceof NameIDType) {
        LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, value);
        ((NameIDType) value).detach();
        return (NameIDType) value;
    }
    if (XSString.class.getSimpleName().equalsIgnoreCase(valueType)) {
        val builder = new XSStringBuilder();
        val attrValueObj = builder.buildObject(elementName, XSString.TYPE_NAME);
        attrValueObj.setValue(value.toString());
        LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, attrValueObj);
        return attrValueObj;
    }
    if (XSURI.class.getSimpleName().equalsIgnoreCase(valueType)) {
        val builder = new XSURIBuilder();
        val attrValueObj = builder.buildObject(elementName, XSURI.TYPE_NAME);
        attrValueObj.setURI(value.toString());
        LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, attrValueObj);
        return attrValueObj;
    }
    if (XSBoolean.class.getSimpleName().equalsIgnoreCase(valueType)) {
        val builder = new XSBooleanBuilder();
        val attrValueObj = builder.buildObject(elementName, XSBoolean.TYPE_NAME);
        attrValueObj.setValue(XSBooleanValue.valueOf(value.toString().toLowerCase()));
        LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, attrValueObj);
        return attrValueObj;
    }
    if (XSInteger.class.getSimpleName().equalsIgnoreCase(valueType)) {
        val builder = new XSIntegerBuilder();
        val attrValueObj = builder.buildObject(elementName, XSInteger.TYPE_NAME);
        attrValueObj.setValue(Integer.valueOf(value.toString()));
        LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, attrValueObj);
        return attrValueObj;
    }
    if (XSDateTime.class.getSimpleName().equalsIgnoreCase(valueType)) {
        val builder = new XSDateTimeBuilder();
        val attrValueObj = builder.buildObject(elementName, XSDateTime.TYPE_NAME);
        attrValueObj.setValue(ZonedDateTime.parse(value.toString()).toInstant());
        LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, attrValueObj);
        return attrValueObj;
    }
    if (XSBase64Binary.class.getSimpleName().equalsIgnoreCase(valueType)) {
        val builder = new XSBase64BinaryBuilder();
        val attrValueObj = builder.buildObject(elementName, XSBase64Binary.TYPE_NAME);
        attrValueObj.setValue(value.toString());
        LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, attrValueObj);
        return attrValueObj;
    }
    if (XSObject.class.getSimpleName().equalsIgnoreCase(valueType)) {
        val mapper = new JacksonXmlSerializer();
        val builder = new XSAnyBuilder();
        val attrValueObj = builder.buildObject(elementName);
        attrValueObj.setTextContent(mapper.writeValueAsString(value));
        LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, attrValueObj);
        return attrValueObj;
    }
    val builder = new XSAnyBuilder();
    val attrValueObj = builder.buildObject(elementName);
    attrValueObj.setTextContent(value.toString());
    LOGGER.trace(LOG_MESSAGE_ATTR_CREATED, attrValueObj);
    return attrValueObj;
}
Also used : lombok.val(lombok.val) XSBase64BinaryBuilder(org.opensaml.core.xml.schema.impl.XSBase64BinaryBuilder) XSBooleanBuilder(org.opensaml.core.xml.schema.impl.XSBooleanBuilder) XSIntegerBuilder(org.opensaml.core.xml.schema.impl.XSIntegerBuilder) XSBase64Binary(org.opensaml.core.xml.schema.XSBase64Binary) XSAnyBuilder(org.opensaml.core.xml.schema.impl.XSAnyBuilder) XSObject(org.apache.xerces.xs.XSObject) XSString(org.opensaml.core.xml.schema.XSString) XSStringBuilder(org.opensaml.core.xml.schema.impl.XSStringBuilder) XSURI(org.opensaml.core.xml.schema.XSURI) XSDateTime(org.opensaml.core.xml.schema.XSDateTime) XSDateTimeBuilder(org.opensaml.core.xml.schema.impl.XSDateTimeBuilder) XSBoolean(org.opensaml.core.xml.schema.XSBoolean) XSInteger(org.opensaml.core.xml.schema.XSInteger) JacksonXmlSerializer(org.apereo.cas.util.serialization.JacksonXmlSerializer) NameIDType(org.opensaml.saml.saml2.core.NameIDType) XSURIBuilder(org.opensaml.core.xml.schema.impl.XSURIBuilder)

Aggregations

lombok.val (lombok.val)3 NameIDType (org.opensaml.saml.saml2.core.NameIDType)3 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 BaseSamlIdPConfigurationTests (org.apereo.cas.support.saml.BaseSamlIdPConfigurationTests)2 SamlRegisteredServiceServiceProviderMetadataFacade (org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade)2 SamlProfileBuilderContext (org.apereo.cas.support.saml.web.idp.profile.builders.SamlProfileBuilderContext)2 SamlProfileObjectBuilder (org.apereo.cas.support.saml.web.idp.profile.builders.SamlProfileObjectBuilder)2 Assertions (org.junit.jupiter.api.Assertions)2 Tag (org.junit.jupiter.api.Tag)2 Test (org.junit.jupiter.api.Test)2 SAMLConstants (org.opensaml.saml.common.xml.SAMLConstants)2 AttributeStatement (org.opensaml.saml.saml2.core.AttributeStatement)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Qualifier (org.springframework.beans.factory.annotation.Qualifier)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 TestPropertySource (org.springframework.test.context.TestPropertySource)2 XSObject (org.apache.xerces.xs.XSObject)1