# knownprint in the backend: the checks, for any language

Part of the knownprint quickstart for AI coding agents, Step 6. Read
https://knownprint.com/quickstart.md first: it holds the rules that apply here (never read or print
secrets, ask before touching a live server, an unknown result is never a pass). This file writes out
what the Node, Python, PHP and Go modules do, so you can write the same in any other language, or
review a module against it.

## Read the event

The backend receives `kp_request_id` with the protected request. If it is not a string matching
`evt_` and 26 characters from `a` to `z` and `2` to `7` (match the whole string, so a trailing
newline fails), the request id is missing. Otherwise read the event:

```http
GET {KNOWNPRINT_API}/v1/events/{request_id}
Authorization: Bearer {KNOWNPRINT_SECRET_KEY}
Accept: application/json
```

Use a timeout of about 5 seconds. `404` (no such event) and `409` (its visitor is being deleted)
mean no request id. Any other non-2xx answer, a timeout or a network error is an error: log the
status, never the request id, the address or a secret, and take the "unknown" path.

## The six checks

Run them in this order. Burn the id first, so that a failed attempt also uses it up:

| Check           | Fails when                                                                                         |
| --------------- | -------------------------------------------------------------------------------------------------- |
| 1. Single use   | This request id was accepted before: an atomic "set if absent" in a shared store, kept 70 seconds. |
| 2. Fresh        | `received_at` is more than 10 seconds ago, or more than 60 seconds ahead of the server clock.      |
| 3. Not replayed | `integrity.replayed` is `true`.                                                                    |
| 4. Your origin  | `integrity.origin` is not exactly one of `KNOWNPRINT_SITE_ORIGINS`.                                |
| 5. Same address | `network.ip` is `null` or not the visitor address the site's own proxy reports.                    |
| 6. Confidence   | `confidence` is below a minimum, only for decisions that rest on recognising a returning browser.  |

## The visitor address

Trust `X-Forwarded-For` only when the connection comes from the site's own proxy: when the socket's
peer address is that proxy (loopback, where nginx or Caddy runs on the same host), use the **last**
entry, the one the proxy appended; never the first, which the client controls. Otherwise use the
peer address itself; under `fastcgi_pass` that is `REMOTE_ADDR`, already the visitor. Write the
address as knownprint does before comparing: IPv4 as a dotted quad (`::ffff:203.0.113.24` becomes
`203.0.113.24`), IPv6 short and lower case (`2001:db8::1`). Make sure the app is reachable only
through the proxy (it listens on `127.0.0.1`, or a firewall closes its port).

## Decide

knownprint gives no verdict; these rules are a starting point to tune:

- No request id, or any failed check: unknown. Challenge (for example a one-time code by email),
  never allow and never block on this alone: real users with content blockers send no request id.
- `signals.bot.value` is `"bad"`: block.
- `signals.suspect_score.value` is `null` (unknown): challenge.
- `signals.tampering.value` is `true`, or the suspect score is 60 or more: review.
- The suspect score is 30 or more: challenge.
- Otherwise: allow.

## Attach the account id

After the account exists (signup), attach its id to the event, so knownprint keeps which accounts
each browser has used:

```http
PATCH {KNOWNPRINT_API}/v1/events/{request_id}
Authorization: Bearer {KNOWNPRINT_SECRET_KEY}
Content-Type: application/json

{"linked_id": "<the account id as a string, 1 to 128 characters>"}
```

A failure here is logged and never fails the signup.

More: the event's fields at https://docs.knownprint.com/events.md, the API at
https://docs.knownprint.com/api.md, and the full backend guide at
https://docs.knownprint.com/install/server.md.

Next: https://knownprint.com/quickstart.md, the end of Step 6.
