# Choose which details an app receives

> Decide which of a user's details each scope releases to apps, point details at your own attribute names, and add scopes of your own.

When a user signs in to an app with OpenID Connect, the app can learn details about them: name, email, phone. You
decide which details each scope releases, and from which attribute each one is read. The app gets only what the
scopes it was granted release.

**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 attribute name your users' detail is held under (here `dept`).

## Three things you can change

- **Which details a scope releases:** `scopeClaims`. The realm starts with the standard mapping: `profile` releases
  names, username, locale and time zone; `email` releases the email address.
- **Where each detail comes from:** `claimAttributes`. By default `given_name` is read from `firstName`, `family_name`
  from `lastName`, `preferred_username` from `uid`, `email` from `email` and `phone_number` from `telephoneNumber`. If
  your users' attributes have other names, point each detail at the right one.
- **Which details the realm releases at all:** `claims`. A detail missing here is never released, whatever the scopes
  say.

A scope you add yourself releases nothing until you map it.

## Add a scope and its detail

The example adds a `department` scope that releases a `department` detail read from the `dept` attribute, and puts
details in the ID token. The calls use the admin API; [Call the API](../../../../develop/api/) shows how to get a session.

<Steps>

1. Read the current settings, and note the `ETag` the answer carries:

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

2. Change them, sending that `ETag` as `If-Match`, so the write is refused `412` if someone changed the settings
   since your read. Send each list as you read it, with the new value added. Maps merge key by key, and the standard
   entries keep their defaults, so send only the new entries:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H 'If-Match: "<ETag from step 1>"' \
     -H "Content-Type: application/json" \
     -d '{
       "scopes": ["…existing scopes", "department"],
       "claims": ["…existing claims", "department"],
       "scopeClaims": { "department": ["department"] },
       "claimAttributes": { "department": "dept" },
       "idTokenScopeClaims": true
     }'
   ```

   <Aside type="caution" title="These lists replace the realm's scopes and claims">
     The `scopes` and `claims` you send become the realm's whole lists. A value you leave out is no longer offered or
     released, for every app, so always start from the lists you read.
   </Aside>

3. Let the app ask for the scope: read the client with `GET https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>`, then
   send its `scopes` back with `department` added, with `PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>`. This
   list, too, replaces the client's whole list.

</Steps>

## Check it worked

Sign in to the app with the `department` scope and call `https://foobar.cvyuh.com/oauth2/userinfo` with the access token.
The answer carries `department` for a user who has a `dept` attribute. With `idTokenScopeClaims` on, the ID token
carries it too, so the app needn't call userinfo.

What the app always sees:

- The **ID token** names the user (`sub`), when and how they signed in, and the session. `sub` is the user's
  permanent ID in the realm; it doesn't change when they change their username.
- **Userinfo** returns the released details. A detail the user doesn't have is left out.

The change is in the realm's change history as a `PATCHED` record on `oauth2Settings`, before and after: see
**Audit → Config** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `412` on the write | Someone changed the settings since your read | Read again and resend with the new `ETag` |
| Userinfo leaves the detail out | The user has no such attribute, the detail isn't in `claims`, or the app wasn't granted the scope | Check each, in that order |
| The detail is in userinfo but not the ID token | `idTokenScopeClaims` is off | Turn it on, or have the app call userinfo |
| A client's own `scopeClaims` is refused | It names a detail the realm's `claims` doesn't offer | Add it to `claims` first |
| Other apps lost a scope or detail | The `scopes` or `claims` list sent replaced the realm's | Send the lists back whole |

## Give one app less

An app can receive fewer details than the realm releases, for example a partner app that mustn't see email. Set the
client's own `scopeClaims`: for each scope it names, the app gets that list in place of the realm's. A scope it
doesn't name keeps the realm's. The list may name only details the realm offers in `claims`; the write is refused
otherwise.

```sh
curl -X PATCH 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/<client-id>' \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "scopeClaims": { "profile": ["name"] } }'
```

That app's userinfo and ID token now carry `name` for `profile`, and no `given_name` or `family_name`. Every other app
is unchanged.

## Have userinfo signed

Set the client's `userinfoSignedResponseAlg` (`RS256`, `ES256`, or `PS256` where the realm offers it). Its userinfo
answer is then a JWT signed with the realm's key, naming the realm as `iss` and the app as `aud`. The algorithm must be
one the realm lists in `keySettings.algorithm.oauth2.idToken`; if it isn't, userinfo fails rather than answering
unsigned. An app that registers itself can ask for it as `userinfo_signed_response_alg`.

## Undo it

Remove a mapping by sending its key as `null`: `{ "scopeClaims": { "department": null } }`. A standard key you remove
this way goes back to its default.

## Related

- [OpenID Connect sign-in](../../../applications/single-sign-on/openid-connect/): ID tokens, userinfo and the claim settings.
- [Name users by your own directory's attributes](../name-people-by-directory-attributes/): which attribute users sign
  in with.
- [Choose how ID tokens are signed](../choose-id-token-signing/): the algorithm per app.
