Search in sources :

Example 1 with HttpErrorException

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

the class ClientRegistrationTester method main.

public static void main(String[] args) throws ClientRegistrationException {
    ClientRepresentation rep = createRep1();
    ClientRegistration reg = ClientRegistration.create().url("http://localhost:8081/auth", "test").build();
    try {
        ClientRepresentation createdRep = reg.create(rep);
        System.out.println("Created client: " + createdRep.getClientId());
    } catch (ClientRegistrationException ex) {
        HttpErrorException httpEx = (HttpErrorException) ex.getCause();
        System.err.println("HttpException when registering client. Status=" + httpEx.getStatusLine().getStatusCode() + ", Details=" + httpEx.getErrorResponse());
    }
}
Also used : ClientRegistration(org.keycloak.client.registration.ClientRegistration) ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) HttpErrorException(org.keycloak.client.registration.HttpErrorException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 2 with HttpErrorException

use of org.keycloak.client.registration.HttpErrorException 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 3 with HttpErrorException

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

the class RegistrationAccessTokenTest method assertRead.

private ClientRepresentation assertRead(String id, String registrationAccess, boolean expectSuccess) throws ClientRegistrationException {
    if (expectSuccess) {
        reg.auth(Auth.token(registrationAccess));
        ClientRepresentation rep = reg.get(id);
        assertNotNull(rep);
        return rep;
    } else {
        reg.auth(Auth.token(registrationAccess));
        try {
            reg.get(client.getClientId());
            fail("Expected 403");
        } catch (Exception e) {
            assertEquals(401, ((HttpErrorException) e.getCause()).getStatusLine().getStatusCode());
        }
    }
    return null;
}
Also used : ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) HttpErrorException(org.keycloak.client.registration.HttpErrorException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Aggregations

ClientRegistrationException (org.keycloak.client.registration.ClientRegistrationException)3 HttpErrorException (org.keycloak.client.registration.HttpErrorException)3 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)2 IOException (java.io.IOException)1 Objects (java.util.Objects)1 ClientRegistration (org.keycloak.client.registration.ClientRegistration)1 OAuth2ErrorRepresentation (org.keycloak.representations.idm.OAuth2ErrorRepresentation)1