Lesson 2 · Unit 03
OAuth
An app acts for a user without the password: codes, PKCE, scopes and two tokens
What you practise
- Letting an app act for a user without their password: the authorization-code flow with PKCE step by step, scopes, access and refresh tokens, machine-to-machine credentials
Try a question from this lesson
Pick one
A print shop app wants your photos from PhotoCloud. The simple way: you type your PhotoCloud password into the print app. What can the print app do now?
Correct answer
Everything you can: read, delete, change the password
Why
A password is all-or-nothing and has no expiry. The print app could delete your albums, and the only way to take access back is to change the password, which cuts off everything else too. OAuth exists to replace this.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Four roles
OAuth lets an app act for you without your password.
You (the resource owner) log in at the authorization server, never at the app. It asks you what the app (the client) may do, then hands the app a token. The app shows that token to the API (the resource server), which checks it and answers.
you ──log in, approve──> auth server
app <────── token ────── auth server
app ─────── token ─────> photos APIConcept
PKCE: prove you started it
An app on a phone or in a browser cannot keep a secret. So how does the server know that whoever redeems the code is the app that asked for it?
The app invents a random verifier, sends only its hash (the challenge) with the first request, and reveals the verifier when it trades in the code. A stolen code is useless without the verifier.
verifier = random string, kept by the app
challenge = BASE64URL(SHA256(verifier))Concept
Two tokens
The access token is what the API sees on every call. It is short-lived, so a leaked one soon dies.
The refresh token goes only to the authorization server, to get a new access token without bothering the user. It lives longer, can be revoked, and is the one to guard.
{
"access_token": "eyJhbGciOi...",
"token_type": "Bearer",
"expires_in": 900,
"refresh_token": "8xLOxBtZp8",
"scope": "photos.read"
}9 more exercises in the app
- 1× match the pairs
- 4× multiple choice
- 1× fill the blanks
- 2× type the answer
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.