Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

OpenID Connect and the ID token

After this lesson: I can check an ID token the way the OpenID Connect rules require.

60 min lesson4 commandsIdentity and access lab: OpenID Connect, OAuth 2.0 and JWTs

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

A token is not a name

After the code flow, the Timetable holds an access token. That token lets it call an API. It does not, by itself, tell the Timetable who you are.

Early apps guessed, by calling some API and reading back a user id. That went wrong in many ways. OpenID Connect is the layer on top of OAuth 2.0 that answers "who signed in?" in one agreed way.

By the end of this lesson

Note

I can check an ID token the way the OpenID Connect rules require.

Key words

WordWhat it meansExample
OpenID Connectan identity layer on top of OAuth 2.0
ID tokena signed statement of who signed in, for the client
Claimone fact inside a token"email": "[email protected]"
Discovery documentthe provider's list of its own addresses and abilities
JWKSthe provider's public keys, published as JSON
Noncea random value the client sends and finds again in the ID token

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Ask for openid and get an ID token

A client asks for OpenID Connect by putting openid in the scope, often with email and profile. The provider then returns an ID token beside the access token.

Every provider publishes a discovery document at a fixed address: its issuer name, its endpoints, the scopes and the signing algorithms it supports. A client reads it once, instead of being told each address by hand.

Read Dex's discovery document

Command
curl -s http://idp:5556/dex/.well-known/openid-configuration

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: five lines that matter

FieldDex saidA client uses it to
issuerhttp://idp:5556/dexCheck iss in every token
token_endpointhttp://idp:5556/dex/tokenSwap the code
jwks_urihttp://idp:5556/dex/keysFetch the public keys
id_token_signing_alg_values_supportedRS256Know which algorithm to expect
code_challenge_methods_supportedS256, plainChoose S256 for PKCE

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

An ID token is a signed JWT

An ID token is a JWT: a header, a set of claims and a signature, each in base64url, joined by dots. The header names the algorithm and kid, which key signed it.

Six claims matter. iss says who issued it and aud which client it is for. sub is the provider's fixed id for the person. exp and iat say when it ends and began, and nonce is echoed from the sign-in request.

Get Amira an ID token and check it

Command
docker exec tools python3 steps/idtoken.py amira

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: Amira's ID token

ClaimValueWhat the client checks
isshttp://idp:5556/dexEquals the discovery document's issuer
audtimetableIs this client's own id
email[email protected]Shown, never used as the key
subCiQ3ZDFlMmYz...The id to store for the person
exp - iat600 secondsStill in the future

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try it: decoding is not checking

Code: python
import base64
import json

# A made-up payload, encoded the way a JWT encodes it.
claims = {"iss": "http://idp:5556/dex", "aud": "timetable", "email": "[email protected]"}
part = base64.urlsafe_b64encode(json.dumps(claims).encode()).rstrip(b"=").decode()
print("encoded:", part[:40] + "...")

# Anybody can turn it back: base64url is not encryption.
back = json.loads(base64.urlsafe_b64decode(part + "=" * (-len(part) % 4)))
print("decoded:", back)
# Change "aud" to "noticeboard" and run it. Did anything stop you?

Make a free account to save your work and track your progress.

Six checks before you believe it

First a client checks the signature, with the key from the JWKS that kid names. Then iss must be the provider and aud must hold its own client id. exp must be in the future and nonce the one it sent. Last, at_hash must match the access token.

Fetch the JWKS again when a token names a kid you have not seen, because providers change keys. idtoken.py made all six checks by hand.

Read the provider's public keys

Command
curl -s http://idp:5556/dex/keys

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think reading the claims of an ID token is the same as checking it. Anybody can decode base64url and anybody can write a token. Believe a token only after its signature, issuer, audience, expiry and nonce all pass.

The UserInfo endpoint

A client can also ask the provider who an access token is for, at the UserInfo endpoint, sending the token as Authorization: Bearer. It answers with claims. Dex answers with the same claims as the ID token.

The ID token is for the client. The access token is for an API, and the client should treat it as an opaque string it passes on.

Ask UserInfo about Amira

Command
docker exec tools python3 steps/userinfo.py amira

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Assignment: the claim checks

The signature is already checked. Write claim_problems(claims, issuer, client_id, nonce, now): it gets an ID token's claims as a dictionary and returns a list of what is wrong, in this order, each at most once.

  • "iss" when iss is not issuer.
  • "aud" when aud (a string or a list) does not hold client_id.
  • "azp" when aud is a list of more than one and azp is not client_id.
  • "exp" when exp is 60 seconds or more before now: allow 60 seconds for clocks that differ.
  • "iat" when iat is more than 60 seconds after now.
  • "nonce" when nonce is not the one given.
Code: python
def claim_problems(claims, issuer, client_id, nonce, now):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.