# Request paths

> What happens, step by step, in a sign-in, a token refresh, a machine token request and a Data API query.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/architecture/request-paths/

For agents: Request paths: (1) password sign-in: host → project via registry, rate limit check in Postgres, argon2id verify, session row + refresh token row (hash), access JWT signed with the project's Ed25519 key, audit row. (2) refresh: hash lookup, reuse window check, rotate (new row with parent), revoke old, new JWT; reuse after window → revoke session. (3) client credentials with private_key_jwt + DPoP: verify assertion against client JWKS, jti single-use, DPoP proof, issue JWT with cnf.jkt. (4) Data API: AuthContext from key/token (JWKS cached), parse URL, schema cache, one transaction with SET LOCAL ROLE and request.jwt.claims, one statement, JSON. Typical latencies: sign-in 120–250 ms (argon2id), refresh 15–30 ms, Data API 5–20 ms plus the query.

Every request starts the same way: the address's host names the project (`<ref>.data.sypnia.com`, or a self-hosted engine's configured host), the **registry** gives that project's database, and a pooled connection to it is taken.

## A password sign-in

1. `POST /auth/v1/token?grant_type=password` with the publishable key.
2. The rate limit for this IP and e-mail is checked and counted in `sypnia_auth.rate_limits` (one statement).
3. The user is read; the password is checked with argon2id (about 100 ms, on purpose).
4. A **session** row and a **refresh token** row (only its SHA-256) are written; the session policy decides `not_after`.
5. The access token is signed with the project's Ed25519 key (decrypted once per process and kept in memory).
6. An audit row is written. Answer: the session JSON.

Typical time: 120–250 ms, most of it the password hash.

## A token refresh

1. `POST /auth/v1/token?grant_type=refresh_token`.
2. The token's hash is looked up. If it was already rotated: within the reuse window (10 s), the newest token of the chain is returned again; after it, **the whole session is revoked** and the call fails.
3. Otherwise a new refresh token row is written with the old one as parent, the old one is marked revoked, and a new access token is signed — all in one transaction.

Typical time: 15–30 ms.

## A machine token with a key pair and DPoP

1. `POST /auth/v1/oauth/token` with `client_assertion` and a `DPoP` header.
2. The client is read; the assertion's signature is checked against the client's registered public keys; `iss`, `sub`, `aud` and `exp` are checked; its `jti` is inserted in `sypnia_auth.assertion_jti` (a duplicate fails).
3. The DPoP proof is checked: signature, method, address, age, unused `jti`.
4. Scopes and resource are intersected with what the client may have; the access token is signed with `cnf.jkt` set to the proof key's thumbprint.

## A Data API query

1. `GET /rest/v1/orders?select=id,total&status=eq.paid` with a publishable key and a user's access token.
2. The **auth context** is built: the key says which project; the token's signature is checked with the project's public keys (cached); if the token is DPoP-bound, the proof is checked; the result is a principal and a database role.
3. The URL is parsed and checked against the **schema cache**; unknown tables or columns fail here with a clear error.
4. One transaction: `set local role`, the claims into `request.jwt.claims`, **one SQL statement** that returns JSON directly from Postgres, commit.
5. The JSON is streamed back.

Typical time: 5–20 ms on top of what the query itself costs in your database.
