Keycloak is a versatile open-source Identity and Access Management solution, designed for modern applications and services. It is self-hosted, offering extensive customization to meet various authentication requirements.
PALM supports configuring user roles for a session by inheriting them from Keycloak. To achieve this, a users Keycloak profile must include a valid role. PALM will use the INHERITED_OAUTH_ROLE_PATH environment variable to search for this role within the OAuth Profile object.
Note: The role inherited from Keycloak does not persist in our database and only exists as part of the user's active session.
Here are the minimum steps required to integrate Keycloak roles with PALM:
- Select the relevant realm
- Navigate to Users in the left-hand menu.
- Click the Add user button in the upper right-hand corner.
- Enter user details and click Save.
- Within the user's page, select the Attributes tab.
- Add an Attribute with a key of
roleand a value ofAdmin- PALM defaults an absent or invalid role to
User; valid roles areUserandAdmin.
- PALM defaults an absent or invalid role to
- Click the Add button on the right-hand side and click Save.
- Navigate to Client Scopes in the left-hand navigation menu.
- Click the Create button in the upper right-hand corner.
- Enter
roleas the Name and add a unique Description. - Click Save.
- Within the page of your newly created client scope, select the Mappers tab and click Create in the upper right-hand corner.
- Enter
rolefor name and selectUser Attributeas the Mapper Type. - Enter
rolefor the User Attribute and Token Claim Name fields. - Select
Stringas the Claim JSON Type and click Save.
- Navigate to Clients using the left-hand menu.
- Select the client created for PALM.
- Within the client page, select the Client Scopes tab
- Under Default Client Scopes, select the newly created
rolescope and click the Add selected button.
- In your local
.env.localfile, setINHERITED_OAUTH_ROLE_PATH=role - Recompose your frontend container by running
docker compose up -d frontend
Now, when creating a user, you can assign them a role of either User or Admin. PALM will default an absent or invalid role to the role set for the User in the DB (which defaults to User), so you only need to explicitly set this field for users who need to be Admin.
Running docker compose up -d spins up a Keycloak container accessible at http://localhost:8080. This section guides you through the minimal setup necessary to integrate Keycloak with PALM.
-
Navigate to http://localhost:8080. Enter the Administration Console.
-
Sign in using the administrator account: Username=
admin& Password=admin -
At the top-left corner, hover over Master and click 'Add realm'.
- Provide a name, ensure 'Enabled' is
ON, then click 'Create'. - Set
KEYCLOAK_ISSUERin your.env.localfile tohttp://keycloak:8080/realms/<realm-name>, replacing<realm-name>with your new realm's name.
- Provide a name, ensure 'Enabled' is
-
Notice your realm name is now in the top-left corner. Click on 'Clients' in the left-hand navigation menu under Configure.
- On the right hand side, click 'Create'.
- Enter a Client ID. This is the value set for the
KEYCLOAK_IDin yourenv.local. Click 'Save'. - For 'Access Type', select
confidential. Ensure 'Standard Flow Enabled' and 'Direct Access Grants Enabled' areON. - Fill in
http://localhost:3000/*for 'Valid Redirect URIs' andhttp://localhost:3000for 'Web Origins', then save.
-
The 'Credentials' tab should now appear up top. Select it and copy the 'Secret' to your
env.localas theKEYCLOAK_SECRET. -
Click on 'Users' in the left-hand navigation menu under Manage.
- Click 'Add user'.
- Fill in a 'Username' and click 'Save'.
- Select the 'Credentials' tab.
- Fill in a password for the user, toggle 'Temporary' to
OFF, then click 'Set Password'. Confirm 'Set password' to the popup modal.
-
Verify in your
env.localthatENABLED_NEXTAUTH_PROVIDERSincludeskeycloakandKEYCLOAK_ID,KEYCLOAK_SECRET, andKEYCLOAK_ISSUERare correctly set. -
Reactivate the frontend container with:
docker compose up -d frontend
-
To ensure that the browser correctly resolves the address
http://keycloak:8080ashttp://localhost:8080, add a line to yourhostsfile:127.0.0.1 keycloak
Warning: Directly editing your
hostsfile should be done with caution to avoid disrupting existing configurations. Always create a backup before making changes.-
macOS - file is located at
/etc/hosts- Note: Modifying
/etc/hostsusually requires superuser permissions. Therefore, you might need to prepend sudo to your command when editing this file, such as usingsudo nano /etc/hostsor another text editor of your choice. - Create a backup: by executing:
sudo cp /etc/hosts /etc/hosts.backup
- Note: Modifying
-
Windows - file is located at
C:\Windows\System32\drivers\etc\hosts. To edit:- Run Notepad as an administrator.
- Open the file listed above. It does not have an extension type, there may be a
hosts.icsfile which is the wrong one. - Add the line to the file and save.
This modification allows your system to map the
keycloakhostname to your local machine, facilitating communication between your browser and the containerized Keycloak server. -
Now, visit http://localhost:3000 and select the "Sign in with Keycloak" option. You should now be able to log in using the credentials established earlier.
- Key aspects on the configurations needed to use Keycloak in production can be found in this guide: https://www.keycloak.org/server/configuration-production.
- A list of guides provided by Keycloak can be found here: https://www.keycloak.org/guides
- Finally, Keycloak's documentation can be found here https://www.keycloak.org/documentation.