Skip to Content
Documentation

Tokens and claims

Reference for Ostiary's OpenID Connect endpoints, scopes, the claims in ID tokens, access tokens and UserInfo, token lifetimes, and signing keys.

Ostiary is the authorization server built into the auth app (Better Auth's OAuth provider). Every path below is under /api/auth on the auth app's domain, and the issuer is that base URL:

https://auth.example.com/api/auth

Most OIDC libraries only need the issuer, a client ID and (for confidential clients) a secret: they read everything else from the discovery document. See Connect an app to register an app and wire a library.

Endpoints

EndpointPathNotes
OpenID configuration/api/auth/.well-known/openid-configurationAlso served at /.well-known/openid-configuration
Authorization server metadata (RFC 8414)/.well-known/oauth-authorization-server/api/authAlso served at /.well-known/oauth-authorization-server. Same content as the OpenID configuration
Authorization/api/auth/oauth2/authorizeAuthorization code flow. PKCE uses S256
Token/api/auth/oauth2/tokenAuthorization code, refresh token, client credentials and device code grants
UserInfo/api/auth/oauth2/userinfoGET or POST with an access token that has the openid scope
Introspection (RFC 7662)/api/auth/oauth2/introspectFor confidential clients. See Protect an API
Revocation (RFC 7009)/api/auth/oauth2/revokeRevokes an access or refresh token
JWKS/api/auth/jwksPublic keys for ID tokens and JWT access tokens. See Signing keys
Device authorization (RFC 8628)/api/auth/device/codeThe user approves the code on the auth app's /device page. See Connect an app
End session/api/auth/oauth2/end-sessionRP-initiated logout, for apps with Sign-out from the app (RP-initiated logout) (enable_end_session). See Connect an app
Dynamic client registration (RFC 7591)/api/auth/oauth2/registerOnly while self-registration is on. See MCP and AI agents

The discovery documents and the /oauth2/* endpoints answer cross-origin requests (CORS), so single-page apps and browser-based MCP clients can call them. The discovery documents are cached for 15 seconds.

What the discovery document advertises

An excerpt of GET https://auth.example.com/api/auth/.well-known/openid-configuration on an instance with one API scope (orders:read) and self-registration off:

{
  "issuer": "https://auth.example.com/api/auth",
  "authorization_endpoint": "https://auth.example.com/api/auth/oauth2/authorize",
  "token_endpoint": "https://auth.example.com/api/auth/oauth2/token",
  "userinfo_endpoint": "https://auth.example.com/api/auth/oauth2/userinfo",
  "jwks_uri": "https://auth.example.com/api/auth/jwks",
  "introspection_endpoint": "https://auth.example.com/api/auth/oauth2/introspect",
  "revocation_endpoint": "https://auth.example.com/api/auth/oauth2/revoke",
  "end_session_endpoint": "https://auth.example.com/api/auth/oauth2/end-session",
  "device_authorization_endpoint": "https://auth.example.com/api/auth/device/code",
  "scopes_supported": ["openid", "profile", "email", "offline_access", "orders:read"],
  "response_types_supported": ["code"],
  "response_modes_supported": ["query"],
  "grant_types_supported": [
    "authorization_code",
    "client_credentials",
    "refresh_token",
    "urn:ietf:params:oauth:grant-type:device_code"
  ],
  "token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post", "private_key_jwt"],
  "code_challenge_methods_supported": ["S256"],
  "subject_types_supported": ["public"],
  "id_token_signing_alg_values_supported": ["EdDSA"],
  "claims_supported": [
    "sub", "iss", "aud", "exp", "iat", "sid", "scope", "azp",
    "name", "picture", "given_name", "family_name", "email", "email_verified"
  ],
  "prompt_values_supported": ["login", "consent", "create", "select_account", "none"],
  "authorization_response_iss_parameter_supported": true
}

When self-registration is on, the document also lists registration_endpoint, client_id_metadata_document_supported (metadata documents) and none in token_endpoint_auth_methods_supported. Public clients registered from the admin console work either way.

Rate limits

In production, Ostiary counts requests per client IP address, in the database, so the limits hold across every instance (see Configuration for RATE_LIMIT_ENABLED and the client IP settings).

PathLimit per IP address
/oauth2/authorize30 a minute
/oauth2/token300 a minute
/oauth2/userinfo60 a minute
/oauth2/introspect100 a minute
/oauth2/revoke30 a minute
/oauth2/register5 a minute
/jwksNot limited

Scopes

ScopeWhat it gives
openidAn ID token, and access to UserInfo
profilename and picture in the ID token and UserInfo; given_name and family_name in UserInfo
emailemail and email_verified in the ID token and UserInfo
offline_accessA refresh token, see Refresh tokens
API scopesScopes your APIs declare, for example orders:read. Requested together with the resource parameter

API scopes come from the APIs registered in the admin console (APIs) and from the OAUTH_API_SCOPES variable. New scopes reach the auth server within a minute. An API scope only goes into a token for its own API: a token for one API never carries another API's scopes. See Protect an API for registering APIs and requesting their tokens.

An application may request the scopes it was registered with; an application registered without a scope list may request any scope. The consent screen lists the requested scopes for the user to approve, unless the application skips consent.

ID token

Issued with the openid scope, signed with the current signing key (EdDSA, Ed25519), valid 1 hour.

ClaimValue
issThe issuer, https://auth.example.com/api/auth
subThe user's ID
audThe application's client ID
iat, expIssued at, and expiry (1 hour later)
auth_timeWhen the user signed in
acrAlways "0"
nonceThe nonce of the authorization request, when it had one
at_hashHash of the access token issued with it
sidThe session ID, only for applications with Sign-out from the app (RP-initiated logout) (enable_end_session) or a back-channel logout URI
name, pictureWith profile. picture only when the user has one
email, email_verifiedWith email
Custom claimsThe application's custom claims for the ID token

The ID token has no role and no organization claims. Add them as custom claims if your app needs them there.

{
  "iss": "https://auth.example.com/api/auth",
  "sub": "iMRpZ3x0eV5mTqQh2wJ8",
  "aud": "kXy7N2vR9tP4qL1m",
  "iat": 1763650000,
  "exp": 1763686000,
  "auth_time": 1763649950,
  "acr": "0",
  "nonce": "n-0S6_WzA2Mj",
  "at_hash": "77QmUPtjPfzWtF2AnpK9RQ",
  "name": "Ada Lovelace",
  "picture": "https://example.com/ada.png",
  "email": "ada@example.com",
  "email_verified": true
}

Access tokens

The token endpoint returns one of two kinds of access token:

  • Opaque (a random string) when the request has no resource parameter. Use it at UserInfo, or introspect it.
  • JWT when the request names an API with resource=https://api.example.com. Your API verifies it against the JWKS without calling Ostiary. See Protect an API.

A JWT access token has the header typ: "at+jwt" and these claims:

ClaimValue
issThe issuer
subThe user's ID. For client credentials (no user), the client ID
audThe API's identifier. With the openid scope, an array of the API and the UserInfo URL
client_id, azpThe application's client ID
scopeThe granted scopes, space-separated
sidThe session ID (tokens issued for a user)
iat, exp, jtiIssued at, expiry, unique token ID
cnfThe DPoP key thumbprint, for DPoP-bound tokens
roleThe user's platform role (user or admin). Absent for client credentials
API custom claimsThe API's own custom claims (APIs > Tokens > Change)
Custom claimsThe application's custom claims for the access token
{
  "iss": "https://auth.example.com/api/auth",
  "sub": "iMRpZ3x0eV5mTqQh2wJ8",
  "aud": ["https://api.example.com", "https://auth.example.com/api/auth/oauth2/userinfo"],
  "client_id": "kXy7N2vR9tP4qL1m",
  "azp": "kXy7N2vR9tP4qL1m",
  "scope": "openid orders:read",
  "sid": "Yk3fQ0aZp8sW1vN6",
  "iat": 1763650000,
  "exp": 1763653600,
  "jti": "x0Pq2vM7bJf4Tn8cR1sZ5kWd9hLa3yGe",
  "role": "user"
}

The role claim is the platform role from the admin console (Users), not a role in an organization. Organization roles go in tokens through custom claims, see Organizations.

Introspecting an opaque token returns the same claims (except jti), plus active and token_type. Introspection has no session, so organization claims that depend on the active organization are left out there.

UserInfo

Call UserInfo with an access token that has the openid scope, in the Authorization header:

curl https://auth.example.com/api/auth/oauth2/userinfo \
  -H "Authorization: Bearer $ACCESS_TOKEN"
{
  "sub": "iMRpZ3x0eV5mTqQh2wJ8",
  "name": "Ada Lovelace",
  "picture": "https://example.com/ada.png",
  "given_name": "Ada",
  "family_name": "Lovelace",
  "email": "ada@example.com",
  "email_verified": true
}
  • sub is always there. The other claims follow the token's scopes, as in the Scopes table.
  • given_name and family_name are derived from name: the last word is the family name. A one-word name gives neither.
  • The application's custom claims for UserInfo are added. UserInfo has no role claim.
  • A token without openid gets 400 with invalid_scope. A missing, expired or revoked token gets 401.

Token lifetimes

TokenLifetimeCan change
Authorization code10 minutes, single useNo
Access token (user)1 hourShorter per API
Access token (client credentials)1 hourShorter per API
ID token1 hourNo
Refresh token30 days, renewed on each refreshShorter per API
Device code10 minutes (poll every 5 seconds)No

To shorten the access or refresh token lifetime of one API: APIs > Tokens > Change, then Access token lifetime (minutes) or Refresh token lifetime (days). Values can only be shorter than the defaults. They apply to the next token issued for that API. See Protect an API.

The response of the token endpoint gives the access token's lifetime in expires_in (seconds) and expires_at (Unix time).

Refresh tokens

You get a refresh token when all of these are true:

  • The request includes the offline_access scope.
  • The application has the refresh token grant. Applications registered in the admin console have it by default.
  • A user signed in (client credentials never return a refresh token).

A request with offline_access must use PKCE, or, for a confidential client, include openid and a nonce.

Exchange it at the token endpoint:

curl https://auth.example.com/api/auth/oauth2/token \
  -u "$CLIENT_ID:$CLIENT_SECRET" \
  -d grant_type=refresh_token \
  -d refresh_token=$REFRESH_TOKEN

How refresh tokens behave:

  • Rotation. Each refresh returns a new refresh token and revokes the one you sent. Store the new one every time.
  • Retries. Sending the same refresh token again within 30 seconds returns the same answer as the first refresh, so a retry after a network error, or two refreshes in parallel, gets the same new tokens.
  • Reuse detection. Sending a refresh token that was already used, after those 30 seconds, returns invalid_grant and revokes every refresh token of that application for that user, with their access tokens. The user has to sign in again.
  • Sliding lifetime. The new refresh token is valid 30 days (or the API's shorter lifetime) from the refresh.
  • Narrower scopes. You may send scope with a subset of the original scopes. A scope that was not granted returns invalid_scope. To add scopes, start a new authorization.
  • Custom claims. Tokens from a refresh carry the application's custom claims too.

Refresh tokens start with ost_rt_ and client secrets with ost_cs_, so secret scanners such as GitHub secret scanning can spot a leaked one. Refresh tokens and secrets issued before the prefixes keep working. Opaque access tokens have no prefix.

Ostiary revokes a user's access and refresh tokens when the user is banned, deactivated by SCIM, or resets their password. JWT access tokens already issued stay valid until they expire (1 hour at most), since APIs verify them without calling Ostiary. Your app can revoke a token itself at /api/auth/oauth2/revoke.

Custom claims

An app often needs more in its tokens than the standard claims: a tenant, a plan, the user's role in an organization. In Auth0 that takes an Action (a script). In Ostiary it is a list of claims you declare per application. Nothing is evaluated: no scripts, no expressions, no paths into the database.

Add a claim

  1. In the admin console, open Applications.
  2. In the application's row menu, choose Token claims.
  3. Click Add claim, then fill in:
    • Claim name: letters, digits and _ . : / -, starting with a letter or _, 200 characters at most. Namespaced names such as https://example.com/tenant are recommended (OIDC Core §5.1.2) but not required.
    • Value from: Text (a fixed string), JSON (a fixed number, boolean, array or object; no null, 4 levels deep at most) or User attribute (see the table below).
    • Tokens: ID token, Access token, UserInfo, or several.
  4. Check the Preview: it shows each token's claims for an example user, and for a machine-to-machine (client credentials) token.
  5. Click Save claims.

Claims apply from the next token issued. An application has at most 20 claims and 4 KB in all. Only applications registered by an admin can have claims: self-registered clients cannot.

User attributes

AttributeLabel in the consoleValue
user.rolePlatform roleThe platform role (user, admin)
user.email_verifiedEmail verifiedtrue or false
user.usernameUsernameThe username, when the user has one
user.two_factor_enabledTwo-factor enabledtrue or false
organization.idActive organization IDThe session's active organization
organization.slugActive organization slugIts slug
organization.roleRole in the active organizationThe user's role in it
organizationsOrganization membershipsEvery organization the user belongs to, oldest membership first: [{ "id", "slug", "role" }] (50 at most)

A claim without a value (no active organization, no username) is left out rather than set to null. See Organizations for how the active organization is chosen.

Reserved names

These names are refused, whatever the token:

  • The JWT and OIDC claims the server owns: iss, sub, aud, exp, nbf, iat, jti, nonce, azp, auth_time, acr, amr, at_hash, c_hash, s_hash, sid, scope, client_id, cnf, typ, events.
  • The introspection fields active and token_type.
  • The OIDC standard claims of §5.1: name, given_name, family_name, middle_name, nickname, preferred_username, profile, picture, website, email, email_verified, gender, birthdate, zoneinfo, locale, phone_number, phone_number_verified, address, updated_at.
  • Ostiary's role.

The console refuses them on save, and the auth server skips them again when it issues a token. To put the platform role in the ID token, use another name, for example https://example.com/role with the attribute user.role.

When claims apply

  • Only to that application's tokens, on every grant: authorization code, refresh, device sign-in, and client credentials. Client credentials have no user, so only the fixed values (Text and JSON) are added.
  • Access token claims are in JWT access tokens (requested with resource) and in the introspection answer for opaque ones. Introspection has no session, so the organization.* claims are left out there.
  • API claims win. In an API's access tokens, the API's own custom claims (APIs > Tokens) win over an application claim with the same name.

Example

With a claim https://example.com/tenant (Text, acme, access token) and a claim org_role (attribute organization.role, ID token and access token), a JWT access token for that application carries:

{
  "https://example.com/tenant": "acme",
  "org_role": "admin"
}

Claims are stored in the oauth_client_claims table (deleted with the client) and added through the oauth-provider plugin's claims extension (packages/core/src/lib/client-claims/extension.ts). Every change is in the audit log as Changed token claims, with the claims' names, tokens and sources, not their values.

Signing keys

ID tokens and JWT access tokens are signed with an Ed25519 key (EdDSA). Apps and APIs verify them against the public keys at https://auth.example.com/api/auth/jwks, the jwks_uri of the discovery document:

{
  "keys": [
    { "alg": "EdDSA", "crv": "Ed25519", "kty": "OKP", "x": "4Xc0bJ8Q...", "kid": "Wm9Tq2..." }
  ]
}

The admin console's Signing keys page lists the keys: Key ID (kid), Algorithm, Created, Signs until, Published until and Status:

StatusMeaning
CurrentSigns every new token
Published for verificationRetired, still in the JWKS so tokens it signed keep verifying
ExpiredPast its grace period, no longer in the JWKS

Private keys are stored encrypted with BETTER_AUTH_SECRET and never shown. The first key is created when the first token is signed (or the JWKS is first fetched), or when you rotate.

Rotate the key now

  1. Open Signing keys.
  2. Click Rotate now, then confirm with Rotate now.

A new key signs every token from then on. The previous key stays in the JWKS for the grace period, so tokens it signed keep verifying until they expire. Rotations are in the audit log as Rotated signing key.

Warning: If a key may have leaked, rotate now, then set a short grace period: a retired key verifies tokens for as long as it is published. The grace period applies to keys already retired too.

Rotate automatically

  1. Open Signing keys, then Edit under Automatic rotation.
  2. Set Rotate the key to every 30, 90, 180 or 365 days, or Never (rotate by hand only) (the default).
  3. Set the Grace period: 1, 7, 30 (the default) or 90 days.
  4. Click Save.

The new key is created when the first token is signed after the current one reaches that age. Changing the interval also applies to the current key: it retires when it reaches the new age, or at the next token if it is already older. Keep the grace period longer than your longest token lifetime (access tokens and ID tokens: 1 hour). Settings reach the auth server within a minute.

Verify tokens across rotations

Rotation needs nothing from your apps as long as they read keys from the JWKS by kid and fetch it again when they meet an unknown one. jose's createRemoteJWKSet, openid-client, Auth.js and Better Auth all do. For example, in an API:

import { createRemoteJWKSet, jwtVerify } from "jose";

const ISSUER = "https://auth.example.com/api/auth";
const JWKS = createRemoteJWKSet(new URL(`${ISSUER}/jwks`));

export async function verifyAccessToken(token: string) {
  const { payload } = await jwtVerify(token, JWKS, {
    issuer: ISSUER,
    audience: "https://api.example.com",
    typ: "at+jwt",
  });
  return payload; // { sub, scope, role, ... }
}

Apps that pin a single public key must be updated after each rotation.

On this page