Search in sources :

Example 1 with Attribute

use of org.opensaml.saml.saml1.core.Attribute in project OpenAttestation by OpenAttestation.

the class SamlGenerator method createStringAttribute.

// create the host attributes 
/**
         * An attribute can be multi-valued, but this method builds a single-valued
         * String attribute such as FirstName=John or IPAddress=1.2.3.4
         * @param name
         * @param value
         * @return
         * @throws ConfigurationException 
         */
private Attribute createStringAttribute(String name, String value) throws ConfigurationException {
    SAMLObjectBuilder attrBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
    Attribute attr = (Attribute) attrBuilder.buildObject();
    attr.setName(name);
    XMLObjectBuilder xmlBuilder = builderFactory.getBuilder(XSString.TYPE_NAME);
    XSString attrValue = (XSString) xmlBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
    attrValue.setValue(value);
    attr.getAttributeValues().add(attrValue);
    return attr;
}
Also used : SAMLObjectBuilder(org.opensaml.common.SAMLObjectBuilder) Attribute(org.opensaml.saml2.core.Attribute) XMLObjectBuilder(org.opensaml.xml.XMLObjectBuilder) XSString(org.opensaml.xml.schema.XSString)

Example 2 with Attribute

use of org.opensaml.saml.saml1.core.Attribute in project cas by apereo.

the class AbstractSaml20ObjectBuilder method newAttributeStatement.

/**
     * New attribute statement.
     *
     * @param attributes            the attributes
     * @param setFriendlyName       the set friendly name
     * @param configuredNameFormats the configured name formats
     * @return the attribute statement
     */
public AttributeStatement newAttributeStatement(final Map<String, Object> attributes, final boolean setFriendlyName, final Map<String, String> configuredNameFormats) {
    final AttributeStatement attrStatement = newSamlObject(AttributeStatement.class);
    for (final Map.Entry<String, Object> e : attributes.entrySet()) {
        if (e.getValue() instanceof Collection<?> && ((Collection<?>) e.getValue()).isEmpty()) {
            LOGGER.info("Skipping attribute [{}] because it does not have any values.", e.getKey());
            continue;
        }
        final Attribute attribute = newAttribute(setFriendlyName, e, configuredNameFormats);
        attrStatement.getAttributes().add(attribute);
    }
    return attrStatement;
}
Also used : Attribute(org.opensaml.saml.saml2.core.Attribute) AttributeStatement(org.opensaml.saml.saml2.core.AttributeStatement) XMLObject(org.opensaml.core.xml.XMLObject) Map(java.util.Map)

Example 3 with Attribute

use of org.opensaml.saml.saml1.core.Attribute in project cas by apereo.

the class Saml10ObjectBuilder method newAttributeStatement.

/**
     * New attribute statement.
     *
     * @param subject the subject
     * @param attributes the attributes
     * @param attributeNamespace the attribute namespace
     * @return the attribute statement
     */
public AttributeStatement newAttributeStatement(final Subject subject, final Map<String, Object> attributes, final String attributeNamespace) {
    final AttributeStatement attrStatement = newSamlObject(AttributeStatement.class);
    attrStatement.setSubject(subject);
    for (final Map.Entry<String, Object> e : attributes.entrySet()) {
        if (e.getValue() instanceof Collection<?> && ((Collection<?>) e.getValue()).isEmpty()) {
            LOGGER.info("Skipping attribute [{}] because it does not have any values.", e.getKey());
            continue;
        }
        final Attribute attribute = newSamlObject(Attribute.class);
        attribute.setAttributeName(e.getKey());
        if (StringUtils.isNotBlank(attributeNamespace)) {
            attribute.setAttributeNamespace(attributeNamespace);
        }
        addAttributeValuesToSaml1Attribute(e.getKey(), e.getValue(), attribute.getAttributeValues());
        attrStatement.getAttributes().add(attribute);
    }
    return attrStatement;
}
Also used : Attribute(org.opensaml.saml.saml1.core.Attribute) AttributeStatement(org.opensaml.saml.saml1.core.AttributeStatement) XMLObject(org.opensaml.core.xml.XMLObject) SAMLObject(org.opensaml.saml.common.SAMLObject) Map(java.util.Map)

Example 4 with Attribute

use of org.opensaml.saml.saml1.core.Attribute in project ddf by codice.

the class SecurityPluginTest method setupMockSubject.

