use of org.keycloak.dom.saml.v2.assertion.AttributeType in project keycloak by keycloak.
the class StatementUtil method asMap.
public static Map<String, Object> asMap(Set<AttributeStatementType> attributeStatementTypes) {
Map<String, Object> attrMap = new HashMap<>();
if (attributeStatementTypes != null && !attributeStatementTypes.isEmpty()) {
attrMap = new HashMap<>();
for (StatementAbstractType statement : attributeStatementTypes) {
if (statement instanceof AttributeStatementType) {
AttributeStatementType attrStat = (AttributeStatementType) statement;
List<ASTChoiceType> attrs = attrStat.getAttributes();
for (ASTChoiceType attrChoice : attrs) {
AttributeType attr = attrChoice.getAttribute();
String attributeName = attr.getFriendlyName();
if (attributeName == null) {
attributeName = attr.getName();
}
List<Object> values = attr.getAttributeValue();
if (values != null) {
if (values.size() == 1) {
attrMap.put(attributeName, values.get(0));
} else {
attrMap.put(attributeName, values);
}
}
}
}
}
}
return attrMap;
}
use of org.keycloak.dom.saml.v2.assertion.AttributeType in project keycloak by keycloak.
the class SAMLParserTest method testEmptyAttributeValue.
@Test
public void testEmptyAttributeValue() throws Exception {
ResponseType resp = assertParsed("KEYCLOAK-4790-Empty-attribute-value.xml", ResponseType.class);
assertThat(resp.getAssertions(), hasSize(1));
final AssertionType a = resp.getAssertions().get(0).getAssertion();
assertThat(a, notNullValue());
assertThat(a.getAttributeStatements(), hasSize(1));
final List<ASTChoiceType> attributes = a.getAttributeStatements().iterator().next().getAttributes();
assertThat(attributes, hasSize(3));
assertThat(attributes, everyItem(notNullValue(ASTChoiceType.class)));
final AttributeType attr0 = attributes.get(0).getAttribute();
final AttributeType attr1 = attributes.get(1).getAttribute();
final AttributeType attr2 = attributes.get(2).getAttribute();
assertThat(attr0.getName(), is("urn:oid:0.9.2342.19200300.100.1.2"));
assertThat(attr0.getAttributeValue(), hasSize(1));
assertThat(attr0.getAttributeValue().get(0), instanceOf(String.class));
assertThat((String) attr0.getAttributeValue().get(0), is(""));
assertThat(attr1.getName(), is("urn:oid:0.9.2342.19200300.100.1.3"));
assertThat(attr1.getAttributeValue(), hasSize(1));
assertThat(attr1.getAttributeValue().get(0), instanceOf(String.class));
assertThat((String) attr1.getAttributeValue().get(0), is("aa"));
assertThat(attr2.getName(), is("urn:oid:0.9.2342.19200300.100.1.4"));
assertThat(attr2.getAttributeValue(), hasSize(1));
assertThat(attr2.getAttributeValue().get(0), instanceOf(String.class));
assertThat((String) attr2.getAttributeValue().get(0), is(""));
}
use of org.keycloak.dom.saml.v2.assertion.AttributeType in project keycloak by keycloak.
the class SAMLParserTest method testSaml20AssertionsAnyTypeAttributeValue.
@Test
public void testSaml20AssertionsAnyTypeAttributeValue() throws Exception {
AssertionType assertion = assertParsed("saml20-assertion-anytype-attribute-value.xml", AssertionType.class);
AttributeStatementType attributeStatementType = assertion.getAttributeStatements().iterator().next();
assertThat(attributeStatementType.getAttributes(), hasSize(5));
for (AttributeStatementType.ASTChoiceType choiceType : attributeStatementType.getAttributes()) {
AttributeType attr = choiceType.getAttribute();
String attrName = attr.getName();
Object value = attr.getAttributeValue().get(0);
// test selected attributes
switch(attrName) {
case "attr:type:string":
assertThat(value, is((Object) "CITIZEN"));
break;
case "attr:notype:string":
assertThat(value, instanceOf(String.class));
assertThat(value, is((Object) "CITIZEN"));
break;
case "attr:notype:element":
assertThat(value, instanceOf(String.class));
assertThat((String) value, containsString("hospitaal 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;
}
}
}
use of org.keycloak.dom.saml.v2.assertion.AttributeType in project keycloak by keycloak.
the class BrokerTest method testNoNameIDAndPrincipalFromAttribute.
@Test
public void testNoNameIDAndPrincipalFromAttribute() throws IOException {
final String userName = "newUser-" + UUID.randomUUID();
final RealmResource realm = adminClient.realm(REALM_NAME);
final IdentityProviderRepresentation rep = addIdentityProvider("https://saml.idp/");
rep.getConfig().put(SAMLIdentityProviderConfig.NAME_ID_POLICY_FORMAT, "undefined");
rep.getConfig().put(SAMLIdentityProviderConfig.PRINCIPAL_TYPE, SamlPrincipalType.ATTRIBUTE.toString());
rep.getConfig().put(SAMLIdentityProviderConfig.PRINCIPAL_ATTRIBUTE, "user");
try (IdentityProviderCreator idp = new IdentityProviderCreator(realm, rep)) {
new SamlClientBuilder().authnRequest(getAuthServerSamlEndpoint(REALM_NAME), SAML_CLIENT_ID_SALES_POST, SAML_ASSERTION_CONSUMER_URL_SALES_POST, POST).build().login().idp(SAML_BROKER_ALIAS).build().processSamlResponse(REDIRECT).transformObject(this::createAuthnResponse).transformObject(resp -> {
final ResponseType rt = (ResponseType) resp;
final AssertionType assertion = rt.getAssertions().get(0).getAssertion();
// Remove NameID from subject
assertion.getSubject().setSubType(null);
// Add attribute to get principal from
AttributeStatementType attrStatement = new AttributeStatementType();
AttributeType attribute = new AttributeType("user");
attribute.addAttributeValue(userName);
attrStatement.addAttribute(new ASTChoiceType(attribute));
rt.getAssertions().get(0).getAssertion().addStatement(attrStatement);
return rt;
}).targetAttributeSamlResponse().targetUri(getSamlBrokerUrl(REALM_NAME)).build().followOneRedirect().updateProfile().username(userName).firstName("someFirstName").lastName("someLastName").email("some@email.com").build().followOneRedirect().assertResponse(org.keycloak.testsuite.util.Matchers.statusCodeIsHC(200)).execute();
}
final UserRepresentation userRepresentation = realm.users().search(userName).stream().findFirst().get();
final List<UserSessionRepresentation> userSessions = realm.users().get(userRepresentation.getId()).getUserSessions();
assertThat(userSessions, hasSize(1));
}
Aggregations