Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

The UserInfo endpoint

After a sign-in, the access token that came with the ID token 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 document, and it calls the endpoint like any API that accepts bearer tokens:

GET /userinfo HTTP/1.1
Host: auth.photos.example
Authorization: Bearer demo-access-token-12

The 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 HTTPS, and the collage app checks the server's certificate as it does for every call that carries a token. Here that check does a second job. The response is plain JSON with no signature, 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 party can instead register to receive the response as a JWT, signed by the provider, encrypted for the relying party, or both. A signed response names its issuer and audience and is checked much like an ID token. Signed and encrypted messages, in Advanced OIDC, 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 tokenUserInfo response
ArrivesIn the token response.From a separate call with the access token.
Addressed toThe collage app, named in aud.Whoever presents the access token.
Protected byThe provider's signature.HTTPS, unless a signed response was registered.
Tied to this attemptBy 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.

UserInfo answers about whoever owns the access token, so its sub must match the ID token before any claim is used.

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.

The ID token's sub is user-2048, but the UserInfo response says user-2049. What should the collage app do? 1 of 4

Choose one answer.

The collage app wants to show Ava's name on every page. Why should it not call UserInfo on each page load? 2 of 4

Choose one answer.

Ava is a returning customer. The collage app validates her ID token, but its call to the UserInfo endpoint fails because the photo service is briefly unavailable. What should the collage app do? 3 of 4

Choose one answer.

The collage app calls UserInfo with an access token from an ordinary photo connection, issued for photos.read alone. What is the likely answer? 4 of 4

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity