Protecting APIs with Apinx-issued tokens¶
How-to guideThe steps for one task.
Apinx can issue access tokens to your own applications: you
give an application a client ID and a client secret, the application trades them for a
short-lived token at the gateway's token endpoint, and every API that binds the Apinx access
token policy (oauth2) accepts that token. There is no identity provider to run and nothing to
configure on the policy.
Use it for applications: a mobile app, a partner's backend, an internal job. When people sign in (passwords, single sign-on, MFA), keep them in your identity provider and accept its tokens with the JWT policy. Both can be bound on one API.
Three steps¶
- Consumers → your application → Client credentials → Issue the first secret. The secret
is shown once, with the application's client ID and two ready-made
curllines. - Bind the Apinx access token policy on the API (or on some operations), and publish.
- Send a test request. In the operation's Try it, choose Token for application… in Authorization: the console adds a five-minute token of that application.
How an application gets a token¶
curl -u "$CLIENT_ID:$CLIENT_SECRET" -d grant_type=client_credentials https://api.example.com/oauth2/token
{"access_token": "eyJ…", "token_type": "Bearer", "expires_in": 3600,
"refresh_token": "apr_…", "refresh_token_expires_in": 2592000, "scope": "orders:read"}
Then it calls your APIs with the token:
The client ID and secret may also go in the form body (client_id=…&client_secret=…) for
clients that cannot send the Basic header. Any OAuth 2 client library that supports the client
credentials grant works as it is. In Apinx Cloud the token endpoint is under your environment's
address, like your APIs: https://acme.apinx.io/oauth2/token.
Refresh tokens and sign-out¶
Every token answer carries a refresh token, so an app sends its secret once per install and then keeps itself signed in:
curl -d grant_type=refresh_token -d client_id="$CLIENT_ID" -d refresh_token="$REFRESH" https://api.example.com/oauth2/token
The answer is a new access token and a new refresh token. Each refresh token works once:
keep the newest one and drop the one you sent. Refresh a few minutes before expires_in runs
out, or when a call is refused with 401.
If a refresh token that was already used comes back, the gateway cannot tell whether the app or
someone who copied it sent it, so it ends that install's session: both lose it, and the app
signs in again with its secret. A repeat within 30 seconds of the first use is treated as a retry
after a lost answer on a mobile network and gets a fresh pair instead. The answer to a reused
token is invalid_grant with apinx_code APX-PL-0034, and the session shows token used
twice on the application's page.
To sign out, the app ends its session:
A session ends on its own when it is not refreshed for 30 days, and 90 days after it started whatever happens. Both lifetimes, the access token's lifetime (1 hour) and whether refresh tokens are issued at all are set per environment in Admin → Access tokens.
Sessions¶
Each install signed in with a secret is a session, listed under the application's client credentials with when it started, when it was last refreshed and when it ends. Revoke ends one; Revoke all sessions ends every one. Their access tokens are refused from the next request, not at their expiry. Sessions at most on the application caps its live sessions: at the cap a new sign-in ends the oldest.
In Apinx Cloud a plan bounds the live sessions of an environment (the Free plan: 1,000). At the
limit a new sign-in is refused with APX-SAAS-0065; installs already signed in keep refreshing.
Scopes¶
Edit an application to list the scopes it may ask for, such as orders:read orders:write.
A token carries them all unless the request asks for fewer (scope=orders:read); a refresh may
narrow them, never widen them. Require a scope on an operation with the policy's
required_scopes:
Rotating secrets and the signing key¶
Rotate on a secret issues a replacement and keeps the old one working for the overlap you choose. Sessions belong to the secret that started them: they keep refreshing during the overlap and end when it does, so every install still holding only the old secret must sign in again with the new one. For a mobile app, choose an overlap long enough for the app update that carries the new secret to reach its users (the overlap may be up to 30 days). Revoke ends the secret and every session it started at once. An application may have five active secrets.
The gateway signs tokens with an RSA key of the environment, sealed in the store with your master key. Admin → Access tokens → Rotate signing key (administrators) makes a new key sign at once; the previous one keeps verifying until every token it signed has expired.
Moving an application from API keys to tokens¶
Bind api-key and oauth2 together. Authentication policies bound together are alternatives:
a request needs one credential that one of them accepts, so the old app version keeps sending
its key while the new one sends tokens. A credential that is present and wrong is refused at once;
a request with neither gets 401 naming both ways in. When every install has moved, remove the
api-key binding.
What the backend receives¶
As with an API key: X-Apinx-Application and X-Apinx-Application-Id, X-Apinx-Subject
(the application's id) and X-Apinx-Auth: oauth2. The token is removed first (strip: true).
The application is the caller's identity for rate limits, quotas, products and plans and the
application's dashboard.
Verifying a token in the backend¶
A backend that wants to check the token itself (strip: false) reads the environment's
authorization server metadata:
It names the issuer, the token and revocation endpoints and jwks_uri, the key set at
/.well-known/jwks.json. Tokens are RS256 JWTs with sub (the application's id), client_id,
scope, exp and iat. On premises, set proxy.public_url to the address callers use, so the
issuer is fixed; without it, each token names the address its request arrived on.
Security notes¶
- On premises, put the token endpoint behind TLS, as API keys already require: a secret sent in clear can be read on the way.
- Secrets and refresh tokens are random 256-bit values, stored only as keyed hashes and shown once. They never appear in exports, traces, logs or the audit; token requests are never traced.
- Ten failed sign-ins in a minute block that client ID and that address for the rest of the
minute (
APX-PL-0036). - A secret built into a mobile app can be extracted from it, and a refresh token stored in plain text on a rooted phone is a copy of that session. That is why sessions are listed and revocable one by one, why a copied refresh token used twice ends its session, and why tokens are short. Where you need to know the person, not only the app, sign people in with your identity provider and accept its tokens with the JWT policy.
Reference¶
- Policy:
oauth2(Apinx access token) - Errors:
APX-PL-0033(the token endpoint refused the request, with the OAutherror),APX-PL-0034(refresh token reused),APX-PL-0035(signing key unavailable),APX-PL-0036(too many attempts),APX-SAAS-0065(session limit) in the error reference. - The paths
/oauth2/and/.well-known/belong to the gateway: no API can be published under them.