# Approvals

Some things your agents do wait for you first. That might be a deploy, a message to a stranger, a role's admin window,
or a secret marked "ask each time". Your [access policy](https://fleetlayer.ai/docs/access-control.md) decides which ones (its `ask:` rules).
Each request reaches you wherever you are, with everything you need to decide:

- who asks: the session and its machine;
- what it wants to do, and to what;
- the agent's own words: its reason, and the call or command it wants to run.

You answer in whichever place you see it first:

- **On your phone:** the Fleetlayer app's Approvals tab. Approve with Face ID, or refuse.
- **On your Mac:** a window opens with the request, ready for your fingerprint. Touch to approve ("Touch ID to
  approve"), or press Refuse.
- **In the dashboard:** the Access page, from any browser you're signed in on.

The first answer wins, and the others close. Every request and every answer goes into the audit log.

Some requests take more than a click: an 'ask each time' secret, or any call a tool always asks about. Only your own
Touch ID or Face ID approves those, or your passkey on that very request. A button in the dashboard, an approve
token or the admin token never does, so an agent that can reach your browser or a token can't approve its own
request. The dashboard shows "Approve with passkey" for them.

## What a request shows

```
infra@mac-agents wants to call reveal on 1password@secrets
  op://internal-infra/db/password
  op://terraform/aws-deploy/secret-key
to run: ansible-playbook site.yml
why: apply the database firewall change
once
```

The first line is access control's own wording, built from names it checked. Below it is the agent's text, labelled
as the agent's. A long request that can't be shown whole (about 8,000 characters) isn't put to you at all: it's
refused, and the agent is told to ask for less.

`once` means the approval covers exactly that request, one time. A policy can let an approval hold for a while
(`for: 10m`). It then covers the same session doing the same thing to the same target until it ends. For calls that
always ask (a 1Password reveal), it covers exactly the same call: the same references, command, folder and reason.

## Approving with Touch ID or Face ID

Your Mac and your phone each hold an **approver key**: a key made in the device's Secure Enclave that never leaves it
and works only after your fingerprint or face.

When you approve, the device signs that one request: its ID, who asked, what for, the exact text you were shown, and
how long it holds. The control plane checks the signature against the request as it stored it. A signature made for
anything else doesn't count: another request, another hold, or text changed on the way.

Four things make the prompt trustworthy even with agents running as you on the same Mac:

- **The app fetches the request itself**, by its ID, from the control plane. It shows only what it fetched, and the
  bytes it signs are built from the same fields. Nothing handed to the app (by the plugin that opens it, or anything
  else) is shown or signed, so nobody can put a misleading reason in front of a real Touch ID prompt.
- **Only the signed app can use the key.** The key lives in the app's own keychain group. A program an agent writes
  can't ask the Secure Enclave to sign with it behind a prompt of its own.
- **A refusal needs no fingerprint**, since it only narrows what an agent may do. An approval always needs one.
- **Agents can't use the approval pages through your browser.** The shared browser refuses the fleet's own pages
  (the dashboard, the sign-in service, the control plane), so an agent driving your signed-in Chrome can't click
  Approve or confirm a step-up as you ([Share your signed-in browser](https://fleetlayer.ai/docs/guides/browser.md#the-fleets-own-pages-are-off-limits)).

### Where the boundary is

These protections are against agents that aren't you: agents on other machines, and agents running as other Unix
users. They hold whatever those agents can reach. They can't drive your browser except through the shared tool,
which refuses Fleetlayer's own pages, and they can't use your devices' keys.

On your own Mac user it's different. An agent running as you there can do anything your user can: drive Chrome
directly, read your session cookies once the Keychain is unlocked, click in the dashboard. On that user the boundary is
your passkey, Touch ID and Face ID:
- Requests marked strong (an 'ask each time' secret, any call a tool always asks about) need one of them, so an agent
  running as you still can't approve its own.
- An ordinary request approved with a click in the dashboard is only as safe as your Mac user. Keep agents you don't
  trust with that on another Unix user ([Trust](https://fleetlayer.ai/docs/trust.md)).

### Setting it up on a Mac

1. Install the Fleetlayer Approve app (it ships with Fleetlayer's macOS release) in `/Applications`.
2. Open it and click **Set up**. It signs in through your sign-in page, the same way the phone app does, and becomes
   a device of yours. Then it makes its keys and shows their two fingerprints. Your sign-in page opens on the
   enrolment request:
   - confirm it with your passkey;
   - check the page shows the same two fingerprints as the app, then click **These match**.
   Nothing else can enrol it: not an agent, not a terminal command, not the admin token.

   The sign-in service needs the app as a client, as it does the phone app ([An app on a person's own device](https://fleetlayer.ai/docs/plugins/signin.md#an-app-on-a-persons-own-device)):
   ```yaml
   clients:
     - name: approve-mac
       device: true
       redirect_uris: ["ai.fleetlayer.approve:/signin"]
   ```
3. Add the plugin that opens the app to the Mac's `node.yaml`, then restart the node:

   ```yaml
   plugins:
     - name: approve-mac
       command: fleetlayer-approve-mac
       grants: [event.subscribe]
       config:
         match: ["*"]            # which requests to show by their target; every one by default
   ```

   The access policy must let it hear requests: `allow: [{who: "plugin:approve-mac@<mac's node>", can: subscribe,
   to: "channel:access"}]`.

The plugin hears each new request and starts the app with only its ID. When you answer somewhere else, it closes the
window. `fleetlayer status` shows a problem when the app isn't installed or the plugin can't hear requests.

### Setting it up on a phone

In the Fleetlayer app, open Settings and tap **Set up** under "Approve from this phone". The app makes its keys and
shows their fingerprints ("Approves" and "Reads"). Then it opens the sign-in page:
- confirm with your passkey;
- check the page shows the same two fingerprints, and tap **These match**.

Until then, the Approvals tab says "Waiting for your passkey", and "Confirm with your passkey" opens the page again.
The same screen lists your other approving devices, each with when it was last used; swipe one to remove it.
"Remove from this phone" removes the key later. In the tab, Approve is a button behind Face ID, so a glance at the
phone never approves anything. Refuse needs no Face ID.

**Why it's done this way.** A key that approves as you mustn't be addable by anything running as you, so:
- each device enrols its own key, over HTTPS with its own device sign-in, never through a Fleetlayer node or the
  command line;
- only you approve it, with your passkey and your own check of the fingerprints;
- an enrolment you don't confirm within 15 minutes lapses.
If the fingerprints on the page don't match your device, tap **They don't match**: the key is removed, and something
else asked to act as you.

**Your first device, and every one after it.**
- **The first device** you enrol is active once you've confirmed it, but approves nothing for ten minutes. It's listed
  at once under "Approving devices" on the dashboard's Access page, and announced on the `access` channel. If you
  didn't add it, Remove it there or from the phone.
- **Every device after that** also needs a yes from one you already have. The enrolment request shows up on that
  device like any other request, and you approve it with Touch ID or Face ID there. It counts once both are done
  (passkey with "These match", and the other device's yes), and then it approves at once.

### Keys you no longer use

`GET /v1/approver_keys` lists every key: whose it is, its name, where it was made, and when it was last used.
`DELETE /v1/approver_keys/<id>` removes one for good. The phone app does both from Settings. A key whose person stops
being one of the tenant's admins stops working at once.

## For plugins and actions: asking the person

Anything a node checks with the control plane can wait for you. Plugins and actions don't build their own approvals:

- **Shared tools** behind the [MCP gate](https://fleetlayer.ai/docs/access-control.md#the-mcp-gate) are asked call by call. `call_targets` names
  a call by the parts of its arguments that matter, and `for: once` binds an approval to its exact arguments. The
  1Password plugin's `reveal` works this way ([1Password](https://fleetlayer.ai/docs/plugins/1password.md#ask-each-time)).
- **Shared commands** are asked run by run, showing the program and every argument
  ([Run approved commands on another machine](https://fleetlayer.ai/docs/guides/shared-commands.md)).
- **Role windows, session starts, messages and sync** follow the policy's `ask:` rules
  ([Access control](https://fleetlayer.ai/docs/access-control.md)).

Each request is an `access.ask` event on the `access` channel, with the request's ID, access control's line, who
asked, the verb, the target and the agent's detail (marked as outside data). Each answer is an `access.decided`
event, with the ID, whether it was approved and who decided. Whatever shows requests elsewhere (a chat bot, a pager)
can subscribe to both.

## The device API

The Fleetlayer apps use these endpoints, and so can any other device that keeps its keys in secure hardware. Keys are
P-256. Public keys are sent as base64 of the uncompressed point (65 bytes, X9.63). Signatures are ECDSA with SHA-256,
ASN.1 DER, in base64.

| Call | What it does |
|---|---|
| `GET /v1/approvals` | Pending requests (newest first) |
| `GET /v1/approvals/<id>` | One request, `{request: {id, from, verb, to, detail, holdMs, createdAt, status}, line}` |
| `POST /v1/approvals/<id>` `{approve}` | Decide it, signed |
| `POST /v1/approver_keys` `{name, signKey, readKey}` | Enrol a key, with the device's own device token (phone or Mac app) |
| `GET /v1/approver_keys`, `DELETE /v1/approver_keys/<id>` | List and remove keys |

A device signs with three headers: `Fleetlayer-Approver-Key` (its key's ID), `Fleetlayer-Approver-Signature`, and
for reads `Fleetlayer-Approver-Time` (Unix milliseconds, within five minutes of the control plane's clock).

A read is signed by the read key (no fingerprint). The bytes, each line ending in a newline, are:

```
fleetlayer approver read v1
key <key ID>
request <request ID, or * for the list>
at <the time header>
```

A decision is signed by the sign key, which needs a fingerprint. A refusal may use the read key. The bytes are one
line per field, each the field's name, the value's length in bytes, a colon and the value:

```
fleetlayer approval decision v1
id <len>:<id>
decision <len>:approve or refuse
from <len>:<from>
verb <len>:<verb>
to <len>:<to>
detail-sha256 <len>:<SHA-256 of the detail, lower-case hex, or - when there's none>
hold-ms <len>:<holdMs>
created-at <len>:<createdAt>
```

A decision takes no `for`: it holds for what the request asked. `404` means someone answered first. `403` means the
signature doesn't match the request as it stands, or the key isn't active. The control plane's tests hold a worked
example (`approvers.rs`, `canonical_bytes_match_the_shared_test_vector`).

Enrolling (`POST /v1/approver_keys` with the device's own token) files an `enroll` request on
`approver:<handle>/<key ID>`. It's approved one way only:
- the person's passkey on that request at the sign-in service's `/stepup?approval=<id>` page;
- then "These match" on the page that follows (`/stepup/match`), which shows the key's name and fingerprints.

`POST /v1/approvals/<id>` refuses to approve an enrolment, whatever the token, the admin token included. It may still
refuse one. Until it's approved the key decides nothing, and a pending key lapses after 15 minutes. When the person already
has an active key, the new one also needs that key's signed approval of the enrolment request (a signed
`POST /v1/approvals/<id>`). The answer is `{vouched: true, waiting: "match"}` until the passkey half is done too. A
person's first key has `usableAt` ten minutes after it's activated: until then it may refuse but not approve. Each
newly active key is announced as an `access.approver` event. A read signed by a
key that isn't active answers `403`, saying whether the key "is pending", "is lapsed" or "is removed". A device token removes only its own person's keys.

`/stepup` returns to `return=` only in three places:
- its own origin;
- a client's redirect origin;
- the phone app's own `ai.fleetlayer.mobile:/stepup`.
