# Sign users in to a web app

> Register a web app with a server behind it, so users sign in through the realm and the app learns who they are and calls its API.

Users sign in to your team wiki or customer portal with their realm account, and the app never sees their password. It
learns who they are from an ID token, calls its API with an access token, and keeps them signed in with a refresh
token. This guide is for a web app with a server behind it, which can keep a secret.

**Before you start:**

- **Who can do it:** a realm administrator, in a group that holds `RealmOwner`
  ([Delegated administration](/platform/realms/delegated-administration/)). `RealmViewer` can read the settings but
  not change them.
- **Where:** today this is done through the API; [Call the API](/develop/api/) shows how to get `$CVYUH_SESSION`.
- **First:** know the app's return address and the address of the API it calls.

## Register the app

<Steps>

1. Make sure the realm offers every scope the app needs. It already offers `openid`, `profile`, `email` and
   `offline_access`. To add your API's scopes, read the realm's list first:

   ```sh
   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

   Then send that list back with the new scopes added:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "scopes": ["…existing scopes", "wiki:read", "wiki:write"] }'
   ```

   <Aside type="caution" title="This list replaces the realm's scopes">
     The `scopes` you send become the realm's whole list. A scope you leave out is no longer offered, to this app or
     any other, so always start from the list you read.
   </Aside>

2. Register the app:

   ```sh
   curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=team-wiki' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "status": "active",
       "clientName": "Team Wiki",
       "tokenEndpointAuthMethod": "client_secret_basic",
       "clientSecret": "<a long random secret>",
       "grantTypes": ["authorization_code", "refresh_token"],
       "redirectUris": ["https://wiki.example.com/callback"],
       "postLogoutRedirectUris": ["https://wiki.example.com/"],
       "scopes": ["openid", "profile", "email", "offline_access",
                  "wiki:read", "wiki:write"],
       "defaultScopes": ["openid", "wiki:read"],
       "allowedResources": ["https://wiki.example.com/api"],
       "defaultAudience": "https://wiki.example.com/api"
     }'
   ```

   The realm answers `201`. `redirectUris` must be the exact addresses the app returns to, and their query may not
   carry `code`, `state`, `iss`, `error`, `error_description` or `error_uri`. Every token names the API it is for:
   `defaultAudience` is used when the app names none, and must be one of `allowedResources`.

3. Give the app its settings: the client ID `team-wiki`, the secret, and the issuer `https://foobar.cvyuh.com/oauth2`. Most
   libraries read the rest from the [discovery document](../../../applications/registration/discovery/). The secret is stored
   hashed and never shown again, so keep a copy for the app.

</Steps>

## What users see

They click **Sign in** in the app and arrive at the realm's sign-in page. They sign in through the realm's usual
steps. The first time, a consent page names the app and what it asks for, and they click **Allow**. Then they're back
in the app, signed in.

If they deny, the app is told `access_denied`. If the app's return address isn't one you registered, the user sees an
error page and the app is told nothing.

## What the app sees

The browser returns to the app's registered address with a one-time `code` and the realm's `iss`. The app's server
exchanges the code at `https://foobar.cvyuh.com/oauth2/token`, proving itself with its client secret and the PKCE verifier.
It receives:

- an **access token** for its API, valid five minutes by default;
- an **ID token** naming the user, when it asked for `openid`;
- a **refresh token**, when it asked for `offline_access` and the user allowed it.

<Aside type="caution" title="PKCE is required">
  Every sign-in must carry a PKCE challenge using the `S256` method. A request without one, or with the weaker `plain`
  method, is refused. Most libraries do this for you.
</Aside>

## Check it worked

Sign in to the app as a test user. You should pass the realm's sign-in page and consent, and land back in the app.
A code works once, within a minute: using it a second time is refused, and the tokens it bought are revoked.

Each sign-in is on the user's trail: a `LOGIN` record for the sign-in, then a `TOKEN_ISSUED` record with `grantType`
`authorization_code`, the client, and the scopes and API granted. Find them in **Audit → Authentication** in the
dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The user sees an error page and never returns to the app | The return address isn't registered exactly, or the client is `inactive` | Register the address the app sends, character for character |
| The app is told `invalid_request`, "code_challenge is required" | No PKCE, or `plain` | Send an `S256` challenge |
| The app is told `invalid_scope` | It asked for a scope the client or the realm doesn't offer | Add it to both lists (step 1 and 2) |
| `invalid_grant` at the token endpoint | The code was used before, is over a minute old, or the PKCE verifier doesn't match | Redeem each code once, at once, with its own verifier |
| `invalid_client` at the token endpoint | Wrong secret, or sent by another method than `tokenEndpointAuthMethod` | Check the secret and the method |
| No refresh token | The app didn't ask for `offline_access`, or the user didn't allow it | See [Choose online or offline access](../../sessions/online-and-offline-access/) |

## Undo it

To turn the app off, `PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/team-wiki` with `{ "status": "inactive" }`. Its refresh
tokens are refused from then on.

## Related

- [Authorization code with PKCE](../../../applications/single-sign-on/authorization-code/): the flow the app uses.
- [OpenID Connect sign-in](../../../applications/single-sign-on/openid-connect/): ID tokens and userinfo.
- [Consent](../../../../customer-identity/consent/): the page users see the first time.
- [Refresh tokens](../../../applications/single-sign-on/refresh-tokens/): how the app stays signed in.
- [Sign users in to a single-page app](../single-page-app/): when the app can't keep a secret.
- [Choose which details an app receives](../choose-user-details/): what the ID token and userinfo carry.
