TL;DR
- The identifier (ID) token tells the app who signed in. The access token is for the service being called.
- The app gets the tokens in a second step, after the user returns from the sign-in page.
- The app must refuse a token whose audience is another app, even when its signature is valid.
Every time an app says “sign in with” and sends you to another page, something is handing that app a proof of who you are. Most of the time that something is OpenID Connect. It adds sign-in on top of OAuth, and it defines the token the app reads. This explainer covers what that token contains and what an app must check before it believes it.
Access versus identity#
OAuth answers one question: may this app call this service for this user? The answer is an access token. The service checks the access token, and the app should treat it as opaque. Its content is for the service, not for the app.
OpenID Connect answers a second question: who just signed in? The answer is an ID token. The app reads the ID token, and nothing else should. If you want the three tokens side by side, access, refresh and ID tokens puts them in one table of jobs.
What the ID token contains#
The ID token is a JWT, a signed JSON document. It carries claims about the sign-in. Five matter most. The issuer names the provider that signed the token. The subject is a stable identifier for the user at that provider. The audience must be the client ID of your app. The expiry time says when the token stops being valid. The issued-at time says when the provider created it.
The app asks for the ID token by including the openid scope in the sign-in request. Other scopes such as profile and email add more claims, such as a name or an email address. Scopes and consent explains what the user agrees to when those scopes appear.
The sign-in, step by step#
The app sends the browser to the provider, with the openid scope in the request. The user signs in there and agrees to share what the app asked for. The provider sends the browser back to the app with a short code, not with the tokens. The app’s server then sends that code to the provider’s token endpoint, and gets the tokens back in a direct request.
The diagram follows the same path. It also shows the check that stops a bad token, which is the step to remember.
What the app must check#
Before the app trusts an ID token, it checks the signature against the provider’s published keys. It checks the issuer, the audience and the expiry time. An ID token whose audience is another app was issued for that app. The app should refuse it, even if the signature is valid. A valid token for the wrong app is still the wrong token.
Two mix-ups to avoid#
The first mix-up is sending the ID token to an API. An API should receive an access token, because the access token is the one meant for the service. The ID token is for the app that signed the user in.
The second mix-up is treating the subject as an email address. The subject is an identifier, so use it as a key in your database. Show the email to the user from its own claim, and expect it to change.
Where to go next#
OAuth 2.0 flows explained shows which flow carries this sign-in, and why the browser redirect matters. MFA types and passkeys covers how the provider decides that the person signing in is the user.
Sources#
This post as Markdown · RSS · JSON feed
New posts by email
One email a week with new posts and labs. Unsubscribe anytime.
Your address is stored with Kit, which sends the emails. Details