private void assertAttributeValue(List<Attribute> attributeList, String name, String... expectedValue) { for (Attribute attribute : attributeList) { if (attribute.getName().equals(name)) { List<XMLObject> xsString = attribute.getAttributeValues(); List<String> attributeValues = xsString.stream().map(xs -> ((XSString)xs).getValue()).collect(Collectors.toList()); assertThat(String.format("Attribute mismatch for '%s'.", name), attributeValues, containsInAnyOrder(expectedValue)); return; } } Assert.fail(String.format("No attribute value with name of '%s'.", name)); }
public Attribute buildStringAttribute(String name, List<String> values) { @SuppressWarnings("unchecked") SAMLObjectBuilder<Attribute> attributeBuilder = (SAMLObjectBuilder<Attribute>) builderFactory .getBuilder(Attribute.DEFAULT_ELEMENT_NAME); Attribute attribute = (Attribute) attributeBuilder.buildObject(); attribute.setName(name); @SuppressWarnings("unchecked") XMLObjectBuilder<XSString> xsStringBuilder = (XMLObjectBuilder<XSString>) builderFactory .getBuilder(XSString.TYPE_NAME); for (String value : values) { // Set custom Attributes XSString attributeValue = xsStringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME); attributeValue.setValue(value); attribute.getAttributeValues().add(attributeValue); } return attribute; }
public Collection<? extends GrantedAuthority> retrieveSamlAuthorities(SamlIdentityProviderDefinition definition, SAMLCredential credential) { Collection<SamlUserAuthority> authorities = new ArrayList<>(); if (definition.getAttributeMappings().get(GROUP_ATTRIBUTE_NAME)!=null) { List<String> attributeNames = new LinkedList<>(); if (definition.getAttributeMappings().get(GROUP_ATTRIBUTE_NAME) instanceof String) { attributeNames.add((String) definition.getAttributeMappings().get(GROUP_ATTRIBUTE_NAME)); } else if (definition.getAttributeMappings().get(GROUP_ATTRIBUTE_NAME) instanceof Collection) { attributeNames.addAll((Collection) definition.getAttributeMappings().get(GROUP_ATTRIBUTE_NAME)); } for (Attribute attribute : credential.getAttributes()) { if ((attributeNames.contains(attribute.getName())) || (attributeNames.contains(attribute.getFriendlyName()))) { if (attribute.getAttributeValues() != null && attribute.getAttributeValues().size() > 0) { for (XMLObject group : attribute.getAttributeValues()) { authorities.add(new SamlUserAuthority(getStringValue(attribute.getName(),definition,group))); } } } } } return authorities == null ? Collections.EMPTY_LIST : authorities; }
private static Attribute buildAttribute(String name, List<String> values) { XSStringBuilder stringBuilder = (XSStringBuilder) Configuration.getBuilderFactory().getBuilder(XSString.TYPE_NAME); Attribute attribute = buildSAMLObject(Attribute.class, Attribute.DEFAULT_ELEMENT_NAME); attribute.setName(name); attribute.setNameFormat("urn:oasis:names:tc:SAML:2.0:attrname-format:uri"); List<XSString> xsStringList = values.stream().map(value -> { XSString stringValue = stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME); stringValue.setValue(value); return stringValue; }).collect(toList()); attribute.getAttributeValues().addAll(xsStringList); return attribute; }
public MultiValueMap<String, String> retrieveUserAttributes(SamlIdentityProviderDefinition definition, SAMLCredential credential) { logger.debug(String.format("Retrieving SAML user attributes [zone:%s, origin:%s]", definition.getZoneId(), definition.getIdpEntityAlias())); MultiValueMap<String, String> userAttributes = new LinkedMultiValueMap<>(); if (definition != null && definition.getAttributeMappings() != null) { for (Entry<String, Object> attributeMapping : definition.getAttributeMappings().entrySet()) { if (attributeMapping.getValue() instanceof String) { if (credential.getAttribute((String)attributeMapping.getValue()) != null) { String key = attributeMapping.getKey(); for (XMLObject xmlObject : credential.getAttribute((String) attributeMapping.getValue()).getAttributeValues()) { String value = getStringValue(key, definition, xmlObject); if (value!=null) { userAttributes.add(key, value); } } } } } } if (credential.getAuthenticationAssertion() != null && credential.getAuthenticationAssertion().getAuthnStatements() != null) { for (AuthnStatement statement : credential.getAuthenticationAssertion().getAuthnStatements()) { if (statement.getAuthnContext() != null && statement.getAuthnContext().getAuthnContextClassRef() != null) { userAttributes.add(AUTHENTICATION_CONTEXT_CLASS_REFERENCE, statement.getAuthnContext().getAuthnContextClassRef().getAuthnContextClassRef()); } } } return userAttributes; }
/** {@inheritDoc} */ public Attribute encode(BaseAttribute attribute) throws AttributeEncodingException { Attribute samlAttribute = attributeBuilder.buildObject(); samlAttribute.setName(getAttributeName()); samlAttribute.setNameFormat(getNameFormat()); samlAttribute.setFriendlyName(getFriendlyName()); samlAttribute.getAttributeValues() .addAll(encodeAttributeValues(AttributeValue.DEFAULT_ELEMENT_NAME, attribute)); List<XMLObject> attributeValues = samlAttribute.getAttributeValues(); if (attributeValues == null || attributeValues.isEmpty()) { log.debug("Unable to encode {} attribute. It does not contain any values", attribute.getId()); return null; } return samlAttribute; } }
private String[] getValueArray(Attribute attribute) { return getAttributeArray(attribute.getName()); } }
String attrNamesapce = attr.getNameFormat(); String attrName = attr.getName(); String name = attrNamesapce + "/" + attrName; List attributeValues = attr.getAttributeValues(); Iterator values = attributeValues.iterator(); StringBuilder buff = new StringBuilder();
/** QName for the attribute resource */ public static Attribute createAttribute(String name, String friendlyName,String nameFormat) { Attribute attribute = new AttributeBuilder().buildObject(); attribute.setName(name); attribute.setFriendlyName(friendlyName); attribute.setNameFormat(nameFormat); return attribute; }
if (attributes != null) { for (Attribute attribute : attributes) { String attributeName = attribute.getDOM() .getAttribute("Name"); if (attributeName.equals(loginAttributeName)) { List<XMLObject> attributeValues = attribute .getAttributeValues();
/** {@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); } } } }
public SAMLAuthenticationInfo(Authentication authentication) throws MessageEncodingException{ SAMLCredential credential = (SAMLCredential) authentication.getCredentials(); //General general = new General(authentication); //Principal's SAML Attributes attributes = new ArrayList<>(); for(Attribute attr : credential.getAttributes()){ Map<String, String> attrMap = new HashMap<>(); attrMap.put("name", attr.getName()); attrMap.put("friendlyName", attr.getFriendlyName()); attrMap.put("value", credential.getAttributeAsString(attr.getName())); attributes.add(attrMap); } //Subject confirmation subjectConfirmation = new SubjectConfirmation(authentication); //Authentication statement authenticationStatement = new AuthenticationStatement(authentication); //Conditions conditions = new Conditions(authentication); //Assertion XML assertion = XMLHelper.nodeToString(SAMLUtil.marshallMessage(credential.getAuthenticationAssertion())); }
/** {@inheritDoc} */ protected void processAttribute(XMLObject samlObject, Attr attribute) throws UnmarshallingException { Attribute attrib = (Attribute) samlObject; if (attribute.getLocalName().equals(Attribute.NAME_ATTTRIB_NAME)) { attrib.setName(attribute.getValue()); } else if (attribute.getLocalName().equals(Attribute.NAME_FORMAT_ATTRIB_NAME)) { attrib.setNameFormat(attribute.getValue()); } else if (attribute.getLocalName().equals(Attribute.FRIENDLY_NAME_ATTRIB_NAME)) { attrib.setFriendlyName(attribute.getValue()); } else { QName attribQName = XMLHelper.getNodeQName(attribute); if (attribute.isId()) { attrib.getUnknownAttributes().registerID(attribQName); } attrib.getUnknownAttributes().put(attribQName, attribute.getValue()); } } }
String attributeNameFormat; for (Attribute attribute : attributes) { attributeName = attribute.getName(); attributeNameFormat = attribute.getNameFormat(); if (DatatypeHelper.isEmpty(attributeNameFormat)) {
@Override public Attribute build() { Attribute attribute = new AttributeBuilder().buildObject(); XSString xsString = new XSStringBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME); attribute.setName(attributeName); attribute.setNameFormat(attributeFormat); xsString.setValue(attributeValue); attribute.getAttributeValues().add(xsString); return attribute; }
/** * This method takes an attribute and extracts the base64Encoded value from the first * attribute value. * * @param attrib The attribute containing the string value. * @return The string value (or if there are multiple values, the concatenated string value.) */ private static byte[] extractFirstAttributeValueBase64Binary(Attribute attrib) { byte[] retValue = null; List attrVals = attrib.getAttributeValues(); if ((attrVals != null) && (attrVals.size() > 0)) { if (attrVals.get(0) instanceof byte[]) { retValue = (byte[]) attrVals.get(0); } } return retValue; }
private String[] getValueArray(Attribute attribute) { return getAttributeArray(attribute.getName()); } }
/** * Create an Attribute object. * * @param friendlyName of type String * @param name of type String * @param nameFormat of type String * @return an Attribute object */ @SuppressWarnings("unchecked") public static Attribute createAttribute(String friendlyName, String name, String nameFormat) { if (attributeBuilder == null) { attributeBuilder = (SAMLObjectBuilder<Attribute>) builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME); } Attribute attribute = attributeBuilder.buildObject(); attribute.setFriendlyName(friendlyName); if (nameFormat == null) { attribute.setNameFormat(SAML2Constants.ATTRNAME_FORMAT_URI); } else { attribute.setNameFormat(nameFormat); } attribute.setName(name); return attribute; }
if (attributes != null) { for (Attribute attribute : attributes) { String attributeName = attribute.getDOM() .getAttribute("Name"); if (attributeName.equals(loginAttributeName)) { List<XMLObject> attributeValues = attribute .getAttributeValues();
attrMap.put("name", attr.getName()); attrMap.put("friendlyName", attr.getFriendlyName()); attrMap.put("value", credential.getAttributeAsString(attr.getName())); LOGGER.debug("name : {}, value : {}, friendlyName : {}", attrMap.get("name"), attrMap.get("value"), attrMap.get("friendlyName"));