Search in sources :

Example 6 with ClientRegistration

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

the class FixedHostnameTest method assertInitialAccessTokenFromMasterRealm.

private void assertInitialAccessTokenFromMasterRealm(Keycloak testAdminClient, String realm, String expectedBaseUrl) throws JWSInputException, ClientRegistrationException {
    ClientInitialAccessCreatePresentation rep = new ClientInitialAccessCreatePresentation();
    rep.setCount(1);
    rep.setExpiration(10000);
    ClientInitialAccessPresentation initialAccess = testAdminClient.realm(realm).clientInitialAccess().create(rep);
    JsonWebToken token = new JWSInput(initialAccess.getToken()).readJsonContent(JsonWebToken.class);
    assertEquals(expectedBaseUrl + "/auth/realms/" + realm, token.getIssuer());
    ClientRegistration clientReg = ClientRegistration.create().url(authServerUrl, realm).build();
    clientReg.auth(Auth.token(initialAccess.getToken()));
    ClientRepresentation client = new ClientRepresentation();
    client.setEnabled(true);
    ClientRepresentation response = clientReg.create(client);
    String registrationAccessToken = response.getRegistrationAccessToken();
    JsonWebToken registrationToken = new JWSInput(registrationAccessToken).readJsonContent(JsonWebToken.class);
    assertEquals(expectedBaseUrl + "/auth/realms/" + realm, registrationToken.getIssuer());
}
Also used : ClientRegistration(org.keycloak.client.registration.ClientRegistration) ClientInitialAccessCreatePresentation(org.keycloak.representations.idm.ClientInitialAccessCreatePresentation) JWSInput(org.keycloak.jose.jws.JWSInput) ClientInitialAccessPresentation(org.keycloak.representations.idm.ClientInitialAccessPresentation) JsonWebToken(org.keycloak.representations.JsonWebToken) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation)

Example 7 with ClientRegistration

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

the class FluentTestsHelper method createClient.

/**
 * Creates a new client based on its representation.
 *
 * @param clientRepresentation Client data.
 * @return <code>this</code>
 */
public FluentTestsHelper createClient(ClientRepresentation clientRepresentation) throws ClientRegistrationException, JsonProcessingException {
    assert isInitialized;
    ClientRegistration reg = ClientRegistration.create().url(keycloakBaseUrl, testRealm).build();
    reg.auth(Auth.token(accessToken));
    clientRepresentation = reg.create(clientRepresentation);
    String registrationAccessCode = clientRepresentation.getRegistrationAccessToken();
    reg.auth(Auth.token(registrationAccessCode));
    createdClients.put(clientRepresentation.getClientId(), new ClientData(clientRepresentation, registrationAccessCode));
    return this;
}
Also used : ClientRegistration(org.keycloak.client.registration.ClientRegistration)

Example 8 with ClientRegistration

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

the class FluentTestsHelper method deleteClient.

/**
 * Deletes a client previously created by this helper. This will throw an error if you try to delete an
 * arbitrary client.
 *
 * @param clientId Client id to be deleted.
 * @return <code>this</code>
 * @throws ClientRegistrationException Thrown when client registration error occurs.
 */
public FluentTestsHelper deleteClient(String clientId) throws ClientRegistrationException {
    assert isInitialized;
    ClientData clientData = createdClients.get(clientId);
    if (clientData == null) {
        throw new ClientRegistrationException("This client wasn't created by this helper!");
    }
    ClientRegistration reg = ClientRegistration.create().url(keycloakBaseUrl, testRealm).build();
    reg.auth(Auth.token(clientData.getRegistrationCode()));
    reg.delete(clientId);
    return this;
}
Also used : ClientRegistration(org.keycloak.client.registration.ClientRegistration) ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException)

Example 9 with ClientRegistration

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

the class TestsHelper method createClient.

public static String createClient(ClientRepresentation clientRepresentation) {
    ClientRegistration reg = ClientRegistration.create().url(keycloakBaseUrl, testRealm).build();
    reg.auth(Auth.token(initialAccessCode));
    try {
        clientRepresentation = reg.create(clientRepresentation);
        registrationAccessCode = clientRepresentation.getRegistrationAccessToken();
        ObjectMapper mapper = new ObjectMapper();
        reg.auth(Auth.token(registrationAccessCode));
        clientConfiguration = mapper.writeValueAsString(reg.getAdapterConfig(clientRepresentation.getClientId()));
    } catch (ClientRegistrationException e) {
        e.printStackTrace();
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return clientConfiguration;
}
Also used : ClientRegistration(org.keycloak.client.registration.ClientRegistration) ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ClientRegistration (org.keycloak.client.registration.ClientRegistration)9 ClientRegistrationException (org.keycloak.client.registration.ClientRegistrationException)5 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)5 ClientInitialAccessCreatePresentation (org.keycloak.representations.idm.ClientInitialAccessCreatePresentation)3 ClientInitialAccessPresentation (org.keycloak.representations.idm.ClientInitialAccessPresentation)3 Test (org.junit.Test)2 JWSInput (org.keycloak.jose.jws.JWSInput)2 JsonWebToken (org.keycloak.representations.JsonWebToken)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HttpErrorException (org.keycloak.client.registration.HttpErrorException)1 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)1 OAuthClient (org.keycloak.testsuite.util.OAuthClient)1