Keycloak
Setting Up Keycloak
This guide provides step-by-step instructions to set up Keycloak for OpenCDMP. Keycloak is used for authentication and authorization within the OpenCDMP platform.
Prerequisites
- Keycloak installed and running.
- Administrative access to Keycloak Admin Console.
Step 1: Create a New Realm
-
Log In to the Keycloak Admin Console at
http://localhost:8080/auth(replacelocalhostwith your Keycloak server's address). -
Click on the Master realm dropdown on the left sidebar.
-
Click on Add Realm.
-
Name the new realm (e.g.,
OpenCDMP) and click Create.
Step 2: Create OpenID Connect Clients
Create the following five OpenID Connect clients:
apiannotationnotificationpluginswebappswagger
Client names and ids are configurable and are used as examples.
General Client Settings
For api, annotation, notification, plugins clients, set the following options:
- Client Authentication: On
- Direct Access Grants Enabled: On
- Service Accounts Enabled: On
- Standard Flow Enabled: Off
For swagger client, set the following options:
- Client Authentication: Off
- Direct Access Grants Enabled: On
- Service Accounts Enabled: Off
- Standard Flow Enabled: On
- Implicit Flow Enabled: On
For the webapp client, set:
- Client Authentication: Off
- Direct Access Grants Enabled: Off
- Service Accounts Enabled: Off
- Standard Flow Enabled: On
Client: api
-
Navigate to Clients in the left menu and click Create.
-
Client ID:
api -
Client Protocol: openid-connect
-
Root URL: (leave blank)
-
Click Save.
-
In the Settings tab, ensure the general client settings are applied as specified.
-
Click Save.
-
Go to the Roles tab and create the following roles:
- Admin
- User
- InstallationAdmin
Client: annotation
-
Create a new client with Client ID:
annotation. -
Apply the general client settings.
-
In the Roles tab, create:
- Admin
- User
Client: notification
-
Create a new client with Client ID:
notification. -
Apply the general client settings.
-
In the Roles tab, create:
- Admin
- User
Client: plugins
-
Create a new client with Client ID:
plugins. -
Apply the general client settings.
-
In the Roles tab, create:
- app-service
Client: webapp
-
Create a new client with Client ID:
webapp. -
Client Protocol: openid-connect
-
Root URL:
https://(APP_URL)/home -
Click Save.
-
In the Settings tab, set:
- Client Authentication: Off
- Direct Access Grants Enabled: Off
- Service Accounts Enabled: Off
- Standard Flow Enabled: On
- Root URL:
https://(APP_URL)/home - Home URL:
https://(APP_URL)/home - Valid Redirect URIs:
https://(APP_URL)/* - Web Origins:
https://(APP_URL) - Admin URL:
https://(APP_URL)/home
-
Click Save.
Client: swagger
-
Create a new client with Client ID:
swagger. -
Client Protocol: openid-connect
-
Root URL:
https://(APP_URL)/home -
Click Save.
-
In the Settings tab, set:
- Client Authentication: Off
- Direct Access Grants Enabled: On
- Service Accounts Enabled: Off
- Standard Flow Enabled: On
- Implicit Flow Enabled: On
- Valid Redirect URIs:
https://(APP_URL)/api/swagger-ui/oauth2-redirect.html - Web Origins:
/*
-
Click Save.
Step 3: Create Realm Roles
-
In the left menu, click on Realm Roles.
-
Click Add Role.
-
Create the following roles:
- Role Name:
Admin- Click Save.
- Role Name:
User- Click Save.
- Role Name:
Step 4: Create Client Scopes
Create the following six client scopes:
apiannotationnotificationpluginsidentity_providertenant_role
For each client scope:
-
Go to Client Scopes in the left menu.
-
Click Create.
-
Name: (one of the client scope names above)
-
Description: (optional)
-
Type:
- all client scopes except
tenant_role: None tenant_role: Optional
- all client scopes except
-
Protocol: openid-connect
-
Click Save.
-
For
identity_providerandtenant_roleclient scopes:- Include in Token Scope: On
Step 5: Configure Mappers for Client Scopes
Audience Mappers for api, annotation, notification, plugins
For each of the client scopes api, annotation, notification, and plugins:
-
Go to Client Scopes and select the client scope.
-
Navigate to the Mappers tab.
-
Click Create.
-
Name:
Audience -
Mapper Type: Audience
-
Included Client Audience: (select the client scope name, e.g.,
api) -
Add to access token: On
-
Add to token introspection: On
-
Click Save.
Assign Roles to Client Scopes
For the api, annotation, and notification client scopes:
-
Go to the Scope tab of each client scope.
-
In Available Roles, select the Realm Roles:
- Admin
- User
-
Click Add Selected to move them to Assigned Roles.
User Session Note Mapper for identity_provider
-
Go to Client Scopes and select
identity_provider. -
Navigate to the Mappers tab.
-
Click Create.
-
Name:
identity_provider -
Mapper Type: User Session Note
-
User Session Note:
identity_provider -
Token Claim Name:
identity_provider -
Claim JSON Type: String
-
Add to ID token: On
-
Add to Access Token: On
-
Add to userinfo: On
-
Add to access token response: On
-
Add to token introspection: On
-
Click Save.
User Attribute Mapper for tenant_role
-
Go to Client Scopes and select
tenant_role. -
Navigate to the Mappers tab.
-
Click Create.
-
Name:
tenant_role -
Mapper Type: User Attribute
-
User Attribute:
tenant_role -
Token Claim Name:
tenant_roles -
Claim JSON Type:
String -
Add to ID token: On
-
Add to Access Token: On
-
Add to userinfo: On
-
Add to token introspection: On
-
Multivalued: On
-
Aggregate Attribute Values: On
-
Click Save.
Step 6: Assign Client Scopes to Clients
Client: api
-
Go to Clients and select
api. -
Navigate to the Client Scopes tab.
-
In Optional Client Scopes, add:
plugins
Client: webapp
-
Go to Clients and select
webapp. -
Navigate to the Client Scopes tab.
-
In Default Client Scopes, add:
identity_providertenant_role
-
In Optional Client Scopes, add:
apiannotationnotification
Client: swagger
-
Go to Clients and select
swagger. -
Navigate to the Client Scopes tab.
-
In Default Client Scopes, add:
api
Step 7: Set Up Groups
Groups represent roles within the application. Assigning users to groups grants them specific access rights.
Create Root Group: opencdmp
-
Navigate to Groups in the left menu.
-
Click New.
-
Name:
opencdmp -
Click Save.
Create Subgroups
Under the opencdmp group, create the following subgroups:
role-adminrole-installation-adminrole-usertenant-config-managertenant-plan-managertenant-role-admintenant-role-user
Steps to Create a Subgroup
-
Select the
opencdmpgroup. -
Go to the Sub Groups tab.
-
Click New.
-
Name: (e.g.,
role-admin) -
Click Save.
Assign Roles to Groups
Group: role-admin
-
Select
role-adminunderopencdmp. -
Navigate to Role Mappings.
-
In Client Roles, select client
api:- Add Admin
-
Repeat for clients
annotationandnotification:- Add Admin
Group: role-installation-admin
-
Select
role-installation-adminunderopencdmp. -
Navigate to Role Mappings.
-
In Client Roles, select client
api:- Add InstallationAdmin
- Add User
-
For client
notification:- Add User
Group: role-user
-
Select
role-userunderopencdmp. -
Navigate to Role Mappings.
-
In Client Roles, select clients
api,annotation,notification:- Add User
Create Subgroups for Tenant Roles
For each tenant role group (tenant-config-manager, tenant-plan-manager, tenant-role-admin, tenant-role-user):
-
Select the tenant role group under
opencdmp. -
Go to the Sub Groups tab.
-
Click New.
-
Name:
tenant-default -
Click Save.
-
Select the
tenant-defaultsubgroup. -
Navigate to the Attributes tab.
-
Click Add Attribute.
-
Key:
tenant_role -
Value: (corresponding value)
- For
tenant-config-manager:TenantConfigManager:default - For
tenant-plan-manager:TenantPlanManager:default - For
tenant-role-admin:TenantAdmin:default - For
tenant-role-user:TenantUser:default
- For
-
Click Save.
Step 8: Set Default Groups for Users
All users should by default be members of:
/opencdmp/role-user/opencdmp/tenant-role-user/tenant-default
Assign Default Groups
-
Navigate to Realm Settings.
-
Go to the Default Groups tab.
-
Click Add Groups.
-
Select:
/opencdmp/role-user/opencdmp/tenant-role-user/tenant-default
-
Click Add Selected.
Step 9: Create Users
Create an Admin User
-
Navigate to Users.
-
Click Add User.
-
Username: (e.g.,
admin) -
Email Verified: On
-
Click Save.
-
Go to the Credentials tab.
- Set a password.
- Disable Temporary Password.
-
Go to the Groups tab.
- Click Join Groups.
- Add
/opencdmp/role-admin.
Create a Regular User
-
Repeat steps to create a new user.
-
Add user to
/opencdmp/role-userand/opencdmp/tenant-role-user/tenant-defaultgroups.
Step 10: Create keycloak-api User
This user allows the application to access Keycloak's REST API.
-
Navigate to Users.
-
Click Add User.
-
Username:
keycloak-api -
Email Verified: On
-
Click Save.
-
Go to the Credentials tab.
- Set a password.
- Disable Temporary Password.
-
Go to the Role Mappings tab.
- In Client Roles, select
realm-management. - Add realm-admin.
- In Client Roles, select
-
Go to the Groups tab.
-
Add to:
/opencdmp/role-user/opencdmp/tenant-role-user/tenant-default
-
Step 11: Configure Realm Settings
Enable Email Verification
-
Navigate to Realm Settings.
-
In the Login tab, enable:
- Email Verified: On
Configure Email Settings
-
In Realm Settings, go to the Email tab.
-
Set up SMTP settings for sending emails.
- Host
- Port
- From
- Encryption
- Authentication
-
Click Save.
Enable User Registration
-
In Realm Settings, go to the Login tab.
-
Enable User Registration: On
-
Under Default Roles, add:
/opencdmp/role-user/opencdmp/tenant-role-user/tenant-default
Step 12: Configure Identity Providers (Optional)
-
Navigate to Identity Providers in the left menu.
-
Click Add Provider.
-
Select the provider you wish to integrate (e.g., Google, Facebook).
-
Enter the required credentials and settings.
-
Click Save.
Conclusion
Your Keycloak setup for OpenCDMP is now complete. This configuration enables secure authentication and authorization across the various services and components of the platform.
- Important: Replace
(APP_URL)with your actual application URL throughout the configuration. - Note: Ensure that all settings are double-checked for accuracy.
References
- Keycloak Documentation: https://www.keycloak.org/documentation
- OpenID Connect Clients: Keycloak Clients
- Client Scopes: Keycloak Client Scopes