Issuing document tokens

Giving an end user access to one document by minting a document token from your backend.

A browser opens a document with a document token. Your backend mints it, after deciding that this user may open this document. This page shows the call, its options, and a typical endpoint. For what a token is and grants, see Authentication and access.

Minting a token

const database = sdk.database(databaseId);

const { capability, expires } = await database.mintCapability({
  collection: collectionId,
  document: "roadmap",
  permission: "write",
  origins: ["https://app.example.com"],
  ttlSeconds: 600,
});
NameTypeDefaultDescription
collection *string—The collection's id.
document *string—The document the token is for.
permission *"read" | "write"—What the holder may do.
ttlSecondsnumber300How long the token is valid, from 60 to 86400 seconds.
originsreadonly string[]—Browser origins the token may be used from, at most 32. Absent or empty allows any origin.
substring—A subject your application names for the holder, 1 to 128 printable ASCII characters. Shown on its presence and its edits.

The result has:

  • capability: the token, to hand to the browser.
  • expires: when it expires, in seconds since the Unix epoch.

The browser opens its session at the server's /v1/sessions endpoint with it; see Creating a client. The token names its document, so the session must open that document.

An origin is scheme://host[:port], compared with the default port dropped: https://app.example.com and https://app.example.com:443 are the same. A listed entry that is not an origin (a path, a query) makes the token invalid.

Which credential mints what

Minting needs a user or a database key; a document token cannot mint. The credential bounds the tokens it can mint: one with read on the database mints read tokens only, and minting a write token needs write. A backend that serves both readers and editors therefore needs a write credential, and decides per request which permission to put in the token.

A minted token stays valid only while its minter does: revoking the key that minted it, or disabling the user, ends the token too.

A token endpoint

The client calls your credential function whenever it opens a session and before its token expires. Expose an endpoint it can call at any time:

app.get("/api/documents/:id/access", async (request, response) => {
  const user = await requireUser(request);
  const access = await accessOf(user, request.params.id); // "read" | "write" | undefined
  if (access === undefined) {
    response.status(404).end();
    return;
  }

  const { capability, expires } = await database.mintCapability({
    collection: collectionId,
    document: request.params.id,
    permission: access,
    origins: ["https://app.example.com"],
    ttlSeconds: 600,
    sub: user.id,
  });
  response.json({ token: capability, expires });
});

app, requireUser and accessOf stand for your own web framework and authorization code. This endpoint is where your application's access rules live: the server trusts the token, and the token says only what your backend put into it.

On the client, call the endpoint from the credential function; see Creating a client.

Good practice

  • Choose the lifetime by how soon access must end. A token stays valid until it expires, for new sessions too, so a user whose access you remove keeps it until then. The client renews it before it expires without reconnecting, so a short lifetime costs one request to your backend per renewal.
  • Set origins for browser clients to the origins your application is served from. A token that leaks then cannot be used from another site.
  • Mint the smallest permission. Give read to viewers.
  • Name a subject when the holder is someone your users should see, such as an agent: other participants see it on the holder's presence and edits.
  • Reuse a token while it is valid. The client does, across reconnects.

To end every token of a database at once, before they expire, see Signing tokens yourself.

Failures

mintCapability fails with an EngineError:

codeWhen
permission_deniedThe credential may not mint this permission on this database, or it is a document token
unauthenticatedThe credential is not valid
invalid_argumentThe options are invalid, for example a lifetime outside 60 to 86400 seconds
unavailableThe server cannot sign right now, for example in the first seconds after it started

A mint is not retried by the SDK; a mint that failed unavailable can simply be asked again.

Next