Search in sources :

Example 1 with ClientRepresentation

use of org.keycloak.representations.account.ClientRepresentation in project keycloak by keycloak.

the class AccountRestService method modelToRepresentation.

private ClientRepresentation modelToRepresentation(ClientModel model, List<String> inUseClients, List<String> offlineClients, Map<String, UserConsentModel> consents) {
    ClientRepresentation representation = new ClientRepresentation();
    representation.setClientId(model.getClientId());
    representation.setClientName(StringPropertyReplacer.replaceProperties(model.getName(), getProperties()));
    representation.setDescription(model.getDescription());
    representation.setUserConsentRequired(model.isConsentRequired());
    representation.setInUse(inUseClients.contains(model.getClientId()));
    representation.setOfflineAccess(offlineClients.contains(model.getClientId()));
    representation.setRootUrl(model.getRootUrl());
    representation.setBaseUrl(model.getBaseUrl());
    representation.setEffectiveUrl(ResolveRelative.resolveRelativeUri(session, model.getRootUrl(), model.getBaseUrl()));
    UserConsentModel consentModel = consents.get(model.getClientId());
    if (consentModel != null) {
        representation.setConsent(modelToRepresentation(consentModel));
        representation.setLogoUri(model.getAttribute(ClientModel.LOGO_URI));
        representation.setPolicyUri(model.getAttribute(ClientModel.POLICY_URI));
        representation.setTosUri(model.getAttribute(ClientModel.TOS_URI));
    }
    return representation;
}
Also used : UserConsentModel(org.keycloak.models.UserConsentModel) ClientRepresentation(org.keycloak.representations.account.ClientRepresentation)

Example 2 with ClientRepresentation

use of org.keycloak.representations.account.ClientRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method listApplications.

