KEYCLOAK-7740 Support parsing of xs:date type

This commit is contained in:
Hynek Mlnarik 2018-09-26 10:10:55 +02:00 committed by Hynek Mlnařík
parent 4b9b189016
commit 17a1a33987
3 changed files with 80 additions and 73 deletions

View file

@ -23,6 +23,7 @@ import org.keycloak.saml.common.exceptions.ParsingException;
import org.keycloak.saml.common.parsers.StaxParser; import org.keycloak.saml.common.parsers.StaxParser;
import org.keycloak.saml.common.util.StaxParserUtil; import org.keycloak.saml.common.util.StaxParserUtil;
import org.keycloak.saml.processing.core.parsers.util.SAMLParserUtil; import org.keycloak.saml.processing.core.parsers.util.SAMLParserUtil;
import org.keycloak.saml.processing.core.saml.v2.util.XMLTimeUtil;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Objects; import java.util.Objects;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
@ -98,6 +99,8 @@ public class SAMLAttributeValueParser implements StaxParser {
return parseAnyTypeAsString(xmlEventReader); return parseAnyTypeAsString(xmlEventReader);
} else if(typeValue.contains(":base64Binary")){ } else if(typeValue.contains(":base64Binary")){
return StaxParserUtil.getElementText(xmlEventReader); return StaxParserUtil.getElementText(xmlEventReader);
} else if(typeValue.contains(":date")){
return XMLTimeUtil.parse(StaxParserUtil.getElementText(xmlEventReader));
} else if(typeValue.contains(":boolean")){ } else if(typeValue.contains(":boolean")){
return StaxParserUtil.getElementText(xmlEventReader); return StaxParserUtil.getElementText(xmlEventReader);
} }

View file

