Search in sources :

Example 36 with UsersResource

use of org.keycloak.admin.client.resource.UsersResource in project keycloak by keycloak.

the class IdpInitiatedLoginTest method testTwoConsequentIdpInitiatedLogins.

@Test
public void testTwoConsequentIdpInitiatedLogins() {
    new SamlClientBuilder().idpInitiatedLogin(getAuthServerSamlEndpoint(REALM_NAME), "sales-post").build().login().user(bburkeUser).build().processSamlResponse(Binding.POST).transformObject(ob -> {
        assertThat(ob, Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS));
        ResponseType resp = (ResponseType) ob;
        assertThat(resp.getDestination(), is(SAML_ASSERTION_CONSUMER_URL_SALES_POST));
        return null;
    }).build().idpInitiatedLogin(getAuthServerSamlEndpoint(REALM_NAME), "sales-post2").build().login().sso(true).build().processSamlResponse(Binding.POST).transformObject(ob -> {
        assertThat(ob, Matchers.isSamlResponse(JBossSAMLURIConstants.STATUS_SUCCESS));
        ResponseType resp = (ResponseType) ob;
        assertThat(resp.getDestination(), is(SAML_ASSERTION_CONSUMER_URL_SALES_POST2));
        return null;
    }).build().execute();
    final UsersResource users = adminClient.realm(REALM_NAME).users();
    final ClientsResource clients = adminClient.realm(REALM_NAME).clients();
    UserRepresentation bburkeUserRepresentation = users.search(bburkeUser.getUsername()).stream().findFirst().get();
    List<UserSessionRepresentation> userSessions = users.get(bburkeUserRepresentation.getId()).getUserSessions();
    assertThat(userSessions, hasSize(1));
    Map<String, String> clientSessions = userSessions.get(0).getClients();
    Set<String> clientIds = clientSessions.values().stream().flatMap(c -> clients.findByClientId(c).stream()).map(ClientRepresentation::getClientId).collect(Collectors.toSet());
    assertThat(clientIds, containsInAnyOrder(SAML_CLIENT_ID_SALES_POST, SAML_CLIENT_ID_SALES_POST2));
}
Also used : UserSessionRepresentation(org.keycloak.representations.idm.UserSessionRepresentation) SamlClientBuilder(org.keycloak.testsuite.util.SamlClientBuilder) UsersResource(org.keycloak.admin.client.resource.UsersResource) ClientsResource(org.keycloak.admin.client.resource.ClientsResource) Matchers.containsString(org.hamcrest.Matchers.containsString) ResponseType(org.keycloak.dom.saml.v2.protocol.ResponseType) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Aggregations

UsersResource (org.keycloak.admin.client.resource.UsersResource)36 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)32 Test (org.junit.Test)18 UserResource (org.keycloak.admin.client.resource.UserResource)10 RealmResource (org.keycloak.admin.client.resource.RealmResource)9 Response (javax.ws.rs.core.Response)7 ClientsResource (org.keycloak.admin.client.resource.ClientsResource)7 Map (java.util.Map)6 List (java.util.List)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Before (org.junit.Before)5 UserSessionRepresentation (org.keycloak.representations.idm.UserSessionRepresentation)5 IdentityProviderResource (org.keycloak.admin.client.resource.IdentityProviderResource)4 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)4 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 Matchers.hasSize (org.hamcrest.Matchers.hasSize)3 Assert.assertThat (org.junit.Assert.assertThat)3 RolesResource (org.keycloak.admin.client.resource.RolesResource)3 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)3