# Identities and tokens

URL: https://docs.folkways.io/docs/concepts/authentication

How connections prove who they are with short-lived tokens, what a token grants, and where tokens come from.

Every connection to the Realtime API presents a **token**: a short-lived JWT that says which segment it is for, who
the connection is (its **identity**) and what it may do (its **capabilities**). There are no long-lived secrets in
apps, and there are no secret API keys.

## Where tokens come from [#where-tokens-come-from]

| Your app | Token comes from | Identity |
| --- | --- | --- |
| Users sign in | **Your backend** signs it with the segment's signing key | The user, e.g. `user:42` |
| Public, read-only | The Realtime API, in exchange for a **publishable key** | Anonymous |
| A server or job | **Your backend** signs a `service` token for itself | Your service, e.g. `server` |

<Diagram name="token-flow" alt="The app asks your backend for a token with its session cookie. Your backend signs a token with the private key and returns it. The app sends it in Hello; the Realtime API verifies it with the public key from the Console and answers Welcome with the identity, capabilities and limits. Sixty seconds before expiry the Realtime API sends TokenExpiring; the app fetches a new token from your backend and sends Reauth, and keeps the same connection and subscriptions." />

Your backend signs tokens in its own process: no call to Folkways is needed, so you can sign one on every request.
The private key never leaves your servers; Folkways keeps only the public key to check signatures.

## What a token contains [#what-a-token-contains]

```json title="JWT payload"
{
  "seg": "00000000-0000-4000-8000-000000000042",
  "sub": "user:42",
  "kind": "user",
  "cap": [
    { "topic": "rooms/42", "ops": ["subscribe", "publish", "set", "signal"] },
    { "topic": "$online/>", "ops": ["presence"] }
  ],
  "iat": 1792000000,
  "exp": 1792000600,
  "jti": "9b1d3c0e-0a4f-4f3e-9c55-2f7c1d2a8e11"
}
```

The JWT header carries `alg` (`EdDSA` or `ES256`) and `kid`, the ID of the signing key. The full claim list is in
[Realtime API authentication](/docs/realtime-api/authentication).

### Identity [#identity]

`sub` is who the connection is. It is the `publisher` on every event the connection sends, verified by Folkways, so
other users can trust it. Use a stable ID from your own user table, such as `user:42`. One identity may hold
several connections at once (a phone, a laptop, two tabs), and they all act as the same identity.

`kind` says what the identity is:

| Kind | For | Notes |
| --- | --- | --- |
| `user` | A person signed in to your app | The default |
| `agent` | An AI agent or bot acting in your app | Treated like a user |
| `service` | Your backend | Writes personal topics, manages members, opens DMs, kicks users |
| `anonymous` | A visitor who hasn't signed in | No `sub`; the segment must allow anonymous connections |

An anonymous connection's identity is `anon:` followed by its session ID, new on every connection.

### Capabilities [#capabilities]

`cap` lists topic patterns and the operations allowed on them. With no capabilities a token can connect and nothing
else.

| Op | Allows |
| --- | --- |
| `subscribe` | Receiving a topic's events, including replay |
| `publish` | Writing any event type: stream events, and KV and signal events too |
| `set` | Writing and deleting KV values |
| `signal` | Sending signals |
| `presence` | Reading who is present in a topic, or online |
| `history` | Accepted for compatibility; replay currently needs only `subscribe` |

So a viewer with `subscribe`, `set` and `signal` can mark messages read and show typing without posting messages.
Patterns use `*` for one level and a trailing `>` for one or more, as in [topic rules](/docs/concepts/topics#names-and-patterns).

A token's capabilities are a ceiling. The topic's rule must allow the operation too, and on a private topic so must
the user's member entry. See [who may use a topic](/docs/concepts/topics#who-may-use-a-topic).

### Lifetime [#lifetime]

Tokens live at most **15 minutes** (`exp` no more than 15 minutes after signing; 10 minutes is a good default).
Thirty seconds of clock skew are tolerated. Before a token expires, the Realtime API tells the connection, and the
client fetches a new one and sends it on the same connection. A refresh must keep the same segment and identity,
except that an anonymous connection may sign in. See [Connections](/docs/concepts/connections#token-refresh).

Short lifetimes are what make tokens safe to hand to apps: a leaked token stops working within minutes, and a
change of permissions takes effect at the next refresh.

## Cutting someone off early [#cutting-someone-off-early]

Your backend can act before a token expires:

- **Kick** an identity: its connections close with `AuthRevoked`. Its tokens still work, so stop issuing new ones
  too.
- **Revoke** one token by its `jti`: connections holding it close, and it is refused from then on.

Revocation is held in memory for the token's remaining lifetime (at most about 16 minutes) and does not yet
survive a restart of the service. Keep tokens short-lived and stop issuing them to a user you want to block. See
[`POST /v1/kick` and `POST /v1/revoke`](/docs/realtime-api/kick-and-revoke).

## Keys [#keys]

| Key | Lives in | Used for |
| --- | --- | --- |
| **Signing key** (private half) | Your backend's secrets | Signing user, agent and service tokens |
| **Signing key** (public half) | The Console | Checking those signatures |
| **Publishable key** | Your app's code; it is public | Trading for an anonymous, read-only token |
| **Platform API key** | Your servers' secrets | Reading your workspace, segments and usage from the [Platform API](/docs/platform-api) |

A segment may have several signing keys at once, which is how you rotate one with no downtime: add the new key, move
your backend to it, then delete the old one once its tokens have expired. See
[Signing keys](/docs/console/signing-keys).
