Search in sources :

Example 1 with ClientRegistration

use of org.keycloak.client.registration.ClientRegistration 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 ClientRegistration

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

the class KeyRotationTest method testTokens.

@Test
public void testTokens() throws Exception {
    // Create keys #1
    Map<String, String> keys1 = createKeys1();
    // Get token with keys #1
    oauth.doLogin("test-user@localhost", "password");
    OAuthClient.AccessTokenResponse response = oauth.doAccessTokenRequest(oauth.getCurrentQuery().get("code"), "password");
    assertEquals(200, response.getStatusCode());
    assertTokenKid(keys1.get(Algorithm.RS256), response.getAccessToken());
    assertTokenKid(keys1.get(Algorithm.HS256), response.getRefreshToken());
    // Create client with keys #1
    ClientInitialAccessCreatePresentation initialToken = new ClientInitialAccessCreatePresentation();
    initialToken.setCount(100);
    initialToken.setExpiration(0);
    ClientInitialAccessPresentation accessRep = adminClient.realm("test").clientInitialAccess().create(initialToken);
    String initialAccessToken = accessRep.getToken();
    ClientRegistration reg = ClientRegistration.create().url(suiteContext.getAuthServerInfo().getContextRoot() + "/auth", "test").build();
    reg.auth(Auth.token(initialAccessToken));
    ClientRepresentation clientRep = reg.create(ClientBuilder.create().clientId("test").build());
    // Userinfo with keys #1
    assertUserInfo(response.getAccessToken(), 200);
    // Token introspection with keys #1
    assertTokenIntrospection(response.getAccessToken(), true);
    // Get client with keys #1 - registration access token should not have changed
    ClientRepresentation clientRep2 = reg.auth(Auth.token(clientRep.getRegistrationAccessToken())).get("test");
    assertEquals(clientRep.getRegistrationAccessToken(), clientRep2.getRegistrationAccessToken());
    // Create keys #2
    Map<String, String> keys2 = createKeys2();
    assertNotEquals(keys1.get(Algorithm.RS256), keys2.get(Algorithm.RS256));
    assertNotEquals(keys1.get(Algorithm.HS256), keys2.get(Algorithm.HS512));
    // Refresh token with keys #2
    response = oauth.doRefreshTokenRequest(response.getRefreshToken(), "password");
    assertEquals(200, response.getStatusCode());
    assertTokenKid(keys2.get(Algorithm.RS256), response.getAccessToken());
    assertTokenKid(keys2.get(Algorithm.HS256), response.getRefreshToken());
    // Userinfo with keys #2
    assertUserInfo(response.getAccessToken(), 200);
    // Token introspection with keys #2
    assertTokenIntrospection(response.getAccessToken(), true);
    // Get client with keys #2 - registration access token should be changed
    ClientRepresentation clientRep3 = reg.auth(Auth.token(clientRep.getRegistrationAccessToken())).get("test");
    assertNotEquals(clientRep.getRegistrationAccessToken(), clientRep3.getRegistrationAccessToken());
    // Drop key #1
    dropKeys1();
    // Refresh token with keys #1 dropped - should pass as refresh token should be signed with key #2
    response = oauth.doRefreshTokenRequest(response.getRefreshToken(), "password");
    assertTokenKid(keys2.get(Algorithm.RS256), response.getAccessToken());
    assertTokenKid(keys2.get(Algorithm.HS256), response.getRefreshToken());
    // Userinfo with keys #1 dropped
    assertUserInfo(response.getAccessToken(), 200);
    // Token introspection with keys #1 dropped
    assertTokenIntrospection(response.getAccessToken(), true);
    // Get client with keys #1 - should fail
    try {
        reg.auth(Auth.token(clientRep.getRegistrationAccessToken())).get("test");
        fail("Expected to fail");
    } catch (ClientRegistrationException e) {
    }
    // Get client with keys #2 - should succeed
    ClientRepresentation clientRep4 = reg.auth(Auth.token(clientRep3.getRegistrationAccessToken())).get("test");
    assertNotEquals(clientRep2.getRegistrationAccessToken(), clientRep4.getRegistrationAccessToken());
    // Drop key #2
    dropKeys2();
    // Userinfo with keys #2 dropped
    assertUserInfo(response.getAccessToken(), 401);
    // Token introspection with keys #2 dropped
    assertTokenIntrospection(response.getAccessToken(), false);
    // Refresh token with keys #2 dropped - should fail as refresh token is signed with key #2
    response = oauth.doRefreshTokenRequest(response.getRefreshToken(), "password");
    assertEquals(400, response.getStatusCode());
    assertEquals("Invalid refresh token", response.getErrorDescription());
}
Also used : ClientRegistration(org.keycloak.client.registration.ClientRegistration) OAuthClient(org.keycloak.testsuite.util.OAuthClient) ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException) ClientInitialAccessCreatePresentation(org.keycloak.representations.idm.ClientInitialAccessCreatePresentation) ClientInitialAccessPresentation(org.keycloak.representations.idm.ClientInitialAccessPresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 3 with ClientRegistration

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

the class ClientRegistrationTest method registerClientInMasterRealm.

@Test
public void registerClientInMasterRealm() throws Exception {
    ClientRegistration masterReg = ClientRegistration.create().url(suiteContext.getAuthServerInfo().getContextRoot() + "/auth", "master").build();
    String token = oauth.doGrantAccessTokenRequest("master", "admin", "admin", null, Constants.ADMIN_CLI_CLIENT_ID, null).getAccessToken();
    masterReg.auth(Auth.token(token));
    ClientRepresentation client = new ClientRepresentation();
    client.setClientId(CLIENT_ID);
    client.setSecret(CLIENT_SECRET);
    ClientRepresentation createdClient = masterReg.create(client);
    assertNotNull(createdClient);
    adminClient.realm("master").clients().get(createdClient.getId()).remove();
}
Also used : ClientRegistration(org.keycloak.client.registration.ClientRegistration) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 4 with ClientRegistration

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

the class TestsHelper method deleteClient.

public static void deleteClient(String clientId) {
    ClientRegistration reg = ClientRegistration.create().url(keycloakBaseUrl, testRealm).build();
    try {
        reg.auth(Auth.token(registrationAccessCode));
        reg.delete(clientId);
    } catch (ClientRegistrationException e) {
        e.printStackTrace();
    }
}
Also used : ClientRegistration(org.keycloak.client.registration.ClientRegistration) ClientRegistrationException(org.keycloak.client.registration.ClientRegistrationException)

Example 5 with ClientRegistration

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

the class DefaultHostnameTest 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 + "/realms/" + realm, token.getIssuer());
    ClientRegistration clientReg = ClientRegistration.create().url(AUTH_SERVER_ROOT, 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 + "/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)

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