# Signing in with Zitadel

> Connect the control plane to Zitadel step by step, with Zitadel roles for the control plane's roles and sign-out at Zitadel.

## What this guide sets up

At the end, people sign in to the control plane with their Zitadel account. Roles of a Zitadel project give them their role in the control plane, and signing out of the control plane signs them out of Zitadel 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 Zitadel 4.19. **Single sign-on** explains every control plane 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` |
| Zitadel's address | `https://192.168.0.63:9643` |
| The project | `onprem-ai` |
| The roles | `onprem-ai-admins`, `onprem-ai-operators`, `onprem-ai-viewers` |

## Before you start

- **The control plane is reached over HTTPS.** Open the control plane at its `https://` address.
- **Zitadel is reached over HTTPS, at one fixed address.** Zitadel names its issuer from its external domain, port and scheme (`ZITADEL_EXTERNALDOMAIN`, `ZITADEL_EXTERNALPORT`, `ZITADEL_EXTERNALSECURE=true`), for example `https://192.168.0.63:9643`. The control plane and every browser must reach Zitadel at exactly this address. A plain IP address works.
- **The clocks are correct.** The control plane 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**, for example `https://192.168.0.63:3443/api/auth/oidc/callback`. It is built from the address in your browser, so open the control plane at the address people will use.

## Step 1: The project and its roles

In the Zitadel console, in your organization:

1. **Projects → Create New Project**, name `onprem-ai`.
2. In the project, under **Roles**, add three roles. Their **Key** is what the control plane reads: `onprem-ai-admins`, `onprem-ai-operators`, `onprem-ai-viewers`. You need only the roles you use.
3. Give each person the role of their part: **Users** → the user → **Role Assignments → Create Role Assignment**, project `onprem-ai`, the role. A person with several roles gets each.

## Step 2: The application

In the project, **Applications → New**, type **Web**, then:

- **Authentication method**: **Code**, with client secret (**Basic**). Copy the **Client ID** and the **Client Secret** that Zitadel shows once.
- **Redirect URIs**: the redirect URI from the Configuration page, `https://192.168.0.63:3443/api/auth/oidc/callback`.
- **Post Logout URIs**: the control plane's address followed by `/signed-out`, `https://192.168.0.63:3443/signed-out`. Without it, Zitadel stops the sign-out on an error page and keeps the person signed in.
- When people open the control plane at more than one address, add both URIs for each one.

Then, in the application's **Token Settings** (**AuthToken Options**), turn on **Include user's profile info in the ID Token**. The control plane reads the username and the email address from the ID token.

## Step 3: The roles as a `groups` claim

Zitadel puts a person's roles into the claim `urn:zitadel:iam:org:project:roles`, as an object keyed by role. The control plane reads a list of names from a groups claim, so a short Zitadel action writes the role keys into a `groups` claim. In the console, **Actions**:

1. **Scripts → New**, name `groupsClaim`, with this script:

   ```js
   function groupsClaim(ctx, api) {
     if (ctx.v1.user.grants == undefined || ctx.v1.user.grants.count == 0) { return; }
     let groups = [];
     ctx.v1.user.grants.grants.forEach(grant => {
       grant.roles.forEach(role => { groups.push(role); });
     });
     api.v1.claims.setClaim('groups', groups);
   }
   ```

   The function's name must be the action's name.
2. **Flows**: flow type **Complement Token**, **Add trigger**, trigger type **Pre Userinfo creation**, action `groupsClaim`.

The roles of every project the person holds become groups this way; give the three roles names no other project uses.

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

Skip this step when Zitadel'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`.

## Step 5: Connect the control plane

1. Open **Configuration → Access → Sign-in**.
2. Enter:
   - **Issuer URL**: `https://192.168.0.63:9643`, without a `/` at the end, exactly as Zitadel states it.
   - **Client ID** and **Client 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:9643/ui/console/users/me`, Zitadel's page where people change their password. It shows as **Account** in their user menu.
3. Click **Test connection**. Zitadel offers no `groups` scope, so the test says that sign-in does not ask for it and warns that it cannot see whether the groups arrive: 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**.

The same settings can come from the deployment instead (`OIDC_*` in `/etc/onprem-ai/control-plane/config.env`, the client secret in `secrets/oidc_client_secret`): see **Single sign-on**.

## 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 Zitadel as a person with the role `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 person with each other role, and once as someone with none of the three: an operator, a viewer, and a viewer.
4. Click **Logout**. The browser passes Zitadel and comes back to a page that says you are signed out. Zitadel has ended the session: the next sign-in asks for the password again.

## Optional: Only some people may sign in

Enter a group as the **allowed group** (`OIDC_ALLOWED_GROUP`), for example a fourth role `onprem-ai-users`. Only people with it may sign in; everyone else comes back to the login page with a message telling them to ask their administrator. Give it to everyone who may sign in, administrators and operators included.

## When it does not work

| What you see | Why | What to do |
|---|---|---|
| **Test connection** says the identity provider states another issuer | The issuer URL ends with `/`, or Zitadel's external domain or port differ from the address you use | Enter the issuer exactly as the test names it |
| **Test connection** says the certificate is not trusted | The control plane does not trust the authority of Zitadel's certificate | Do step 4 |
| Everyone is a viewer, also people with the role `onprem-ai-admins` | The `groupsClaim` action does not run | Check the flow **Complement Token**, trigger **Pre Userinfo creation** (step 3) |
| A person signs in without an email address, or a first sign-in is refused because Zitadel "did not say who you are" | **Include user's profile info in the ID Token** is off | Turn it on (step 2) |
| After **Logout**, Zitadel shows an error page and the person stays signed in at Zitadel | `/signed-out` is not among the application's **Post Logout URIs** | Add it (step 2) |
| The login page says Zitadel refused the sign-in request | The client ID or the client secret is wrong, or the redirect URI is not registered | Copy both from the application again; add the redirect URI the Configuration page shows for this address |
