When an API needs stronger authentication
Deleting photos cannot be undone, so the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. wants each deletion to come from someone who signed in recently, with a method a phishingTricking someone, usually with a message and a look-alike sign-in page, into handing over the evidence that opens their account, such as a password or a one-time code. page cannot capture. Only the API sees what a request does, so the step-up challenge lets it tell the client that a tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret.'s sign-in is not enough, and the client fetch a new token whose sign-in is.
A request that needs more
The photo editor at editor.example, a browser application registered as the public clientAn OAuth client, such as code running in a browser or an app on a phone, that cannot keep a credential secret because the people using it can inspect it. It relies on protections such as PKCE instead of a client secret. photo-editor, has a tidy-up tool. It finds blurry shots and duplicates and, when Ava agrees, deletes them from her library. Ava connected the editor with photos.read and photos.delete. She last signed in to the photo service yesterday morning, with her password and a code from her authenticator appSomething a person controls and uses to prove control of an account, such as a memorized password, an app that generates one-time codes, or a security key. It counts only after it has been registered to that account., and the editor has kept the connection working with its refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again. ever since. A copied token, or a laptop left open with yesterday's sessionThe ongoing connection between later requests and an earlier sign-in, so you are not asked for your password on every click. It is often tracked with a cookie holding an unpredictable session identifier., should not be enough to empty an album.
The Authentication policy and SSO lesson described two ways a service responds to a sensitive action: reauthentication, which asks for a fresh check, and step-up authentication, which asks for stronger evidence than the person gave earlier. The photo service needs both here, because a recent sign-in alone proves little when a relay can steal a fresh session, as Limiting what stolen access can do showed. The difference in OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. is where the question comes up.
The deletion arrives at the photo API as an ordinary request with an access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in.. Only the API sees that this request deletes photos, and it may weigh more than the operation itself: how many photos, how quickly, from which kind of client. The authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. authenticated Ava yesterday and has been issuing tokens since, without knowing what any of them would be used for.
The simple alternatives cost too much. Requiring a passkeyA credential that your phone, computer or password manager creates for one website and keeps for you, unlocked with a fingerprint, face or PIN. Nothing you could type is sent, and it works only for the site it was made for, which helps it resist phishing. at every sign-in, or making every token last a few minutes, would make each ordinary visit harder in order to protect a rare one. What the API needs is narrower: a way to learn from the token how and when Ava authenticated, and a way to tell the client that this request needs more.
What a token can say about sign-in
The Authentication policy and SSOSigning in once and then using several applications without entering credentials again, usually because they all rely on the same identity provider. lesson met the two claims that carry this information, auth_time and acr, in OpenID Connect, whose lessons explain them in full, starting with Session age and reauthentication and Authentication context and methods. The JWT access token profile lets an access token carry them too, describing the sign-in behind the authorization.
In an access token they mean what they mean in an ID token. auth_time records when the person last actively authenticated at the authorization server, not when the token was issued. acr names the kind of authentication that took place, using values the authorization server and the API have agreed on. The photo service uses the two classes it already publishes for OpenID Connect sign-in:
| Value | Meaning at the photo service |
|---|---|
urn:photos:acr:two-factor | Two different kinds of factor, such as a password and a code from an enrolled authenticator app. |
urn:photos:acr:passkey | A passkey or security key with user verification, a method a phishing page cannot capture. |
There is no universal ladder of acr values. "Stronger" means only what the parties have agreed, and the word "step-up" is a metaphor for moving from a level the API will not accept to one it will.
All domains, identifiers, codes, and tokens in these examples are fictional. Decoded, the editor's current access token reads:
{
"iss": "https://auth.photos.example",
"sub": "user-2048",
"aud": "https://api.photos.example",
"client_id": "photo-editor",
"scope": "photos.read photos.delete",
"iat": 1790845200,
"exp": 1790845800,
"jti": "demo-token-id-31",
"auth_time": 1790758800,
"acr": "urn:photos:acr:two-factor"
}The token was issued at 09:00 UTC today, when the editor last refreshed, but auth_time is 09:00 yesterday. Tokens obtained by refreshing carry the same auth_time and acr as the sign-in behind the authorization, because no new authentication happened. An API that relies on introspection can receive the same two values in the introspection response.
These claims are for the API. As the Using access tokens lesson explained, the editor treats the token as opaque and does not read them.
Letting the API ask
The OAuth 2.0 Step Up Authentication Challenge Protocol, published as RFC 9470 in September 2023, gives the API a standard way to say that a token's authentication is not enough. The exchange has four steps:
- The API refuses the request with the error
insufficient_user_authenticationand states what it requires: acceptableacrvalues, a maximum age for the sign-in, or both. - The client sends the person back to the authorization server with a new authorization request carrying those requirements.
- The authorization server authenticates them as required and issues a new access token whose
acrandauth_timedescribe that sign-in. - The client repeats the original request with the new token, and the API checks again.
Requesting stronger authentication showed an application deciding for itself to send someone back for a stronger sign-in. Here the API makes that decision, and the client only passes it on. An authorization server that already implements OpenID Connect can take part with little or no change, and it advertises support by listing acr_values_supported in its metadata.
Step-up changes what the API knows, not what the editor knows. The editor still receives an access token, not a statement about who Ava is, and the specification is explicit that this mechanism does not turn OAuth into a sign-in protocol. The API's other checks stay in place as well. A fresh passkey sign-in does not let the editor delete photos in an album that is not hers.
Receiving the challenge
At 09:02 UTC Ava opens the tidy-up view and chooses Delete 38 photos. The editor starts with the first photo on the list and sends the deletion with the token it obtained this morning:
DELETE /photos/9137 HTTP/1.1
Host: api.photos.example
Authorization: Bearer demo-access-token-31The photo API validates the token as usual, with the checks from Token formats and validation. Its signature, issuer, audience, and expiry are fine, and it carries photos.delete. Then the API applies its policy for deletions: acr must be urn:photos:acr:passkey, and auth_time must be no more than 300 seconds ago. The token says two-factor, and its sign-in happened 86,520 seconds ago. The API refuses:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="insufficient_user_authentication",
error_description="A recent passkey sign-in is required",
acr_values="urn:photos:acr:passkey",
max_age="300"The header is split across lines for display. error names the problem: the authentication behind this token does not meet the API's requirements. acr_values lists the acceptable authentication context classes, separated by spaces and in order of preference, and the API will accept any one of them. max_age is the number of seconds allowed since the person's last active authentication: the last time they responded to a sign-in prompt at the authorization server. A session that quietly continues does not count. A challenge can carry either requirement or both, and if the token also lacked a scope, it could include a scope attribute as well. Some APIs name only one missing requirement in each challenge, so a client that meets the first may be challenged again for the second.
The response uses status 401, as an invalid token would, because the remedy is a different token. The error_description is optional text meant for developers. As with other error descriptions, the editor does not insert it into its page as markup.
Asking for a new sign-in
The editor remembers what it was doing, the 38 photos waiting to be deleted, and tells Ava that deleting photos needs a fresh sign-in with her passkey. When she chooses to continue, it builds a new authorization request, copying the two requirements from the challenge exactly as it received them:
https://auth.photos.example/authorize
?response_type=code
&client_id=photo-editor
&redirect_uri=https%3A%2F%2Feditor.example%2Foauth%2Fcallback
&scope=photos.read%20photos.delete
&acr_values=urn%3Aphotos%3Aacr%3Apasskey
&max_age=300
&state=demo-attempt-31
&code_challenge=gmBRnRQIy0EQKWg3CemaN64l-xOox_nHZAphyPcG6hI
&code_challenge_method=S256Everything else is an ordinary authorization request, with a fresh state and PKCE challenge for this attempt. The requirements are the only new part, and they travel in two OpenID Connect parameters that mean exactly what they mean there: max_age, from Session age and reauthentication, and acr_values, from Authentication context and methods.
At the photo service, Ava still has yesterday's session. On an ordinary visit, the server would recognize her and continue without a prompt. Yesterday's sign-in is far older than 300 seconds, so the server asks her to authenticate again, and it prompts for her passkey because that is the kind of sign-in acr_values names, much as Requesting stronger authentication described. Ava uses her passkey at 09:03:00.
Under the step-up specification, the authorization server should treat the requested acr as a requirement, not a preference. It puts that value in the new access token only when the sign-in actually met it. If the sign-in falls short, the request fails instead of producing a token the API would refuse.
Ava's browser returns to https://editor.example/oauth/callback with a code, the attempt's state, and the iss parameter. The editor checks them as it would for any callback, then exchanges the code with its PKCE verifier. It is a public client, so it sends its client ID and no client credential.
Retrying with the new token
The new access token, decoded, describes this morning's passkey sign-in:
{
"iss": "https://auth.photos.example",
"sub": "user-2048",
"aud": "https://api.photos.example",
"client_id": "photo-editor",
"scope": "photos.read photos.delete",
"iat": 1790845385,
"exp": 1790845985,
"jti": "demo-token-id-32",
"auth_time": 1790845380,
"acr": "urn:photos:acr:passkey"
}auth_time is 09:03:00, and the token was issued five seconds later. The editor does not decode the token to confirm any of this. It repeats the request it was trying to make and lets the API decide:
DELETE /photos/9137 HTTP/1.1
Host: api.photos.example
Authorization: Bearer demo-access-token-32HTTP/1.1 204 No ContentThe API runs the same checks as before. At 09:03:10, the sign-in is 10 seconds old, well inside the 300 allowed, and its acr is one the API accepts. The photo is deleted, and the editor continues with the other 37.
The new sign-in will not stay fresh. At 09:08:00 it turns five minutes old, and from then on this token no longer meets the deletion policy, although it still works for browsing until it expires. If the tidy-up were still running then, the API would challenge again, and the editor would have to decide whether to ask Ava for another sign-in or stop.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Read how Ava signed in, then follow a step-up challenge to a retryRead auth_time and acr in an everyday token, have the sample photo API refuse a deletion with insufficient_user_authentication, confirm with Ava's passkey, and retry with the new token.