# Connecting people's Devices

Source: https://tiltas.openpanel.sh/docs/partner/sign-ins

> Approve a person's computer or phone into their organisation's Network from your own product.

A person connects a computer or phone with a short code, like `WDJB-MJHT`. Your product shows the approval page and
calls Tiltas to approve it into the organisation's Network.

## The flow

1. The person opens the ordinary Tiltas app and starts a sign-in, or your deep link opens the app and starts it for
   you (`"audience": "openpanel"`, which also sends them to your approval page). The app shows the code.
2. The person enters the code in your product. Your approval page reads the sign-in, shows the Device and asks the
   person to check that the code matches the app.
3. Your server approves it for the person, into their organisation's Network. From then on the Device belongs to that
   Network.
4. The app finishes by itself: it receives its credentials and joins.

**Any pending code counts**, whether the ordinary Tiltas app started it or your deep link did. **Whoever approves
first wins:** once a code is approved or denied, here or on Tiltas's own page, it is no longer pending for anyone.
A code started for another partner is `404` to your key, and so is an ordinary code a person already opened on
Tiltas's own page: it is bound to their browser there.

Because a code is all it takes, the guards below matter. Show the person the Device's details, and send `viewer_ip`
so Tiltas can refuse a Device that asked from another network: this stops the lure "type this code into your
workspace to connect the new server".

## Read the sign-in

Send `viewer_ip`, the address of the person on your approval page. `same_network` then says whether the Device asked
from the same network (the same IPv4 address, or the same IPv6 /64).

```sh
curl -sS "$TILTAS/partner/device-sign-ins/WDJB-MJHT?viewer_ip=203.0.113.7" -H "$AUTH"
```

```json
{
  "user_code": "WDJB-MJHT",
  "device": { "name": "Jono MacBook", "os": "macos", "app_version": "1.2.0", "client": "openpanel" },
  "status": "pending",
  "device_name": "jono-macbook",
  "requested_from": { "ip": "203.0.113.7", "country": null },
  "same_network": true
}
```

`status` is `pending`, `approved`, `denied`, `used` (the Device collected its credentials) or `expired`. A sign-in
lasts 10 minutes. Codes are case-insensitive.

## Approve

```sh
curl -sS -X POST "$TILTAS/partner/device-sign-ins/WDJB-MJHT/approve" -H "$AUTH" \
  -H 'Content-Type: application/json' -d '{
    "account_id": "acc_x4tq9mzr2e",
    "person": { "external_id": "user_123", "email": "jono@acme.example", "name": "Jono" },
    "name": "Jono MacBook",
    "viewer_ip": "203.0.113.7"
  }'
```

| Field | Rule |
|---|---|
| `account_id` | A Network your key created. |
| `person.external_id` | Your id for the person. The same id is the same Member; `email` and `name` are updated each time. |
| `person.email` | Shown in Tiltas and the apps; never used to sign in. |
| `person.name` | Optional, at most 100 characters. |
| `name` | Optional Device name. |
| `viewer_ip` | The approving person's address. **Send it.** |
| `allow_different_network` | `true` only after your page warned the person and they confirmed. |

`204` on success. The Device is in the Network at once, owned by the person, and you receive `sign_in.approved`.

**A Device on another network.** With `viewer_ip`, approving a Device that asked from a different network answers
`409 different_network`, and nothing changes. Show the person a clear warning (the Device's address, and that it is
not where they are). Only if they confirm the Device is theirs, approve again with `"allow_different_network": true`.
The audit log records `viewer_ip` and `same_network` for every approval, and `sign_in.approved` carries
`same_network`.

| Error | When |
|---|---|
| `404 not_found` | Unknown code, a code started for another partner, an ordinary code already opened on Tiltas's own page, or an `account_id` your key did not create. |
| `409 expired_token` | Expired, or already approved or denied. The person starts again in the app. |
| `409 different_network` | See above. |

## Deny

```sh
curl -sS -X POST "$TILTAS/partner/device-sign-ins/WDJB-MJHT/deny" -H "$AUTH"
```

`204`; the sign-in is then denied for everyone. `404` if the code is unknown, no longer pending, or started for
another partner.
