Access Tokens and Refresh Rotation
What's in a Securely access token, how to verify it, and how to handle one-time-use refresh tokens correctly.
Access Tokens and Refresh Rotation
This guide covers everything you need to know about the tokens Securely Connect issues — what they contain, how long they live, how to verify them, and the one critical rule about refresh tokens: they're single-use.
The access token
Every successful token request returns:
{
"access_token": "eyJhbGciOiJSUzI1NiIs...",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "eyJhbGciOiJSUzI1NiIs..."
}access_token is an RS256-signed JWT. You send it on every resource API request:
GET /v2/merchants/123/payment-links HTTP/1.1
Host: connect.securely.io
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...Claims inside the access token
| Claim | Type | Always present | Meaning |
|---|---|---|---|
iss | string | ✓ | Issuer — https://auth.securely.io |
aud | string | ✓ | Audience — https://connect.securely.io |
sub | string | ✓ | Subject — the client ID |
client_id | string | ✓ | Same as sub |
party_type | int | ✓ | 2=SECOND_PARTY, 3=THIRD_PARTY |
iat | int | ✓ | Issued at (Unix seconds) |
exp | int | ✓ | Expires at (Unix seconds) |
business_id | int | optional | Set when the token is scoped to a business (always set for third-party tokens issued via consent) |
You can decode the token client-side for display or routing, but the resource API validates the signature before honoring the call — you don't have to trust the client.
Verifying the signature
Tokens are signed with RS256. Pull the public keys from /.well-known/jwks.json:
curl https://auth.securely.io/.well-known/jwks.json{
"keys": [
{
"kty": "RSA",
"kid": "abc123...",
"use": "sig",
"alg": "RS256",
"n": "...",
"e": "AQAB"
}
]
}Most server-side OAuth libraries can fetch and cache JWKS automatically when you point them at the issuer's discovery document at /.well-known/openid-configuration.
Lifetime
| Token | Default lifetime |
|---|---|
| Access token | 1 hour |
| Refresh token | Longer (refresh re-issues both) |
When the access token expires, you have two choices depending on how you originally obtained it:
- Got it from
authorization_code→ use the refresh token to mint a new pair. - Got it from
client_credentials→ just call/oauth/tokenagain with your credentials.
Refresh token rotation — the one rule
Every refresh consumes the refresh token and returns a new one.The old refresh token is immediately revoked when a new one is issued. Using it again returns
400 invalid_grantwith"Refresh token has been revoked".
This is intentional. It means if a refresh token leaks, the attacker and the legitimate client are racing each other — whoever uses the token first invalidates the other. The first sign of a leaked token is a legitimate refresh failing with "revoked." That's a signal to log the user out and start over.
How a refresh works
curl -X POST https://auth.securely.io/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token" \
-d "refresh_token=CURRENT_REFRESH_TOKEN" \
-d "client_id=YOUR_CLIENT_ID"Confidential clients also send client_secret.
The response is identical in shape to the original token response — a new access_token and a new refresh_token. The previous refresh token is now dead.
Storing the new refresh token
After every refresh, you must:
- Persist the new
refresh_tokenreturned in the response. - Discard the old one.
If you persist the new access token but forget the new refresh token, the next refresh will fail with invalid_grant. This is a common source of "my user keeps getting logged out" bugs.
async function refresh(stored) {
const params = new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: stored.refreshToken,
client_id: stored.clientId,
});
const res = await fetch('https://auth.securely.io/oauth/token', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: params,
});
if (res.status === 400) {
// refresh token was revoked or invalid — force re-auth
throw new RefreshTokenRevokedError();
}
const { access_token, refresh_token, expires_in } = await res.json();
// CRITICAL: store BOTH new tokens
await store.save({
accessToken: access_token,
refreshToken: refresh_token,
expiresAt: Date.now() + expires_in * 1000,
});
}Handling a revoked refresh token
When /oauth/token returns 400 with invalid_grant on a refresh, it means one of:
- You retried with a token you already exchanged (you didn't save the new one).
- The refresh token has been explicitly revoked.
- Concurrency: two requests refreshed simultaneously and only one won the race.
In all three cases, your stored refresh token is dead. Treat the user as logged out and run the authorization flow again.
Don't retry oninvalid_grantRetrying with the same revoked token will keep failing. The only recovery is a fresh authorization request.
Concurrency: two parallel refreshes
If two requests in your app notice an expired access token at the same time, both may try to refresh, but only one will succeed — the second one will get invalid_grant because the first one already rotated the token.
Two patterns help:
- Single-flight lock. Serialize refreshes so only one runs at a time.
- Refresh-on-expiry, not on 401. Track
expires_atand refresh proactively a few seconds before expiration, so requests don't race the first 401.
The recipe Refresh an Access Token Correctly shows both patterns.
Detecting token misuse
If you receive invalid_grant on a refresh that you know you only used once, treat it as a possible compromise:
- Force the user back through
/oauth/authorize. - Log the event with the user ID and client ID.
- Consider alerting the user via your normal security channel.
Verifying tokens at the resource API
You don't have to do this yourself for normal calls — connect.securely.io validates the token before processing your request. But if you're building middleware or another service that needs to verify Securely tokens:
| Check | How |
|---|---|
| Signature | RS256 against JWKS at /.well-known/jwks.json |
iss | Equals https://auth.securely.io |
aud | Equals https://api.securely.io |
exp | After "now" (with small clock skew tolerance, e.g. 60s) |
iat | Before "now" |
Updated 5 months ago
What’s Next
Client Credentials Flow — for confidential server-to-server clients.Recipe: Refresh an Access Token Correctly — code in four languages.
