Search in sources :

Example 66 with Permission

use of org.keycloak.representations.idm.authorization.Permission in project keycloak by keycloak.

the class EntitlementAPITest method testObtainAllEntitlementsForScope.

@Test
public void testObtainAllEntitlementsForScope() 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();
    Set<String> resourceIds = new HashSet<>();
    ResourceRepresentation resource = new ResourceRepresentation();
    resource.setName(KeycloakModelUtils.generateId());
    resource.addScope("sensors:view", "sensors:update", "sensors:delete");
    try (Response response = authorization.resources().create(resource)) {
        resourceIds.add(response.readEntity(ResourceRepresentation.class).getId());
    }
    resource = new ResourceRepresentation();
    resource.setName(KeycloakModelUtils.generateId());
    resource.addScope("sensors:view", "sensors:update");
    try (Response response = authorization.resources().create(resource)) {
        resourceIds.add(response.readEntity(ResourceRepresentation.class).getId());
    }
    ScopePermissionRepresentation permission = new ScopePermissionRepresentation();
    permission.setName(KeycloakModelUtils.generateId());
    permission.addScope("sensors:view", "sensors:update");
    permission.addPolicy(policy.getName());
    authorization.permissions().scope().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, "sensors:view");
    AuthorizationResponse response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    Collection<Permission> permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(2, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertTrue(resourceIds.containsAll(Arrays.asList(grantedPermission.getResourceId())));
        assertEquals(1, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("sensors:view")));
    }
    request.addPermission(null, "sensors:view", "sensors:update");
    response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(2, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertTrue(resourceIds.containsAll(Arrays.asList(grantedPermission.getResourceId())));
        assertEquals(2, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("sensors:view", "sensors:update")));
    }
    request.addPermission(null, "sensors:view", "sensors:update", "sensors:delete");
    response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(2, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertTrue(resourceIds.containsAll(Arrays.asList(grantedPermission.getResourceId())));
        assertEquals(2, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("sensors:view", "sensors:update")));
    }
    request = new AuthorizationRequest();
    request.addPermission(null, "sensors:view");
    request.addPermission(null, "sensors:update");
    response = authzClient.authorization(accessToken).authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(2, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertTrue(resourceIds.containsAll(Arrays.asList(grantedPermission.getResourceId())));
        assertEquals(2, grantedPermission.getScopes().size());
        assertTrue(grantedPermission.getScopes().containsAll(Arrays.asList("sensors:view", "sensors: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) 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) HashSet(java.util.HashSet) ScopePermissionRepresentation(org.keycloak.representations.idm.authorization.ScopePermissionRepresentation) Test(org.junit.Test)

Example 67 with Permission

use of org.keycloak.representations.idm.authorization.Permission in project keycloak by keycloak.

the class PolicyEnforcerTest method testCustomClaimProvider.

@Test
public void testCustomClaimProvider() {
    KeycloakDeployment deployment = KeycloakDeploymentBuilder.build(getAdapterConfiguration("enforcer-bearer-only-with-cip.json"));
    PolicyEnforcer policyEnforcer = deployment.getPolicyEnforcer();
    oauth.realm(REALM_NAME);
    oauth.clientId("public-client-test");
    oauth.doLogin("marta", "password");
    String code = oauth.getCurrentQuery().get(OAuth2Constants.CODE);
    OAuthClient.AccessTokenResponse response = oauth.doAccessTokenRequest(code, null);
    String token = response.getAccessToken();
    OIDCHttpFacade httpFacade = createHttpFacade("/api/resourcea", token);
    AuthorizationContext context = policyEnforcer.enforce(httpFacade);
    Permission permission = context.getPermissions().get(0);
    Map<String, Set<String>> claims = permission.getClaims();
    assertTrue(context.isGranted());
    assertEquals("test", claims.get("resolved-claim").iterator().next());
}
Also used : Set(java.util.Set) OAuthClient(org.keycloak.testsuite.util.OAuthClient) OIDCHttpFacade(org.keycloak.adapters.OIDCHttpFacade) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) Permission(org.keycloak.representations.idm.authorization.Permission) PolicyEnforcer(org.keycloak.adapters.authorization.PolicyEnforcer) AuthorizationContext(org.keycloak.AuthorizationContext) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Example 68 with Permission

use of org.keycloak.representations.idm.authorization.Permission in project keycloak by keycloak.

the class EntitlementAPITest method testPermissionsWithResourceAttributes.

@Test
public void testPermissionsWithResourceAttributes() throws Exception {
    ClientResource client = getClient(getRealm(), RESOURCE_SERVER_TEST);
    AuthorizationResource authorization = client.authorization();
    JSPolicyRepresentation onlyPublicResourcesPolicy = new JSPolicyRepresentation();
    onlyPublicResourcesPolicy.setName(KeycloakModelUtils.generateId());
    onlyPublicResourcesPolicy.setCode("var createPermission = $evaluation.getPermission();\n" + "var resource = createPermission.getResource();\n" + "\n" + "if (resource) {\n" + "    var attributes = resource.getAttributes();\n" + "    var visibility = attributes.get('visibility');\n" + "    \n" + "    if (visibility && \"private\".equals(visibility.get(0))) {\n" + "        $evaluation.deny();\n" + "      } else {\n" + "        $evaluation.grant();\n" + "    }\n" + "}");
    authorization.policies().js().create(onlyPublicResourcesPolicy).close();
    JSPolicyRepresentation onlyOwnerPolicy = createOnlyOwnerPolicy();
    authorization.policies().js().create(onlyOwnerPolicy).close();
    ResourceRepresentation typedResource = new ResourceRepresentation();
    typedResource.setType("resource");
    typedResource.setName(KeycloakModelUtils.generateId());
    try (Response response = authorization.resources().create(typedResource)) {
        typedResource = response.readEntity(ResourceRepresentation.class);
    }
    ResourceRepresentation userResource = new ResourceRepresentation();
    userResource.setName(KeycloakModelUtils.generateId());
    userResource.setType("resource");
    userResource.setOwner("marta");
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("visibility", Arrays.asList("private"));
    userResource.setAttributes(attributes);
    try (Response response = authorization.resources().create(userResource)) {
        userResource = response.readEntity(ResourceRepresentation.class);
    }
    ResourcePermissionRepresentation typedResourcePermission = new ResourcePermissionRepresentation();
    typedResourcePermission.setName(KeycloakModelUtils.generateId());
    typedResourcePermission.setResourceType("resource");
    typedResourcePermission.addPolicy(onlyPublicResourcesPolicy.getName());
    try (Response response = authorization.permissions().resource().create(typedResourcePermission)) {
        typedResourcePermission = response.readEntity(ResourcePermissionRepresentation.class);
    }
    // marta can access any public resource
    AuthzClient authzClient = getAuthzClient(AUTHZ_CLIENT_CONFIG);
    AuthorizationRequest request = new AuthorizationRequest();
    request.addPermission(typedResource.getId());
    request.addPermission(userResource.getId());
    AuthorizationResponse response = authzClient.authorization("marta", "password").authorize(request);
    assertNotNull(response.getToken());
    Collection<Permission> permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertEquals(typedResource.getName(), grantedPermission.getResourceName());
    }
    typedResourcePermission.addPolicy(onlyOwnerPolicy.getName());
    typedResourcePermission.setDecisionStrategy(DecisionStrategy.AFFIRMATIVE);
    authorization.permissions().resource().findById(typedResourcePermission.getId()).update(typedResourcePermission);
    response = authzClient.authorization("marta", "password").authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(2, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertThat(Arrays.asList(typedResource.getName(), userResource.getName()), Matchers.hasItem(grantedPermission.getResourceName()));
    }
    typedResource.setAttributes(attributes);
    authorization.resources().resource(typedResource.getId()).update(typedResource);
    response = authzClient.authorization("marta", "password").authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertThat(userResource.getName(), Matchers.equalTo(grantedPermission.getResourceName()));
    }
    userResource.addScope("create", "read");
    authorization.resources().resource(userResource.getId()).update(userResource);
    typedResource.addScope("create", "read");
    authorization.resources().resource(typedResource.getId()).update(typedResource);
    ScopePermissionRepresentation createPermission = new ScopePermissionRepresentation();
    createPermission.setName(KeycloakModelUtils.generateId());
    createPermission.addScope("create");
    createPermission.addPolicy(onlyPublicResourcesPolicy.getName());
    authorization.permissions().scope().create(createPermission).close();
    response = authzClient.authorization("marta", "password").authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    assertEquals(1, permissions.size());
    for (Permission grantedPermission : permissions) {
        assertThat(userResource.getName(), Matchers.equalTo(grantedPermission.getResourceName()));
        assertThat(grantedPermission.getScopes(), Matchers.not(Matchers.hasItem("create")));
    }
    typedResource.setAttributes(new HashMap<>());
    authorization.resources().resource(typedResource.getId()).update(typedResource);
    response = authzClient.authorization("marta", "password").authorize();
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    for (Permission grantedPermission : permissions) {
        if (grantedPermission.getResourceName().equals(userResource.getName())) {
            assertThat(grantedPermission.getScopes(), Matchers.not(Matchers.hasItem("create")));
        } else if (grantedPermission.getResourceName().equals(typedResource.getName())) {
            assertThat(grantedPermission.getScopes(), Matchers.containsInAnyOrder("create", "read"));
        }
    }
    request = new AuthorizationRequest();
    request.addPermission(typedResource.getId());
    request.addPermission(userResource.getId());
    response = authzClient.authorization("marta", "password").authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    for (Permission grantedPermission : permissions) {
        if (grantedPermission.getResourceName().equals(userResource.getName())) {
            assertThat(grantedPermission.getScopes(), Matchers.not(Matchers.hasItem("create")));
        } else if (grantedPermission.getResourceName().equals(typedResource.getName())) {
            assertThat(grantedPermission.getScopes(), Matchers.containsInAnyOrder("create", "read"));
        }
    }
    request = new AuthorizationRequest();
    request.addPermission(userResource.getId());
    request.addPermission(typedResource.getId());
    response = authzClient.authorization("marta", "password").authorize(request);
    assertNotNull(response.getToken());
    permissions = toAccessToken(response.getToken()).getAuthorization().getPermissions();
    for (Permission grantedPermission : permissions) {
        if (grantedPermission.getResourceName().equals(userResource.getName())) {
            assertThat(grantedPermission.getScopes(), Matchers.not(Matchers.hasItem("create")));
        } else if (grantedPermission.getResourceName().equals(typedResource.getName())) {
            assertThat(grantedPermission.getScopes(), Matchers.containsInAnyOrder("create", "read"));
        }
    }
}
Also used : AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) 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) 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) List(java.util.List) ArrayList(java.util.ArrayList) ScopePermissionRepresentation(org.keycloak.representations.idm.authorization.ScopePermissionRepresentation) Test(org.junit.Test)

