# First-party app sign-in

> Your own native app signs users in with its own screens, running the realm's sign-in steps directly, without a browser.

Your own mobile or desktop app shows its own sign-in screens and sends the user's answers to the realm, step by
step. The realm runs the same sign-in steps as its web pages, and at the end returns a code that the app trades for
tokens, as in a [browser sign-in](../authorization-code/). The sign-in is a real session: signing out, back-channel
logout and step-up all apply to it.

Use it only for apps that are truly yours, where the realm and the app are the same organization. The app sees the
user's password, which is why it is off by default and you must mark both the realm and the client. For any other
app, use the [browser sign-in](../authorization-code/).

## Before you begin

- **Who can do it:** a realm administrator (a group holding `RealmOwner`) turns it on and marks the client;
  `RealmViewer` can read both but not change them.
- **Where:** today this is done through the API ([Call the API](/develop/api/) shows how to get
  `$CVYUH_SESSION`).
- **First:** the app is registered, with `authorization_code` among its grants.

## How it works

**1. The app starts the sign-in** at the authorization challenge endpoint, with what a browser sign-in carries:
scopes, the API it wants and its PKCE challenge. It proves who it is as at the token endpoint (a public app sends
only its `client_id`):

```sh
curl https://foobar.cvyuh.com/oauth2/authorize-challenge \
  -d client_id=field-app \
  --data-urlencode 'scope=openid field:read' \
  --data-urlencode resource=https://field.example.com/api \
  -d code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM \
  -d code_challenge_method=S256
```

**2. The realm answers with the first step** to show. It's a `400` with the error `insufficient_authorization`, the
step's prompts in `callbacks`, and an `auth_session` to send back:

```json
{
  "error": "insufficient_authorization",
  "header": "Sign in",
  "callbacks": [
    { "type": "NameCallback", "output": [{ "name": "prompt", "value": "User Name:" }],
      "input": [{ "value": "" }] },
    { "type": "PasswordCallback", "output": [{ "name": "prompt", "value": "Password:" }],
      "input": [{ "value": "" }] }
  ],
  "auth_session": "eyJhbGciOi…"
}
```

The prompts are in the language the app asks for with `Accept-Language`.

**3. The app sends the user's answers** as `callbacks`, a JSON array in a form field, with the `auth_session`:

```sh
curl https://foobar.cvyuh.com/oauth2/authorize-challenge \
  -d client_id=field-app \
  -d auth_session=eyJhbGciOi… \
  -d 'callbacks=<the callbacks below, URL-encoded>'
```

Decoded, `callbacks` is:

```json
[
  { "type": "NameCallback", "input": [{ "value": "maya" }] },
  { "type": "PasswordCallback", "input": [{ "value": "…" }] }
]
```

If the realm asks for more, such as a one-time code, it answers with the next step and a new `auth_session`, and the
app repeats. Once the sign-in is done, it answers `200`:

```json
{ "authorization_code": "eyJhbGciOi…" }
```

**4. The app redeems the code** at the token endpoint as any code, with its `code_verifier`. It sends no
`redirect_uri`, since the code was sent to none.

## Set it up

These steps use the admin API; [Call the API](../../../../develop/api/) shows how to get a session.

1. Turn it on for the realm: set `firstPartyAppsAllowed` to `true` with
   `PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Settings/oauth2Settings`.
2. Mark the app's client: set `firstParty` to `true` in `https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client`, with
   `authorization_code` among its `grantTypes`.
3. Point the app at `https://foobar.cvyuh.com/oauth2/authorize-challenge`.

| Where | Setting | What it does |
|---|---|---|
| Realm | `firstPartyAppsAllowed` | Offers first-party sign-in at all (default off). While it's on, discovery publishes `authorization_challenge_endpoint` |
| Client | `firstParty` | Marks this client as yours |
| Realm and client | `grantTypes` includes `authorization_code` | Needed on both |
| Realm | `acrValues` | Lets the app ask for a stronger sign-in by name ([step-up](../step-up/)) |

## What the user sees

Only the app's own screens. The realm sends each step's prompts (username, password, a code from an authenticator
app) and the app draws them. A wrong answer counts toward the realm's [lockout](../../../authentication/lockout/) as
on the realm's own pages, and the warning before a lock comes back with the step for the app to show.

## Limits and defaults

| Setting or limit | Default | Range |
|---|---|---|
| `firstPartyAppsAllowed` | off | on or off |
| `firstParty` on a client | off | never on for an agent's client |
| Time to answer each step | ten minutes | fixed |
| A first-party code under FAPI 2.0 | at most a minute | fixed |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| `unauthorized_client` on the first call | The realm or the client isn't marked first-party, or a grant is missing | Turn on `firstPartyAppsAllowed`, set `firstParty`, add `authorization_code` to both `grantTypes` |
| `invalid_session` halfway through | The user took more than ten minutes on a step, or the app resent an answered `auth_session` | Start again; always send the newest `auth_session` |
| `consent_required` | The app asked for `offline_access` the user never allowed | Have the user allow it once through the browser sign-in |

## Audit record

The sign-in writes `LOGIN` and its steps (or `LOGIN_FAILED`) as a browser sign-in does, and the code's redemption a
`TOKEN_ISSUED` record with `grantType` `authorization_code`. See them in **Audit → Authentication**.

## Good to know

- **An agent's client is never first-party.** An agent that held a user's password could act as them without anyone
  seeing it was an agent. Agents reach a user's access through [token exchange](../../tokens/token-exchange/) instead.
- **Consent is not asked**, since the app and the realm are one party. The exception is offline access: the user
  must have allowed it earlier, through the browser sign-in.
- **Each `auth_session` is answered once**, only by the client that started it, and within ten minutes of the step
  before.
- **A sign-in begun with a [DPoP](../../client-security/dpop/) key must use the same key throughout.**
- **`acr_values` picks the sign-in chain by name** ([step-up](../step-up/)). `prompt`, `max_age` and `login_hint`
  are ignored.
- **Under the [FAPI 2.0 profile](../../client-security/security-profiles/), a first-party code lives at most a minute.**

## Reference

### Errors

| Error | When |
|---|---|
| `insufficient_authorization` | Not a failure: the next step to show, with a new `auth_session` |
| `invalid_session` | The `auth_session` is expired, another client's, already answered, or not the realm's own; or the sign-in timed out. Start again |
| `access_denied` | The sign-in failed, such as a wrong password, or the user can't be issued tokens |
| `consent_required` | The app asked for `offline_access`, and the user hasn't allowed it through the browser sign-in |
| `unauthorized_client` | The realm or the client isn't marked first-party, the client is an agent's, or either doesn't take the authorization code grant |
| `invalid_dpop_proof` | The proof isn't by the key the sign-in began with |
| `invalid_request` | `callbacks` isn't a JSON array, or the PKCE challenge is missing or not `S256` |
| `invalid_client` (401) | The client didn't prove who it is |

## Related

- [Sign users in to your own native app](../../../guides/sign-in-to-apps/native-app-sign-in/): the task, start to
  finish.
- [Authorization code with PKCE](../authorization-code/): the browser sign-in every other app uses.
- [Step-up and re-authentication](../step-up/): the levels `acr_values` names.