private Subject setupMockSubject() {
    XSString mockAttributeValue = mock(XSString.class);
    when(mockAttributeValue.getValue()).thenReturn(TEST_USER);
    List<XMLObject> listOfAttributeValues = Arrays.asList(mockAttributeValue);
    Attribute mockAttribute = mock(Attribute.class);
    when(mockAttribute.getName()).thenReturn(SubjectUtils.EMAIL_ADDRESS_CLAIM_URI);
    when(mockAttribute.getAttributeValues()).thenReturn(listOfAttributeValues);
    List<Attribute> listOfAttributes = Arrays.asList(mockAttribute);
    AttributeStatement mockAttributeStatement = mock(AttributeStatement.class);
    when(mockAttributeStatement.getAttributes()).thenReturn(listOfAttributes);
    List<AttributeStatement> listOfAttributeStatements = Arrays.asList(mockAttributeStatement);
    Subject mockSubject = mock(Subject.class);
    PrincipalCollection mockPrincipals = mock(PrincipalCollection.class);
    SecurityAssertion mockSecurityAssertion = mock(SecurityAssertion.class);
    when(mockSecurityAssertion.getAttributeStatements()).thenReturn(listOfAttributeStatements);
    when(mockPrincipals.oneByType(SecurityAssertion.class)).thenReturn(mockSecurityAssertion);
    when(mockSubject.getPrincipals()).thenReturn(mockPrincipals);
    return mockSubject;
}
Also used : Attribute(org.opensaml.saml.saml2.core.Attribute) AttributeStatement(org.opensaml.saml.saml2.core.AttributeStatement) XMLObject(org.opensaml.core.xml.XMLObject) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) XSString(org.opensaml.core.xml.schema.XSString) SecurityAssertion(ddf.security.assertion.SecurityAssertion) Subject(ddf.security.Subject)

Example 5 with Attribute

use of org.opensaml.saml.saml1.core.Attribute in project ddf by codice.

the class SubjectUtils method getAttribute.

/**
     * Get any attribute from a subject by key.
     *
     * @param subject
     * @param key
     * @return attribute values or an empty list if not found.
     */
public static List<String> getAttribute(@Nullable Subject subject, String key) {
    Validate.notNull(key);
    if (subject == null) {
        LOGGER.debug("Incoming subject was null, cannot look up {}.", key);
        return Collections.emptyList();
    }
    PrincipalCollection principals = subject.getPrincipals();
    if (principals == null) {
        LOGGER.debug("No principals located in the incoming subject, cannot look up {}.", key);
        return Collections.emptyList();
    }
    SecurityAssertion assertion = principals.oneByType(SecurityAssertion.class);
    if (assertion == null) {
        LOGGER.debug("Could not find Security Assertion, cannot look up {}.", key);
        return Collections.emptyList();
    }
    return assertion.getAttributeStatements().stream().flatMap(as -> as.getAttributes().stream()).filter(a -> a.getName().equals(key)).flatMap(a -> a.getAttributeValues().stream()).filter(o -> o instanceof XSString).map(o -> (XSString) o).map(XSString::getValue).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) X500Principal(javax.security.auth.x500.X500Principal) SortedSet(java.util.SortedSet) LoggerFactory(org.slf4j.LoggerFactory) BCStyle(org.bouncycastle.asn1.x500.style.BCStyle) TreeSet(java.util.TreeSet) AttributeTypeAndValue(org.bouncycastle.asn1.x500.AttributeTypeAndValue) X500Name(org.bouncycastle.asn1.x500.X500Name) Attribute(org.opensaml.saml.saml2.core.Attribute) Subject(org.apache.shiro.subject.Subject) AttributeStatement(org.opensaml.saml.saml2.core.AttributeStatement) StringTokenizer(java.util.StringTokenizer) Map(java.util.Map) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) XSString(org.opensaml.core.xml.schema.XSString) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) Nullable(javax.annotation.Nullable) SecurityAssertion(ddf.security.assertion.SecurityAssertion) Logger(org.slf4j.Logger) RDN(org.bouncycastle.asn1.x500.RDN) Predicate(java.util.function.Predicate) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) GuestPrincipal(ddf.security.principal.GuestPrincipal) List(java.util.List) Principal(java.security.Principal) Collections(java.util.Collections) Validate(org.apache.commons.lang.Validate) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) XSString(org.opensaml.core.xml.schema.XSString) SecurityAssertion(ddf.security.assertion.SecurityAssertion)

Aggregations

Attribute (org.opensaml.saml.saml2.core.Attribute)9 AttributeStatement (org.opensaml.saml.saml2.core.AttributeStatement)7 XMLObject (org.opensaml.core.xml.XMLObject)5 Attribute (org.opensaml.saml2.core.Attribute)5 SecurityAssertion (ddf.security.assertion.SecurityAssertion)3 Map (java.util.Map)3 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)3 SAMLObjectBuilder (org.opensaml.common.SAMLObjectBuilder)3 XMLObjectBuilder (org.opensaml.xml.XMLObjectBuilder)3 GuestPrincipal (ddf.security.principal.GuestPrincipal)2 Principal (java.security.Principal)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 KerberosPrincipal (javax.security.auth.kerberos.KerberosPrincipal)2 X500Principal (javax.security.auth.x500.X500Principal)2 XSString (org.opensaml.core.xml.schema.XSString)2 AttributeStatement (org.opensaml.saml2.core.AttributeStatement)2 XSAny (org.opensaml.xml.schema.XSAny)2 Throwables (com.google.common.base.Throwables)1