use of org.opensaml.core.xml.schema.XSString 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());
}
use of org.opensaml.core.xml.schema.XSString in project ddf by codice.
the class SecurityAssertionImpl method parseToken.
/**
* Parses the SecurityToken by wrapping within an AssertionWrapper.
*
* @param securityToken SecurityToken
*/
private void parseToken(SecurityToken securityToken) {
XMLStreamReader xmlStreamReader = StaxUtils.createXMLStreamReader(securityToken.getToken());
try {
AttrStatement attributeStatement = null;
AuthenticationStatement authenticationStatement = null;
Attr attribute = null;
int attrs = 0;
while (xmlStreamReader.hasNext()) {
int event = xmlStreamReader.next();
switch(event) {
case XMLStreamConstants.START_ELEMENT:
{
String localName = xmlStreamReader.getLocalName();
switch(localName) {
case NameID.DEFAULT_ELEMENT_LOCAL_NAME:
name = xmlStreamReader.getElementText();
for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
if (xmlStreamReader.getAttributeLocalName(i).equals(NameID.FORMAT_ATTRIB_NAME)) {
nameIDFormat = xmlStreamReader.getAttributeValue(i);
break;
}
}
break;
case AttributeStatement.DEFAULT_ELEMENT_LOCAL_NAME:
attributeStatement = new AttrStatement();
attributeStatements.add(attributeStatement);
break;
case AuthnStatement.DEFAULT_ELEMENT_LOCAL_NAME:
authenticationStatement = new AuthenticationStatement();
authenticationStatements.add(authenticationStatement);
attrs = xmlStreamReader.getAttributeCount();
for (int i = 0; i < attrs; i++) {
String name = xmlStreamReader.getAttributeLocalName(i);
String value = xmlStreamReader.getAttributeValue(i);
if (AuthnStatement.AUTHN_INSTANT_ATTRIB_NAME.equals(name)) {
authenticationStatement.setAuthnInstant(DateTime.parse(value));
}
}
break;
case AuthnContextClassRef.DEFAULT_ELEMENT_LOCAL_NAME:
if (authenticationStatement != null) {
String classValue = xmlStreamReader.getText();
classValue = classValue.trim();
AuthenticationContextClassRef authenticationContextClassRef = new AuthenticationContextClassRef();
authenticationContextClassRef.setAuthnContextClassRef(classValue);
AuthenticationContext authenticationContext = new AuthenticationContext();
authenticationContext.setAuthnContextClassRef(authenticationContextClassRef);
authenticationStatement.setAuthnContext(authenticationContext);
}
break;
case Attribute.DEFAULT_ELEMENT_LOCAL_NAME:
attribute = new Attr();
if (attributeStatement != null) {
attributeStatement.addAttribute(attribute);
}
attrs = xmlStreamReader.getAttributeCount();
for (int i = 0; i < attrs; i++) {
String name = xmlStreamReader.getAttributeLocalName(i);
String value = xmlStreamReader.getAttributeValue(i);
if (Attribute.NAME_ATTTRIB_NAME.equals(name)) {
attribute.setName(value);
} else if (Attribute.NAME_FORMAT_ATTRIB_NAME.equals(name)) {
attribute.setNameFormat(value);
}
}
break;
case AttributeValue.DEFAULT_ELEMENT_LOCAL_NAME:
XSString xsString = new XMLString();
xsString.setValue(xmlStreamReader.getElementText());
if (attribute != null) {
attribute.addAttributeValue(xsString);
}
break;
case Issuer.DEFAULT_ELEMENT_LOCAL_NAME:
issuer = xmlStreamReader.getElementText();
break;
case Conditions.DEFAULT_ELEMENT_LOCAL_NAME:
attrs = xmlStreamReader.getAttributeCount();
for (int i = 0; i < attrs; i++) {
String name = xmlStreamReader.getAttributeLocalName(i);
String value = xmlStreamReader.getAttributeValue(i);
if (Conditions.NOT_BEFORE_ATTRIB_NAME.equals(name)) {
notBefore = DatatypeConverter.parseDateTime(value).getTime();
} else if (Conditions.NOT_ON_OR_AFTER_ATTRIB_NAME.equals(name)) {
notOnOrAfter = DatatypeConverter.parseDateTime(value).getTime();
}
}
break;
case SubjectConfirmation.DEFAULT_ELEMENT_LOCAL_NAME:
attrs = xmlStreamReader.getAttributeCount();
for (int i = 0; i < attrs; i++) {
String name = xmlStreamReader.getAttributeLocalName(i);
String value = xmlStreamReader.getAttributeValue(i);
if (SubjectConfirmation.METHOD_ATTRIB_NAME.equals(name)) {
subjectConfirmations.add(value);
}
}
break;
case Assertion.DEFAULT_ELEMENT_LOCAL_NAME:
attrs = xmlStreamReader.getAttributeCount();
for (int i = 0; i < attrs; i++) {
String name = xmlStreamReader.getAttributeLocalName(i);
String value = xmlStreamReader.getAttributeValue(i);
if (Assertion.VERSION_ATTRIB_NAME.equals(name)) {
if ("2.0".equals(value)) {
tokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0";
} else if ("1.1".equals(value)) {
tokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1";
}
}
}
}
break;
}
case XMLStreamConstants.END_ELEMENT:
{
String localName = xmlStreamReader.getLocalName();
switch(localName) {
case AttributeStatement.DEFAULT_ELEMENT_LOCAL_NAME:
attributeStatement = null;
break;
case Attribute.DEFAULT_ELEMENT_LOCAL_NAME:
attribute = null;
break;
default:
break;
}
break;
}
}
}
} catch (XMLStreamException e) {
LOGGER.info("Unable to parse security token.", e);
} finally {
try {
xmlStreamReader.close();
} catch (XMLStreamException ignore) {
//ignore
}
}
}
use of org.opensaml.core.xml.schema.XSString in project spring-security by spring-projects.
the class TestOpenSamlObjects method attributeStatements.
static List<AttributeStatement> attributeStatements() {
List<AttributeStatement> attributeStatements = new ArrayList<>();
AttributeStatementBuilder attributeStatementBuilder = new AttributeStatementBuilder();
AttributeBuilder attributeBuilder = new AttributeBuilder();
AttributeStatement attrStmt1 = attributeStatementBuilder.buildObject();
Attribute emailAttr = attributeBuilder.buildObject();
emailAttr.setName("email");
// gh-8864
XSAny email1 = new XSAnyBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSAny.TYPE_NAME);
email1.setTextContent("john.doe@example.com");
emailAttr.getAttributeValues().add(email1);
XSAny email2 = new XSAnyBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME);
email2.setTextContent("doe.john@example.com");
emailAttr.getAttributeValues().add(email2);
attrStmt1.getAttributes().add(emailAttr);
Attribute nameAttr = attributeBuilder.buildObject();
nameAttr.setName("name");
XSString name = new XSStringBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
name.setValue("John Doe");
nameAttr.getAttributeValues().add(name);
attrStmt1.getAttributes().add(nameAttr);
Attribute ageAttr = attributeBuilder.buildObject();
ageAttr.setName("age");
XSInteger age = new XSIntegerBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSInteger.TYPE_NAME);
age.setValue(21);
ageAttr.getAttributeValues().add(age);
attrStmt1.getAttributes().add(ageAttr);
attributeStatements.add(attrStmt1);
AttributeStatement attrStmt2 = attributeStatementBuilder.buildObject();
Attribute websiteAttr = attributeBuilder.buildObject();
websiteAttr.setName("website");
XSURI uri = new XSURIBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSURI.TYPE_NAME);
uri.setValue("https://johndoe.com/");
websiteAttr.getAttributeValues().add(uri);
attrStmt2.getAttributes().add(websiteAttr);
Attribute registeredAttr = attributeBuilder.buildObject();
registeredAttr.setName("registered");
XSBoolean registered = new XSBooleanBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSBoolean.TYPE_NAME);
registered.setValue(new XSBooleanValue(true, false));
registeredAttr.getAttributeValues().add(registered);
attrStmt2.getAttributes().add(registeredAttr);
attributeStatements.add(attrStmt2);
return attributeStatements;
}
use of org.opensaml.core.xml.schema.XSString in project cas by apereo.
the class SamlMetadataUIInfo method findLocale.
private static Optional<String> findLocale(final String locale, final List<?> items) {
LOGGER.trace("Looking for locale [{}]", locale);
val p = Pattern.compile(locale, Pattern.CASE_INSENSITIVE);
return items.stream().filter(item -> item instanceof LocalizedName).map(item -> (LocalizedName) item).filter(item -> {
val xmlLang = item.getXMLLang();
return StringUtils.isNotBlank(xmlLang) && p.matcher(xmlLang).matches() && StringUtils.isNotBlank(item.getValue());
}).map(XSString::getValue).findFirst();
}
use of org.opensaml.core.xml.schema.XSString in project cas by apereo.
the class SamlIdPConsentableAttributeBuilder method build.
@Override
public CasConsentableAttribute build(final CasConsentableAttribute attribute) {
val result = attributeDefinitionStore.locateAttributeDefinition(defn -> {
if (defn instanceof SamlIdPAttributeDefinition) {
val samlAttr = (SamlIdPAttributeDefinition) defn;
return samlAttr.getName().equalsIgnoreCase(attribute.getName()) && StringUtils.isNotBlank(samlAttr.getFriendlyName());
}
return false;
});
if (result.isPresent()) {
val samlAttr = (SamlIdPAttributeDefinition) result.get();
attribute.setFriendlyName(samlAttr.getFriendlyName());
}
val attributeValues = ObjectUtils.defaultIfNull(attribute.getValues(), new ArrayList<>());
attributeValues.replaceAll(o -> {
if (o instanceof XSString) {
return ((XSString) o).getValue();
}
if (o instanceof XSURI) {
return ((XSURI) o).getURI();
}
if (o instanceof Serializable) {
return o;
}
return o.toString();
});
return attribute;
}
Aggregations