folkwaysDocs
Realtime API

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

FieldValue
algEdDSA (Ed25519) or ES256 (P-256), matching the key. The algorithm is taken from the key, never trusted from the token
kidThe signing key's ID

Claims

ClaimRequiredMeaning
segyesThe segment ID
subyes, unless anonymousThe 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
kindnouser (default), agent, service or anonymous. Anonymous tokens have no sub
capnoCapabilities: [{ "topic": "<pattern>", "ops": ["subscribe", …] }]. Empty: the token can connect and nothing else
iatyesIssued at, Unix seconds
expyesExpiry, Unix seconds, at most 15 minutes in the future
nbfnoNot before, Unix seconds
jtinoA unique token ID. Without one the token can't be revoked
presencenofalse 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.

TypeScript
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:

ToThe service token needs
Write to $user/{identity}publish on $user/{identity}, or on $user/>
Open a DMpublish on the room (e.g. dm/>) and on every member's $user/{identity}
Manage a private topic's memberspublish on the topic
Kick an identitypublish on that identity's $user/{identity}
Revoke a tokenpublish 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

CodeWhen
AuthRequiredNo token
AuthInvalidBad signature, unknown kid, wrong segment, malformed claims, a lifetime over 15 minutes, an anonymous token where they aren't allowed
AuthExpiredThe token has expired. Get a new one
AuthRevokedThe token was revoked, or the identity was kicked. Don't retry with it
SuspendedThe segment is off, or the workspace's package isn't active

On this page