# Authentication

There's no sign-up and no key to create. Your first claim starts a session, and the cookie it sets is what makes that claim yours.

## Sessions without sign-up

A request with no session can do one thing that matters: [create a claim](https://www.prufy.dev/docs/api-reference/create-claim.md). When it does, the server makes an account, starts a session for it, and sets the `prufy_session` cookie on the response. From then on, that cookie is the credential.

The domain is checked before any of that happens, so a typo doesn't leave an empty account behind. A request that already has a session keeps it: claiming a second domain adds to the same account.

**cURL**

```shell
curl -X POST 'https://www.prufy.dev/api/v1/claims' \
  -c cookies.txt -b cookies.txt \
  -H 'content-type: application/json' \
  -d '{
  "input": "yourcompany.com"
}'
```

**Node.js**

```javascript
const response = await fetch("https://www.prufy.dev/api/v1/claims", {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ input: "yourcompany.com" }),
})

// The session cookie is what makes this claim yours. Keep it for later requests.
const cookie = response.headers.getSetCookie()[0]?.split(";")[0]
const data = await response.json()
```

**Python**

```python
import requests

# A Session keeps the cookie the response sets, and sends it from then on.
session = requests.Session()

response = session.post(
    "https://www.prufy.dev/api/v1/claims",
    json={"input": "yourcompany.com"},
)
data = response.json()
```

One network can start 10 new sessions an hour. After that, creating a claim without a cookie answers `rate_limited` until the oldest one ages out. A client that keeps its cookie never meets this limit.

## Carrying the session

Send the cookie back on every later request. A browser does this by itself for same-origin requests. From code, each tool has its own way: curl reads and writes a cookie jar, Python's `requests.Session` remembers cookies, and Node's `fetch` has to be handed the header.

**cURL**

```shell
curl 'https://www.prufy.dev/api/v1/claims/clm_4b1f7c1e-52e7-4c0a-9a3b-7d2f0c6e8a10' \
  -b cookies.txt
```

**Node.js**

```javascript
// `cookie` is the session cookie that Create a claim returned.
const response = await fetch("https://www.prufy.dev/api/v1/claims/clm_4b1f7c1e-52e7-4c0a-9a3b-7d2f0c6e8a10", {
  headers: { cookie },
})
const data = await response.json()
```

**Python**

```python
import requests

# `session` is the requests.Session() that created the claim.
response = session.get("https://www.prufy.dev/api/v1/claims/clm_4b1f7c1e-52e7-4c0a-9a3b-7d2f0c6e8a10")
data = response.json()
```

Without the cookie, a route that needs one answers `401`. A cookie that's malformed or that we never issued gets the same answer.

No session 401 Unauthorized

```json
{
  "error": {
    "kind": "unauthorized",
    "message": "This browser isn't signed in to any domains yet."
  }
}
```

A claim that belongs to another account answers `404 not_found`, exactly like one that doesn't exist. There's no way to tell from the outside whether an id is real.

## The cookie

| Attribute | Value | Why |
| --- | --- | --- |
| Name | `prufy_session` | One cookie, holding one random token of 43 characters. |
| HttpOnly | Always | Scripts on the page can't read it, so an injected script can't take it. |
| SameSite | `Lax` | Another site can't make a signed-in POST. This is the CSRF protection. |
| Secure | Over HTTPS | On production it never travels in the clear. |
| Max-Age | 400 days | The longest a browser will keep a cookie. The session ends then too. |
| Path | `/` | Sent to every route. |

The token is 256 random bits and it's never stored. The database keeps its SHA-256 hash, which is enough to find the session and useless for signing in, so a leaked database doesn't leak sessions.

## Signing in on another device

An account with a confirmed alert address can be reached from anywhere. [Request a sign-in link](https://www.prufy.dev/docs/api-reference/request-sign-in.md) with that address, and we email a link that works once, for 15 minutes. It opens a page with a button. Pressing it makes that device one more session on the same account, with its own cookie.

**cURL**

```shell
curl -X POST 'https://www.prufy.dev/api/v1/sign-in' \
  -H 'content-type: application/json' \
  -d '{
  "email": "owner@yourcompany.com"
}'
```

**Node.js**

```javascript
const response = await fetch("https://www.prufy.dev/api/v1/sign-in", {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ email: "owner@yourcompany.com" }),
})
const data = await response.json()
```

**Python**

```python
import requests

response = requests.post(
    "https://www.prufy.dev/api/v1/sign-in",
    json={"email": "owner@yourcompany.com"},
)
data = response.json()
```

- The answer never says who has an account. Every address gets the same `{ requested: true }`, and the email is sent after the response, so the timing doesn't say either.
- Only a confirmed address works. Typing an address into the alerts form doesn't make it one, and a link stops working if the address is changed or removed before it's used.
- Opening the link does nothing by itself. Mail scanners open every link, so the page asks for a press, and the press is a POST.
- Nothing moves between accounts. A device that was signed in to a different account ends that session. Its claims stay where they were.

A terminal can't press a button, so `prufy login` asks for the link with the SHA-256 of a secret it keeps. Pressing that link's button shows a code instead of signing the browser in. The terminal sends the code and its secret to [Redeem a sign-in code](https://www.prufy.dev/docs/api-reference/redeem-sign-in-code.md), and gets its session in `Set-Cookie`. The code lasts 10 minutes, and the session token never appears in a URL or on the page.

## Signing out

[Sign out](https://www.prufy.dev/docs/api-reference/sign-out.md) ends the session that sends it, on the server, and clears the cookie. Only that one: the account, its claims and its other devices are untouched. Repeating it is safe. [Sign out everywhere else](https://www.prufy.dev/docs/api-reference/sign-out-others.md) ends every other session on the account, for a lost phone or a token that leaked.

**cURL**

```shell
curl -X DELETE 'https://www.prufy.dev/api/v1/session' \
  -b cookies.txt
```

**Node.js**

```javascript
// `cookie` is the session cookie that Create a claim returned.
const response = await fetch("https://www.prufy.dev/api/v1/session", {
  method: "DELETE",
  headers: { cookie },
})
const data = await response.json()
```

**Python**

```python
import requests

# `session` is the requests.Session() that created the claim.
response = session.delete("https://www.prufy.dev/api/v1/session")
data = response.json()
```

## Tokens that stand in for one

Four things have to work for someone who has no session, often on a different device. Each uses a token that does one job and nothing else.

| Token | Where it goes | What it can do | How long |
| --- | --- | --- | --- |
| Setup link | The path of [/api/setup/{token}](https://www.prufy.dev/docs/api-reference/retrieve-setup.md) | See one claim's record and run a check. Nothing else. | 30 days |
| Email confirmation | The body of [Confirm the alert email](https://www.prufy.dev/docs/api-reference/confirm-alert-email.md) | Confirm one address, once. | 24 hours |
| Unsubscribe | The query of [Unsubscribe from alert emails](https://www.prufy.dev/docs/api-reference/unsubscribe-alert-email.md) | Stop alert emails to one address. | Until the address changes |
| Sign-in link | The body of [Use a sign-in link](https://www.prufy.dev/docs/api-reference/redeem-sign-in.md) | Start one session on the account whose alert address it went to, once. | 15 minutes |

A setup link is safe to forward for exactly that reason. Whoever holds it can add the record and ask us to look, and can't get a new code, stop the claim or see its history.

## What each route needs

| A request needs | Routes |
| --- | --- |
| Nothing. Starts a session if there isn't one | [POSTCreate a claim](https://www.prufy.dev/docs/api-reference/create-claim.md) |
| The session cookie | [GETRetrieve a claim](https://www.prufy.dev/docs/api-reference/retrieve-claim.md) [POSTRun a check](https://www.prufy.dev/docs/api-reference/run-check.md) [POSTGet a new code](https://www.prufy.dev/docs/api-reference/rotate-challenge.md) [POSTAbandon a claim](https://www.prufy.dev/docs/api-reference/abandon-claim.md) [GETGet the record as a zone-file line](https://www.prufy.dev/docs/api-reference/zone-file.md) [POSTCreate a setup link](https://www.prufy.dev/docs/api-reference/create-setup-link.md) [PUTSet the alert email](https://www.prufy.dev/docs/api-reference/set-alert-email.md) [DELRemove the alert email](https://www.prufy.dev/docs/api-reference/remove-alert-email.md) [POSTSubscribe a browser to push](https://www.prufy.dev/docs/api-reference/subscribe-push.md) [DELUnsubscribe a browser from push](https://www.prufy.dev/docs/api-reference/unsubscribe-push.md) [POSTSend a test alert](https://www.prufy.dev/docs/api-reference/send-test-alert.md) [DELSign out everywhere else](https://www.prufy.dev/docs/api-reference/sign-out-others.md) [POSTGet a one-click setup URL](https://www.prufy.dev/docs/api-reference/domain-connect-url.md) [GETInspect a name's DNS](https://www.prufy.dev/docs/api-reference/inspect-dns.md) |
| The session cookie, if you have one | [GETList claims](https://www.prufy.dev/docs/api-reference/list-claims.md) [GETRetrieve alert settings](https://www.prufy.dev/docs/api-reference/retrieve-alert-settings.md) [GETRetrieve this session](https://www.prufy.dev/docs/api-reference/retrieve-session.md) [DELSign out](https://www.prufy.dev/docs/api-reference/sign-out.md) |
| A token in the path, query or body | [GETRetrieve a claim by setup link](https://www.prufy.dev/docs/api-reference/retrieve-setup.md) [POSTRun a check by setup link](https://www.prufy.dev/docs/api-reference/run-setup-check.md) [POSTConfirm the alert email](https://www.prufy.dev/docs/api-reference/confirm-alert-email.md) [POSTUnsubscribe from alert emails](https://www.prufy.dev/docs/api-reference/unsubscribe-alert-email.md) [POSTUse a sign-in link](https://www.prufy.dev/docs/api-reference/redeem-sign-in.md) [POSTRedeem a sign-in code](https://www.prufy.dev/docs/api-reference/redeem-sign-in-code.md) |
| Nothing | [POSTRequest a sign-in link](https://www.prufy.dev/docs/api-reference/request-sign-in.md) [GETCheck the API is up](https://www.prufy.dev/docs/api-reference/health.md) [POSTMove the clock forward](https://www.prufy.dev/docs/api-reference/advance-clock.md) [POSTRun the worker](https://www.prufy.dev/docs/api-reference/run-worker.md) |

## What isn't here yet

> **A session is not an identity**
>
> An account with no confirmed alert address can only be reached from the devices already signed in to it. Lose the cookie and its claims are out of reach, even though the domain stays verified. Confirming an address is what makes an account reachable from elsewhere.

- API keys. A long-lived secret you create once and send as a header. Until they exist, a server-side integration has to keep the cookie from its first claim.
- Passkeys. Signing in without an inbox in between. Sign-in links need a confirmed address first.

Both are planned and tracked in [the project's issues](https://github.com/agnelnieves/prufy/issues).

---

Every page of these docs, as a list: https://www.prufy.dev/llms.txt
