Search in sources :

Example 1 with AuthzClient

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

the class KeycloakAdapterPolicyEnforcer method challenge.

@Override
protected boolean challenge(PathConfig pathConfig, PolicyEnforcerConfig.MethodConfig methodConfig, OIDCHttpFacade httpFacade) {
    if (isBearerAuthorization(httpFacade)) {
        HttpFacade.Response response = httpFacade.getResponse();
        AuthzClient authzClient = getAuthzClient();
        String ticket = getPermissionTicket(pathConfig, methodConfig, authzClient, httpFacade);
        if (ticket != null) {
            response.setStatus(401);
            response.setHeader("WWW-Authenticate", new StringBuilder("UMA realm=\"").append(authzClient.getConfiguration().getRealm()).append("\"").append(",as_uri=\"").append(authzClient.getServerConfiguration().getIssuer()).append("\"").append(",ticket=\"").append(ticket).append("\"").toString());
        } else {
            response.setStatus(403);
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Sending challenge");
        }
        return true;
    }
    handleAccessDenied(httpFacade);
    return true;
}
Also used : AuthzClient(org.keycloak.authorization.client.AuthzClient) OIDCHttpFacade(org.keycloak.adapters.OIDCHttpFacade) HttpFacade(org.keycloak.adapters.spi.HttpFacade)

Example 2 with AuthzClient

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

the class ResourceManagementWithAuthzClientTest method testUpdateUri.

@Test
public void testUpdateUri() {
    doCreateResource(new ResourceRepresentation("/api/v1/*", Collections.emptySet(), "/api/v1/*", null));
    AuthzClient authzClient = getAuthzClient();
    List<ResourceRepresentation> resources = authzClient.protection().resource().findByMatchingUri("/api/v1/servers");
    assertNotNull(resources);
    assertEquals(1, resources.size());
    assertEquals("/api/v1/*", resources.get(0).getUri());
    resources.get(0).getUris().clear();
    resources.get(0).getUris().add("/api/v2/*");
    authzClient.protection().resource().update(resources.get(0));
    resources = authzClient.protection().resource().findByMatchingUri("/api/v1/servers");
    assertNotNull(resources);
    assertEquals(0, resources.size());
    resources = authzClient.protection().resource().findByMatchingUri("/api/v2");
    assertNotNull(resources);
    assertEquals(1, resources.size());
    assertEquals("/api/v2/*", resources.get(0).getUri());
}
Also used : AuthzClient(org.keycloak.authorization.client.AuthzClient) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 3 with AuthzClient

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

the class ResourceManagementWithAuthzClientTest method testFindDeep.

@Test
public void testFindDeep() {
    ResourceRepresentation resource1 = new ResourceRepresentation("/*", new HashSet<>());
    resource1.addScope("a", "b", "c");
    resource1.setType("type");
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("a", Arrays.asList("a"));
    attributes.put("b", Arrays.asList("b"));
    attributes.put("c", Arrays.asList("c"));
    resource1.setAttributes(attributes);
    resource1.setIconUri("icon");
    resource1.setUris(new HashSet<>(Arrays.asList("/a", "/b", "/c")));
    ResourceRepresentation resource = doCreateResource(resource1);
    AuthzClient authzClient = getAuthzClient();
    List<ResourceRepresentation> representations = authzClient.protection().resource().find(resource.getId(), null, null, null, null, null, false, true, null, null);
    assertEquals(1, representations.size());
    assertEquals(resource.getId(), representations.get(0).getId());
    assertEquals(resource.getName(), representations.get(0).getName());
    assertEquals(resource.getIconUri(), representations.get(0).getIconUri());
    assertThat(resource.getUris(), Matchers.containsInAnyOrder(representations.get(0).getUris().toArray()));
    assertThat(resource.getAttributes().entrySet(), Matchers.containsInAnyOrder(representations.get(0).getAttributes().entrySet().toArray()));
}
Also used : AuthzClient(org.keycloak.authorization.client.AuthzClient) HashMap(java.util.HashMap) List(java.util.List) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) Test(org.junit.Test)

Example 4 with AuthzClient

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

the class PolicyEnforcerClaimsTest method testEnforceUMAAccessWithClaimsUsingBearerToken.

