Search in sources :

Example 6 with ClientRegistrationException

use of org.keycloak.client.registration.ClientRegistrationException in project keycloak by keycloak.

the class ClientRegistrationTest method registerOrUpdateClientExpectingValidationErrors.

private void registerOrUpdateClientExpectingValidationErrors(ClientRepresentation rep, boolean register, boolean redirectUris, String... expectedErrors) {
    HttpErrorException errorException = null;
    try {
        if (register) {
            registerClient(rep);
        } else {
            reg.update(rep);
        }
        fail("Expected exception");
    } catch (ClientRegistrationException e) {
        errorException = (HttpErrorException) e.getCause();
    }
    expectedErrors = Arrays.stream(expectedErrors).filter(Objects::nonNull).toArray(String[]::new);
    assertEquals(errorException.getStatusLine().getStatusCode(), 400);
    OAuth2ErrorRepresentation errorRep;
    try {
        errorRep = JsonSerialization.readValue(errorException.getErrorResponse(), OAuth2ErrorRepresentation.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    List<String> actualErrors = asList(errorRep.getErrorDescription().split("; "));
    assertThat(actualErrors, containsInAnyOrder(expectedErrors));
    assertEquals(redirectUris ? INVALID_REDIRECT_URI : INVALID_CLIENT_METADATA, errorRep.getError());
}
Also used : Objects(java.util.Objects) ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) OAuth2ErrorRepresentation(org.keycloak.representations.idm.OAuth2ErrorRepresentation) IOException(java.io.IOException) HttpErrorException(org.keycloak.client.registration.HttpErrorException)

Example 7 with ClientRegistrationException

use of org.keycloak.client.registration.ClientRegistrationException in project keycloak by keycloak.

the class ClientRegistrationTest method deleteClientAsAdminWithNoAccess.

@Test
public void deleteClientAsAdminWithNoAccess() throws ClientRegistrationException {
    authManageClients();
    ClientRepresentation client = registerClient();
    try {
        authNoAccess();
        deleteClient(client);
        fail("Expected 403");
    } catch (ClientRegistrationException e) {
        assertEquals(403, ((HttpErrorException) e.getCause()).getStatusLine().getStatusCode());
    }
}
Also used : ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 8 with ClientRegistrationException

use of org.keycloak.client.registration.ClientRegistrationException in project keycloak by keycloak.

the class ClientRegistrationTest method updateClientNotFound.

@Test
public void updateClientNotFound() throws ClientRegistrationException {
    authManageClients();
    try {
        ClientRepresentation client = new ClientRepresentation();
        client.setClientId("invalid");
        reg.update(client);
        fail("Expected 404");
    } catch (ClientRegistrationException e) {
        assertEquals(404, ((HttpErrorException) e.getCause()).getStatusLine().getStatusCode());
    }
}
Also used : ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 9 with ClientRegistrationException

use of org.keycloak.client.registration.ClientRegistrationException in project keycloak by keycloak.

the class ClientRegistrationTest method testClientUriValidation.

private void testClientUriValidation(boolean register, String expectedRootUrlError, String expectedBaseUrlError, String expectedBackchannelLogoutUrlError, String expectedRedirectUrisError, String... testUrls) {
    ClientRepresentation rep;
    if (register) {
        authCreateClients();
        rep = buildClient();
    } else {
        try {
            registerClientAsAdmin();
            rep = reg.get(CLIENT_ID);
        } catch (ClientRegistrationException e) {
            throw new RuntimeException(e);
        }
    }
    for (String testUrl : testUrls) {
        if (expectedRootUrlError != null) {
            rep.setRootUrl(testUrl);
            registerOrUpdateClientExpectingValidationErrors(rep, register, false, expectedRootUrlError);
        }
        rep.setRootUrl(null);
        if (expectedBaseUrlError != null) {
            rep.setBaseUrl(testUrl);
            registerOrUpdateClientExpectingValidationErrors(rep, register, false, expectedBaseUrlError);
        }
        rep.setBaseUrl(null);
        if (expectedBackchannelLogoutUrlError != null) {
            OIDCAdvancedConfigWrapper.fromClientRepresentation(rep).setBackchannelLogoutUrl(testUrl);
            registerOrUpdateClientExpectingValidationErrors(rep, register, false, expectedBackchannelLogoutUrlError);
        }
        OIDCAdvancedConfigWrapper.fromClientRepresentation(rep).setBackchannelLogoutUrl(null);
        if (expectedRedirectUrisError != null) {
            rep.setRedirectUris(Collections.singletonList(testUrl));
            registerOrUpdateClientExpectingValidationErrors(rep, register, true, expectedRedirectUrisError);
        }
        rep.setRedirectUris(null);
        if (expectedRootUrlError != null)
            rep.setRootUrl(testUrl);
        if (expectedBaseUrlError != null)
            rep.setBaseUrl(testUrl);
        if (expectedRedirectUrisError != null)
            rep.setRedirectUris(Collections.singletonList(testUrl));
        registerOrUpdateClientExpectingValidationErrors(rep, register, expectedRedirectUrisError != null, expectedRootUrlError, expectedBaseUrlError, expectedRedirectUrisError);
        rep.setRootUrl(null);
        rep.setBaseUrl(null);
        rep.setRedirectUris(null);
    }
}
Also used : ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 10 with ClientRegistrationException

use of org.keycloak.client.registration.ClientRegistrationException in project keycloak by keycloak.

the class ClientRegistrationTest method registerClientAsAdminWithoutScope.

@Test
public void registerClientAsAdminWithoutScope() throws ClientRegistrationException {
    Set<String> realmDefaultClientScopes = new HashSet<>(adminClient.realm(REALM_NAME).getDefaultDefaultClientScopes().stream().filter(scope -> Objects.equals(scope.getProtocol(), OIDCLoginProtocol.LOGIN_PROTOCOL)).map(i -> i.getName()).collect(Collectors.toList()));
    Set<String> realmOptionalClientScopes = new HashSet<>(adminClient.realm(REALM_NAME).getDefaultOptionalClientScopes().stream().filter(scope -> Objects.equals(scope.getProtocol(), OIDCLoginProtocol.LOGIN_PROTOCOL)).map(i -> i.getName()).collect(Collectors.toList()));
    authManageClients();
    ClientRepresentation client = new ClientRepresentation();
    client.setClientId(CLIENT_ID);
    client.setSecret(CLIENT_SECRET);
    ClientRepresentation createdClient = reg.create(client);
    assertEquals(CLIENT_ID, createdClient.getClientId());
    client = adminClient.realm(REALM_NAME).clients().get(createdClient.getId()).toRepresentation();
    assertEquals(CLIENT_ID, client.getClientId());
    // Remove this client after test
    getCleanup().addClientUuid(createdClient.getId());
    assertTrue(realmDefaultClientScopes.equals(new HashSet<>(client.getDefaultClientScopes())));
    assertTrue(realmOptionalClientScopes.equals(new HashSet<>(client.getOptionalClientScopes())));
}
Also used : Arrays(java.util.Arrays) AuthServerContainerExclude(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude) Constants(org.keycloak.models.Constants) INVALID_REDIRECT_URI(org.keycloak.services.clientregistration.ErrorCodes.INVALID_REDIRECT_URI) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Arrays.asList(java.util.Arrays.asList) OIDCAdvancedConfigWrapper(org.keycloak.protocol.oidc.OIDCAdvancedConfigWrapper) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Is.is(org.hamcrest.core.Is.is) HttpErrorException(org.keycloak.client.registration.HttpErrorException) Assert.fail(org.junit.Assert.fail) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AuthServer(org.keycloak.testsuite.arquillian.annotation.AuthServerContainerExclude.AuthServer) OAuth2ErrorRepresentation(org.keycloak.representations.idm.OAuth2ErrorRepresentation) ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Assert.assertNotNull(org.junit.Assert.assertNotNull) Matchers(org.hamcrest.Matchers) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) ProtocolMapperRepresentation(org.keycloak.representations.idm.ProtocolMapperRepresentation) Auth(org.keycloak.client.registration.Auth) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) JsonSerialization(org.keycloak.util.JsonSerialization) Objects(java.util.Objects) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) INVALID_CLIENT_METADATA(org.keycloak.services.clientregistration.ErrorCodes.INVALID_CLIENT_METADATA) OIDCLoginProtocol(org.keycloak.protocol.oidc.OIDCLoginProtocol) ClientRegistration(org.keycloak.client.registration.ClientRegistration) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) HashSet(java.util.HashSet) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Aggregations