@Test
public void listApplications() throws Exception {
    oauth.clientId("in-use-client");
    OAuthClient.AccessTokenResponse tokenResponse = oauth.doGrantAccessTokenRequest("secret1", "view-applications-access", "password");
    assertNull(tokenResponse.getErrorDescription());
    TokenUtil token = new TokenUtil("view-applications-access", "password");
    List<ClientRepresentation> applications = SimpleHttp.doGet(getAccountUrl("applications"), httpClient).header("Accept", "application/json").auth(token.getToken()).asJson(new TypeReference<List<ClientRepresentation>>() {
    });
    assertFalse(applications.isEmpty());
    Map<String, ClientRepresentation> apps = applications.stream().collect(Collectors.toMap(x -> x.getClientId(), x -> x));
    Assert.assertThat(apps.keySet(), containsInAnyOrder("in-use-client", "always-display-client", "direct-grant"));
    assertClientRep(apps.get("in-use-client"), "In Use Client", null, false, true, false, null, inUseClientAppUri);
    assertClientRep(apps.get("always-display-client"), "Always Display Client", null, false, false, false, null, alwaysDisplayClientAppUri);
    assertClientRep(apps.get("direct-grant"), null, null, false, true, false, null, null);
}
Also used : AssertEvents(org.keycloak.testsuite.AssertEvents) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Messages(org.keycloak.services.messages.Messages) DefaultAuthenticationFlows(org.keycloak.models.utils.DefaultAuthenticationFlows) ResolveRelative(org.keycloak.services.util.ResolveRelative) OAuthClient(org.keycloak.testsuite.util.OAuthClient) Assert.assertThat(org.junit.Assert.assertThat) SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) Map(java.util.Map) AuthenticationExecutionRepresentation(org.keycloak.representations.idm.AuthenticationExecutionRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) TypeReference(com.fasterxml.jackson.core.type.TypeReference) WebAuthnCredentialModel(org.keycloak.models.credential.WebAuthnCredentialModel) SessionRepresentation(org.keycloak.representations.account.SessionRepresentation) UserProfileAttributeMetadata(org.keycloak.representations.account.UserProfileAttributeMetadata) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) AccountCredentialResource(org.keycloak.services.resources.account.AccountCredentialResource) List(java.util.List) Response(javax.ws.rs.core.Response) Details(org.keycloak.events.Details) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) ClientRepresentation(org.keycloak.representations.account.ClientRepresentation) ConsentRepresentation(org.keycloak.representations.account.ConsentRepresentation) EmailValidator(org.keycloak.validate.validators.EmailValidator) WebAuthnRegisterFactory(org.keycloak.authentication.requiredactions.WebAuthnRegisterFactory) OAuth2Constants(org.keycloak.OAuth2Constants) Profile(org.keycloak.common.Profile) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) ConsentScopeRepresentation(org.keycloak.representations.account.ConsentScopeRepresentation) AccountRestApiVersion(org.keycloak.common.enums.AccountRestApiVersion) HashMap(java.util.HashMap) ObjectUtil(org.keycloak.common.util.ObjectUtil) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) AuthenticationExecutionInfoRepresentation(org.keycloak.representations.idm.AuthenticationExecutionInfoRepresentation) WebAuthnAuthenticatorFactory(org.keycloak.authentication.authenticators.browser.WebAuthnAuthenticatorFactory) UserModel(org.keycloak.models.UserModel) EnableFeature(org.keycloak.testsuite.arquillian.annotation.EnableFeature) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) UserBuilder(org.keycloak.testsuite.util.UserBuilder) AuthenticationFlowRepresentation(org.keycloak.representations.idm.AuthenticationFlowRepresentation) WebAuthnPasswordlessRegisterFactory(org.keycloak.authentication.requiredactions.WebAuthnPasswordlessRegisterFactory) UserResource(org.keycloak.admin.client.resource.UserResource) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) RequiredActionProviderSimpleRepresentation(org.keycloak.representations.idm.RequiredActionProviderSimpleRepresentation) TokenUtil(org.keycloak.testsuite.util.TokenUtil) ApiUtil(org.keycloak.testsuite.admin.ApiUtil) Assert.assertNotNull(org.junit.Assert.assertNotNull) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) RequiredActionProviderRepresentation(org.keycloak.representations.idm.RequiredActionProviderRepresentation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) WebAuthnPasswordlessAuthenticatorFactory(org.keycloak.authentication.authenticators.browser.WebAuthnPasswordlessAuthenticatorFactory) EventType(org.keycloak.events.EventType) IOException(java.io.IOException) OTPCredentialModel(org.keycloak.models.credential.OTPCredentialModel) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) UserProfileContext(org.keycloak.userprofile.UserProfileContext) CredentialTypeMetadata(org.keycloak.credential.CredentialTypeMetadata) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) OAuthClient(org.keycloak.testsuite.util.OAuthClient) TokenUtil(org.keycloak.testsuite.util.TokenUtil) List(java.util.List) ClientRepresentation(org.keycloak.representations.account.ClientRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 3 with ClientRepresentation

use of org.keycloak.representations.account.ClientRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method listApplicationsWithRootUrl.

@Test
public void listApplicationsWithRootUrl() throws Exception {
    oauth.clientId("root-url-client");
    OAuthClient.AccessTokenResponse tokenResponse = oauth.doGrantAccessTokenRequest("password", "view-applications-access", "password");
    assertNull(tokenResponse.getErrorDescription());
    TokenUtil token = new TokenUtil("view-applications-access", "password");
    List<ClientRepresentation> applications = SimpleHttp.doGet(getAccountUrl("applications"), httpClient).header("Accept", "application/json").auth(token.getToken()).asJson(new TypeReference<List<ClientRepresentation>>() {
    });
    assertFalse(applications.isEmpty());
    Map<String, ClientRepresentation> apps = applications.stream().collect(Collectors.toMap(x -> x.getClientId(), x -> x));
    Assert.assertThat(apps.keySet(), containsInAnyOrder("root-url-client", "always-display-client", "direct-grant"));
    assertClientRep(apps.get("root-url-client"), null, null, false, true, false, "http://localhost:8180/foo/bar", "/baz");
}
Also used : AssertEvents(org.keycloak.testsuite.AssertEvents) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Messages(org.keycloak.services.messages.Messages) DefaultAuthenticationFlows(org.keycloak.models.utils.DefaultAuthenticationFlows) ResolveRelative(org.keycloak.services.util.ResolveRelative) OAuthClient(org.keycloak.testsuite.util.OAuthClient) Assert.assertThat(org.junit.Assert.assertThat) SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) Map(java.util.Map) AuthenticationExecutionRepresentation(org.keycloak.representations.idm.AuthenticationExecutionRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) TypeReference(com.fasterxml.jackson.core.type.TypeReference) WebAuthnCredentialModel(org.keycloak.models.credential.WebAuthnCredentialModel) SessionRepresentation(org.keycloak.representations.account.SessionRepresentation) UserProfileAttributeMetadata(org.keycloak.representations.account.UserProfileAttributeMetadata) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) AccountCredentialResource(org.keycloak.services.resources.account.AccountCredentialResource) List(java.util.List) Response(javax.ws.rs.core.Response) Details(org.keycloak.events.Details) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) ClientRepresentation(org.keycloak.representations.account.ClientRepresentation) ConsentRepresentation(org.keycloak.representations.account.ConsentRepresentation) EmailValidator(org.keycloak.validate.validators.EmailValidator) WebAuthnRegisterFactory(org.keycloak.authentication.requiredactions.WebAuthnRegisterFactory) OAuth2Constants(org.keycloak.OAuth2Constants) Profile(org.keycloak.common.Profile) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) ConsentScopeRepresentation(org.keycloak.representations.account.ConsentScopeRepresentation) AccountRestApiVersion(org.keycloak.common.enums.AccountRestApiVersion) HashMap(java.util.HashMap) ObjectUtil(org.keycloak.common.util.ObjectUtil) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) AuthenticationExecutionInfoRepresentation(org.keycloak.representations.idm.AuthenticationExecutionInfoRepresentation) WebAuthnAuthenticatorFactory(org.keycloak.authentication.authenticators.browser.WebAuthnAuthenticatorFactory) UserModel(org.keycloak.models.UserModel) EnableFeature(org.keycloak.testsuite.arquillian.annotation.EnableFeature) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) UserBuilder(org.keycloak.testsuite.util.UserBuilder) AuthenticationFlowRepresentation(org.keycloak.representations.idm.AuthenticationFlowRepresentation) WebAuthnPasswordlessRegisterFactory(org.keycloak.authentication.requiredactions.WebAuthnPasswordlessRegisterFactory) UserResource(org.keycloak.admin.client.resource.UserResource) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) RequiredActionProviderSimpleRepresentation(org.keycloak.representations.idm.RequiredActionProviderSimpleRepresentation) TokenUtil(org.keycloak.testsuite.util.TokenUtil) ApiUtil(org.keycloak.testsuite.admin.ApiUtil) Assert.assertNotNull(org.junit.Assert.assertNotNull) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) RequiredActionProviderRepresentation(org.keycloak.representations.idm.RequiredActionProviderRepresentation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) WebAuthnPasswordlessAuthenticatorFactory(org.keycloak.authentication.authenticators.browser.WebAuthnPasswordlessAuthenticatorFactory) EventType(org.keycloak.events.EventType) IOException(java.io.IOException) OTPCredentialModel(org.keycloak.models.credential.OTPCredentialModel) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) UserProfileContext(org.keycloak.userprofile.UserProfileContext) CredentialTypeMetadata(org.keycloak.credential.CredentialTypeMetadata) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) OAuthClient(org.keycloak.testsuite.util.OAuthClient) TokenUtil(org.keycloak.testsuite.util.TokenUtil) List(java.util.List) ClientRepresentation(org.keycloak.representations.account.ClientRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 4 with ClientRepresentation

use of org.keycloak.representations.account.ClientRepresentation in project keycloak by keycloak.

the class AccountRestServiceTest method listApplicationsOfflineAccess.

@Test
public void listApplicationsOfflineAccess() throws Exception {
    oauth.scope(OAuth2Constants.OFFLINE_ACCESS);
    oauth.clientId("offline-client");
    OAuthClient.AccessTokenResponse offlineTokenResponse = oauth.doGrantAccessTokenRequest("secret1", "view-applications-access", "password");
    assertNull(offlineTokenResponse.getErrorDescription());
    oauth.clientId("offline-client-without-base-url");
    offlineTokenResponse = oauth.doGrantAccessTokenRequest("secret1", "view-applications-access", "password");
    assertNull(offlineTokenResponse.getErrorDescription());
    TokenUtil token = new TokenUtil("view-applications-access", "password");
    List<ClientRepresentation> applications = SimpleHttp.doGet(getAccountUrl("applications"), httpClient).header("Accept", "application/json").auth(token.getToken()).asJson(new TypeReference<List<ClientRepresentation>>() {
    });
    assertFalse(applications.isEmpty());
    Map<String, ClientRepresentation> apps = applications.stream().collect(Collectors.toMap(x -> x.getClientId(), x -> x));
    Assert.assertThat(apps.keySet(), containsInAnyOrder("offline-client", "offline-client-without-base-url", "always-display-client", "direct-grant"));
    assertClientRep(apps.get("offline-client"), "Offline Client", null, false, true, true, null, offlineClientAppUri);
    assertClientRep(apps.get("offline-client-without-base-url"), "Offline Client Without Base URL", null, false, true, true, null, null);
}
Also used : AssertEvents(org.keycloak.testsuite.AssertEvents) UserRepresentation(org.keycloak.representations.account.UserRepresentation) Messages(org.keycloak.services.messages.Messages) DefaultAuthenticationFlows(org.keycloak.models.utils.DefaultAuthenticationFlows) ResolveRelative(org.keycloak.services.util.ResolveRelative) OAuthClient(org.keycloak.testsuite.util.OAuthClient) Assert.assertThat(org.junit.Assert.assertThat) SimpleHttp(org.keycloak.broker.provider.util.SimpleHttp) Map(java.util.Map) AuthenticationExecutionRepresentation(org.keycloak.representations.idm.AuthenticationExecutionRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) TypeReference(com.fasterxml.jackson.core.type.TypeReference) WebAuthnCredentialModel(org.keycloak.models.credential.WebAuthnCredentialModel) SessionRepresentation(org.keycloak.representations.account.SessionRepresentation) UserProfileAttributeMetadata(org.keycloak.representations.account.UserProfileAttributeMetadata) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) AccountCredentialResource(org.keycloak.services.resources.account.AccountCredentialResource) List(java.util.List) Response(javax.ws.rs.core.Response) Details(org.keycloak.events.Details) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) ClientRepresentation(org.keycloak.representations.account.ClientRepresentation) ConsentRepresentation(org.keycloak.representations.account.ConsentRepresentation) EmailValidator(org.keycloak.validate.validators.EmailValidator) WebAuthnRegisterFactory(org.keycloak.authentication.requiredactions.WebAuthnRegisterFactory) OAuth2Constants(org.keycloak.OAuth2Constants) Profile(org.keycloak.common.Profile) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) ConsentScopeRepresentation(org.keycloak.representations.account.ConsentScopeRepresentation) AccountRestApiVersion(org.keycloak.common.enums.AccountRestApiVersion) HashMap(java.util.HashMap) ObjectUtil(org.keycloak.common.util.ObjectUtil) ErrorRepresentation(org.keycloak.representations.idm.ErrorRepresentation) AuthenticationExecutionInfoRepresentation(org.keycloak.representations.idm.AuthenticationExecutionInfoRepresentation) WebAuthnAuthenticatorFactory(org.keycloak.authentication.authenticators.browser.WebAuthnAuthenticatorFactory) UserModel(org.keycloak.models.UserModel) EnableFeature(org.keycloak.testsuite.arquillian.annotation.EnableFeature) AuthenticationExecutionModel(org.keycloak.models.AuthenticationExecutionModel) UserBuilder(org.keycloak.testsuite.util.UserBuilder) AuthenticationFlowRepresentation(org.keycloak.representations.idm.AuthenticationFlowRepresentation) WebAuthnPasswordlessRegisterFactory(org.keycloak.authentication.requiredactions.WebAuthnPasswordlessRegisterFactory) UserResource(org.keycloak.admin.client.resource.UserResource) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) RequiredActionProviderSimpleRepresentation(org.keycloak.representations.idm.RequiredActionProviderSimpleRepresentation) TokenUtil(org.keycloak.testsuite.util.TokenUtil) ApiUtil(org.keycloak.testsuite.admin.ApiUtil) Assert.assertNotNull(org.junit.Assert.assertNotNull) ClientScopeRepresentation(org.keycloak.representations.idm.ClientScopeRepresentation) RequiredActionProviderRepresentation(org.keycloak.representations.idm.RequiredActionProviderRepresentation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) WebAuthnPasswordlessAuthenticatorFactory(org.keycloak.authentication.authenticators.browser.WebAuthnPasswordlessAuthenticatorFactory) EventType(org.keycloak.events.EventType) IOException(java.io.IOException) OTPCredentialModel(org.keycloak.models.credential.OTPCredentialModel) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) PasswordCredentialModel(org.keycloak.models.credential.PasswordCredentialModel) UserProfileContext(org.keycloak.userprofile.UserProfileContext) CredentialTypeMetadata(org.keycloak.credential.CredentialTypeMetadata) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) OAuthClient(org.keycloak.testsuite.util.OAuthClient) TokenUtil(org.keycloak.testsuite.util.TokenUtil) List(java.util.List) ClientRepresentation(org.keycloak.representations.account.ClientRepresentation) Test(org.junit.Test) AbstractAuthenticationTest(org.keycloak.testsuite.admin.authentication.AbstractAuthenticationTest)

