Tokens and claims
Access, ID and refresh tokens — lifetimes, claims, verification against the JWKS and the mistakes that cost an afternoon.
The three tokens
- Access
- FormatJWT, RS256,
typ: at+jwt,kidin the header - Lifetime900 seconds
- What it is forCalling an API. Exactly one audience.
- ID
- FormatJWT, RS256
- Lifetime3,600 seconds
- What it is forTelling your application who signed in.
- Refresh
- FormatOpaque, rotated on every use
- Lifetime30 days
- What it is forMinting a new access token without a prompt.
The token endpoint
One endpoint serves all three grants. It takes form-encoded fields, never JSON — a Content-Type: application/json body is refused before anything is parsed.
{issuer}/tokenNone — the client credential travels in the bodyExchange a code, refresh a session, or mint a service token. The grant decides which fields apply.
Headers
- Content-Type
application/x-www-form-urlencoded- Accept
application/json
Form fields
grant_typestringrequiredauthorization_code,refresh_tokenorclient_credentials.codestringauthorization_codeonly. Lives 60 seconds and may be exchanged once.code_verifierstringauthorization_codeonly, and required: the PKCE verifier whose S256 hash you sent ascode_challenge.redirect_uristringauthorization_codeonly. Must match the authorize request byte for byte, or the exchange is refused.client_idstringrequired- The registered client. Public clients send it here; confidential clients may authenticate with HTTP Basic instead.
client_secretstring- Confidential clients only —
confidential_webandconfidential_service. refresh_tokenstringrefresh_tokengrant only. The current one; you receive a replacement.scopestringclient_credentialsonly, e.g.appcenter:service.resourcestringclient_credentialsonly, e.g.urn:versified:api:appcenter. A resource the client is not assigned fails withinvalid_target.
POST /oidc/token HTTP/1.1
Host: apps-api.versified.com.gh
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=8f2c1e0a…
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fauth%2Fcallback
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
&client_id=retill-web{
"access_token": "eyJhbGciOiJSUzI1NiIs…",
"token_type": "Bearer",
"expires_in": 900,
"id_token": "eyJhbGciOiJSUzI1NiIs…",
"refresh_token": "p9Kx3Lm2…",
"scope": "openid email profile offline_access appcenter"
}expires_in is 900. id_token is present because openid was granted.
{
"access_token": "eyJhbGciOiJSUzI1NiIs…",
"token_type": "Bearer",
"expires_in": 900,
"refresh_token": "Qm7d0Vb4…",
"scope": "openid email profile offline_access appcenter"
}refresh_token is a new value; the one you sent is now spent.
{
"error": "invalid_grant",
"error_description": "grant request is invalid"
}Expired code, reused code, wrong verifier, or a spent refresh token. Send the person back through authorize.
Failures to handle
- 400
invalid_request - A required field is missing, PKCE was not used, or the body was not form-encoded.
- 400
invalid_grant - The code or refresh token is expired, already used, or does not match the verifier.
- 400
invalid_target - The requested
resourceis not in the client's assignment. Widening it needs re-authorization. - 401
invalid_client - Unknown client, wrong secret, or a public client that tried to authenticate.
- Refresh tokens live 30 days and rotate on every use. Storing the old one means the next refresh presents a spent token and the grant is refused — that is the reuse-detection signal, not a bug.
- A
client_credentialsexchange returns noid_tokenand norefresh_token: there is no person and no session to renew.
A decoded access token
The header names the algorithm and the key. The payload carries the person, one audience, the granted scopes, and three legacy-compatibility claims (user_id, email, useruuid) that predate this provider.
{
"header": {
"alg": "RS256",
"typ": "at+jwt",
"kid": "0X8c2b1e…"
},
"payload": {
"sub": "0d5a7f4e-6c1b-4a3e-9f2d-1c8b5e7a9d31",
"aud": "urn:versified:api:appcenter",
"scope": "openid email profile offline_access appcenter",
"client_id": "retill-web",
"user_id": 412,
"email": "ama@example.com",
"useruuid": "0d5a7f4e-6c1b-4a3e-9f2d-1c8b5e7a9d31",
"iat": 1794141000,
"exp": 1794141900,
"iss": "https://apps-api.versified.com.gh/oidc"
}
}user_id is the legacy numeric id. Key on sub — it is the only claim that is stable across products.
A decoded ID token
{
"sub": "0d5a7f4e-6c1b-4a3e-9f2d-1c8b5e7a9d31",
"aud": "retill-web",
"iss": "https://apps-api.versified.com.gh/oidc",
"email": "ama@example.com",
"email_verified": true,
"name": "Ama Mensah",
"iat": 1794141000,
"exp": 1794144600
}aud is your client id, not a resource audience. name appears only when the profile scope was granted.
An ID token is not an access token
The ID token is 3,600 seconds of proof about who signed in, addressed to your client. Send the access token to an API and the ID token nowhere. Swapping them is the second most common integration bug after skipping the audience check.
The signing keys
{
"keys": [
{
"kty": "RSA",
"use": "sig",
"alg": "RS256",
"kid": "0X8c2b1e…",
"n": "sXchDaQeiM0…",
"e": "AQAB"
}
]
}One key today, rotated rarely. Fetch it once, cache it, and refetch when a token names a kid you do not hold.
Never take a key from the token header
jku, jwk and x5u are attacker-controlled. The only trusted source of keys is the JWKS URL from the discovery document, fetched over HTTPS by you.
Access token claims
iss- MeaningThe issuer. Compare it literally with the discovery document.
sub- MeaningThe person (
userUuid). Stable, and the only correct primary key. aud- MeaningOne resource audience, e.g.
urn:versified:api:appcenter. scope- MeaningSpace-separated granted scopes.
client_id- MeaningThe OAuth client the token was minted for.
exp,iat- MeaningExpiry and issue time, in seconds. 30 seconds of clock skew is tolerated.
Verifying
Verify locally. Do not call the provider on every request — fetch the JWKS once, cache it, and refresh when you see a kid you do not hold.
import { createRemoteJWKSet, jwtVerify } from "jose";
const jwks = createRemoteJWKSet(new URL(`${issuer}/jwks`));
const { payload } = await jwtVerify(token, jwks, {
issuer,
audience: "urn:versified:api:appcenter",
clockTolerance: 30,
algorithms: ["RS256"],
});
const userUuid = payload.sub as string;import jwt
from jwt import PyJWKClient
# PyJWKClient caches the key set and refetches on an unknown kid.
jwks = PyJWKClient(f"{issuer}/jwks")
payload = jwt.decode(
token,
jwks.get_signing_key_from_jwt(token).key,
algorithms=["RS256"],
issuer=issuer,
audience="urn:versified:api:appcenter",
leeway=30,
)
user_uuid = payload["sub"]import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.jwk.source.RemoteJWKSet;
import com.nimbusds.jose.proc.JWSVerificationKeySelector;
import com.nimbusds.jose.proc.SecurityContext;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.proc.DefaultJWTClaimsVerifier;
import com.nimbusds.jwt.proc.DefaultJWTProcessor;
import java.net.URL;
import java.util.Set;
// RemoteJWKSet caches the key set and refetches on an unknown kid.
JWKSource<SecurityContext> jwks =
new RemoteJWKSet<>(new URL(issuer + "/jwks"));
var processor = new DefaultJWTProcessor<SecurityContext>();
processor.setJWSKeySelector(
new JWSVerificationKeySelector<>(JWSAlgorithm.RS256, jwks));
// Audience, issuer and presence checks. Clock skew defaults to 60s.
processor.setJWTClaimsSetVerifier(new DefaultJWTClaimsVerifier<>(
Set.of("urn:versified:api:appcenter"),
new JWTClaimsSet.Builder().issuer(issuer).build(),
Set.of(),
Set.of("sub", "exp")));
var claims = processor.process(token, null);
String userUuid = claims.getSubject();Three ways to get this wrong
Decoding without verifying; verifying the signature but not the audience; and treating email as the person's identity. All three end with one customer reading another's data.
Reading the request identity
The person always comes from the token's sub. Headers such as accountid and userid only select which account is being acted on — equal values mean the owner acting on their own account, different values mean staff acting on an owner's. They never establish identity. See Acting on the right account.
Something here out of date or unclear?
The platform team answers integration questions directly.
