Cryptography / OpenID Connect

OpenID Connect

How “Sign in with…” works: the OAuth 2.0 flow plus one scope, an ID token that says who signed in, the checks an app must make, and how that token differs from an access token.

Intermediate7 steps~7 min
Ana — end-user: the person signing in
Print App — relying party: needs to know who she is
OpenID provider — signs Ana in and issues the tokens
ID token — signed statement about the sign-in
// THE SHORT VERSION

In 30 seconds

OpenID Connect is how many “Sign in with…” buttons work. It adds a small identity layer on top of OAuth 2.0.

The app runs the usual OAuth flow with the scope openid. Alongside the access token it receives an ID token: a signed JSON Web Token whose claims say who signed in, at which provider, for which app and until when.

The app verifies that token before trusting it, and can fetch more profile details from the provider’s userinfo endpoint. The ID token is for the app; the access token is for APIs.

OpenID Connect, step by step

STEPS01 / 07

Shown with the authorization code flow, the usual choice. Token strings and claim values are examples. A real app fetches the provider’s public keys from its published jwks_uri to check the signature.

STEP 01 / 07 An identity layer on OAuth
THE MATH
OIDC = OAuth 2.0 + ID token + userinfo endpoint
  1. Step 1: An identity layer on OAuth. OAuth 2.0 tells an app what it may do, not who is using it. OpenID Connect (OIDC) is a thin layer on top that adds the missing answer in a standard form. The flow and the parties stay the same under new names: the user is the end-user, the app is the relying party, and the authorization server is the OpenID provider.
  2. Step 2: One extra scope: openid. The app starts an ordinary authorization code flow but includes the scope openid, which marks the request as a login. The extra scopes profile and email ask for her name and address. The app also sends a random nonce that must come back inside the token. Ana signs in at the provider and approves.
  3. Step 3: Two tokens come back. The code returns through the browser and the app exchanges it at the token endpoint, exactly as in OAuth. The response now holds two tokens: the usual access token and a new ID token. The ID token is the provider’s signed statement that Ana just signed in, addressed to this app.
  4. Step 4: Inside the ID token. The ID token is a JSON Web Token: header, payload and signature. Its payload uses standard claim names. iss says who issued it, sub is a stable identifier for Ana at that provider, aud names the app it is meant for, and exp and iat bound its lifetime. Claims such as name and email appear because of the scopes requested.
  5. Step 5: Check before you trust. The app must not accept an ID token unchecked. It verifies the signature with the provider’s published public key, then checks that the issuer is the expected one, the audience is its own client ID, the token has not expired and the nonce matches this login. Only then does it start a session for the user named by iss and sub.
  6. Step 6: Ask userinfo for more. The provider also runs a userinfo endpoint, an ordinary protected API. The app calls it with the access token and gets the user’s claims back as JSON. That keeps the ID token small and lets the app fetch fresh details later. The sub it returns must equal the sub in the ID token.
  7. Step 7: ID token vs access token. The two tokens have different readers. The ID token is for the app: it reads and verifies it, then does not send it on to APIs. The access token is for APIs: the app passes it along without relying on what is inside. Using an access token as proof of login, or an ID token to call an API, is a classic mistake.
VALUES SO FAR
scope····asked
nonce····random
access····for API
id_token····for app
sub····claim
aud····claim
exp····claim
name····userinfo

Spotted a mistake on this page?