# AI governance sign-in

> A user of your realm approves a device with a code in any browser, and the device joins the realm's AI fleet as theirs.

An enrolled device reports its AI tools only once a user of your realm has approved it. The approval ties the
device's AI tools to a real user of the realm, so the fleet shows whose machine each tool runs on, and the device's
record lists who works on it.

## Before you begin

- **An enrolled Linux or macOS device**; see [Get started with Endpoint Management](../../../endpoint-management/get-started/).
  The AI tools agent comes with the device client.
- **A user of your realm** who can sign in as usual. Approving needs no administrator privilege.
- **A shell on the device** as root (with `sudo`) or as a member of the device client's group, to print the code.
- **Nothing to set up in the realm.** Every realm gets its AI governance sign-in when it's created; it turns on the
  realm's [device authorization](../../../workforce-identity/applications/single-sign-on/device-authorization/) grant
  and adds an app for it to the realm's clients. Leave both in place.
- **To read the result:** any privilege that reads your realm's configuration, such as `RealmViewer` or `RealmOwner`,
  through the API ([Fleet records](../fleet-records/)).

## How it works

The sign-in uses the device authorization grant (RFC 8628) at your realm's own OpenID Connect issuer.

1. **The AI tools agent asks for approval.** On a newly enrolled device, the AI tools agent (the part of the device
   client that finds the device's AI tools) starts a sign-in and waits, holding a sign-in address and a short code.
2. **A user approves it.** The user opens the address in any browser, on any device, signs in to your realm as they
   always do, and approves the code.
3. **The device joins the fleet.** The AI tools agent joins the realm's AI fleet as that user, and the realm adds the
   user to the machine's device record, in `users` (see
   [Device records](../../../endpoint-management/devices/device-records/)).

From then on the AI tools agent reports to the fleet on its own. Every report is tied to the device and to the user who
approved it, and the agent's private key never leaves the device.

**One user per device.** A device has one approving user, and its AI tools are attributed to them. The agent stays
signed in as that user, and the fleet checks that sign-in again while the device is connected: if the user is
disabled or their sign-in revoked, the fleet closes the device's connection within five minutes and stops taking its
reports, and the agent asks for a new approval.

## Set it up

On the device, print the waiting address and code with the AI tools agent's command, `cvyuh-aig` on a standard
install, in the same folder as the device client: `/var/cvyuh` on Linux, `/Library/Cvyuh` on macOS.

```sh
sudo /var/cvyuh/bin/cvyuh-aig --socket /var/cvyuh/var/run/aig.sock enrollment
```

On macOS, `sudo /Library/Cvyuh/bin/cvyuh-aig --socket /Library/Cvyuh/var/run/aig.sock enrollment`. It prints the
state, the address and the code:

```
awaitingAuthentication
Sign in at: <sign-in address>
Code: <code>
```

Give the address and code to the user whose device it is. Once they've approved it, run the same command again: it
prints `enrolled`. Run it with `discover` in place of `enrollment` to see what the agent finds on the device.

### Hand a device to another user

1. Drop the device from the fleet: `DELETE` on its fleet record ([Fleet records](../fleet-records/#drop-a-device)).
2. The device leaves the fleet at once. Its agent starts a new approval the next time it reconnects to the fleet or
   its inventory changes; restart the device to make that happen now. Run the `enrollment` command again for the new
   code.
3. The new user approves it. The device joins the fleet again, as theirs, under a new fleet device id.

The earlier user stays listed in the device record's `users`; see [Limits and defaults](#limits-and-defaults).

## What the user sees

In the browser, the user sees your realm's own sign-in page, with whatever second factor and policy the realm sets,
then the page that asks for the code and whether to allow the device. On the device, the command prints one of these
states:

| State | What it means | What to do |
|---|---|---|
| `notConfigured` | The device client hasn't set the agent up | Finish [device enrollment](../../../endpoint-management/enrollment/device-enrollment/) |
| `starting` | The agent is starting a sign-in, or starting over after one failed | Run the command again in a minute |
| `awaitingAuthentication` | A code is waiting, with its address | Have the user approve it |
| `enrolling` | Approved; the agent is joining the fleet | Run the command again |
| `enrolled` | The device is in your realm's fleet | Nothing: it reports from now on |
| `failed` | The agent lost its connection to the fleet | It retries on its own; see [Troubleshooting](#troubleshooting) |

## Limits and defaults

| What | Value | Can it change? |
|---|---|---|
| How long a code lives | Your realm's device code lifetime, 10 minutes by default | Yes: `deviceCodeLifetimeSecs` on [Device authorization](../../../workforce-identity/applications/single-sign-on/device-authorization/) |
| Approving users per device | One | No |
| Sign-in checked again | Within five minutes while connected | No |
| Users on the device record | Each user who has approved the device; none is taken off when the device is dropped or approved by someone else | No |

## Good to know

- **Only into its own realm.** The device signs in at the issuer of the realm it was enrolled in, so only a user of
  that realm can approve it.
- **Any user of your realm can approve a code.** Whoever approves it becomes the device's user. Give a code only to the
  person whose device it is.
- **No listening port.** The sign-in needs no local web server on the device; the code is approved in a browser
  anywhere.
- **Your sign-in rules apply.** The user signs in through the realm's usual sign-in, with whatever second factor and
  policy the realm sets.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| The browser says the code is wrong or expired | The code was mistyped, or it outlived its lifetime | Run the command again for a new code. After an expiry the agent starts over on its own, and prints a new code within a minute or two |
| The state stays `starting` and no code appears | The realm's device authorization grant was turned off, or the agent can't reach the realm | Put `urn:ietf:params:oauth:grant-type:device_code` back in the realm's `grantTypes` ([Device authorization](../../../workforce-identity/applications/single-sign-on/device-authorization/#set-it-up)); check the device is online |
| A user of another realm can't sign in at the address | The address is your realm's own sign-in | Have a user of your realm approve it |
| An approved device asks for approval again | It was dropped from the fleet, or its user was disabled or their sign-in revoked | Have a user of your realm approve it again |
| A dropped device hasn't asked for approval | It's still on its earlier connection, and nothing has changed on it | Restart the device |
| The command can't reach the agent | The device client isn't running, or you ran it without `sudo` | Start the device client; run the command with `sudo` |

## Audit record

The approving user's own trail shows the approval: their sign-in (`LOGIN` and its steps) and a `TOKEN_ISSUED` for the
device authorization grant, then one each time the agent renews its token. Read it with
[Read a user's or agent's trail](../../../audit-reports/audit-trail/read-a-trail/). The user's addition to the device
record isn't recorded in the audit trail.

## Related

- [Get started with Endpoint Management](../../../endpoint-management/get-started/): the step before this one.
- [Get started with AI Governance](../../get-started/): what you read once the device has joined.
- [Device authorization](../../../workforce-identity/applications/single-sign-on/device-authorization/): how the realm runs the code flow.
- [Fleet records](../fleet-records/): what the fleet keeps about the device.
