Package org.trustdeck.service
Class OidcService
java.lang.Object
org.trustdeck.service.OidcService
- All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean
@Service
public class OidcService
extends Object
implements org.springframework.beans.factory.InitializingBean
This class provides services for managing clients, roles, and groups
within a Keycloak realm. It uses the Keycloak Admin Client to interact with the server and
offers various operations, such as creating roles, groups, and assigning roles to groups.
- Author:
- Eric Wündisch, Armin Müller
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected JwtPropertiesConfiguration properties for JWT (JSON Web Token) authentication.protected RoleConfigConfiguration for defining the operation-roles required by the application. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddRolesToUser(List<String> roleNames, String userId) Adds the specified roles to a user.booleanaddRoleToUser(String roleName, String userId) Adds a single role to a user.addUserToGroup(String groupId, String userId) Adds a user to a specified group in the Keycloak server.addUserToGroups(List<String> groupIds, String userId) Adds a user to multiple groups in the Keycloak server.voidInitializes the Keycloak client and sets up the necessary roles and groups.voidassignRoleToGroup(String groupId, org.keycloak.representations.idm.RoleRepresentation roleRepresentation) Assigns a specified client role to a group in the Keycloak server.org.keycloak.representations.idm.RoleRepresentationcreateClientRole(String roleName) Creates a new client role in the Keycloak server.org.keycloak.representations.idm.GroupRepresentationcreateParentGroup(String groupName) Creates a new parent group in the Keycloak server.org.keycloak.representations.idm.GroupRepresentationcreateSubGroup(String parentGroupId, String groupName) Creates a new subgroup under a specified parent group in the Keycloak server.protected org.keycloak.admin.client.resource.ClientResourceRetrieves the client resource from Keycloak based on the configured client ID.org.keycloak.representations.idm.RoleRepresentationgetClientRoleByName(String roleName) Retrieves the representation of a client role by its name.Retrieves a list of all client role names for the currently configured Keycloak client.Retrieves a map of all configured user storage providers (e.g.List<org.keycloak.representations.idm.GroupRepresentation> getGroupsByUserId(String userId) Retrieves a list of groups that the specified user belongs to.protected org.keycloak.admin.client.resource.RealmResourceRetrieves the Keycloak realm resource based on the configured realm name.org.keycloak.representations.idm.GroupRepresentationRetrieves the main group representation for ACE (domain handling) from the Keycloak server.org.keycloak.representations.idm.GroupRepresentationRetrieves the main group representation for KING (project handling) from the Keycloak server.List<org.keycloak.representations.idm.GroupRepresentation> Retrieves a list of all (top-level) groups within the configured Keycloak realm.List<org.keycloak.representations.idm.GroupRepresentation> This method first fetches all top-level groups and then recursively retrieves all their subgroups to build a complete group hierarchy.protected voidinit()Initializes the Keycloak client and sets up the necessary groups and roles.booleanremoveClientRole(String roleName) Deletes a specified client role from the Keycloak server.booleanremoveClientRole(org.keycloak.representations.idm.RoleRepresentation roleRepresentation) Deletes a specified client role from the Keycloak server.voidremoveGroupById(String groupId) Deletes a group from the Keycloak server using its unique identifier.booleanremoveRoleFromUser(String roleName, String userId) Removes a single role from a user.booleanremoveRolesFromUser(List<String> roleNames, String userId) Removes the specified roles from a user.removeUserFromGroup(String groupId, String userId) Removes a user from a specified group in the Keycloak server.removeUserFromGroups(List<String> groupIds, String userId) Removes a user from multiple groups in the Keycloak server.List<org.keycloak.representations.idm.UserRepresentation> searchUsers(String query) Searches for users based on a search term.
-
Field Details
-
jwtProperties
Configuration properties for JWT (JSON Web Token) authentication. -
roleConfig
Configuration for defining the operation-roles required by the application.
-
-
Constructor Details
-
OidcService
public OidcService()
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()Initializes the Keycloak client and sets up the necessary roles and groups. Called by the Spring framework after the OidcService-Bean's properties have been set.- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
getKeycloakRealm
protected org.keycloak.admin.client.resource.RealmResource getKeycloakRealm()Retrieves the Keycloak realm resource based on the configured realm name. The realm resource is the access point for managing all aspects of a Keycloak realm.- Returns:
RealmResourcethe Keycloak realm resource for the configured realm.
-
getClientResource
protected org.keycloak.admin.client.resource.ClientResource getClientResource() throws UnexpectedResultSizeExceptionRetrieves the client resource from Keycloak based on the configured client ID. If the client UUID is already known, it uses it directly; otherwise, it searches for the client by its ID.- Returns:
ClientResourcethe client resource for the specified client.- Throws:
UnexpectedResultSizeException- if the number of clients with the given ID is not exactly one.
-
init
protected void init()Initializes the Keycloak client and sets up the necessary groups and roles. This method ensures that the required roles and groups are present in the Keycloak realm. -
createClientRole
Creates a new client role in the Keycloak server. This method creates a role with the specified name and assigns it as a client role for the currently configured Keycloak client. If the role is successfully created, it retrieves and returns the created role representation.- Parameters:
roleName- the name of the role to be created- Returns:
RoleRepresentationthe representation of the newly created role, ornullif the role could not be created
-
getClientRoles
Retrieves a list of all client role names for the currently configured Keycloak client. This method interacts with the Keycloak Admin API to fetch all roles defined for the client. It returns a list of the names of these roles. Each role is represented using theRoleRepresentationclass, and only the role names are extracted and returned. -
getClientRoleByName
Retrieves the representation of a client role by its name.- Parameters:
roleName- the name of the role to be retrieved- Returns:
RoleRepresentationcontaining the details of the specified role, ornullif the role is not found or an error occurs.
-
removeClientRole
Deletes a specified client role from the Keycloak server.- Parameters:
roleName- the name of the role to be deleted- Returns:
trueif the role is successfully deleted, orfalseif an error occurs.
-
removeClientRole
public boolean removeClientRole(org.keycloak.representations.idm.RoleRepresentation roleRepresentation) Deletes a specified client role from the Keycloak server.- Parameters:
roleRepresentation- theRoleRepresentationobject representing the role to be deleted- Returns:
trueif the role is successfully deleted, orfalseif an error occurs.
-
getRealmGroups
Retrieves a list of all (top-level) groups within the configured Keycloak realm.- Returns:
ListofGroupRepresentationcontaining all groups in the current realm.
-
getRealmGroupsWithSubGroups
This method first fetches all top-level groups and then recursively retrieves all their subgroups to build a complete group hierarchy.- Returns:
- A list of all GroupRepresentation objects including top-level groups and all their nested subgroups
-
getMainGroupACE
public org.keycloak.representations.idm.GroupRepresentation getMainGroupACE()Retrieves the main group representation for ACE (domain handling) from the Keycloak server.- Returns:
GroupRepresentationof the main group if found, ornullif no such group exists.
-
getMainGroupKING
public org.keycloak.representations.idm.GroupRepresentation getMainGroupKING()Retrieves the main group representation for KING (project handling) from the Keycloak server.- Returns:
GroupRepresentationof the main group if found, ornullif no such group exists.
-
getGroupsByUserId
Retrieves a list of groups that the specified user belongs to.- Parameters:
userId- the unique identifier of the user for whom the groups are to be retrieved.- Returns:
ListofGroupRepresentationcontaining all groups the user is a member of, or an empty list if the user ID is invalid or the user has no groups.
-
createParentGroup
Creates a new parent group in the Keycloak server.- Parameters:
groupName- the name of the parent group- Returns:
GroupRepresentationof the newly created parent group, ornullif the creation fails
-
createSubGroup
public org.keycloak.representations.idm.GroupRepresentation createSubGroup(String parentGroupId, String groupName) Creates a new subgroup under a specified parent group in the Keycloak server.- Parameters:
parentGroupId- the unique identifier of the parent group under which the new subgroup will be createdgroupName- the name of the new subgroup to be created- Returns:
GroupRepresentationof the newly created subgroup, ornullif the creation fails
-
removeGroupById
Deletes a group from the Keycloak server using its unique identifier.- Parameters:
groupId- the unique identifier of the group to be deleted
-
assignRoleToGroup
public void assignRoleToGroup(String groupId, org.keycloak.representations.idm.RoleRepresentation roleRepresentation) Assigns a specified client role to a group in the Keycloak server.- Parameters:
groupId- the unique identifier of the group to which the role will be assignedroleRepresentation- theRoleRepresentationobject representing the role to be assigned
-
addUserToGroup
Adds a user to a specified group in the Keycloak server.- Parameters:
groupId- the unique identifier of the group that the user will joinuserId- the unique identifier of the user to be added to the group- Returns:
trueif the user is successfully added to the group, orfalseif an error occurs.
-
addUserToGroups
Adds a user to multiple groups in the Keycloak server.- Parameters:
groupIds- a list of group IDs representing the groups to which the user will be addeduserId- the unique identifier of the user to be added to the groups- Returns:
trueif the user is successfully added to all groups, orfalseif any operation fails.
-
removeUserFromGroup
Removes a user from a specified group in the Keycloak server.- Parameters:
groupId- the unique identifier of the group that the user will leaveuserId- the unique identifier of the user to be removed from to the group- Returns:
trueif the user is successfully removed from the group, orfalseif an error occurs.
-
removeUserFromGroups
Removes a user from multiple groups in the Keycloak server.- Parameters:
groupIds- a list of group IDs representing the groups from which the user will be removeduserId- the unique identifier of the user to be removed from the groups- Returns:
trueif the user is successfully removed from all groups, orfalseif any operation fails.
-
removeRolesFromUser
Removes the specified roles from a user.- Parameters:
roleNames- a list of roles that should be deleteduserId- the id of the user- Returns:
trueif removal was successful,falseotherwise
-
removeRoleFromUser
Removes a single role from a user.- Parameters:
roleName- the role that should be deleteduserId- the id of the user- Returns:
trueif removal was successful,falseotherwise
-
addRolesToUser
Adds the specified roles to a user.- Parameters:
roleNames- a list of roles that should be addeduserId- the id of the user- Returns:
trueif addition was successful,falseotherwise
-
addRoleToUser
Adds a single role to a user.- Parameters:
roleName- the role that should be addeduserId- the id of the user- Returns:
trueif addition was successful,falseotherwise
-
getFederationProviderMap
Retrieves a map of all configured user storage providers (e.g. LDAP, Kerberos, etc.) aka federation providers.- Returns:
- a map that associates the IDs of the federation providers with their names.
-
searchUsers
Searches for users based on a search term.- Parameters:
query- The search term used to find users.- Returns:
- a list of the found users.
-