# Authentication

Every authenticated route accepts an Authorization: Bearer header carrying either an API key or a 15-minute web session token. Only /v1/auth/refresh and /v1/auth/code use cookies and browser origins.

## Credential formats

PolicyGrain uses distinct prefixes on every secret so secret scanners can recognise leaked credentials. Secrets are shown once at creation and stored only as SHA-256 hashes.

Secret prefixes and where each is accepted

| Prefix | Kind | Format | Where used |
| --- | --- | --- | --- |
| pgk_ | API key | pgk_<16 hex>_<43 base64url> | Authorization: Bearer header on API routes |
| pgs_ | Session access token | pgs_<16 hex>_<43 base64url> | Authorization: Bearer header from the web app; expires in 15 minutes |
| pgr_ | Refresh token | pgr_<16 hex>_<43 base64url> | HttpOnly, Secure, SameSite=Strict cookie pg_refresh on Path=/v1/auth/refresh only |
| pgt_ | Link token | pgt_<16 hex>_<43 base64url> | Expiring share, export, handoff and email-verify links (at most 90 days) |

## Authenticating with an API key

Pass your key as a Bearer token on any API request. Agents, scripts and CI jobs always use API keys, never cookies.

```bash
curl https://api.policygrain.com/v1/account \
  -H "Authorization: Bearer $POLICYGRAIN_API_KEY"
```

An account created through POST /v1/keys with a new email starts unverified. An unverified account can use documents, sites:write and keys scopes, but evidence:read returns 403 account_unverified until the email owner signs in.

## Web sign-in and session refresh

People sign in through WorkOS AuthKit (Google or email link) or a six-digit WorkOS Magic Auth code emailed when an agent requests a key for an existing email.

Built sign-in and session endpoints

| Method and path | Caller | Behaviour |
| --- | --- | --- |
| GET /v1/auth/sign-in | Browser | Sets a 10-minute HttpOnly SameSite=Lax pg_auth_state cookie on /v1/auth/callback and redirects (302) to WorkOS AuthKit. |
| GET /v1/auth/callback | Browser (WorkOS redirect) | Verifies state and code, claims or creates the account, sets the pg_refresh cookie (30 days), and redirects to the web app (or returns JSON when no signed-in URL is set). |
| POST /v1/auth/code | Web app (allowed Origin) | Accepts { email, code } where code is the six digits from the sign-in email (valid once for 10 minutes), claims/verifies the account, and sets pg_refresh. |
| POST /v1/auth/refresh | Web app (allowed Origin + pg_refresh cookie) | Rotates the refresh cookie and returns { access_token, token_type: 'Bearer', expires_in: 900, expires_at, scopes }. |
| DELETE /v1/auth/refresh | Web app (allowed Origin + pg_refresh cookie) | Revokes the refresh token, clears pg_refresh, and returns 204. |

Refresh tokens rotate on every POST /v1/auth/refresh. A rotated token re-used within 10 seconds returns 409 refresh_conflict (parallel-tab grace window). A rotated token re-used after 10 seconds ends every session on the account and returns 401 refresh_required.

POST /v1/auth/refresh, DELETE /v1/auth/refresh and POST /v1/auth/code enforce an exact Origin allowlist. Requests from any other origin receive 403 origin_not_allowed. Request bodies on identity POST routes are capped at 8 KB.

## Account claiming and pre-claim key suspension

When a person completes WorkOS sign-in with a verified email that matches an agent-created account, PolicyGrain links the WorkOS user and marks the account verified. Every API key created before the claim (made_before_claim: true) is immediately suspended and returns 403 key_suspended until the signed-in person explicitly keeps it with POST /v1/keys/{id}/keep or revokes it.

If a second, different WorkOS user tries to sign in for an already-claimed email, the API returns 409 account_claimed. An unverified WorkOS email returns 403 email_not_verified and never claims an account.

## Checking the current account

```json
{
  "id": "0193a8f2-5c1e-7b40-9d2a-6e8f0c3b1a57",
  "email": "owner@example.in",
  "email_verified_at": "2026-11-19T05:00:00.000Z",
  "claimed": true,
  "keys_waiting": 0,
  "auth": {
    "via": "api_key",
    "scopes": ["documents", "sites:write"]
  }
}
```
