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
| Word | What it means | Example |
|---|---|---|
| OpenID Connect | an identity layer on top of OAuth 2.0 | |
| ID token | a signed statement of who signed in, for the client | |
| Claim | one fact inside a token | "email": "[email protected]" |
| Discovery document | the provider's list of its own addresses and abilities | |
| JWKS | the provider's public keys, published as JSON | |
| Nonce | a 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 questionsAsk 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
curl -s http://idp:5556/dex/.well-known/openid-configurationShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: five lines that matter
| Field | Dex said | A client uses it to |
|---|---|---|
| issuer | http://idp:5556/dex | Check iss in every token |
| token_endpoint | http://idp:5556/dex/token | Swap the code |
| jwks_uri | http://idp:5556/dex/keys | Fetch the public keys |
| id_token_signing_alg_values_supported | RS256 | Know which algorithm to expect |
| code_challenge_methods_supported | S256, plain | Choose S256 for PKCE |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsAn 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
docker exec tools python3 steps/idtoken.py amiraShown here, not run. Press Start lab to type it into your own lab’s terminal.
Worked example: Amira's ID token
| Claim | Value | What the client checks |
|---|---|---|
| iss | http://idp:5556/dex | Equals the discovery document's issuer |
| aud | timetable | Is this client's own id |
| [email protected] | Shown, never used as the key | |
| sub | CiQ3ZDFlMmYz... | The id to store for the person |
| exp - iat | 600 seconds | Still in the future |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsTry it: decoding is not checking
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
curl -s http://idp:5556/dex/keysShown 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
docker exec tools python3 steps/userinfo.py amiraShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsAssignment: 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"whenissis notissuer."aud"whenaud(a string or a list) does not holdclient_id."azp"whenaudis a list of more than one andazpis notclient_id."exp"whenexpis 60 seconds or more beforenow: allow 60 seconds for clocks that differ."iat"wheniatis more than 60 seconds afternow."nonce"whennonceis not the one given.
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