Example 69 with Permission

use of org.keycloak.representations.idm.authorization.Permission in project keycloak by keycloak.

the class PermissionClaimTest method testClaimsFromDifferentScopePermissions.

@Test
public void testClaimsFromDifferentScopePermissions() throws Exception {
    ClientResource client = getClient(getRealm());
    AuthorizationResource authorization = client.authorization();
    ResourceRepresentation resourceA = new ResourceRepresentation(KeycloakModelUtils.generateId(), "create", "update");
    authorization.resources().create(resourceA).close();
    ResourceRepresentation resourceB = new ResourceRepresentation(KeycloakModelUtils.generateId(), "create", "update");
    authorization.resources().create(resourceB).close();
    ScopePermissionRepresentation allScopesPermission = new ScopePermissionRepresentation();
    allScopesPermission.setName(KeycloakModelUtils.generateId());
    allScopesPermission.addScope("create", "update");
    allScopesPermission.addPolicy(claimAPolicy.getName(), claimBPolicy.getName());
    authorization.permissions().scope().create(allScopesPermission).close();
    ScopePermissionRepresentation updatePermission = new ScopePermissionRepresentation();
    updatePermission.setName(KeycloakModelUtils.generateId());
    updatePermission.addScope("update");
    updatePermission.addPolicy(claimCPolicy.getName());
    try (Response response = authorization.permissions().scope().create(updatePermission)) {
        updatePermission = response.readEntity(ScopePermissionRepresentation.class);
    }
    AuthzClient authzClient = getAuthzClient();
    AuthorizationRequest request = new AuthorizationRequest();
    request.addPermission(null, "create", "update");
    AuthorizationResponse response = authzClient.authorization("marta", "password").authorize(request);
    assertNotNull(response.getToken());
    AccessToken rpt = toAccessToken(response.getToken());
    Authorization authorizationClaim = rpt.getAuthorization();
    List<Permission> permissions = new ArrayList<>(authorizationClaim.getPermissions());
    assertEquals(2, permissions.size());
    for (Permission permission : permissions) {
        Map<String, Set<String>> claims = permission.getClaims();
        assertNotNull(claims);
        assertThat(claims.get("claim-a"), Matchers.containsInAnyOrder("claim-a", "claim-a1"));
        assertThat(claims.get("claim-b"), Matchers.containsInAnyOrder("claim-b"));
        assertThat(claims.get("claim-c"), Matchers.containsInAnyOrder("claim-c"));
    }
    updatePermission.addPolicy(denyPolicy.getName());
    authorization.permissions().scope().findById(updatePermission.getId()).update(updatePermission);
    response = authzClient.authorization("marta", "password").authorize(request);
    assertNotNull(response.getToken());
    rpt = toAccessToken(response.getToken());
    authorizationClaim = rpt.getAuthorization();
    permissions = new ArrayList<>(authorizationClaim.getPermissions());
    assertEquals(2, permissions.size());
    for (Permission permission : permissions) {
        Map<String, Set<String>> claims = permission.getClaims();
        assertNotNull(claims);
        assertThat(claims.get("claim-a"), Matchers.containsInAnyOrder("claim-a", "claim-a1"));
        assertThat(claims.get("claim-b"), Matchers.containsInAnyOrder("claim-b"));
        assertThat(claims.get("claim-c"), Matchers.containsInAnyOrder("claim-c"));
        assertThat(claims.get("deny-policy"), Matchers.containsInAnyOrder("deny-policy"));
    }
}
Also used : AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) AuthorizationResource(org.keycloak.admin.client.resource.AuthorizationResource) ResourceRepresentation(org.keycloak.representations.idm.authorization.ResourceRepresentation) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) AuthorizationResponse(org.keycloak.representations.idm.authorization.AuthorizationResponse) Response(javax.ws.rs.core.Response) Authorization(org.keycloak.representations.AccessToken.Authorization) 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) ScopePermissionRepresentation(org.keycloak.representations.idm.authorization.ScopePermissionRepresentation) Test(org.junit.Test)

