You
Own the photos and decide which apps may use them.
A beginner's guide to OAuth 2.0
OAuth is what runs behind "Connect your account" and "Sign in with" buttons. This guide takes you from zero to reading a real authorization request. You will step through the full flow, build a request, decode a token and finish with a short quiz.
This is a real-looking OAuth request. By chapter 4 you will be able to read every part of it.
https://auth.snapshelf.example/authorize ?response_type=code &client_id=printly-web-4821 &redirect_uri=https%3A%2F%2Fprintly.example%2Fcallback &scope=photos.read%20profile &state=af0ifjsldkj &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM &code_challenge_method=S256
Imagine Printly is a service that prints photos, and your photos live at Snapshelf. Printly needs to read them. The old way to make that work was to give Printly your Snapshelf email and password.
That works, and it is a bad deal. Printly can now do everything you can do, including deleting every photo or changing your password. It keeps that power until you change the password, which also breaks every other app you trusted. Snapshelf cannot tell Printly's requests from yours.
OAuth replaces the password with a pass. Snapshelf gives Printly a token that opens only the doors you approved, expires on its own, and can be cancelled without touching anything else. Think of a valet key. It starts the car, and it will not open the trunk or the glovebox.
| Sharing your password | Using OAuth | |
|---|---|---|
| What the app can do | Everything you can do | Only what you approved |
| How long it lasts | Until you change your password | Minutes to hours, then renewed or ended |
| Cutting it off | Change your password and break every other app | Revoke that one app |
| Who sees your password | Every app you shared it with | Only Snapshelf's own login page |
OAuth handles authorization, which is what an app may do for you. Proving who you are is a different job called authentication. Chapter 6 covers the layer that adds it.
The spec gives each part a formal name. You will see these names in documentation, so each card shows both.
Own the photos and decide which apps may use them.
The app that wants access. It never sees your password.
Checks who you are, asks what you allow, and issues tokens.
Holds the photos. Answers a request only when it carries a valid token.
Small services often run the authorization server and the resource server together. They are still two separate jobs, and the flow treats them that way.
One thing happens before any of this. Printly's developer registers the app with Snapshelf and receives a client_id. The developer also lists the exact URLs Snapshelf may send users back to. That list matters in chapter 4.
This is the Authorization Code flow with PKCE. It is the one you will use for almost everything. Use the buttons or the arrow keys to move through it.
Steps 2 and 4 travel through your browser. Anything in a redirect URL lands in history, logs and extensions, so it can leak. That is why this channel only carries a short-lived code that cannot do anything alone.
Steps 5 to 8 go directly between servers over HTTPS. Nothing in your browser can see them. Tokens only ever travel here.
Step 2 sent your browser to Snapshelf with a long URL. Tap any piece of it below to see what it does and why it is there.
Tick some boxes. The scope part of the URL changes, and so does the screen Snapshelf shows you in step 3.
PKCE is pronounced "pixie". Printly invents a secret called the code verifier. It sends only the hash of that secret in step 2 and reveals the secret itself in step 5. Someone who steals the code from the redirect still lacks the verifier. Edit the verifier and watch the challenge change.
Step 6 returned two tokens. They have different jobs and different risks.
When the access token expires, the API answers 401. Printly then asks the token endpoint for a new one:
POST /token HTTP/1.1 Host: auth.snapshelf.example Content-Type: application/x-www-form-urlencoded grant_type=refresh_token &refresh_token=tGzv3JOkF0XG5Qx2TlKWIA &client_id=printly-web-4821
This design pays off when you change your mind. Open Snapshelf's connected apps page and remove Printly. Snapshelf invalidates Printly's tokens, and nothing else in your account changes.
Many access tokens are opaque strings. Client apps should treat them as opaque and never try to read them. Only the API needs to understand what is inside.
Now suppose Printly also wants a "Sign in with Snapshelf" button. An access token cannot serve as proof of who you are. It is written for the API, and it tells the app very little about you.
OpenID Connect, usually shortened to OIDC, is a thin layer on top of OAuth that fixes this. Add the scope openid to the request and the token response includes an ID token. That is a signed JSON Web Token (JWT) that says who just logged in.
A JWT is three pieces joined by dots. Here is one, decoded in your browser.
Anyone can read a JWT. The signature does not hide the contents. It proves nobody changed them after Snapshelf signed it.
Before trusting an ID token, an app checks that:
iss is the server you expectedaud is your own client_idexp is still in the futurenonce matches the one you sent in the requestOAuth defines several flows, called grant types. Pick the kind of app you are building.
Two older flows are discouraged by current security guidance. The implicit flow returned tokens in the URL. The password flow made apps collect your password, which brings back the original problem. The OAuth 2.1 draft drops both.
Most OAuth security problems come from skipping one of the small checks you have already seen.
An attacker can feed your app a callback for a login they started.
Do this. Generate a random value per request, store it in the session, and compare it on return.
Wildcards and prefix matches let attackers steer codes to their own pages.
Do this. Register exact URLs and require an exact string match.
URLs end up in browser history, server logs and Referer headers.
Do this. Send access tokens only in the Authorization header.
Anything shipped to a browser or phone can be extracted, so a secret there is public.
Do this. Treat those apps as public clients. Use PKCE and keep secrets on a server.
It says an app may call an API. It does not say who is using the app.
Do this. Use OpenID Connect and validate the ID token.
Long consent screens scare users, and a leaked token can do more.
Do this. Request the minimum, and ask for more when a feature needs it.
A leaked token works until it expires.
Do this. Keep access tokens short and rotate refresh tokens.