~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
179 lines · 9.4 KB Raw History
# account security

ZDS keeps account security state in SQLite so credentials and sessions can be
listed, revoked, and audited without trusting self-contained tokens alone.

## sessions

Password login creates a durable session row keyed by the access and refresh JWT
IDs. Refresh rotates that row to new JWT IDs, and revocation marks the stored
row revoked. A session JWT is accepted only when its JTI still has an active
storage row.

What a client hears back follows the reference PDS, because clients switch on
the error name:

- a session token this server signed whose `exp` has passed answers
  `400 ExpiredToken` on every bearer route, including `com.atproto.space.*`.
  `ExpiredToken` is only ever reported after the signature verifies; a token
  that never verified is `401 InvalidToken`, and a missing or non-bearer
  header is `401 AuthenticationRequired`.
- a refresh token the server revoked, or whose row is gone, answers
  `400 ExpiredToken` on `refreshSession`. A revoked access token stays
  `401 InvalidToken` so the client refreshes and learns the outcome there.
- refresh is idempotent for 30 seconds: the retired refresh token answers
  with the same successor pair the winning request got, so a client with two
  requests in flight is not logged out by refreshing twice. Outside that
  window the retired token is refused.
- OAuth access tokens are a different profile. Their expiry answer is a `401`
  with a `WWW-Authenticate: ... error="invalid_token"` challenge, which is
  what OAuth clients refresh on.

The session row also records `auth_method` and optional `app_password_name`.
Account-management endpoints that create, list, or revoke app passwords require
a real password session, matching the official PDS boundary that app passwords
cannot mint or manage other app passwords.

## password rotation and recovery

Operators can rotate a password with `com.atproto.admin.updateAccountPassword`
(`did`, `password`, admin authentication). Residents can request an emailed code
with `com.atproto.server.requestPasswordReset` (`email`) and redeem it through
`com.atproto.server.resetPassword` (`token`, `password`). Codes expire after ten
minutes and work once, only for password reset. A later request replaces the code.
Changing the email address invalidates pending reset and confirmation codes.
Invalid reset codes are rejected before password hashing and checked again inside
the password-change transaction.

Password changes atomically update the hash, consume any reset code, revoke all
direct access/refresh sessions, and remove remembered browser logins. Unlike the
reference PDS, ZDS's durable session checks also invalidate existing direct access
tokens immediately. App-password credentials, passkeys and OAuth grants remain
available and can be revoked separately. Changes are recorded in the account audit
log with distinct subject and actor identities. Passwords are limited to 256 bytes.

## app passwords

App passwords follow the official PDS model: each row belongs to an account DID
and name, stores a hashed password, creation time, privileged bit, and optional
scope/controller metadata. The raw generated password is returned once from
`com.atproto.server.createAppPassword`.

Supported routes:

- `com.atproto.server.createAppPassword`
- `com.atproto.server.listAppPasswords`
- `com.atproto.server.revokeAppPassword`

`createSession` accepts either the account password or a stored app password.
App-password access JWTs use the official `com.atproto.appPass` or
`com.atproto.appPassPrivileged` scope, while refresh JWTs keep
`com.atproto.refresh`. Sessions created through app passwords are attributed to
the app-password name. Revoking an app password deletes the credential and
revokes active sessions that were created with it.

Privileged app passwords are for trusted clients that need the official
privileged app-password scope and access to sensitive account features. In the
current Bluesky-compatible ecosystem, that also includes chat transition access:
ZDS stores privileged app passwords with `transition:generic` and
`transition:chat.bsky`; ordinary app passwords store only `transition:generic`.

## management UI

`/account` is the local resident account hub. Its security section,
`/account/manage`, signs in with the account password, then lists passkeys and
app passwords for that account. App passwords can be created and revoked from
the page; the generated password is displayed once and is not recoverable later
because only the hash is stored. The privileged-app-password checkbox is labeled
as trusted-client behavior because ordinary clients should not request it
casually.

The same hub also exposes app/OAuth sessions, direct API token families,
resident email and account-status controls, and the experimental
permissioned-space record browser. `/security` and `/passkeys` remain
compatibility redirects to `/account/manage`.

