Authentication and access

The credentials the server accepts, what each one grants, and how an end user gets access to a single document.

The server accepts three kinds of credentials. Two of them, a user's password and a database key, belong on your backend. The third, a document token, is what a browser uses. This page explains each, what it grants, and how they fit together.

Overview

CredentialSent asUsed byGrants
UserHTTP Basic (name:password), on the HTTP API onlyOperators, backendsWhat the user's grants allow, per database
Database keyAuthorization: Bearer mk_…, or in a session's first frameBackends, servicesOne permission on one database (or on every database)
Document tokenAuthorization: Bearer v2.…, or in a session's first frameBrowsersread or write on one document, until it expires

End users of your application never hold a user password or a database key. Your backend authenticates them however it likes, decides what they may open, and mints a document token for exactly that.

A credential is never sent in a URL.

Permissions on a database

Access is granted per database, at one of three levels. Each level includes the ones before it.

PermissionAllows
readReading documents and outcomes, sessions and presence; listing collections, schema versions and migrations; minting read tokens
writeAlso creating, changing and deleting documents; presence leases; minting write tokens
adminAlso the database's grants, keys and token keys, imports, and its collections and migrations

Nothing granted on one database reaches another. A caller without a permission on a database is refused permission_denied whether or not the database exists, and a document the caller may not see reads as not_found.

Users

A user has a name and a password and authenticates with HTTP Basic, on the HTTP API only (a session takes a database key or a document token). A user holds a grant per database: one of the permissions above.

A superuser can do everything on every database. Creating and removing databases, managing users and deployment-wide keys, and setting limits are for superusers only. The first superuser is created when a deployment starts for the first time; see Self-hosting.

Database keys

A database key is the credential for a service. It carries one permission and is either scoped to one database or deployment-wide; a deployment-wide key holds read or write, never admin. A database admin can create keys for that database; only a superuser can create a deployment-wide key.

A key is presented as mk_<key id>_<secret>. The secret is returned once, when the key is created. The server keeps only a digest of it, so a lost secret cannot be recovered: revoke the key and create another.

Prefer a database-scoped key with the smallest permission that works. A backend that only mints read tokens needs read; one that creates documents needs write; only your deployment tooling, which creates collections and runs migrations, needs admin.

A revoked key or grant, a disabled user or a changed password stops working as soon as the server's processes have taken up the change: within seconds, and at most access_max_staleness_ms (10 seconds by default).

See Access management for the calls.

Document tokens

A document token (the server calls it a capability) grants sessions and requests on one document:

  • It names one database, collection and document, and a permission, read or write.
  • It may be used again, for any number of sessions, until it expires. A client keeps using the one it has, for example when it reconnects.
  • It expires. Minted by the server, the default lifetime is 5 minutes, the shortest 1 minute and the longest 24 hours.
  • It can be limited to a list of browser origins, so a token that leaks cannot be used from another site. An empty or absent list allows any origin.
  • It never grants more than the credential that minted it held: a read key cannot mint a write token, and a token minted by a key stops working when that key is revoked.
  • It may carry a subject (sub), a name your application gives the holder, such as an agent's id. Other participants see it on the token holder's presence and on the edits it makes.

The usual flow:

  1. The browser asks your backend for access to a document.
  2. Your backend checks that this user may open it, and asks the server for a token with its database key.
  3. The browser opens a session at /v1/sessions with the token, and asks your backend for a new one when it expires.
const { capability, expires } = await sdk.database(databaseId).mintCapability({
  collection: collectionId,
  document: documentId,
  permission: "write",
  origins: ["https://app.example.com"],
  ttlSeconds: 600,
});

See Issuing document tokens.

Signing tokens yourself

Asking the server for a token costs one request per token. An application can instead sign document tokens itself with an Ed25519 key, a token key (the server calls it a capability key):

  1. A database admin registers the public key under a key id, once, with PUT /v1/admin/databases/{db}/capability-keys/{id} and the body { "public": "…" }: the raw 32-byte Ed25519 public key in unpadded base64url. The key id is 1 to 64 characters of letters, digits, ., _ and -, and does not start with gw_.
  2. The application builds tokens of the form v2.<base64url(claims)>.<base64url(signature)>, where the signature is the Ed25519 signature over the ASCII bytes v2.<base64url(claims)>, and base64url is unpadded.

The claims are a JSON object:

ClaimMeaning
kidThe id the key was registered under
dbThe database id
cThe collection id
dThe document id
perm"read" or "write"
expExpiry in seconds since the Unix epoch, at most 24 hours ahead
iatWhen it was issued, in seconds since the Unix epoch; exp is at most 24 hours after it
jtiAn id for this token, 1 to 64 characters of letters, digits and - . _ : @
originsOptional list of at most 32 allowed origins
subOptional subject your application names for the holder, 1 to 128 printable ASCII characters

A claim not in this list makes the token invalid. A registered key verifies tokens for documents of its own database only. A token is at most 8 KiB. Set iat: a database admin can end every token issued before a moment with PUT /v1/admin/databases/{db}/capabilities-not-before, and from then on a token without iat is refused. Revoking the key (POST /v1/admin/databases/{db}/capability-keys/{id}/revoke) ends every token it signed.

The TypeScript packages have no helper for registering a key or signing tokens; use the HTTP API and an Ed25519 library.

Browsers and origins

The server answers cross-origin browser requests to its HTTP API only from the origins it was configured to allow. It reads no cookie and never sends Access-Control-Allow-Credentials, so a page can only use credentials it holds itself. An application whose browser code only opens sessions with document tokens does not call the HTTP API from the browser at all.

Next