# Sign-in, people and recovery

On a members network, people sign in to the gateway with a device key or a passkey, and every call is checked against a signed membership log.

This page covers the part of [networking](/docs/networking) that is about people: who may sign in,
how to bring someone in, and how a network is recovered. It is built and has tests; the commands and calls below come from the code and its protocol file, and the sign-in flow was not run end to end for this page. The design is
RFC 0014.

## When it applies

Sign-in is on only when all of these hold:

- the network is a members network (it has a `ca`);
- this node is a member of the network's authority group (`authority.member: true`);
- the caller is not on this machine, or `protocol.allow_remote` is set.

A gateway on `127.0.0.1` with no `allow_remote` never asks for a sign-in; that is how the
[getting started](/docs/getting-started) setup works. Passwords and accounts at another provider
are not offered: identity is key-based only.

## The authority group

The people, credentials and grants live in a membership log, version 2. A small group of nodes
(3 or 5 is the design) agrees on each new entry with Raft, so the network
keeps working while one of them is down, and nobody can rewrite the log alone. Each member of
the group says so in its config, with the peer IDs of the first voters, the same list on each:

```yaml
p2p:
  enabled: true
  networks:
    - id: acme
      ca: ed25519:...
      authority:
        member: true
        voters: ["12D3KooW...", "12D3KooW...", "12D3KooW..."]
        # dir: acme.authority      # Raft's files and the sessions, in p2p.dir
```

`voters` is read once, when the group starts in an empty folder. If it is left out, the group is
this node alone. A network at log version 1 (the `p2p admit` log in [networking](/docs/networking))
keeps working as it is; v2 is what `authority.member` turns on.

## Sign in

`ahto ui` is the first sign-in. On the machine that runs the node, it signs a challenge with the
node's own key and prints a one-time code, good for five minutes, from this machine only:

```sh
ahto ui
```

Open the web UI and enter the code; you are the network's owner. A newer code replaces the last.
A gateway with `protocol.allow_remote` set mints none, so on one of those, sign in with a
device key or a passkey you added earlier.

How long a session lasts depends on the network's session profile, part of its policy
(`GET /v1/identity/policy`):

- **personal** (the default): each call keeps the session alive, and it ends after 30 days
  without use. The policy can set a different idle limit.
- **enterprise**: after an hour without use, the session refuses calls until the same device
  key or passkey signs in again (`POST /v1/identity/signin`). That keeps the session. Either
  way it ends 24 hours after sign-in.

## Bring someone in

An admin creates an invite; the person accepts it with their own device key or a passkey and
becomes a member. The calls are on the gateway's REST API under `/v1/identity`:

| Call | What it does |
|---|---|
| `POST /v1/identity/invites`, `GET /v1/identity/invites` | make an invite, list invites |
| `POST /v1/identity/invites/accept` | the invited person accepts |
| `GET /v1/identity/persons`, `/persons/{id}` | list people (admins see everyone, others see themselves) |
| `POST /v1/identity/roles/grant`, `/roles/revoke` | give or take the role `admin` or `member`, network-wide or on a resource pattern |
| `POST /v1/identity/revoke` | sign out a person, a credential or a workload |
| `POST /v1/identity/credentials`, `GET` the same | add a device key or passkey, list them |
| `POST /v1/identity/workloads`, `GET` the same | admit a machine (a build server, a service) |
| `GET`, `POST /v1/identity/policy` | the network's session policy |
| `GET /v1/identity/session`, `/sessions`; `POST /sessions/revoke`, `/signout` | your sessions |
| `GET /v1/identity/authority` | whether the group has a leader and is reachable |

The network keeps at least one admin: the last one cannot be taken away. Every call is checked
against the log and is denied unless a rule allows it. The full request and response shapes are
in `proto/ahto/identity/v1/identity.proto`.

## Audit

Every entry of the log is an audit event: who, what, when. Admins read them with
`GET /v1/identity/audit`, or stream them with `/v1/identity/audit/export`. To have a log shipper
pick them up, set a folder; the node writes one file per UTC day there, one JSON object a line,
and removes files older than `retention` (default one year, at least 24 hours):

```yaml
      audit:
        dir: audit            # in p2p.dir unless absolute
        retention: 8760h
```

## Recovery

If the authority group is lost, the network is recovered by guardians: keys held by people you
trust, of which a threshold must sign. Nothing here changes the network until enough of them sign.

```sh
ahto p2p guardian list                       # guardians, threshold, last rehearsal, open requests
ahto p2p guardian set <key[=label]>...       # ask for a new guardian set; each new key signs it too
ahto p2p guardian rehearse                   # practice a recovery with a throwaway key; changes nothing
ahto p2p guardian sign <request id>          # sign an open request with this node's key
ahto p2p guardian cancel <request id>        # cancel an open request
```

A key is 64 hex digits of an Ed25519 key or a peer ID. A request stays open for 7 days. A
guardian who cannot reach a gateway can give `sign --payload <hex> --print` and send the
signature on.

Rehearse the recovery once after you set the guardians, and again when they change.

## Not built yet

- Sign-in on a network that is not a members network.
- Sign-in through a provider, and passwords: not planned.