The UserInfo endpoint
After a sign-in, the 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. that came with the ID tokenIn OpenID Connect, a signed JWT from the provider, addressed to one client, saying which account authenticated, when, and in answer to which request. It reports a sign-in and is not used to call APIs. can fetch the person's claims from the provider. This lesson calls the UserInfo endpoint, checks that the answer describes the same person as the ID token, and decides when calling it is worth a request.
Asking the provider
The collage app has validated Ava's ID token. It knows that user-2048 at the photo service authenticated at 08:59:45, and it still holds the other half of the token response: the access token demo-access-token-12, issued for openid profile email and good for ten minutes. That token is what unlocks her name, picture, and email address. All domains, identifiers, and tokens in these examples are fictional.
The UserInfo endpoint, which Providers and relying parties listed beside the roles, is not a party of its own. It is a protected resource that the provider hosts. It returns claims about the person an access token was issued for. The collage app took its address from the userinfo_endpoint field of the photo service's discovery documentA JSON document that an OpenID Provider publishes at a well-known address under its issuer, listing its endpoints, where to find its signing keys, and its supported features so clients can configure themselves from one value., and it calls the endpoint like any 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. that accepts bearer tokens:
GET /userinfo HTTP/1.1
Host: auth.photos.example
Authorization: Bearer demo-access-token-12The endpoint must accept both GET and POST. GET with the token in the Authorization header is the recommended form, and for the reasons Using access tokens gave, the token never goes in the query string. The connection uses HTTPSHTTP protected by TLS, so traffic is encrypted and checked for changes while it travels. It protects the connection, but does not prove that the site at the other end is honest., and the collage app checks the server's certificateA signed statement that a public key belongs to a name, such as a website's hostname, for stated purposes and a limited period. A certificate authority signs it, so software that trusts the authority can connect the key to the name. as it does for every call that carries a token. Here that check does a second job. The response is plain JSONA text format for structured information, written as named values inside curly braces. APIs, tokens, and discovery documents all use it. with no signatureA value made with a private key that lets anyone holding the matching public key check that a message came from the key's holder and has not been changed. It proves origin and integrity; it does not hide the message., so the certificate is the collage app's only evidence that the answer came from the photo service.
HTTP/1.1 200 OK
Content-Type: application/json
{
"sub": "user-2048",
"name": "Ava Archer",
"given_name": "Ava",
"family_name": "Archer",
"picture": "https://photos.example/avatars/user-2048.jpg",
"locale": "en-US",
"updated_at": 1788220800,
"email": "[email protected]",
"email_verified": true
}These are the claims from Standard and custom claims, arriving where the collage app asked for them. profile requested fourteen claims and the photo service returned the six it holds. email requested two, and both are here. sub is always present, whatever was requested.
A relying partyAn application that accepts and relies on a sign-in from another service, the name used in OpenID Connect and for passkeys. SAML calls the same role a service provider. can instead register to receive the response as a JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing., signed by the provider, encrypted for the relying party, or both. A signed response names its issuerThe party that creates and signs a token, assertion, or certificate, named in a token's iss claim. A recipient must check that the issuer is exactly the one it expected and trusts. and audience and is checked much like an ID token. Signed and encrypted messages, in Advanced OIDCAn identity layer on top of OAuth 2.0 that lets an application sign people in with an account at another service. It adds the ID token, standard claims, and discovery to the OAuth exchange., covers both. The collage app uses plain JSON over HTTPS.
When the endpoint refuses a request, it answers as any bearer-token API does, with a WWW-Authenticate header. Called with a token that has expired, it might reply as follows, with the header wrapped here for display:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token",
error_description="The access token expired"The error codes mean what Using access tokens described. invalid_token covers a token that is expired, revoked, or otherwise unacceptable, and invalid_request a malformed request. UserInfo expects an access token issued through an OpenID Connect sign-in. A token from an ordinary photo connection, issued for photos.read alone, is likely to be refused, often with 403 and insufficient_scope.
Matching the subject
Before using any of those claims, the collage app compares the sub in the response with the sub in the ID token it validated. They must match exactly, character for character, with no trimming and no change of case. If they differ, the collage app must not use any value from the response.
The rule exists because the two answers prove different things:
| ID token | UserInfo response | |
|---|---|---|
| Arrives | In the token response. | From a separate call with the access token. |
| Addressed to | The collage app, named in aud. | Whoever presents the access token. |
| Protected by | The provider's signature. | HTTPS, unless a signed response was registered. |
| Tied to this attempt | By the nonce. | Only through the access token the collage app chose to send. |
The UserInfo response describes whoever the access token belongs to. The collage app's only link between that token and Ava's ID token is that they arrived together. If something breaks that link, the response can describe someone else entirely.
The specification has an attack in mind, called token substitution, in which someone swaps a token from one session into another. Flows that deliver an access token through the browser, which Response types, response modes, and older flows describes, make the swap possible. Suppose an attacker replaced the access token in Ava's sign-in with one issued for their own photo account. The ID token would still name Ava, so the collage app would correctly sign her in. Without the comparison, it would then fill in Ava's collage account with the attacker's name and email address, and her receipts, along with anything else the collage app sends by email, would go to the attacker.
The collage app's own flow is better protected. Both tokens come back together from its direct request to the token endpoint, and nobody in the browser can swap them. Mistakes inside the collage app can still pair them wrongly: a cache keyed by the wrong value, a worker that picks up the most recent token response instead of this attempt's, or a retry that reuses a token from someone else's sign-in. If a worker picked up Ben's access token by mistake, UserInfo would answer truthfully about Ben, with his sub, user-3172, while the ID token still names user-2048. The comparison catches all of these:
userinfo = get(provider.userinfo_endpoint, bearer=attempt.access_token)
if userinfo.sub != id_token.sub: # exact, case-sensitive comparison
record("userinfo_subject_mismatch", provider.issuer, correlation_id)
fail_sign_in()
copy_wanted_claims(account, userinfo)Ava's access token
- ID token sub
- user-2048
- UserInfo sub
- user-2048
copy_wanted_claims(account, userinfo)
Ben's access token by mistake
- ID token sub
- user-2048
- UserInfo sub
- user-3172
Must not use any value from the response
They must match exactly, character for character, with no trimming and no change of case.
The specification requires only that the values not be used. A mismatch means an attack or a bug, though, not a missing claim to work around, so this collage app stops the sign-in and shows a general error. Its record names the stage, the issuer, and a correlation ID, and leaves out the tokens and the claims.
The comparison is meaningful only within one provider. A plain UserInfo response does not name its issuer, so the collage app knows which provider answered only because it chose the endpoint. It must call the UserInfo endpoint from the configuration of the issuer whose ID token it validated. A sign-in with the mail service uses the mail service's endpoint, and the sub it returns should be a94f1c07e2.
When to call it
UserInfo looks a lot like the /me endpoint in From delegated access to sign-in, and it is easy to use it in the same way. The difference is the order. The collage app calls UserInfo only after it has validated an ID token, and only to fill in details about the person that token names. A UserInfo response is never a sign-in. It does not say that the access token was issued to the collage app, when the person authenticated, or which attempt it belongs to. If a token response arrived without an ID token, there would be nothing to compare the response with, and the collage app would treat the sign-in as failed, as Authentication errors described.
The natural moment is during sign-in, right after validation, while the access token is minutes old. The collage app copies the claims it needs into Ava's account and moves on. It does not call UserInfo on every page to show her name. That would add a request to another service to every page the collage app serves, slow the collage app down whenever the photo service is slow, and stop working ten minutes later when the access token expires. This sign-in came with no refresh token, so the collage app could not obtain another access token without sending Ava back to the photo service.
If the call fails during sign-in, perhaps because the photo service is briefly unavailable, the sign-in itself can still succeed. The ID token has already established who Ava is. A returning customer keeps the details the collage app stored last time, and a first-time customer is asked for whatever the collage app cannot do without. The collage app tries again at her next sign-in.
Ava's details can change between sign-ins, and the collage app's copies catch up the next time she signs in, which is good enough for a greeting and a picture. A relying party that needs current claims while the person is away has to ask for continuing access, which Offline access and refresh behavior describes. Claim stability and privacy looks at how the collage app keeps its copies current, and how little it needs to keep at all.
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
- Call UserInfo every allowed way and refuse a response for the wrong personCall UserInfo with GET and POST, read each refusal from WWW-Authenticate, and add the subject comparison that stops a mispaired access token from filling Ava's account with Ben's details.