Sign in with your college account
The college buys a new Timetable app. It shows a button: Sign in with your college account. You press it, type your password on the college's own page, and land back in the Timetable, signed in.
The Timetable never saw your password. OAuth 2.0 is the set of rules that made that work. It lets one app get a limited token from a service you already trust.
Only your own lab
Warning
The login CSRF attack in this lesson is against the weak Noticeboard in your own lab, which has no way out to the internet. On a system you do not own it needs written permission. Without it, it is a crime in the UK under the Computer Misuse Act 1990.
By the end of this lesson
Note
I can follow an OAuth 2.0 authorisation code flow with PKCE and say what each step proves.
Starter
Have a go
Starter
3 questionsKey words
| Word | What it means | Example |
|---|---|---|
| Authorisation server | the service that signs people in and issues tokens | http://idp:5556/dex |
| Client | the app that wants a token | |
| Authorisation code | a one-use value swapped for tokens | |
| PKCE | a secret the client proves it holds when it swaps the code | |
| State | a random value tying the answer to one browser | |
| Client credentials | a grant where a service asks for a token for itself |
The cells students fill in are left blank in this preview.
Four roles and two channels
The authorisation server here is Dex, the lab's identity provider. The client is the Timetable. You are the resource owner, and an API holding your data is the resource server.
The flow uses two channels. The front channel is your browser, sent from site to site by redirects, where anything can be seen or changed. The back channel is the client talking straight to the token endpoint. Tokens travel only on the back channel.
The authorisation code flow
flowchart TB
A["App sends the browser to /auth with state and code_challenge"] --> B["Provider shows its own sign-in form"]
B --> C["Browser returns to the redirect_uri with a code and the state"]
C --> D["App swaps the code and code_verifier at /token"]
D --> E["Provider answers with tokens"]Run the code flow as Amira
docker exec tools python3 steps/flow.py amiraShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsPKCE proves the same client finishes
A code passes through the browser, so it can leak. PKCE, proof key for code exchange, makes a stolen code useless. The client makes a random code_verifier and keeps it. It sends only code_challenge, the base64url of its SHA-256 hash, with the sign-in link.
At the token endpoint it sends the verifier itself. The provider hashes it and compares. Only the client that started the flow knows the verifier.
Make a challenge from a verifier
docker exec tools python3 steps/pkce.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: the example from the PKCE standard
import base64
import hashlib
verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
digest = hashlib.sha256(verifier.encode()).digest()
challenge = base64.urlsafe_b64encode(digest).rstrip(b"=").decode()
print("verifier ", verifier, len(verifier))
print("challenge", challenge)
# Change one letter of the verifier. Does the challenge change a little or completely?
Make a free account to save your work and track your progress.
Three things the provider refuses
docker exec tools python3 steps/refused.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think PKCE is only for phone apps that cannot keep a secret. It also stops a stolen code being used by a different session of a confidential app. Current OAuth advice, RFC 9700, recommends PKCE for every client.
State ties the answer to one browser
Without a check, anyone can hand a browser a valid callback link. In login CSRF, Dan signs in as himself, stops before his own browser opens the callback, and gets Amira's browser to open it. Amira is now signed in to Dan's account, and anything she types goes to him.
The weak copy sends no state and no PKCE. The safe copy keeps a random state in the session and refuses a callback whose state does not match.
Login CSRF on the weak copy
docker exec tools python3 steps/logincsrf.py weakShown here, not run. Press Start lab to type it into your own lab’s terminal.
The same on the safe copy
docker exec tools python3 steps/logincsrf.py safeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Other grants, and two to avoid
A service with no person behind it, such as a printing service, uses client credentials. It proves who it is with its own secret and gets a token for itself. There is no browser and no refresh token.
Two older grants are best avoided. The implicit grant put the token in the browser's address bar. The password grant had the app collect your password. RFC 9700 says not to use either.
A token for the Printers service
docker exec tools python3 steps/machine.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsFixing it and spotting it
The fixes: a random state checked at the callback, PKCE on every flow, and redirect URIs matched exactly. Use the code flow, never the implicit or password grants. A code is used once and lives for a minute or so.
To spot trouble, log refused callbacks and invalid_grant answers. Many from one place suggest someone replaying codes. The safe copy logs "refused a callback whose state does not match this browser".
Assignment: PKCE and the callback
Write three functions a client needs for the code flow.
challenge(verifier) returns the S256 code challenge: the SHA-256 hash of the verifier, in base64url with the = padding removed.
good_verifier(verifier) returns True when the verifier is 43 to 128 characters long and every character is a letter, a digit or one of -, ., _ and ~.
check_callback(params, sent_state) reads the query of the callback as a dictionary. Return "error: " and the value when there is an error; otherwise "no state", "state mismatch" or "no code" for the first that applies, and "ok" when none does.
def challenge(verifier):
return verifier
def good_verifier(verifier):
return True
def check_callback(params, sent_state):
return "ok"
Make a free account to save your work and track your progress.
Exit quiz
Have a go