# CrushMFT as an OpenID Connect provider (App Gateway)

Apps that offer "Sign in with OpenID Connect" (Grafana, GitLab, Jenkins, Nextcloud, most wikis) can sign their people in with CrushMFT. The person signs in at CrushMFT (password, two-factor, single sign-on, as their account says), and the app gets a token that names them. Only people whose roles give the app get one ([who may use an app](Security-App-Gateway.md#who-may-use-an-app)).

It is part of the [App Gateway](Security-App-Gateway.md) (Enterprise). An app can be reached through the gateway and sign in with OpenID Connect, or only sign in here (an app without an entrance).

## Turning it on

1. Set the **sign-in address** (Preferences, App Gateway, Sign-in & portal), such as `https://files.example.com`. It is the issuer.
2. On the **Identity** tab, tick **Apps may sign their users in with CrushMFT** and Save.
3. Add the app (Apps, Add app). Without an entrance, untick **Reached through CrushMFT** and give **The app's address** for the portal's tile.
4. In its **OpenID Connect** part: tick it on, give the **Redirect addresses** the app tells you, pick the scopes, Save app.
5. **New client secret**, copy the secret (it is shown once), and give the app the client id (the app's id) and the secret.
6. Give people the app (User Manager, Apps).

The CrushMFT 27 issuers for clouds and workloads (Preferences, OIDC Server) are separate and unchanged: they answer under their own path.

## The addresses

| Address | What it is |
|---|---|
| `https://files.example.com/.well-known/openid-configuration` | discovery: everything below, for apps that read it |
| `/oidc/authorize` | the sign-in |
| `/oidc/token` | the code for tokens |
| `/oidc/userinfo` | the person's claims |
| `/oidc/jwks` | the keys the ID token is signed with (ES256) |
| `/oidc/logout` | sign-out started by the app |

## What the provider does

- **The authorization code flow only**, with the answer in the query. No implicit flow, no password grant, no refresh tokens: an app that needs a new token sends the browser to sign in again, which goes through without a question while the CrushMFT session lives.
- **PKCE** with S256: required of a **public client** (an app in a browser or on a phone, which has no secret), and checked whenever an app sends it. `plain` is refused.
- **Client authentication** at the token endpoint: the secret in a Basic header (`client_secret_basic`) or in the form (`client_secret_post`); or a JWT the app signed with its own key (`private_key_jwt`: RS256 or ES256, with the app's public keys in **Its public keys**; `iss` and `sub` the client id, `aud` the token address, at most 5 minutes of life, each `jti` taken once); none for a public client.
- **Signing in again** when the app asks: `prompt=login`, or a `max_age` older than the person's CrushMFT sign-in, ends the CrushMFT session and asks for the password; then the app gets its code. `max_age=0` asks once, it does not loop. With `prompt=none` such a request gets `login_required` instead.
- **Redirect addresses** must match exactly. https, or http only for `localhost` and its names. A sign-in with an address the app did not register gets an error page and never a redirect.
- **A code** works once, for 60 seconds, for the app, address and PKCE challenge it was made for, and only while the CrushMFT session lives. The token endpoint checks the person's roles and the app's Access conditions again.
- **Tokens** live 60 minutes unless the app's **Tokens live** says otherwise. The access token is good at userinfo while it and the CrushMFT session live.

## Claims

| Claim | Holds | Scope |
|---|---|---|
| `sub` | the user's id, which never changes | always |
| `preferred_username`, `name`, `given_name`, `family_name` | the user name and names | `profile` |
| `email`, `email_verified` (false: CrushMFT does not check addresses) | the email address | `email` |
| `groups` | the user's groups | `groups` |
| `roles` | the user's roles | `roles` |
| `sid` | a value that stands for the CrushMFT session | ID token |
| `auth_time`, `nonce` | when the person signed in to CrushMFT; the app's nonce | ID token |

An app gets a scope only when it asks for it and its **Scopes it may get** allows it.

## Signing out

- **From the app**: an app that sends the browser to `/oidc/logout` (with `id_token_hint` or `client_id`, and a `post_logout_redirect_uri` from its **After sign-out** list, and `state`) ends the person's CrushMFT session and every app session of it, then goes back to the app. Without a registered address the person sees "Signed out", which also loads the sign-out addresses of the session's other apps ([Signing out](Security-App-Gateway.md)).
- **Back-channel**: when a CrushMFT session ends for any reason (sign-out anywhere, time-out, an administrator), every app that got tokens in it and has a **Back-channel logout** address gets a logout token posted there, so it can end its own session. The token names `sub` and `sid` like the ID token did.

## Grafana

In `grafana.ini`, with the app's id `grafana`, Grafana at `https://grafana.example.com`, and the redirect address `https://grafana.example.com/login/generic_oauth` registered on the app:

```ini
[server]
root_url = https://grafana.example.com/

[auth.generic_oauth]
enabled = true
name = CrushMFT
allow_sign_up = true
client_id = grafana
client_secret = <the client secret>
scopes = openid profile email groups
auth_url = https://files.example.com/oidc/authorize
token_url = https://files.example.com/oidc/token
api_url = https://files.example.com/oidc/userinfo
use_pkce = true
login_attribute_path = preferred_username
email_attribute_path = email
groups_attribute_path = groups
signout_redirect_url = https://files.example.com/oidc/logout?client_id=grafana
```

Add `https://grafana.example.com/login` to the app's **After sign-out** list if Grafana should come back to its own page afterwards.

## Other apps

Most apps ask for the same few things:

| The app asks for | Give it |
|---|---|
| Issuer, or discovery URL | `https://files.example.com`, or that with `/.well-known/openid-configuration` |
| Client id, client secret | the app's id; the secret from New client secret, or the app's own key (`private_key_jwt`) with its JWKS in Its public keys |
| Scopes | `openid profile email` (and `groups` for group mapping) |
| User name claim | `preferred_username` |
| Signing algorithm | ES256 (from discovery) |

## With a cluster

Codes and tokens live on the server that made them. Behind a load balancer, the app's calls to the token and userinfo addresses must reach that same server: keep one server as the sign-in server, or make the balancer sticky by source address.
