> ## Documentation Index
> Fetch the complete documentation index at: https://docs.canton.network/llms.txt
> Use this file to discover all available pages before exploring further.

# Keycloak in the QuickStart

> Realms, users, and clients in the QuickStart's bundled Keycloak, and how to customize them on LocalNet.

Keycloak is an open-source Identity and Access Management (IAM) solution that provides authentication, authorization, and user management for modern applications and services.
It acts as a centralized authentication server that handles user logins, session management, and security token issuance.

The CN QS uses Keycloak to provide secure authentication across its distributed architecture.
Keycloak maintains separation between authentication concerns and business logic.

<Note>
  Keycloak runs only when `AUTH_MODE=oauth2` (`make setup`, **Enable OAUTH2**).
</Note>

## Realm structure

The CN QS defines two Keycloak realms.
The AppProvider realm manages authentication for services and users on the provider side of the application.
The AppUser realm handles authentication for the consumer side.
When components like validators or participant nodes receive requests, they validate the authentication tokens against the appropriate realm.

## Keycloak configuration

User and client settings live in `docker/modules/keycloak/env/app-provider/on/` and `docker/modules/keycloak/env/app-user/on/` (`oauth2.env`, `wallet.oauth2.env`, `ans.oauth2.env`).
The backend service and PQS keep their own in `docker/backend-service/onboarding/env/oauth2.env` and `docker/modules/pqs/onboarding/*/on/oauth2.env`.

The default configuration includes predefined users in each realm:

AppProvider realm:

* **User "app-provider"** (AUTH\_APP\_PROVIDER\_WALLET\_ADMIN\_USER\_NAME=app-provider)

* **UUID**: 553c6754-8879-41c9-ae80-b302f5af92c9 (AUTH\_APP\_PROVIDER\_WALLET\_ADMIN\_USER\_ID)

* **Password**: abc123

AppUser realm:

* **User "app-user"** (AUTH\_APP\_USER\_WALLET\_ADMIN\_USER\_NAME=app-user)

* **UUID**: 92a520cb-2f09-4e55-b465-d178c6cfe5e4 (AUTH\_APP\_USER\_WALLET\_ADMIN\_USER\_ID)

* **Password**: abc123 (AUTH\_APP\_USER\_WALLET\_ADMIN\_USER\_PASSWORD)

## Customizing Keycloak for business needs

You can customize the Keycloak configuration to meet your specific
business requirements.

### Accessing the admin console

The Keycloak admin console is available at:

