forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for initial CRUD operations when managing admin permissions
Signed-off-by: Pedro Igor <[email protected]>
- Loading branch information
Showing
13 changed files
with
354 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 0 additions & 30 deletions
30
...a/org/keycloak/representations/idm/authorization/AdminPermissionsAuthorizationSchema.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...private/src/main/java/org/keycloak/authorization/AdminPermissionsAuthorizationSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.keycloak.authorization; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
|
||
import org.keycloak.authorization.model.Resource; | ||
import org.keycloak.authorization.model.ResourceServer; | ||
import org.keycloak.authorization.store.StoreFactory; | ||
import org.keycloak.models.ClientModel; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.representations.idm.authorization.AuthorizationSchema; | ||
import org.keycloak.representations.idm.authorization.ResourceType; | ||
|
||
public class AdminPermissionsAuthorizationSchema extends AuthorizationSchema { | ||
|
||
public static final ResourceType USERS = new ResourceType("Users", new HashSet<>(Arrays.asList("manage"))); | ||
public static final AdminPermissionsAuthorizationSchema INSTANCE = new AdminPermissionsAuthorizationSchema(); | ||
|
||
private AdminPermissionsAuthorizationSchema() { | ||
super(USERS); | ||
} | ||
|
||
public Resource getOrCreateResource(KeycloakSession session, String type, String id) { | ||
RealmModel realm = session.getContext().getRealm(); | ||
|
||
if (!realm.isAdminPermissionsEnabled()) { | ||
return null; | ||
} | ||
|
||
ClientModel permissionClient = realm.getAdminPermissionsClient(); | ||
|
||
if (permissionClient == null) { | ||
throw new IllegalStateException("Permission client not found"); | ||
} | ||
|
||
StoreFactory storeFactory = getStoreFactory(session); | ||
ResourceServer resourceServer = storeFactory.getResourceServerStore().findByClient(permissionClient); | ||
String resourceName = null; | ||
|
||
if (USERS.getType().equals(type)) { | ||
resourceName = resolveUser(session, id, realm); | ||
} | ||
|
||
if (resourceName == null) { | ||
throw new IllegalStateException("Could not map resource object with type [" + type + "] and id [" + id + "]"); | ||
} | ||
|
||
return getOrCreateResource(session, resourceServer, resourceName); | ||
} | ||
|
||
private String resolveUser(KeycloakSession session, String id, RealmModel realm) { | ||
UserModel user = session.users().getUserById(realm, id); | ||
|
||
if (user == null) { | ||
user = session.users().getUserByUsername(realm, id); | ||
} | ||
|
||
return user == null ? null : user.getId(); | ||
} | ||
|
||
private Resource getOrCreateResource(KeycloakSession session, ResourceServer resourceServer, String id) { | ||
StoreFactory storeFactory = getStoreFactory(session); | ||
Resource resource = storeFactory.getResourceStore().findByName(resourceServer, id); | ||
|
||
if (resource == null) { | ||
return storeFactory.getResourceStore().create(resourceServer, id, resourceServer.getClientId()); | ||
} | ||
|
||
return resource; | ||
} | ||
|
||
private StoreFactory getStoreFactory(KeycloakSession session) { | ||
AuthorizationProvider authzProvider = session.getProvider(AuthorizationProvider.class); | ||
return authzProvider.getStoreFactory(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,6 @@ | |
String ref() default ""; | ||
|
||
String realmRef() default ""; | ||
|
||
boolean createClient() default true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.