forked from dasniko/keycloak-extensions-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom admin realm resource provider
- Loading branch information
Showing
7 changed files
with
156 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="Remote Debugger REST Endpoints" type="Remote"> | ||
<module name="rest-endpoint" /> | ||
<option name="USE_SOCKET_TRANSPORT" value="true" /> | ||
<option name="SERVER_MODE" value="false" /> | ||
<option name="SHMEM_ADDRESS" /> | ||
<option name="HOST" value="localhost" /> | ||
<option name="PORT" value="8787" /> | ||
<option name="AUTO_RESTART" value="false" /> | ||
<RunnerSettings RunnerId="Debug"> | ||
<option name="DEBUG_PORT" value="8787" /> | ||
<option name="LOCAL" value="false" /> | ||
</RunnerSettings> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
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
57 changes: 57 additions & 0 deletions
57
rest-endpoint/src/main/java/dasniko/keycloak/resource/MyAdminRealmResourceProvider.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,57 @@ | ||
package dasniko.keycloak.resource; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import lombok.RequiredArgsConstructor; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.RealmModel; | ||
import org.keycloak.models.UserModel; | ||
import org.keycloak.services.resources.admin.AdminEventBuilder; | ||
import org.keycloak.services.resources.admin.ext.AdminRealmResourceProvider; | ||
import org.keycloak.services.resources.admin.permissions.AdminPermissionEvaluator; | ||
import org.keycloak.services.resources.admin.permissions.UserPermissionEvaluator; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
@RequiredArgsConstructor | ||
public class MyAdminRealmResourceProvider implements AdminRealmResourceProvider { | ||
|
||
private final KeycloakSession session; | ||
|
||
private RealmModel realm; | ||
private AdminPermissionEvaluator auth; | ||
|
||
@Override | ||
public Object getResource(KeycloakSession session, RealmModel realm, AdminPermissionEvaluator auth, AdminEventBuilder adminEvent) { | ||
this.realm = realm; | ||
this.auth = auth; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@GET | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Response getListOfUsers() { | ||
// do the authorization with the existing admin permissions (e.g. realm management roles) | ||
final UserPermissionEvaluator userPermissionEvaluator = auth.users(); | ||
userPermissionEvaluator.requireQuery(); | ||
|
||
// collect/manipulate data accordingly to your requirements | ||
List<Map<String, String>> userList = session.users() | ||
.searchForUserStream(realm, Map.of(UserModel.SEARCH, "*")) | ||
.filter(userModel -> userModel.getServiceAccountClientLink() == null) | ||
.map(userModel -> Map.of("username", userModel.getUsername())) | ||
.toList(); | ||
|
||
// then return the desired result | ||
return Response.ok(userList).build(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...endpoint/src/main/java/dasniko/keycloak/resource/MyAdminRealmResourceProviderFactory.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,44 @@ | ||
package dasniko.keycloak.resource; | ||
|
||
import com.google.auto.service.AutoService; | ||
import org.keycloak.Config; | ||
import org.keycloak.common.Profile; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.models.KeycloakSessionFactory; | ||
import org.keycloak.provider.EnvironmentDependentProviderFactory; | ||
import org.keycloak.services.resources.admin.ext.AdminRealmResourceProvider; | ||
import org.keycloak.services.resources.admin.ext.AdminRealmResourceProviderFactory; | ||
|
||
@AutoService(AdminRealmResourceProviderFactory.class) | ||
public class MyAdminRealmResourceProviderFactory implements AdminRealmResourceProviderFactory, EnvironmentDependentProviderFactory { | ||
|
||
public static final String PROVIDER_ID = "my-admin-rest-resource"; | ||
|
||
@Override | ||
public AdminRealmResourceProvider create(KeycloakSession session) { | ||
return new MyAdminRealmResourceProvider(session); | ||
} | ||
|
||
@Override | ||
public void init(Config.Scope config) { | ||
} | ||
|
||
@Override | ||
public void postInit(KeycloakSessionFactory factory) { | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return PROVIDER_ID; | ||
} | ||
|
||
@Override | ||
public boolean isSupported() { | ||
return Profile.isFeatureEnabled(Profile.Feature.ADMIN2); | ||
} | ||
|
||
} |
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
1 change: 0 additions & 1 deletion
1
...n/resources/META-INF/services/org.keycloak.services.resource.RealmResourceProviderFactory
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
rest-endpoint/src/test/java/dasniko/keycloak/resource/MyAdminRealmResourceProviderTest.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,33 @@ | ||
package dasniko.keycloak.resource; | ||
|
||
import dasniko.testcontainers.keycloak.KeycloakContainer; | ||
import org.junit.jupiter.api.Test; | ||
import org.keycloak.admin.client.Keycloak; | ||
import org.keycloak.representations.AccessTokenResponse; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@Testcontainers | ||
public class MyAdminRealmResourceProviderTest { | ||
|
||
@Container | ||
private static final KeycloakContainer keycloak = | ||
new KeycloakContainer().withProviderClassesFrom("target/classes"); | ||
|
||
@Test | ||
public void testEndpoint() { | ||
Keycloak keycloakClient = keycloak.getKeycloakAdminClient(); | ||
AccessTokenResponse accessTokenResponse = keycloakClient.tokenManager().getAccessToken(); | ||
|
||
given().baseUri(keycloak.getAuthServerUrl()) | ||
.basePath("/admin/realms/master/" + MyAdminRealmResourceProviderFactory.PROVIDER_ID) | ||
.auth().oauth2(accessTokenResponse.getToken()) | ||
.when().get() | ||
.then().statusCode(200) | ||
.body("size()", is(1)); | ||
} | ||
|
||
} |