Example 5 with ClientRepresentation

use of org.keycloak.representations.account.ClientRepresentation in project keycloak by keycloak.

the class SessionResource method createSessionRepresentation.

private SessionRepresentation createSessionRepresentation(UserSessionModel s, DeviceRepresentation device) {
    SessionRepresentation sessionRep = new SessionRepresentation();
    sessionRep.setId(s.getId());
    sessionRep.setIpAddress(s.getIpAddress());
    sessionRep.setStarted(s.getStarted());
    sessionRep.setLastAccess(s.getLastSessionRefresh());
    sessionRep.setExpires(s.getStarted() + realm.getSsoSessionMaxLifespan());
    sessionRep.setBrowser(device.getBrowser());
    if (isCurrentSession(s)) {
        sessionRep.setCurrent(true);
    }
    sessionRep.setClients(new LinkedList());
    for (String clientUUID : s.getAuthenticatedClientSessions().keySet()) {
        ClientModel client = realm.getClientById(clientUUID);
        ClientRepresentation clientRep = new ClientRepresentation();
        clientRep.setClientId(client.getClientId());
        clientRep.setClientName(client.getName());
        sessionRep.getClients().add(clientRep);
    }
    return sessionRep;
}
Also used : ClientModel(org.keycloak.models.ClientModel) SessionRepresentation(org.keycloak.representations.account.SessionRepresentation) LinkedList(java.util.LinkedList) ClientRepresentation(org.keycloak.representations.account.ClientRepresentation)

Aggregations

ClientRepresentation (org.keycloak.representations.account.ClientRepresentation)9 SessionRepresentation (org.keycloak.representations.account.SessionRepresentation)8 Test (org.junit.Test)7 TypeReference (com.fasterxml.jackson.core.type.TypeReference)6 IOException (java.io.IOException)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 Response (javax.ws.rs.core.Response)6 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)6 Assert (org.junit.Assert)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertFalse (org.junit.Assert.assertFalse)6 Assert.assertNotNull (org.junit.Assert.assertNotNull)6 Assert.assertNull (org.junit.Assert.assertNull)6 Assert.assertThat (org.junit.Assert.assertThat)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 Rule (org.junit.Rule)6