Example 70 with Permission

use of org.keycloak.representations.idm.authorization.Permission in project keycloak by keycloak.

the class PermissionClaimTest method testPermissionWithClaimsDifferentPolicies.

@Test
public void testPermissionWithClaimsDifferentPolicies() throws Exception {
    ClientResource client = getClient(getRealm());
    AuthorizationResource authorization = client.authorization();
    ResourceRepresentation resource = new ResourceRepresentation("Resource B");
    authorization.resources().create(resource).close();
    ResourcePermissionRepresentation permission = new ResourcePermissionRepresentation();
    permission.setName(resource.getName() + " Permission");
    permission.addResource(resource.getName());
    permission.addPolicy(claimAPolicy.getName(), claimBPolicy.getName());
    authorization.permissions().resource().create(permission).close();
    PermissionRequest request = new PermissionRequest();
    request.setResourceId(resource.getName());
    String accessToken = new OAuthClient().realm("authz-test").clientId("test-client").doGrantAccessTokenRequest("secret", "marta", "password").getAccessToken();
    AuthzClient authzClient = getAuthzClient();
    String ticket = authzClient.protection().permission().forResource(request).getTicket();
    AuthorizationResponse response = authzClient.authorization(accessToken).authorize(new AuthorizationRequest(ticket));
    assertNotNull(response.getToken());
    AccessToken rpt = toAccessToken(response.getToken());
    Authorization authorizationClaim = rpt.getAuthorization();
    List<Permission> permissions = new ArrayList<>(authorizationClaim.getPermissions());
    assertEquals(1, permissions.size());
    Map<String, Set<String>> claims = permissions.get(0).getClaims();
    assertTrue(claims.containsKey("claim-a"));
    assertTrue(claims.containsKey("claim-b"));
}
Also used : PermissionRequest(org.keycloak.representations.idm.authorization.PermissionRequest) AuthorizationRequest(org.keycloak.representations.idm.authorization.AuthorizationRequest) HashSet(java.util.HashSet) Set(java.util.Set) OAuthClient(org.keycloak.testsuite.util.OAuthClient) ArrayList(java.util.ArrayList) 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) Authorization(org.keycloak.representations.AccessToken.Authorization) 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) Test(org.junit.Test)

