Java Code Examples for org.opensaml.saml2.core.Attribute#getName()

The following examples show how to use org.opensaml.saml2.core.Attribute#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttributeQuerySchemaValidator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks that all the attributes have a unique Name/NameFormat pair.
 * 
 * @param query the attribute query to validate
 * 
 * @throws ValidationException thrown if more than on Name/NameFormat pair is found in the list of attributes in
 *             this query
 */
protected void validateUniqueAttributeIdentifiers(AttributeQuery query) throws ValidationException {
    List<Attribute> attributes = query.getAttributes();

    HashSet<Pair<String, String>> encounteredNames = new HashSet<Pair<String, String>>();
    String attributeName;
    String attributeNameFormat;
    for (Attribute attribute : attributes) {
        attributeName = attribute.getName();
        attributeNameFormat = attribute.getNameFormat();
        if (DatatypeHelper.isEmpty(attributeNameFormat)) {
            // SAML 2 core, sec. 2.7.3.1, if no format is specified,
            // unspecified is in effect. This avoids bug in processing null value.
            attributeNameFormat = Attribute.UNSPECIFIED;
        }
        
        Pair<String, String> pair = new Pair<String, String>(attributeName, attributeNameFormat);
        if (encounteredNames.contains(pair)) {
            throw new ValidationException(
                    "Attribute query contains more than one attribute with the same Name and NameFormat");
        } else {
            encounteredNames.add(pair);
        }
    }
}
 
Example 2
Source File: AttributeMarshaller.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
protected void marshallAttributes(XMLObject samlElement, Element domElement) throws MarshallingException {
    Attribute attribute = (Attribute) samlElement;

    if (attribute.getName() != null) {
        domElement.setAttributeNS(null, Attribute.NAME_ATTTRIB_NAME, attribute.getName());
    }

    if (attribute.getNameFormat() != null) {
        domElement.setAttributeNS(null, Attribute.NAME_FORMAT_ATTRIB_NAME, attribute.getNameFormat());
    }

    if (attribute.getFriendlyName() != null) {
        domElement.setAttributeNS(null, Attribute.FRIENDLY_NAME_ATTRIB_NAME, attribute.getFriendlyName());
    }

    Attr attr;
    for (Entry<QName, String> entry : attribute.getUnknownAttributes().entrySet()) {
        attr = XMLHelper.constructAttribute(domElement.getOwnerDocument(), entry.getKey());
        attr.setValue(entry.getValue());
        domElement.setAttributeNodeNS(attr);
        if (Configuration.isIDAttribute(entry.getKey())
                || attribute.getUnknownAttributes().isIDAttribute(entry.getKey())) {
            attr.getOwnerElement().setIdAttributeNode(attr, true);
        }
    }
}
 
Example 3
Source File: SAML2SSOAuthenticator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Get the username from the SAML2 Assertion
 *
 * @param assertion SAML2 assertion
 * @return username
 */
private String[] getRolesFromAssertion(Assertion assertion) {
    String[] roles = null;
    String roleClaim = getRoleClaim();
    List<AttributeStatement> attributeStatementList = assertion.getAttributeStatements();

    if (attributeStatementList != null) {
        for (AttributeStatement statement : attributeStatementList) {
            List<Attribute> attributesList = statement.getAttributes();
            for (Attribute attribute : attributesList) {
                String attributeName = attribute.getName();
                if (attributeName != null && roleClaim.equals(attributeName)) {
                    // Assumes role claim appear only once
                    Element value = attribute.getAttributeValues().get(0).getDOM();
                    String attributeValue = value.getTextContent();

                    if (log.isDebugEnabled()) {
                        log.debug("AttributeName : " + attributeName + ", AttributeValue : " + attributeValue);
                    }

                    roles = attributeValue.split(getAttributeSeperator());
                    if (log.isDebugEnabled()) {
                        log.debug("Role list : " + Arrays.toString(roles));
                    }
                }
            }
        }
    }
    return roles;
}