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; }
/** {@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); } } } }
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 List<Attribute> getAttributes(final String name, Object value) { Attribute attribute = mock(Attribute.class); when(attribute.getName()).thenReturn(name); when(attribute.getFriendlyName()).thenReturn(name);
private String getValueFromAttributeStatements(final Assertion assertion, final String name) { final List<AttributeStatement> attributeStatements = assertion.getAttributeStatements(); for (AttributeStatement attributeStatement : attributeStatements) { final List<Attribute> attributes = attributeStatement.getAttributes(); for (Attribute attribute : attributes) { if (name.equals(attribute.getName())) { return attribute.getAttributeValues().get(0).getDOM().getTextContent(); } } } return ""; }
public static String getValueFromAttributeStatements(final List<AttributeStatement> attributeStatements, final String attributeKey) { if (attributeStatements == null || attributeStatements.size() < 1 || attributeKey == null) { return null; } for (AttributeStatement attributeStatement : attributeStatements) { if (attributeStatement == null || attributeStatements.size() < 1) { continue; } for (Attribute attribute : attributeStatement.getAttributes()) { if (attribute.getAttributeValues() != null && attribute.getAttributeValues().size() > 0) { String value = attribute.getAttributeValues().get(0).getDOM().getTextContent(); s_logger.debug("SAML attribute name: " + attribute.getName() + " friendly-name:" + attribute.getFriendlyName() + " value:" + value); if (attributeKey.equals(attribute.getName()) || attributeKey.equals(attribute.getFriendlyName())) { return value; } } } } return null; }
private String[] getValueArray(Attribute attribute) { return getAttributeArray(attribute.getName()); } }
private String[] getValueArray(Attribute attribute) { return getAttributeArray(attribute.getName()); } }
private String getValue(Attribute attribute) { return getAttribute(attribute.getName()); }
private String getValue(Attribute attribute) { return getAttribute(attribute.getName()); }
public String getAttributeValue(SAMLCredential credential, String attributeName){ String returnValue = null; if(credential != null && credential.getAttributes() != null){ Optional<Attribute> foundAttribute = credential.getAttributes().stream() .filter(attribute -> attribute.getName().equals(attributeName)) .findFirst(); if(foundAttribute.isPresent()){ returnValue = credential.getAttributeAsString(foundAttribute.get().getName()); } } return returnValue; } }
/** * Method searches for the first occurrence of the attribute with given name and returns it. * Name comparing is only done by "name" attribute, disregarding "friendly-name" and "name-format". * Attributes are searched in order as received in SAML message. * * Attribute names are case-insensitive. * * @param name name of attribute to find * @return the first occurrence of the attribute with the given name or null if not found */ public Attribute getAttribute(String name) { for (Attribute attribute : getAttributes()) { if (name.equalsIgnoreCase(attribute.getName())) { return attribute; } } return null; }
@Override public Principal loadUserBySAML(SAMLCredential credential) { LOG.debug("loadUserBySAML {}", credential); List<SAMLAttribute> attributes = credential.getAttributes().stream().map(attribute -> new SAMLAttribute( attribute.getName(), attribute.getAttributeValues().stream().map(SAMLBuilder::getStringValueFromXMLObject) .filter(Optional::isPresent).map(Optional::get).collect(toList()))).collect(toList()); NameID nameID = credential.getNameID(); return new SAMLPrincipal(nameID.getValue(), nameID.getFormat(), attributes); }
/** * Checks that the Name attribute is present. * * @param attribute * @throws ValidationException */ protected void validateName(Attribute attribute) throws ValidationException { if (DatatypeHelper.isEmpty(attribute.getName())) { throw new ValidationException("Name is required attribute"); } } }
private List<SAML2Attribute> parseAttributes(Assertion assertion){ List<AttributeStatement> attribStatmts = assertion.getAttributeStatements(); List<SAML2Attribute> saml2Attributes = new ArrayList<SAML2Attribute>(); for ( AttributeStatement attrStatement : attribStatmts ) { List<Attribute> attributes = attrStatement.getAttributes(); for ( Attribute attr : attributes ){ List<XMLObject> vals = attr.getAttributeValues(); String value = vals.get(0).getDOM().getFirstChild().getNodeValue(); String name = attr.getName(); String logMess = " " + name + " : " + value; logger.info(logMess); SAML2Attribute sa2attr = new SAML2Attribute(name,value); saml2Attributes.add(sa2attr); } } return saml2Attributes; } }
private Map<String, String> getAssertionStatements(Assertion assertion) { Map<String, String> results = new HashMap<String, String>(); if (assertion != null && assertion.getAttributeStatements() != null) { List<AttributeStatement> attributeStatementList = assertion.getAttributeStatements(); for (AttributeStatement statement : attributeStatementList) { List<Attribute> attributesList = statement.getAttributes(); for (Attribute attribute : attributesList) { Element value = attribute.getAttributeValues().get(0).getDOM(); String attributeValue = value.getTextContent(); results.put(attribute.getName(), attributeValue); } } } return results; }
private Map<String, Object> getUserAttributes(ResponseImpl samlResponse) { Map<String, Object> userAttributes = new HashMap<>(); // Add 'Subject' Assertion assertion = samlResponse.getAssertions().get(0); userAttributes.put(SAMLConstants.SAML2_ASSERTION_SUBJECT, assertion.getSubject().getNameID().getValue()); // Add other user attributes. List<AttributeStatement> attributeStatements = assertion.getAttributeStatements(); if (attributeStatements != null) { for (AttributeStatement attributeStatement : attributeStatements) { List<Attribute> attributes = attributeStatement.getAttributes(); for (Attribute attribute : attributes) { if (attribute.getAttributeValues().size() > 1) { List<XMLObject> attributeValues = attribute.getAttributeValues(); List<String> attributeValuesList = new ArrayList<String>(); for (XMLObject attributeValue : attributeValues) { attributeValuesList.add(attributeValue.getDOM().getTextContent()); } userAttributes.put(attribute.getName(), attributeValuesList); } else { userAttributes.put(attribute.getName(), attribute.getAttributeValues().get(0).getDOM().getTextContent()); } } } } return userAttributes; }
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())); }
private static void processAttribute(Attribute attribute, Map<String, Object> attributeMap) throws AssertionValidationException { String attributeName = attribute.getName(); if (attribute.getAttributeValues().get(0) instanceof XSString) { XSString attributeValue = (XSString) attribute.getAttributeValues() .get(0); attributeMap.put(attributeName, attributeValue.getValue()); } else if (attribute.getAttributeValues().get(0) instanceof XSInteger) { XSInteger attributeValue = (XSInteger) attribute .getAttributeValues().get(0); attributeMap.put(attributeName, attributeValue.getValue()); } else if (attribute.getAttributeValues().get(0) instanceof XSDateTime) { XSDateTime attributeValue = (XSDateTime) attribute .getAttributeValues().get(0); attributeMap.put(attributeName, attributeValue.getValue() .toDateTime(ISOChronology.getInstanceUTC())); } else if (attribute.getAttributeValues().get(0) instanceof XSBase64Binary) { XSBase64Binary attributeValue = (XSBase64Binary) attribute .getAttributeValues().get(0); attributeMap.put(attributeName, Base64.decode(attributeValue.getValue())); } else { throw new AssertionValidationException("Unsupported attribute of " + "type: " + attribute.getAttributeValues().get(0).getClass() .getName()); } }
final String name = atb.getName(); final List<String> values = new ArrayList<>(); for (XMLObject obj : atb.getAttributeValues()) {