Aggregations

Permission (org.keycloak.representations.idm.authorization.Permission)73 Test (org.junit.Test)50 AuthorizationResponse (org.keycloak.representations.idm.authorization.AuthorizationResponse)44 AccessToken (org.keycloak.representations.AccessToken)36 AuthorizationRequest (org.keycloak.representations.idm.authorization.AuthorizationRequest)29 ResourceRepresentation (org.keycloak.representations.idm.authorization.ResourceRepresentation)27 AuthorizationResource (org.keycloak.admin.client.resource.AuthorizationResource)23 AuthzClient (org.keycloak.authorization.client.AuthzClient)22 ClientResource (org.keycloak.admin.client.resource.ClientResource)20 ArrayList (java.util.ArrayList)19 ResourcePermissionRepresentation (org.keycloak.representations.idm.authorization.ResourcePermissionRepresentation)19 OAuthClient (org.keycloak.testsuite.util.OAuthClient)15 ScopePermissionRepresentation (org.keycloak.representations.idm.authorization.ScopePermissionRepresentation)14 JSPolicyRepresentation (org.keycloak.representations.idm.authorization.JSPolicyRepresentation)13 Response (javax.ws.rs.core.Response)12 AuthorizationDeniedException (org.keycloak.authorization.client.AuthorizationDeniedException)12 AccessTokenResponse (org.keycloak.representations.AccessTokenResponse)12 PermissionRequest (org.keycloak.representations.idm.authorization.PermissionRequest)12 PermissionResponse (org.keycloak.representations.idm.authorization.PermissionResponse)12 Authorization (org.keycloak.representations.AccessToken.Authorization)11