Package org.simexid.keycloak.service
Class KeycloakUtil
java.lang.Object
org.simexid.keycloak.service.KeycloakUtil
Utility class for interacting with Keycloak.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
addRoleToUser
(KeycloakEnum.SearchUserType searchUserType, String searchText, String role, KeycloakEnum.Type type, String clientUUID) Adds a role to a user in Keycloak.boolean
Adds attributes to a user in Keycloak.boolean
Authorizes the client with Keycloak.boolean
callForAddAttributes
(String sub, String payload) Calls Keycloak to add/remove (rewrite) attributes to a user.boolean
callForAddRole
(String sub, String payload, KeycloakEnum.Type type, String clientUUID) Calls Keycloak to add/remove (rewrite) a role to a user.boolean
deleteRoleToUser
(KeycloakEnum.SearchUserType searchUserType, String searchText, String role, KeycloakEnum.Type type, String clientUUID) Deletes a role from a user in Keycloak.boolean
deleteUserAttributes
(String sub, List<String> attributes) Deletes attributes from a user in Keycloak.getToken()
Get the current keycloak token and expirationgetUserInfo
(String sub) Retrieves the user information from Keycloak.retrieveUserRole
(String sub, KeycloakEnum.Type type, String clientUIID) Retrieves the roles of a user in Keycloak.searchRoles
(String name, KeycloakEnum.Type type, String clientUUID) Searches for roles in Keycloak.searchUser
(KeycloakEnum.SearchUserType searchUserType, String searchText) Searches for a user in Keycloak.boolean
updateUser
(String sub, SSOUser user) Updates a user in Keycloak.
-
Constructor Details
-
KeycloakUtil
public KeycloakUtil()
-
-
Method Details
-
authorized
Authorizes the client with Keycloak. The token is stored in memory until it expires.- Returns:
- true if authorization is successful, false otherwise
- Throws:
AuthorizationException
- if an error occurs during authorization
-
getUserInfo
Retrieves the user information from Keycloak.- Parameters:
sub
- the user ID- Returns:
- the user information
- Throws:
GenericException
- if an error occurs during the operationAuthorizationException
- if the client is not authorized
-
updateUser
Updates a user in Keycloak.- Parameters:
sub
- the user IDuser
- the user sso representation- Returns:
- true if the user was updated successfully, false otherwise
- Throws:
AuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operation
-
addUserAttributes
public boolean addUserAttributes(String sub, List<HashMap<String, List<String>>> attributes) throws AuthorizationException, GenericException, AttributesExceptionAdds attributes to a user in Keycloak.- Parameters:
sub
- the user IDattributes
- the attributes to add- Returns:
- true if the attributes were added successfully, false otherwise
- Throws:
AuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operationAttributesException
- if an error occurs during assigment of attributes
-
deleteUserAttributes
public boolean deleteUserAttributes(String sub, List<String> attributes) throws AuthorizationException, GenericException, AttributesException Deletes attributes from a user in Keycloak.- Parameters:
sub
- the user IDattributes
- the attributes to delete- Returns:
- true if the attributes were deleted successfully, false otherwise
- Throws:
AuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operationAttributesException
- if an error occurs during assigment of attributes
-
callForAddAttributes
public boolean callForAddAttributes(String sub, String payload) throws AuthorizationException, AttributesException Calls Keycloak to add/remove (rewrite) attributes to a user.- Parameters:
sub
- the user IDpayload
- the payload containing the attributes- Returns:
- true if the attributes were added successfully, false otherwise
- Throws:
AuthorizationException
- if the client is not authorizedAttributesException
- if an error occurs during assigment of attributes
-
addRoleToUser
public boolean addRoleToUser(KeycloakEnum.SearchUserType searchUserType, String searchText, String role, KeycloakEnum.Type type, String clientUUID) throws MultipleUsersFoundException, MultipleRolesFoundException, AuthorizationException, GenericException Adds a role to a user in Keycloak.- Parameters:
searchUserType
- the type of searchsearchText
- the search textrole
- the role to addtype
- the type of role (REALM or CLIENT)clientUUID
- the client UUID- Returns:
- true if the role was added successfully, false otherwise
- Throws:
MultipleUsersFoundException
- if multiple users are foundMultipleRolesFoundException
- if multiple roles are foundAuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operation
-
deleteRoleToUser
public boolean deleteRoleToUser(KeycloakEnum.SearchUserType searchUserType, String searchText, String role, KeycloakEnum.Type type, String clientUUID) throws MultipleUsersFoundException, MultipleRolesFoundException, AuthorizationException, GenericException Deletes a role from a user in Keycloak.- Parameters:
searchUserType
- the type of searchsearchText
- the search textrole
- the role to deletetype
- the type of role (REALM or CLIENT)clientUUID
- the client UUID- Returns:
- true if the role was deleted successfully, false otherwise
- Throws:
MultipleUsersFoundException
- if multiple users are foundMultipleRolesFoundException
- if multiple roles are foundAuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operation
-
callForAddRole
public boolean callForAddRole(String sub, String payload, KeycloakEnum.Type type, String clientUUID) throws AuthorizationException, GenericException Calls Keycloak to add/remove (rewrite) a role to a user.- Parameters:
sub
- the user IDpayload
- the payload containing the roletype
- the type of role (REALM or CLIENT)clientUUID
- the client UUID- Returns:
- true if the role was added successfully, false otherwise
- Throws:
AuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operation
-
searchUser
public List<SSOUser> searchUser(KeycloakEnum.SearchUserType searchUserType, String searchText) throws AuthorizationException, GenericException Searches for a user in Keycloak.- Parameters:
searchUserType
- the type of searchsearchText
- the search text- Returns:
- the user information
- Throws:
AuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operation
-
searchRoles
public List<SSORoles> searchRoles(String name, KeycloakEnum.Type type, String clientUUID) throws AuthorizationException, GenericException Searches for roles in Keycloak.- Parameters:
name
- the role nametype
- the type of role (REALM or CLIENT)clientUUID
- the client UUID- Returns:
- the role information
- Throws:
AuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operation
-
retrieveUserRole
public List<SSORoles> retrieveUserRole(String sub, KeycloakEnum.Type type, String clientUIID) throws AuthorizationException, GenericException Retrieves the roles of a user in Keycloak.- Parameters:
sub
- the user IDtype
- the type of role (REALM or CLIENT)clientUIID
- the client UUID- Returns:
- the list of roles
- Throws:
AuthorizationException
- if the client is not authorizedGenericException
- if an error occurs during the operation
-
getToken
Get the current keycloak token and expiration- Returns:
- an HashMap with the token string as "token" and the expiration in long format as "expiration" if authorized, else a null object
- Throws:
AuthorizationException
- if the client is not authorized
-