Package eu.europeana.apikey.keycloak
Class KeycloakManager
- java.lang.Object
-
- eu.europeana.apikey.keycloak.KeycloakManager
-
@Service public class KeycloakManager extends Object
Class for working with Keycloak and it's Rest Admin API.
Note that there are 2 client id's used by keycloak:- ClientId which is the same as an apikey (string)
- id which is an internal id (hash) of the client. This is called "id of client (not clientId)" in the keycloak * documentation and saved as keycloakId in an apikey object.
apiKey.getId().equals(keycloakClient.getClientId()); apiKey.getKeycloakId.equals(keycloakClient.getId());
-
-
Constructor Summary
Constructors Constructor Description KeycloakManager(KeycloakProperties kcProperties)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclean()ApiKeySecretcreateClient(KeycloakSecurityContext securityContext, ApiKeyRequest requestClient)Create a new client in Keycloak.voiddeleteClient(KeycloakSecurityContext securityContext, String apiKey)Deletes a client from KeycloakvoiddisableClient(String clientId, KeycloakSecurityContext securityContext)Disables the client in Keycloak, but only if it is enabledvoidenableClient(String clientId, KeycloakSecurityContext securityContext)Enables the client in Keycloak, but only if it was disabledvoidinit()booleanisManagerClientAuthorized(KeycloakAuthenticationToken keycloakAuthenticationToken)Checks whether the token was issued for a manager clientbooleanisOwner(String apiKey, KeycloakAuthenticationToken keycloakAuthenticationToken)Checks whether the client for which the token was issued is the owner of the apikeyStringrecreateClient(KeycloakSecurityContext securityContext, String apiKey, ApiKeyRequest requestClient)Used for creating a new keycloak client based on an already existing apikey (missing client synchronization)voidupdateClient(KeycloakSecurityContext securityContext, ApiKeyRequest apiKeyUpdate, String apiKey)Create a new client in Keycloak.
-
-
-
Constructor Detail
-
KeycloakManager
public KeycloakManager(KeycloakProperties kcProperties)
-
-
Method Detail
-
init
@PostConstruct public void init()
-
clean
@PreDestroy public void clean()
-
createClient
public ApiKeySecret createClient(KeycloakSecurityContext securityContext, ApiKeyRequest requestClient) throws ApiKeyException
Create a new client in Keycloak. An ApiKeyDetails object created by a user is used to gather all the client registration data. Keycloak security context will be used to authorize Keycloak requests with access token. When a client is successfully created in Keycloak the generated secret is retrieved from Keycloak and stored in ApiKey object that will be used to store the entry in apikey database.- Parameters:
securityContext- security context with access tokenrequestClient- object containing registration data from the original request- Returns:
- new ApiKey object with all necessary fields, including the Keycloak ID aka "privateKey"
- Throws:
ApiKeyException- when there is a failure
-
recreateClient
public String recreateClient(KeycloakSecurityContext securityContext, String apiKey, ApiKeyRequest requestClient) throws ApiKeyException
Used for creating a new keycloak client based on an already existing apikey (missing client synchronization)- Parameters:
securityContext- security context with access tokenapiKey- apikey of the client that needs to be recreatedrequestClient- object containing registration data from the original apikey- Returns:
- String containing the new keycloakId of the newly created client in Keycloak
- Throws:
ApiKeyException
-
updateClient
public void updateClient(KeycloakSecurityContext securityContext, ApiKeyRequest apiKeyUpdate, String apiKey) throws ApiKeyException
Create a new client in Keycloak. ApiKeyDetails object is used to populate all the needed client registration data. Keycloak security context will be used to authorize Keycloak requests with access token. When a client is successfully created in Keycloak the generated secret is retrieved from Keycloak and stored in ApiKey object that will be used to store the entry in apikey database.- Parameters:
securityContext- security context with access tokenapiKeyUpdate- containing updated registration data from the original requestapiKey- the id of the client that should be updated- Throws:
ApiKeyException- when there is a failure
-
deleteClient
public void deleteClient(KeycloakSecurityContext securityContext, String apiKey) throws ApiKeyException
Deletes a client from Keycloak- Parameters:
securityContext- security context with access tokenapiKey- the id of the client that is to be deleted- Throws:
ApiKeyException- when trouble strikes
-
isOwner
public boolean isOwner(String apiKey, KeycloakAuthenticationToken keycloakAuthenticationToken)
Checks whether the client for which the token was issued is the owner of the apikey- Parameters:
apiKey- api key to checkkeycloakAuthenticationToken- token issued for the caller of the request- Returns:
- true when authorized, false otherwise
-
isManagerClientAuthorized
public boolean isManagerClientAuthorized(KeycloakAuthenticationToken keycloakAuthenticationToken)
Checks whether the token was issued for a manager client- Parameters:
keycloakAuthenticationToken- token issued for the caller of the request- Returns:
- true when authorized, false otherwise
-
enableClient
public void enableClient(String clientId, KeycloakSecurityContext securityContext) throws ApiKeyException
Enables the client in Keycloak, but only if it was disabled- Parameters:
clientId- client identifiersecurityContext- security context with access token- Throws:
ApiKeyException- when client not found in Keycloak or update failed
-
disableClient
public void disableClient(String clientId, KeycloakSecurityContext securityContext) throws ApiKeyException
Disables the client in Keycloak, but only if it is enabled- Parameters:
clientId- client identifiersecurityContext- security context with access token- Throws:
ApiKeyException- when client not found in Keycloak or update failed
-
-