@ -942,88 +942,86 @@ public class SAMLParserTest {
@Test @Test
public void testSaml20AssertionsAnyTypeAttributeValue() throws Exception { public void testSaml20AssertionsAnyTypeAttributeValue() throws Exception {
AssertionType assertion = assertParsed("saml20-assertion-anytype-attribute-value.xml", AssertionType.class);
String[] xmlSamples = {
"saml20-assertion-anytype-attribute-value.xml",
"saml20-assertion-example.xml"
};
for (String fileName: xmlSamples) {
try (InputStream st = SAMLParserTest.class.getResourceAsStream(fileName)) {
Object parsedObject = parser.parse(st);
assertThat("Problem detected in " + fileName + " sample.", parsedObject, instanceOf(AssertionType.class));
checkCheckParsedResult(fileName, (AssertionType)parsedObject);
} catch (Exception e) {
throw new Exception("Problem detected in " + fileName + " sample.", e);
}
}
}
private void checkCheckParsedResult(String fileName, AssertionType assertion) throws Exception {
AttributeStatementType attributeStatementType = assertion.getAttributeStatements().iterator().next(); AttributeStatementType attributeStatementType = assertion.getAttributeStatements().iterator().next();
if ("saml20-assertion-anytype-attribute-value.xml".equals(fileName)) { assertThat(attributeStatementType.getAttributes(), hasSize(5));
assertTrue("There has to be 3 attributes", attributeStatementType.getAttributes().size() == 3);
for (AttributeStatementType.ASTChoiceType choiceType: attributeStatementType.getAttributes()) { for (AttributeStatementType.ASTChoiceType choiceType: attributeStatementType.getAttributes()) {
AttributeType attr = choiceType.getAttribute(); AttributeType attr = choiceType.getAttribute();
String attrName = attr.getName(); String attrName = attr.getName();
String attrValueStatement = "unexpected value of attribute " + attrName + " of " + fileName; Object value = attr.getAttributeValue().get(0);
String attrTypeStatement = "unexpected type of attribute " + attrName + " of " + fileName;
// test selected attributes // test selected attributes
if (attrName.equals("attr:type:string")) { switch (attrName) {
assertEquals(attrValueStatement, attr.getAttributeValue().get(0), "CITIZEN"); case "attr:type:string":
} else if (attrName.equals("attr:notype:string")) { assertThat(value, is((Object) "CITIZEN"));
assertThat(attrTypeStatement, attr.getAttributeValue().get(0), instanceOf(String.class)); break;
String value = (String)attr.getAttributeValue().get(0); case "attr:notype:string":
assertEquals(attrValueStatement, value, "CITIZEN"); assertThat(value, instanceOf(String.class));
} else if (attrName.equals("attr:notype:element")) { assertThat(value, is((Object) "CITIZEN"));
assertThat(attrTypeStatement, attr.getAttributeValue().get(0), instanceOf(String.class)); break;
String value = (String)attr.getAttributeValue().get(0); case "attr:notype:element":
assertThat(attrValueStatement, value, containsString("hospitaal x")); assertThat(value, instanceOf(String.class));
value = (String)attr.getAttributeValue().get(1); assertThat((String) value, containsString("hospitaal x"));
assertThat(attrValueStatement, value, containsString("hopital x")); value = attr.getAttributeValue().get(1);
assertThat(value, instanceOf(String.class));
assertThat((String) value, containsString("hopital x"));
break;
case "founded":
assertThat(value, is((Object) XMLTimeUtil.parse("2002-05-30T09:30:10-06:00")));
break;
case "expanded":
assertThat(value, is((Object) XMLTimeUtil.parse("2002-06-30")));
break;
default:
break;
} }
} }
} else if ("saml20-assertion-example.xml".equals(fileName)) { }
assertThat("There has to be 9 attributes", attributeStatementType.getAttributes().size(), is(9));
@Test
public void testSaml20AssertionExample() throws Exception {
AssertionType assertion = assertParsed("saml20-assertion-example.xml", AssertionType.class);
AttributeStatementType attributeStatementType = assertion.getAttributeStatements().iterator().next();
assertThat(attributeStatementType.getAttributes(), hasSize(9));
for (AttributeStatementType.ASTChoiceType choiceType: attributeStatementType.getAttributes()) { for (AttributeStatementType.ASTChoiceType choiceType: attributeStatementType.getAttributes()) {
AttributeType attr = choiceType.getAttribute(); AttributeType attr = choiceType.getAttribute();
String attrName = attr.getName(); String attrName = attr.getName();
String attrValueStatement = "unexpected value of attribute " + attrName + " of " + fileName; Object value = attr.getAttributeValue().get(0);
String attrTypeStatement = "unexpected type of attribute " + attrName + " of " + fileName;
// test selected attributes // test selected attributes
if (attrName.equals("portal_id")) { switch (attrName) {
assertEquals(attrValueStatement, attr.getAttributeValue().get(0), "060D00000000SHZ"); case "portal_id":
} else if (attrName.equals("organization_id")) { assertEquals(value, "060D00000000SHZ");
assertThat(attrTypeStatement, attr.getAttributeValue().get(0), instanceOf(String.class)); break;
String value = (String)attr.getAttributeValue().get(0); case "organization_id":
assertThat(attrValueStatement, value, containsString("<n3:stuff xmlns:n3=\"ftp://example.org\">00DD0000000F7L5</n3:stuff>")); assertThat(value, instanceOf(String.class));
} else if (attrName.equals("has_sub_organization")) { assertThat((String) value, containsString("<n3:stuff xmlns:n3=\"ftp://example.org\">00DD0000000F7L5</n3:stuff>"));
assertThat(attrTypeStatement, attr.getAttributeValue().get(0), instanceOf(String.class)); break;
String value = (String)attr.getAttributeValue().get(0); case "has_sub_organization":
assertThat(attrValueStatement, value, containsString("true")); assertThat(value, is((Object) "true"));
} else if (attrName.equals("anytype_test")) { break;
assertThat(attrTypeStatement, attr.getAttributeValue().get(0), instanceOf(String.class)); case "anytype_test":
String value = (String)attr.getAttributeValue().get(0); assertThat(value, instanceOf(String.class));
assertThat(attrValueStatement, value, containsString("<elem2>val2</elem2>")); assertThat((String) value, containsString("<elem2>val2</elem2>"));
} else if (attrName.equals("anytype_no_xml_test")) { break;
assertThat(attrTypeStatement, attr.getAttributeValue().get(0), instanceOf(String.class)); case "anytype_no_xml_test":
String value = (String)attr.getAttributeValue().get(0); assertThat(value, is((Object) "value_no_xml"));
assertEquals(attrValueStatement, value, "value_no_xml"); break;
} else if (attrName.equals("logouturl")) { case "logouturl":
assertThat(attrTypeStatement, attr.getAttributeValue().get(0), instanceOf(String.class)); assertThat(value, is((Object) "http://www.salesforce.com/security/del_auth/SsoLogoutPage.html"));
String value = (String)attr.getAttributeValue().get(0); break;
assertEquals(attrValueStatement, value, "http://www.salesforce.com/security/del_auth/SsoLogoutPage.html"); case "nil_value_attribute":
} else if (attrName.equals("nil_value_attribute")) { assertNull(value);
assertNull(attrValueStatement, attr.getAttributeValue().get(0)); break;
} else if (attrName.equals("status")) { case "status":
assertThat(attrTypeStatement, attr.getAttributeValue().get(0), instanceOf(String.class)); assertThat(value, is((Object) "<status><code><status>XYZ</status></code></status>"));
String value = (String)attr.getAttributeValue().get(0); break;
assertThat(attrValueStatement, value, containsString("<status><code><status>XYZ</status></code></status>")); default:
break;
} }
} }
} else {
throw new RuntimeException("test error: wrong file name to check");
}
} }
@Test(expected = ParsingException.class) @Test(expected = ParsingException.class)

View file

@ -15,5 +15,11 @@
<ns4:Name xml:lang="fr" xmlns="urn:be:fgov:ehealth:aa:complextype:v1" xmlns:ns4="urn:be:fgov:ehealth:aa:complextype:v1">hopital x</ns4:Name> <ns4:Name xml:lang="fr" xmlns="urn:be:fgov:ehealth:aa:complextype:v1" xmlns:ns4="urn:be:fgov:ehealth:aa:complextype:v1">hopital x</ns4:Name>
</saml2:AttributeValue> </saml2:AttributeValue>
</saml2:Attribute> </saml2:Attribute>
<saml2:Attribute FriendlyName="Organisation Founded" Name="founded" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:date">2002-05-30T09:30:10-06:00</saml2:AttributeValue>
</saml2:Attribute>
<saml2:Attribute FriendlyName="Organisation Expanded" Name="expanded" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
<saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:date">2002-06-30</saml2:AttributeValue>
</saml2:Attribute>
</saml2:AttributeStatement> </saml2:AttributeStatement>
</saml2:Assertion> </saml2:Assertion>