Search in sources :

Example 1 with Configuration

use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.

the class ResourcesRestServiceTest method createAuthzClient.

private AuthzClient createAuthzClient(ClientRepresentation client) {
    Map<String, Object> credentials = new HashMap<>();
    credentials.put("secret", "secret");
    return AuthzClient.create(new Configuration(suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth", testRealm().toRepresentation().getRealm(), client.getClientId(), credentials, httpClient));
}
Also used : Configuration(org.keycloak.authorization.client.Configuration) HashMap(java.util.HashMap)

Example 2 with Configuration

use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.

the class EntitlementAPITest method testClientToClientPermissionRequest.

@Test
public void testClientToClientPermissionRequest() throws Exception {
    ClientResource client = getClient(getRealm(), RESOURCE_SERVER_TEST);
    AuthorizationResource authorization = client.authorization();
    JSPolicyRepresentation policy = new JSPolicyRepresentation();
    policy.setName(KeycloakModelUtils.generateId());
    policy.setCode("$evaluation.grant();");
    authorization.policies().js().create(policy).close();
    ResourceRepresentation resource = new ResourceRepresentation();
    resource.setName("Sensors");
    try (Response response = authorization.resources().create(resource)) {
        response.readEntity(ResourceRepresentation.class);
    }
    ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
    permission.setName("View Sensor");
    permission.addPolicy(policy.getName());
    authorization.permissions().resource().create(permission).close();
    ClientRepresentation otherClient = new ClientRepresentation();
    otherClient.setClientId("serviceB");
    otherClient.setServiceAccountsEnabled(true);
    otherClient.setSecret("secret");
    otherClient.setPublicClient(false);
    getRealm().clients().create(otherClient);
    Map<String, Object> credentials = new HashMap<>();
    credentials.put("secret", "secret");
    AuthzClient authzClient = AuthzClient.create(new Configuration(suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth", getRealm().toRepresentation().getRealm(), otherClient.getClientId(), credentials, getAuthzClient(AUTHZ_CLIENT_CONFIG).getConfiguration().getHttpClient()));
    AuthorizationRequest request = new AuthorizationRequest();
    request.setAudience(RESOURCE_SERVER_TEST);
    AuthorizationResponse response = authzClient.authorization().authorize(request);
    assertNotNull(response.getToken());
    // Refresh token should not be present
    assertNull(response.getRefreshToken());
}
Also used : AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) Configuration(org.keycloak.authorization.client.Configuration) HashMap(java.util.HashMap) JSPolicyRepresentation(org.keycloak.representations.idm.authorization.JSPolicyRepresentation) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) ResourcePermissionRepresentation(org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) Response(javax.ws.rs.core.Response) TokenIntrospectionResponse(org.keycloak.authorization.client.representation.TokenIntrospectionResponse) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) AuthzClient(org.keycloak.authorization.client.AuthzClient) ClientResource(org.keycloak.admin.client.resource.ClientResource) Test(org.junit.Test)

Example 3 with Configuration

use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.

the class EntitlementAPITest method getAuthzClient.

private AuthzClient getAuthzClient(String configFile) {
    if (authzClient == null) {
        Configuration configuration;
        try {
            configuration = JsonSerialization.readValue(httpsAwareConfigurationStream(getClass().getResourceAsStream("/authorization-test/" + configFile)), Configuration.class);
        } catch (IOException e) {
            throw new RuntimeException("Failed to read configuration", e);
        }
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setValidateAfterInactivity(10);
        connectionManager.setMaxTotal(10);
        HttpClient client = HttpClients.custom().setConnectionManager(connectionManager).build();
        authzClient = AuthzClient.create(new Configuration(configuration.getAuthServerUrl(), configuration.getRealm(), configuration.getResource(), configuration.getCredentials(), client));
    }
    return authzClient;
}
Also used : Configuration(org.keycloak.authorization.client.Configuration) HttpClient(org.apache.http.client.HttpClient) IOException(java.io.IOException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 4 with Configuration

use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.

the class MyResourcesTest method createAuthzClient.

private AuthzClient createAuthzClient(ClientRepresentation client) {
    Map<String, Object> credentials = new HashMap<>();
    credentials.put("secret", "secret");
    httpClient = HttpClientBuilder.create().build();
    return AuthzClient.create(new Configuration(suiteContext.getAuthServerInfo().getContextRoot().toString() + "/auth", testRealm.getRealm(), client.getClientId(), credentials, httpClient));
}
Also used : Configuration(org.keycloak.authorization.client.Configuration) HashMap(java.util.HashMap)

Example 5 with Configuration

use of org.keycloak.authorization.client.Configuration in project keycloak by keycloak.

the class EntitlementAPITest method testPermissionsAcrossResourceServers.

@Test
public void testPermissionsAcrossResourceServers() throws Exception {
    String rsAId;
    try (Response response = getRealm().clients().create(ClientBuilder.create().clientId("rs-a").secret("secret").serviceAccount().authorizationServicesEnabled(true).build())) {
        rsAId = ApiUtil.getCreatedId(response);
    }
    String rsBId;
    try (Response response = getRealm().clients().create(ClientBuilder.create().clientId("rs-b").secret("secret").serviceAccount().authorizationServicesEnabled(true).build())) {
        rsBId = ApiUtil.getCreatedId(response);
    }
    ClientResource rsB = getRealm().clients().get(rsBId);
    rsB.authorization().resources().create(new ResourceRepresentation("Resource A"));
    JSPolicyRepresentation grantPolicy = new JSPolicyRepresentation();
    grantPolicy.setName("Grant Policy");
    grantPolicy.setCode("$evaluation.grant();");
    rsB.authorization().policies().js().create(grantPolicy);
    ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
    permission.setName("Resource A Permission");
    permission.addResource("Resource A");
    permission.addPolicy(grantPolicy.getName());
    rsB.authorization().permissions().resource().create(permission);
    AuthzClient authzClient = getAuthzClient(AUTHZ_CLIENT_CONFIG);
    Configuration config = authzClient.getConfiguration();
    config.setResource("rs-a");
    authzClient = AuthzClient.create(config);
    AccessTokenResponse accessTokenResponse = authzClient.obtainAccessToken();
    AccessToken accessToken = toAccessToken(accessTokenResponse.getToken());
    config.setResource("rs-b");
    AuthorizationRequest request = new AuthorizationRequest();
    request.addPermission("Resource A");
    AuthorizationResponse response = authzClient.authorization(accessTokenResponse.getToken()).authorize(request);
    assertNotNull(response.getToken());
    Collection<Permission> permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    assertEquals("Resource A", permissions.iterator().next().getResourceName());
}
Also used : AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) Configuration(org.keycloak.authorization.client.Configuration) JSPolicyRepresentation(org.keycloak.representations.idm.authorization.JSPolicyRepresentation) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) ResourcePermissionRepresentation(org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) Response(javax.ws.rs.core.Response) TokenIntrospectionResponse(org.keycloak.authorization.client.representation.TokenIntrospectionResponse) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) PermissionResponse(org.keycloak.representations.idm.authorization.PermissionResponse) AuthzClient(org.keycloak.authorization.client.AuthzClient) AccessToken(org.keycloak.representations.AccessToken) Permission(org.keycloak.representations.idm.authorization.Permission) ClientResource(org.keycloak.admin.client.resource.ClientResource) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) Test(org.junit.Test)

Aggregations

Configuration (org.keycloak.authorization.client.Configuration)5 HashMap (java.util.HashMap)3 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 ClientResource (org.keycloak.admin.client.resource.ClientResource)2 AuthzClient (org.keycloak.authorization.client.AuthzClient)2 TokenIntrospectionResponse (org.keycloak.authorization.client.representation.TokenIntrospectionResponse)2 AccessTokenResponse (org.keycloak.representations.AccessTokenResponse)2 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)2 AuthorizationResponse (org.keycloak.representations.idm.authorization.AuthorizationResponse)2 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)2 PermissionResponse (org.keycloak.representations.idm.authorization.PermissionResponse)2 ResourcePermissionRepresentation (org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation)2 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)2 IOException (java.io.IOException)1 HttpClient (org.apache.http.client.HttpClient)1 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)1 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)1 AccessToken (org.keycloak.representations.AccessToken)1 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)1