# Signing in with Keycloak

> Connect the control plane to Keycloak step by step, on the Configuration page or from the deployment, with groups for the roles, sign-out at Keycloak and a Keycloak certificate from your own certificate authority.

## What this guide sets up

At the end, people sign in to the control plane with their Keycloak account. Keycloak groups give them their role in the control plane, and signing out of the control plane signs them out of Keycloak too. Password sign-in stays on for the local administrator, so you keep a way back in.

The steps were tested in this order against Keycloak 26.4 and 26.8. **Single sign-on** explains every setting in detail; this guide is the order to do them in.

The examples use these values. Replace them with yours:

| Value | Example |
|---|---|
| The control plane's address, as people open it | `https://192.168.0.63:3443` |
| Keycloak's address | `https://192.168.0.63:8443` |
| The realm | `company` |
| The client ID | `onprem-ai` |
| The groups | `onprem-ai-admins`, `onprem-ai-operators`, `onprem-ai-viewers` |

## Before you start

- **The control plane is reached over HTTPS.** The installer sets this up; open the control plane at its `https://` address.
- **Keycloak is reached over HTTPS, at one fixed address.** Keycloak names its issuer from its hostname setting (`--hostname` or `KC_HOSTNAME`), for example `--hostname=https://192.168.0.63:8443`. The control plane and every browser must reach Keycloak at exactly this address. A plain IP address works; no DNS name is needed.
- **The clocks are correct.** The control plane checks the time in each sign-in answer, and allows 2 minutes of difference between the two machines.
- **You know the redirect URI.** Sign in to the control plane as an administrator, open **Configuration → Access → Sign-in**, and copy the **redirect URI**. It is built from the address in your browser's address bar, for example `https://192.168.0.63:3443/api/auth/oidc/callback`. Open the control plane at the address people will use before you copy it: `https://127.0.0.1:3443` gives a different redirect URI than `https://192.168.0.63:3443`.

## Step 1: The realm, the groups and the users in Keycloak

Sign in to the Keycloak admin console (`https://192.168.0.63:8443/admin`).

1. Create a realm for your people, or use an existing one. The example uses `company`.
2. **Groups → Create group**: create `onprem-ai-admins`, `onprem-ai-operators` and `onprem-ai-viewers`. You need only the groups for the roles you use.
3. Add each person to the group of their role: **Users** → the user → **Groups → Join group**. A person in several groups gets each role.

A user who signs in needs a username or an email address. A Keycloak user always has a username, so this needs nothing more.

## Step 2: The client in Keycloak

1. **Clients → Create client**.
2. **General settings**: **Client type** `OpenID Connect`, **Client ID** `onprem-ai`. Click **Next**.
3. **Capability config**: turn **Client authentication** on. Keep **Standard flow** on. You can turn **Direct access grants** off; the control plane does not use it. Click **Next**.
4. **Login settings**:
   - **Valid redirect URIs**: the redirect URI from the Configuration page, `https://192.168.0.63:3443/api/auth/oidc/callback`.
   - **Valid post logout redirect URIs**: the control plane's address followed by `/signed-out`, `https://192.168.0.63:3443/signed-out`. Without it, Keycloak shows "Invalid redirect uri" when someone signs out.
   - When people open the control plane at more than one address, add the redirect URI and the sign-out address of each one.
5. Click **Save**.
6. Open the client's **Credentials** tab and copy the **Client secret**.

## Step 3: The groups in the sign-in answer

The control plane reads the person's groups from the `groups` claim of the ID token. Keycloak sends it only after you add a mapper:

1. **Clients → onprem-ai → Client scopes**, open **onprem-ai-dedicated**.
2. **Configure a new mapper → Group Membership**:
   - **Name**: `groups`
   - **Token Claim Name**: `groups`
   - **Full group path**: off. With it on, Keycloak sends `/onprem-ai-admins` and the group names on the Configuration page do not match.
   - **Add to ID token**: on. The control plane reads the groups from the ID token.
   - Click **Save**.