ClientRegistrationException (org.keycloak.client.registration.ClientRegistrationException)29 Test (org.junit.Test)22 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)20 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)11 ClientInitialAccessCreatePresentation (org.keycloak.representations.idm.ClientInitialAccessCreatePresentation)7 ClientInitialAccessPresentation (org.keycloak.representations.idm.ClientInitialAccessPresentation)7 ClientRegistration (org.keycloak.client.registration.ClientRegistration)6 IOException (java.io.IOException)5 HttpErrorException (org.keycloak.client.registration.HttpErrorException)5 ClientPolicyException (org.keycloak.services.clientpolicy.ClientPolicyException)5 ClientPoliciesBuilder (org.keycloak.testsuite.util.ClientPoliciesUtil.ClientPoliciesBuilder)5 ClientPolicyBuilder (org.keycloak.testsuite.util.ClientPoliciesUtil.ClientPolicyBuilder)5 ClientProfileBuilder (org.keycloak.testsuite.util.ClientPoliciesUtil.ClientProfileBuilder)5 ClientProfilesBuilder (org.keycloak.testsuite.util.ClientPoliciesUtil.ClientProfilesBuilder)5 Collectors (java.util.stream.Collectors)3 BadRequestException (javax.ws.rs.BadRequestException)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertNotNull (org.junit.Assert.assertNotNull)3 OAuthErrorException (org.keycloak.OAuthErrorException)3 List (java.util.List)2