Authentication
Token claims, signing rules, service tokens and publishable keys, and how to sign a token in any language.
Every request to the Realtime API carries a token: in the Hello frame on a WebSocket, in
Authorization: Bearer <token> on HTTP. Never put a token in a URL. Identities and tokens
explains the model; this page is the exact contract.
Token format
A JWT signed with one of the segment's signing keys.
Header
| Field | Value |
|---|---|
alg | EdDSA (Ed25519) or ES256 (P-256), matching the key. The algorithm is taken from the key, never trusted from the token |
kid | The signing key's ID |
Claims
| Claim | Required | Meaning |
|---|---|---|
seg | yes | The segment ID |
sub | yes, unless anonymous | The identity, at most 256 bytes. Must not start with anon:. Make it one topic level (no /) if the user needs a personal topic or presence |
kind | no | user (default), agent, service or anonymous. Anonymous tokens have no sub |
cap | no | Capabilities: [{ "topic": "<pattern>", "ops": ["subscribe", …] }]. Empty: the token can connect and nothing else |
iat | yes | Issued at, Unix seconds |
exp | yes | Expiry, Unix seconds, at most 15 minutes in the future |
nbf | no | Not before, Unix seconds |
jti | no | A unique token ID. Without one the token can't be revoked |
presence | no | false keeps the connection out of presence (it is invisible). Default true |
Thirty seconds of clock skew are tolerated on exp, nbf and iat.
Ops in cap are subscribe, publish, set, signal, presence and history; see
Capabilities. Patterns use * for one level and a trailing > for one
or more.
topic, not topics
In the JWT, each capability has a topic field holding one pattern. The JavaScript SDK's createToken() takes
the same thing as topics; it writes topic into the token for you.
Sign a token
Any JWT library that supports EdDSA or ES256 works.
import { Folkways } from '@folkwaysio/realtime/server';
const folkways = new Folkways.Rest({ url, segment, signingKey: { kid, privateKey } });
const { token, jti, expiresAt } = await folkways.createToken({
identity: 'user:42',
capabilities: [{ topics: 'rooms/42', ops: ['subscribe', 'publish'] }],
});The segment's Connect tab in the Console has these examples filled in with its own values.
Service tokens
A token with "kind": "service" is for your backend. Besides publishing, it is what the backend endpoints take:
| To | The service token needs |
|---|---|
Write to $user/{identity} | publish on $user/{identity}, or on $user/> |
| Open a DM | publish on the room (e.g. dm/>) and on every member's $user/{identity} |
| Manage a private topic's members | publish on the topic |
| Kick an identity | publish on that identity's $user/{identity} |
| Revoke a token | publish on $user/> |
Service tokens need no member entry on private topics. Sign them only on your servers.
Anonymous tokens
An anonymous token has "kind": "anonymous" and no sub. The segment must allow anonymous connections, or the
token is refused with AuthInvalid. Give anonymous tokens narrow rights, typically subscribe on public topics.
Your backend can sign one, or an app can get one for a publishable key.
Failures
| Code | When |
|---|---|
AuthRequired | No token |
AuthInvalid | Bad signature, unknown kid, wrong segment, malformed claims, a lifetime over 15 minutes, an anonymous token where they aren't allowed |
AuthExpired | The token has expired. Get a new one |
AuthRevoked | The token was revoked, or the identity was kicked. Don't retry with it |
Suspended | The segment is off, or the workspace's package isn't active |