@Test
public void testEnforceUMAAccessWithClaimsUsingBearerToken() {
    initAuthorizationSettings(getClientResource("resource-server-uma-test"));
    KeycloakDeployment deployment = KeycloakDeploymentBuilder.build(getAdapterConfiguration("enforcer-uma-claims-test.json"));
    PolicyEnforcer policyEnforcer = deployment.getPolicyEnforcer();
    HashMap<String, List<String>> headers = new HashMap<>();
    HashMap<String, List<String>> parameters = new HashMap<>();
    parameters.put("withdrawal.amount", Arrays.asList("50"));
    AuthzClient authzClient = getAuthzClient("enforcer-uma-claims-test.json");
    String token = authzClient.obtainAccessToken("marta", "password").getToken();
    headers.put("Authorization", Arrays.asList("Bearer " + token));
    AuthorizationContext context = policyEnforcer.enforce(createHttpFacade("/api/bank/account/1/withdrawal", "POST", token, headers, parameters));
    assertFalse(context.isGranted());
    AuthorizationRequest request = new AuthorizationRequest();
    request.setTicket(extractTicket(headers));
    AuthorizationResponse response = authzClient.authorization("marta", "password").authorize(request);
    token = response.getToken();
    assertNotNull(token);
    context = policyEnforcer.enforce(createHttpFacade("/api/bank/account/1/withdrawal", "POST", token, headers, parameters));
    assertTrue(context.isGranted());
    parameters.put("withdrawal.amount", Arrays.asList("200"));
    context = policyEnforcer.enforce(createHttpFacade("/api/bank/account/1/withdrawal", "POST", token, headers, parameters));
    assertFalse(context.isGranted());
    parameters.put("withdrawal.amount", Arrays.asList("50"));
    context = policyEnforcer.enforce(createHttpFacade("/api/bank/account/1/withdrawal", "POST", token, headers, parameters));
    assertTrue(context.isGranted());
    parameters.put("withdrawal.amount", Arrays.asList("10"));
    context = policyEnforcer.enforce(createHttpFacade("/api/bank/account/1/withdrawal", "POST", token, headers, parameters));
    request = new AuthorizationRequest();
    request.setTicket(extractTicket(headers));
    response = authzClient.authorization("marta", "password").authorize(request);
    token = response.getToken();
    context = policyEnforcer.enforce(createHttpFacade("/api/bank/account/1/withdrawal", "POST", token, headers, parameters));
    assertTrue(context.isGranted());
    request = new AuthorizationRequest();
    request.setTicket(extractTicket(headers));
    response = authzClient.authorization("marta", "password").authorize(request);
    token = response.getToken();
    context = policyEnforcer.enforce(createHttpFacade("/api/bank/account/1/withdrawal", "GET", token, headers, parameters));
    assertTrue(context.isGranted());
    assertEquals(1, context.getPermissions().size());
    Permission permission = context.getPermissions().get(0);
    assertEquals(parameters.get("withdrawal.amount").get(0), permission.getClaims().get("withdrawal.amount").iterator().next());
}
Also used : AuthzClient(org.keycloak.authorization.client.AuthzClient) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) HashMap(java.util.HashMap) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) Permission(org.keycloak.representations.idm.authorization.Permission) PolicyEnforcer(org.keycloak.adapters.authorization.PolicyEnforcer) List(java.util.List) AuthorizationContext(org.keycloak.AuthorizationContext) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Example 5 with AuthzClient

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

the class EntitlementAPITest method testObtainAllEntitlementsForResourceWithResourcePermission.

@Test
public void testObtainAllEntitlementsForResourceWithResourcePermission() 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(KeycloakModelUtils.generateId());
    resource.addScope("scope:view", "scope:update", "scope:delete");
    try (Response response = authorization.resources().create(resource)) {
        resource = response.readEntity(ResourceRepresentation.class);
    }
    ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
    permission.setName(KeycloakModelUtils.generateId());
    permission.addResource(resource.getId());
    permission.addPolicy(policy.getName());
    authorization.permissions().resource().create(permission).close();
    String accessToken = new OAuthClient().realm("authz-test").clientId(RESOURCE_SERVER_TEST).doGrantAccessTokenRequest("secret", "kolo", "password").getAccessToken();
    AuthzClient authzClient = getAuthzClient(AUTHZ_CLIENT_CONFIG);
    AuthorizationRequest request = new AuthorizationRequest();
    request.addPermission(null, "scope:view", "scope:update", "scope:delete");
    AuthorizationResponse response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    Collection<Permission> permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertEquals(resource.getId(), grantedPermission.getResourceId());
        assertEquals(3, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("scope:view")));
    }
    resource.setScopes(new HashSet<>());
    resource.addScope("scope:view", "scope:update");
    authorization.resources().resource(resource.getId()).update(resource);
    request = new AuthorizationRequest();
    request.addPermission(null, "scope:view", "scope:update", "scope:delete");
    response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertEquals(resource.getId(), grantedPermission.getResourceId());
        assertEquals(2, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("scope:view", "scope:update")));
    }
    request = new AuthorizationRequest();
    request.addPermission(resource.getId(), "scope:view", "scope:update", "scope:delete");
    response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertEquals(resource.getId(), grantedPermission.getResourceId());
        assertEquals(2, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("scope:view", "scope:update")));
    }
}
Also used : AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) OAuthClient(org.keycloak.testsuite.util.OAuthClient) 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) 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) Permission(org.keycloak.representations.idm.authorization.Permission) ClientResource(org.keycloak.admin.client.resource.ClientResource) Test(org.junit.Test)

Aggregations

AuthzClient (org.keycloak.authorization.client.AuthzClient)70 Test (org.junit.Test)60 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)50 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)43 AuthorizationResponse (org.keycloak.representations.idm.authorization.AuthorizationResponse)40 PermissionRequest (org.keycloak.representations.idm.authorization.PermissionRequest)31 PermissionResponse (org.keycloak.representations.idm.authorization.PermissionResponse)29 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)27 ClientResource (org.keycloak.admin.client.resource.ClientResource)26 HttpResponseException (org.keycloak.authorization.client.util.HttpResponseException)22 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)20 Permission (org.keycloak.representations.idm.authorization.Permission)20 OAuthClient (org.keycloak.testsuite.util.OAuthClient)20 Response (javax.ws.rs.core.Response)18 TokenIntrospectionResponse (org.keycloak.authorization.client.representation.TokenIntrospectionResponse)17 AccessTokenResponse (org.keycloak.representations.AccessTokenResponse)17 ResourcePermissionRepresentation (org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation)16 ScopePermissionRepresentation (org.keycloak.representations.idm.authorization.ScopePermissionRepresentation)16 AccessToken (org.keycloak.representations.AccessToken)14 ArrayList (java.util.ArrayList)12