# Sign in a TV or command-line tool

> Let a device with no usable browser sign a user in by showing a short code they enter on their phone or computer.

A smart TV, a kiosk, a command-line tool or an agent started from a terminal has no browser to sign in with. It shows
a short code instead; the user signs in where it is easy, on their phone or laptop, and the device gets tokens for
them. The device never sees their password.

**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:** nothing. The page users enter the code on, `https://foobar.cvyuh.com/device`, is already there.

## Set it up

<Steps>

1. Offer the device grant in the realm. Read the realm's grant 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 device grant 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 '{ "grantTypes": ["…existing grant types",
                      "urn:ietf:params:oauth:grant-type:device_code"] }'
   ```

   <Aside type="caution" title="This list replaces the realm's grant types">
     The `grantTypes` you send become the realm's whole list. A grant you leave out is turned off for every app, so
     always start from the list you read.
   </Aside>

2. Register the device app. A device that can't keep a secret is public:

   ```sh
   curl 'https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=living-room-tv' \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{
       "status": "active",
       "clientName": "Living Room TV",
       "tokenEndpointAuthMethod": "none",
       "grantTypes": ["urn:ietf:params:oauth:grant-type:device_code", "refresh_token"],
       "scopes": ["openid", "profile", "offline_access", "media:play"],
       "defaultScopes": ["openid", "media:play"],
       "allowedResources": ["https://media.example.com/api"],
       "defaultAudience": "https://media.example.com/api"
     }'
   ```

</Steps>

## What the user sees

1. The device shows: *Go to `https://foobar.cvyuh.com/device` and enter `BCDF-GHJK`*, or a QR code that opens the page with
   the code filled in.
2. On their phone, they open the page, sign in if needed, and type the code. Case, dashes and spaces don't matter.
3. The page names the device's app and what it asks for. They click **Allow** or **Deny**.
4. The device moves on, signed in. It then appears among the user's apps on **Authorized Apps**, where they can revoke
   it.

## What the device does

It posts to `https://foobar.cvyuh.com/oauth2/device_authorization` and receives a `device_code`, a `user_code`, the page
address (`verification_uri`), the same address with the code filled in (`verification_uri_complete`, for a QR code)
and a polling `interval` (five seconds by default). It then polls `https://foobar.cvyuh.com/oauth2/token` with
`grant_type=urn:ietf:params:oauth:grant-type:device_code` and its `device_code`:

| Answer | Meaning |
|---|---|
| `authorization_pending` | Not answered yet. Wait `interval` seconds and poll again |
| `slow_down` | Polled too soon. Add five seconds to the interval and wait the whole new interval; a poll inside it is told `slow_down` again, and the interval grows by five seconds more |
| `access_denied` | The user clicked **Deny** |
| `expired_token` | Ten minutes passed. Start over |
| tokens | The user allowed it |

Unless the user allowed offline access, the device's access ends with the session they allowed it from.

## Check it worked

Start a sign-in from the device, enter its code at `https://foobar.cvyuh.com/device` and click **Allow**. The device's next
poll returns tokens, and the app shows on your **Authorized Apps** page.

The device's tokens are on the user's trail as a `TOKEN_ISSUED` record with `grantType`
`urn:ietf:params:oauth:grant-type:device_code`. Find it in **Audit → Authentication** in the dashboard.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The device is told `unauthorized_client` | The realm or the client lacks the device grant | Do steps 1 and 2 |
| The code page says the code isn't valid | It was mistyped, or ten minutes passed | Start again on the device for a new code |
| The code page stops accepting codes for a while | Too many wrong codes from that browser or address | Wait, then type the code carefully; see [Device authorization](../../../applications/single-sign-on/device-authorization/) |
| The device keeps getting `slow_down` | It polls faster than `interval` | Wait the whole interval, five seconds longer each time |
| The device loses access when the user signs out | It holds online access | Ask for `offline_access` if it must keep working |

## Undo it

The user revokes the device on **Authorized Apps**. To stop every device sign-in, read the realm's
`grantTypes` and send them back without `urn:ietf:params:oauth:grant-type:device_code`.

## Related

- [Device authorization](../../../applications/single-sign-on/device-authorization/): pacing and guessing limits.
- [Consent](../../../../customer-identity/consent/): the page the user allows the device on.
- [Let users remove apps they allowed](../../../../customer-identity/guides/people-remove-apps/): **Authorized Apps**.
