Token exchange
Exchange an authorization code for an access token, a refresh token and an ID token at the onefold token endpoint.
After the callback, your application exchanges the authorization code for tokens at the token endpoint. A code works once and expires 60 seconds after it is issued.
The request
POST https://onefold.eitozx.com/o/token/
The body is form-encoded (application/x-www-form-urlencoded). The token endpoint does not
read JSON.
| Parameter | Value |
|---|---|
grant_type | authorization_code |
code | The code from the callback |
redirect_uri | The same value you sent in the authorization request |
code_verifier | The PKCE verifier, when you sent a challenge |
client_id | Your client ID |
client_secret | Confidential clients only |
resource | Optional. Omit it, or repeat the value from the authorization request. |
curl -X POST https://onefold.eitozx.com/o/token/ \
-d grant_type=authorization_code \
-d code=AUTHORIZATION_CODE \
-d redirect_uri=https://app.example.com/callback \
-d code_verifier=CODE_VERIFIER \
-d client_id=YOUR_CLIENT_ID \
-d client_secret=YOUR_CLIENT_SECRETClient authentication
| Client type | How it authenticates |
|---|---|
| Confidential | client_id and client_secret in the body, or as HTTP Basic credentials |
| Public | client_id in the body, and no secret |
A public client must not send a client_secret
If a public client sends a secret, in the body or as HTTP Basic credentials, onefold checks
it and the request fails with invalid_client.
The response
{
"access_token": "…",
"token_type": "Bearer",
"expires_in": 900,
"refresh_token": "…",
"scope": "openid identity",
"id_token": "…"
}| Field | What to do with it |
|---|---|
access_token | Send it to the userinfo endpoint as Authorization: Bearer …. It is an opaque string; do not try to decode it. |
expires_in | Seconds until the access token expires |
refresh_token | Store it on your server. See Keep users signed in. |
id_token | Present when you requested openid. Verify it before you trust it. |
An access token issued for a resource is a signed JWT instead of an opaque string. See
Protecting your own API.
Errors
| Status | Body | Meaning | What to do |
|---|---|---|---|
| 400 | {"error": "invalid_grant"} | The code is unknown, expired or already used, the redirect_uri differs from the authorization request, the verifier does not match the challenge, or a verifier was sent for a code requested without a challenge. A code presented again shortly after its exchange also revokes the tokens that exchange issued. | Start a new sign-in |
| 400 | {"error": "invalid_request"} | A parameter is missing, most often code_verifier | Fix the request |
| 400 | {"error": "invalid_target"} | resource is not registered, was sent more than once, or differs from the authorization request | Fix the request |
| 401 | {"error": "invalid_client"} | The client ID or client secret is wrong | Fix the credentials |
| 429 | {"code": "rate_limited", "retry_after": …} | Your application has used its request budget for the current minute | Wait retry_after seconds, then retry |
| 429 | {"code": "token_exchange_locked_out", "retry_after": …} | Too many requests with a wrong client secret | Fix the secret. Retrying with the same one cannot succeed. |
The two 429 responses use the body shape of the HTTP API, with code instead of error, and
both set a Retry-After header. Only successful exchanges count toward the request budget.
From a browser
A single-page application exchanges the code from browser JavaScript, as a public client. For the browser to accept the response, add the application's origin to Allowed Origins on the OAuth2 tab.
- An origin is a scheme, a host and an optional port, for example
https://app.example.com. It has no path. httpsis accepted for any host.httpis accepted only forlocalhostand other loopback addresses.- Wildcards are not accepted.
- An application can list up to 20 origins.
Without a matching origin the exchange succeeds on the server and the browser discards the response. Check this first when a browser exchange fails with no error body.
Authorization request and callback
The parameters of the onefold authorization request, the PKCE and redirect URI rules, and what your callback receives, including the iss parameter.
Verify the ID token
Check the signature and claims of a onefold ID token with the published JSON Web Key Set, and read the user's sub.