[http://keycloak.localhost:8082/admin/master/console/#/master](http://keycloak.localhost:8082/admin/master/console/#/master)

To log in use the default credentials:

* **Username**: `admin`

* **Password**: `admin`

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/41-keycloack-login.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=eb465fd6ceb72b10d130e3f4faa93f51" alt="Keycloak sign in" width="568" height="511" data-path="images/quickstart-keycloak/41-keycloack-login.png" />

## Customization scenarios

### Add a new user

1. Log in to the Keycloak Admin console

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/42-keycloak-new-user.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=a0aaa6e639d81e9226a58e3741946165" style={{width: "30.0%"}} alt="Keycloak admin console" width="1034" height="1054" data-path="images/quickstart-keycloak/42-keycloak-new-user.png" />

2. Select the appropriate realm (AppProvider or AppUser)
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/43-keycloak-realm.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=570ea4c65bb484de66071123ffb7a884" alt="Select Keycloak realm" width="574" height="754" data-path="images/quickstart-keycloak/43-keycloak-realm.png" />

3. Navigate to the “Users” -> “Add user”
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/44-keycloak-add-user.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=b85baef0541ddccbb00c9e28ba7e4579" alt="Keycloak users view" width="980" height="448" data-path="images/quickstart-keycloak/44-keycloak-add-user.png" />

Click **Add user**

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/45-keycloak-user-bob.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=feb8d2f5ff4e28011625f9102bc813cd" alt="add user" width="586" height="128" data-path="images/quickstart-keycloak/45-keycloak-user-bob.png" />

4. Fill in the user details and click **Create**
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/46-keycloak-bob-details.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=d4c828ce8b2ea0a1e41bb123774bfe11" alt="user details" width="383" height="497" data-path="images/quickstart-keycloak/46-keycloak-bob-details.png" />

5. Go to the **Credentials** tab to set a password
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/47-keycloak-credentials.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=5379b4c2aa47dc3057a92fffe687f46b" alt="user password credentials" width="791" height="348" data-path="images/quickstart-keycloak/47-keycloak-credentials.png" />

Set the password

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/48-keycloak-set-pw.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=67e0bd2d3b88351bdf077199e4e6d40e" alt="set password" width="562" height="318" data-path="images/quickstart-keycloak/48-keycloak-set-pw.png" />

6. Save the password
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/49-keycloak-save-pw.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=7f6603fe49ea641d8cfbdd800f2b11a5" alt="save password" width="562" height="181" data-path="images/quickstart-keycloak/49-keycloak-save-pw.png" />

7. You can now sign in using the new user and their password.

   a. Return to the login page and click **AppUser**

   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/50-keycloak-oauth-login.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=92598365ad4e74f8f48fda28c5f7fdf3" alt="login screen" width="397" height="315" data-path="images/quickstart-keycloak/50-keycloak-oauth-login.png" />

Log in as Bob

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/51-keycloak-bob-signin.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=53283597b93cb4ecc044ad6421e75d74" alt="sign in screen" width="548" height="435" data-path="images/quickstart-keycloak/51-keycloak-bob-signin.png" />

8. Bob is now a user
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/52-user-bob.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=8685f33f409e993ed887e9c6ea180e08" alt="logged in as Bob" width="979" height="190" data-path="images/quickstart-keycloak/52-user-bob.png" />

### Modify client settings

1. Return to the realm selection view

2. Navigate to **Clients** -> select the client to modify
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/53-keycloak-client-settings.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=f6212116ab2ee1679d3f21b5acce76fe" alt="modify client settings" width="993" height="677" data-path="images/quickstart-keycloak/53-keycloak-client-settings.png" />

3. Update settings per your needs
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/54-keycloak-update-settings.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=47d8bbc1eaf63b29c7bbda8542699956" alt="update user settings" width="945" height="781" data-path="images/quickstart-keycloak/54-keycloak-update-settings.png" />

4. Save changes

### Add a new client

1. Select the appropriate realm

2. Navigate to “Clients” -> “Create”
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/55-keycloak-add-client.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=ef80afd9738fa4adf9007eb98d5d2c60" alt="client list" width="436" height="107" data-path="images/quickstart-keycloak/55-keycloak-add-client.png" />

3. Configure the client's general settings. Click **Next** for additional configuration options
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/56-keycloak-config-client.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=9c4d154ded4e07df44f7943a08748d99" alt="client details" width="660" height="760" data-path="images/quickstart-keycloak/56-keycloak-config-client.png" />

4. Configure additional settings
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/57-config-adt-settings.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=308fda709b29d23ec6f39621a1145380" alt="additional settings" width="868" height="770" data-path="images/quickstart-keycloak/57-config-adt-settings.png" />

Click **Next** for additional configuration options

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/58-cofig-settings-2.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=9d2f906d0af3a2207c0005ff344bf78a" alt="additional settings 2" width="768" height="765" data-path="images/quickstart-keycloak/58-cofig-settings-2.png" />

5. Save the client

### Update environment variables

After changing the Keycloak configuration, update the matching variables in the env files listed under [Keycloak configuration](#keycloak-configuration):

1. The Keycloak user must have the same ID as the ledger user's ID. This is not the party id.

2. For client changes, update the corresponding client ID and secret

3. For user changes, update the corresponding user ID and credentials

4. Restart the services to apply the changes:

   ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
   make stop && make start
   ```

## Troubleshooting

**Login failures**:

1. Verify Keycloak is running: `make status`
   <img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/59-verify-keycloak-running.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=682c2a6277faffd58b32536a67e0409f" alt="verify keycloak running" width="2048" height="576" data-path="images/quickstart-keycloak/59-verify-keycloak-running.png" />

Look for **keycloak** and **nginx-keycloak** in the list.

**Keycloak** should show as “healthy”

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/60-keycloak-healthy.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=7893dbd1db5a66893fbf33ea0f2b20ec" alt="keycloak healthy" width="1860" height="58" data-path="images/quickstart-keycloak/60-keycloak-healthy.png" />

2. Check the Keycloak issuer URLs in `docker/modules/keycloak/compose.env`

   ```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
   AUTH_APP_PROVIDER_ISSUER_URL=http://keycloak.localhost:8082/realms/AppProvider          # for backend oidc client conf, wallet-ui
   AUTH_APP_PROVIDER_ISSUER_URL_INTERNAL=http://nginx-keycloak:8082/realms/AppProvider     # for backends

   AUTH_APP_USER_ISSUER_URL=http://keycloak.localhost:8082/realms/AppUser                  # for backend, wallet-ui
   AUTH_APP_USER_ISSUER_URL_INTERNAL=http://nginx-keycloak:8082/realms/AppUser             # for backend
   ```

   `keycloak.localhost` resolves from the browser and inside the Docker network; `nginx-keycloak` is the container name.

3. Check that the Keycloak user ID matches the ledger user ID

**App User**

Compare the **ID** value in Keycloak’s User Details with the `AUTH_APP_USER_WALLET_ADMIN_USER_ID` value in `docker/modules/keycloak/env/app-user/on/oauth2.env`.

<Note>
  In this example, `AUTH_APP_USER_WALLET_ADMIN_USER_ID=92a520cb-2f09-4e55-b465-d178c6cfe5e4`
</Note>

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/61-keycloak-alice.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=e369d1479d00b5225ed4163a7ba78f0a" alt="Keycloak user details" width="992" height="370" data-path="images/quickstart-keycloak/61-keycloak-alice.png" />

**App Provider**

Compare the **ID** value in Keycloak’s User Details with the `AUTH_APP_PROVIDER_WALLET_ADMIN_USER_ID` value in `docker/modules/keycloak/env/app-provider/on/oauth2.env`.

<Note>
  In this example, `AUTH_APP_PROVIDER_WALLET_ADMIN_USER_ID=553c6754-8879-41c9-ae80-b302f5af92c9`
</Note>

<img src="https://mintcdn.com/cantonfoundation/bYnfFRxCT3EjbSHM/images/quickstart-keycloak/61-keycloak-participant.png?fit=max&auto=format&n=bYnfFRxCT3EjbSHM&q=85&s=0473d0f081c0f00de02bb672dedc2df0" alt="Keycloak user details" width="2048" height="673" data-path="images/quickstart-keycloak/61-keycloak-participant.png" />

Learn more about using Keycloak through their documentation portal:

[Keycloak official documentation](https://www.keycloak.org/documentation)

[Keycloak server administration guide](https://www.keycloak.org/docs/latest/server_admin/)

[Securing applications with Keycloak](https://www.keycloak.org/guides.html#securing-apps)