## references

- official PDS: app passwords are account-owned rows keyed by DID and name, and
  management requires password auth rather than OAuth or app-password auth.
- Tranquil: extends the same account-security plane with scopes, controller DID
  fields, session attribution, and delegation-oriented identity boundaries.

## OAuth sign-in and consent

Browser authorization has two steps. The sign-in card accepts a password or a
passkey; success does not authorize the app. Instead ZDS binds the browser to
that one authorization request: an opaque `__Host-zds-signin` cookie (Secure,
HttpOnly, SameSite=Lax, living as long as the request) whose SHA-256 digest is
stored on the request with the account and sign-in method. The authorization
page then shows consent for that account. A sign-in for one request never
consents for another, and the account must still be active when consent is
given. The consent POST checks the origin and a CSRF value bound to the
browser's cookie and the request. Deny is always available and redirects with
`access_denied`; sign-in errors are shown on the card with the entered
username kept.

After consent or denial, the return page immediately tries to send the browser
back to the app. If the page stays open, Continue appears after five seconds,
matching the [reference PDS fallback delay](https://github.com/bluesky-social/atproto/blob/7dd086d4de20d22777a64043ee29f4afa9a5811b/packages/oauth/oauth-provider-ui/src/components/redirecting-view.tsx).
With JavaScript disabled, browser consent uses a form redirect; the return
page itself offers the link immediately.

JSON callers of `/oauth/authorize` have no pages; they still submit
credentials and consent in one request.

When the request's `login_hint` names a local account, by handle or DID, the
sign-in card starts from that account, showing its profile display name and
avatar. Signing in as any other account is refused, as before.

## OAuth token lifetimes

OAuth sessions follow the reference provider's limits
(`@atproto/oauth-provider`, `oauth-constants.ts` and `Client` in
`client/client.ts`). Each token family records when its session started; a
family issued before ZDS tracked this starts from the migration that added it.

| client | inactivity window | absolute lifetime |
| --- | --- | --- |
| public (`token_endpoint_auth_method: none`) | 14 days | 14 days |
| confidential (`private_key_jwt`) | 3 months (91.3 days) | 2 years (730.5 days) |

Each refresh sets the refresh token's expiry to the earlier of now plus the
inactivity window and the end of the absolute lifetime, and no access token
outlives the session. A refresh after the session has ended answers
`invalid_grant` "Session expired"; the user signs in again. Whether a client
is confidential is read from its current metadata at each refresh, as the
reference does.

## OAuth refresh rotation

Refresh rotation is idempotent for 30 seconds, as for password sessions: the
retired refresh token answers with the successor pair already stored. That
covers two refreshes of one token in flight at once. Only one can rotate the
row; the other sees its rotation refused, re-reads the family, and answers
with the winner's pair instead of tokens that were never stored. A retired
token presented after the window revokes the family.

## remembered OAuth browser login

Password authorization can opt into remembering one account in this browser for
one year. This is a separate, opaque `__Host-zds-browser` cookie, marked Secure,
HttpOnly, and SameSite=Lax. SQLite stores its SHA-256 digest, account binding,
password credential version, and fixed expiry. Password changes invalidate the
login; inactive accounts cannot reuse it. Application OAuth tokens keep their
own lifetime and revocation rules.

A remembered login still shows the client and requested permissions and requires
explicit consent. Without a `login_hint`, the page first offers an account
picker (the remembered account, or another account); with a matching hint it
goes straight to consent. `login_hint` must match the remembered account and
`prompt=login` requires fresh authentication. The picker offers "Forget this
account on this device", which deletes the stored credential and cookie.
Authorization pages are non-cacheable and cannot be framed.

Only password logins are remembered; passkey login and the `/account/manage`
sign-in do not use the cookie. The protocol allows unlimited confidential OAuth sessions, with
individual refresh tokens recommended to expire within 180 days. Longer client
sessions also require binding refreshes to the original client authentication
key; the browser cookie does not change that policy.