The control plane asks for the `groups` scope only when Keycloak offers one, so a realm without a client scope named `groups` signs in too. When your realm has a client scope named `groups` (step 5's **Test connection** says that Keycloak offers it), make sure it is one of the client's **Default** client scopes, or add the mapper to that scope instead.

## Step 4: Trust Keycloak's certificate on the control plane

Skip this step when Keycloak's certificate comes from a public certificate authority.

When it comes from your own certificate authority, copy that authority's certificate into the folder `/etc/onprem-ai/trusted-certificate-authorities/` on the control plane's machine (**Trusting your own certificate authority**):

```sh
sudo install -d -m 0755 /etc/onprem-ai/trusted-certificate-authorities
sudo cp your-ca.pem /etc/onprem-ai/trusted-certificate-authorities/
sudo chmod 0644 /etc/onprem-ai/trusted-certificate-authorities/your-ca.pem
```

Nothing else is set. Then run `sudo onprem-ai-ctl restart`. Without it, **Test connection** in step 5 says that fetching the discovery document failed with a certificate error.

The browsers of the people who sign in must trust the same certificate authority too, because they open Keycloak's sign-in page themselves.

## Step 5: Connect the control plane

Do this on the Configuration page, or from the deployment (step 5b). Choose one: settings from the deployment replace the Configuration page's settings, and the page then shows them read-only.

### Step 5a: On the Configuration page

1. Open **Configuration → Access → Sign-in**.
2. Enter:
   - **Issuer URL**: `https://192.168.0.63:8443/realms/company`, with no slash at the end. It must be exactly what Keycloak states as its issuer.
   - **Client ID**: `onprem-ai`
   - **Client secret**: the secret from step 2.
   - **Name on the sign-in button**: for example `Company login`.
   - **Administrator group** `onprem-ai-admins`, **operator group** `onprem-ai-operators`, **viewer group** `onprem-ai-viewers`.
   - **Account management URL**: `https://192.168.0.63:8443/realms/company/account`. People then find **Account** in their user menu, where they change their password at Keycloak.
3. Click **Test connection**. It tests what you typed, before you save it. A good result names these checks: the discovery document was fetched, the issuer matches, the signing keys were fetched, whether sign-in asks for the `groups` scope, and that the client secret is checked at the first real sign-in. With Keycloak, the test also warns that it cannot see whether the groups arrive: the mapper from step 3 sends them, and step 6 checks it.
4. Under **Login methods**, tick **Single sign-on**, keep **Password** ticked, and click **Save single sign-on settings**. Switching single sign-on on runs the test again, and nothing is saved when it fails.

### Step 5b: From the deployment

On the control plane's machine, as root, in `/var/lib/onprem-ai/control-plane`:

1. Write the client secret into the file the installer created for it:

   ```sh
   cd /var/lib/onprem-ai/control-plane
   printf '%s' '<client-secret>' > secrets/oidc_client_secret.tmp
   chown 1000:1000 secrets/oidc_client_secret.tmp && chmod 0400 secrets/oidc_client_secret.tmp
   mv secrets/oidc_client_secret.tmp secrets/oidc_client_secret
   ```

2. Add the settings to `/etc/onprem-ai/control-plane/config.env`:

   ```
   LOGIN_METHODS=local,oidc
   OIDC_ISSUER_URL=https://192.168.0.63:8443/realms/company
   OIDC_CLIENT_ID=onprem-ai
   OIDC_CLIENT_SECRET_FILE=/run/secrets/oidc_client_secret
   OIDC_ADMINISTRATOR_GROUP=onprem-ai-admins
   OIDC_OPERATOR_GROUP=onprem-ai-operators
   OIDC_VIEWER_GROUP=onprem-ai-viewers
   OIDC_DISPLAY_NAME=Company login
   OIDC_ACCOUNT_URL=https://192.168.0.63:8443/realms/company/account
   ```

3. Apply them with `sudo onprem-ai-ctl apply`.
4. Check what is in effect with `sudo onprem-ai-ctl recovery show-sign-in`. It shows `Login methods: local, oidc (from the deployment settings)`, the issuer URL, the client ID and `Client secret: set`.

## Step 6: Check that it works

Open the control plane's login page in a private browser window, at the address you registered in step 2.

1. Click **Sign in with Company login**, and sign in at Keycloak as a member of `onprem-ai-admins`. You come back to the control plane, signed in.
2. Open **Configuration → Access → Users**. The person is listed with the role **administrator**.
3. Sign in once as a member of each other group. A member of `onprem-ai-operators` is an operator, a member of `onprem-ai-viewers` a viewer, and someone in none of the three groups a viewer.
4. Open the user menu: **Account** opens Keycloak's account page.
5. Click **Logout**. The browser goes to Keycloak and comes back to a page that says you are signed out. Click **Sign in with Company login** again: Keycloak asks for the password again, because its session ended too.

Every failed sign-in is in the audit log as `auth.login-failed`, with Keycloak's own reason in its details.

## Optional: Only some people may sign in

Enter a group as the **allowed group** (`OIDC_ALLOWED_GROUP`), for example `onprem-ai-viewers`. Only its members may sign in; everyone else comes back to the login page with a message telling them to ask their administrator. An allowed group lets in only its members, so put everyone who may sign in into it, administrators and operators included.

## Optional: Single sign-on only

Untick **Password** under **Login methods** (`LOGIN_METHODS=oidc`). The control plane's login page then sends every browser straight to Keycloak. This needs the administrator group. Before you switch, sign in once as a member of `onprem-ai-admins` and check that this account is an administrator. When nobody can sign in any more, use the recovery command on the machine (see **Single sign-on**, Getting back in).

## When it does not work

| What you see | Why | What to do |
|---|---|---|
| Keycloak shows "Invalid parameter: redirect_uri" | The control plane's address in the browser is not in **Valid redirect URIs** | Add the redirect URI that **Configuration → Access → Sign-in** shows for this address (step 2) |
| The login page says Keycloak refused the sign-in request; the audit log says `invalid_scope` | Keycloak offers a scope it does not let this client use | Add the scope the audit log names to the client's **Default** client scopes |
| The login page says Keycloak refused the sign-in request; the audit log says `unauthorized_client` or `invalid_client` | The client secret is wrong, or **Client authentication** is off | Copy the secret from the client's **Credentials** tab again |
| **Test connection** says fetching the discovery document failed with a certificate error | The control plane does not trust Keycloak's certificate | Do step 4 |
| **Test connection** says the identity provider states another issuer | The issuer URL differs from Keycloak's hostname setting, or ends with a `/` | Enter the issuer exactly as the test names it, or correct Keycloak's hostname setting |
| **Test connection** says fetching the discovery document answered HTTP 404, `Realm does not exist` | The realm name in the issuer URL is wrong | Enter `https://<keycloak>/realms/<realm>` with the realm's exact name |
| **Test connection** says `Hostname mismatch` | The issuer URL uses an address that Keycloak's certificate does not cover | Use the address in Keycloak's hostname setting |
| The login page says another account already has your username | A local account has the same username as the Keycloak user | Rename one of the two; accounts are never merged |
| Keycloak asks the person for an email address before it returns | The Keycloak user has no email address, and the realm's user profile requires one | Enter it at Keycloak, or let the person enter it there |
| The login page says the account is deactivated | An administrator deactivated the account under **Configuration → Access → Users** | **Reactivate** it there |
| Everyone is a viewer, also members of `onprem-ai-admins` | The groups do not reach the control plane, or arrive with a leading `/` | Check step 3: the mapper exists, **Full group path** off, **Add to ID token** on |
| Keycloak shows "Invalid redirect uri" after **Logout** | The sign-out address is not registered | Add `<control plane address>/signed-out` to **Valid post logout redirect URIs** (step 2) |
