Signing in with another account (OAuth)
AdvancedWhat happens when you press “Sign in with…”: redirects, a one-time code, a token exchange the browser never sees, and why the app never gets your password.Builds on Cookies and sessions
GET /callback?code=Splx0B&state=Zx81kq HTTP/1.1Host: app.exampleCookie: login_state=Zx81kq POST /token HTTP/1.1Host: id.exampleAuthorization: Basic bm90ZXM6czNjcmV0Content-Type: application/x-www-form-urlencoded grant_type=authorization_code&code=Splx0B&redirect_uri=https%3A%2F%2Fapp.example%2Fcallback&code_verifier=dBjftJeZ4CVP… HTTP/1.1 200 OKContent-Type: application/json {"access_token":"SlAV32hk","token_type":"Bearer","expires_in":3600,"id_token":"eyJhbGciOi…"} HTTP/1.1 303 See OtherLocation: /notesSet-Cookie: sid=Qw9Lm2Xc; Path=/; HttpOnly; Secure; SameSite=LaxEmpty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
4 min read
On this page
What is OAuth and how does "Sign in with…" work?
OAuth 2 lets one site act on your behalf at another without ever seeing your password. The notes app (the client) sends you to the sign-in provider, you sign in and agree there, and the provider gives the app a limited, short-lived access token.
"Sign in with…" adds OpenID Connect on top: besides the access token, the provider returns an ID token that says who you are. The app checks it and then starts its own session, exactly like after a normal login.
The steps are always the same:
- The app redirects you to the provider, saying which app it is and what it wants (the scope).
- You sign in at the provider and give your consent.
- The provider redirects you back with a one-time .
- The app's server trades the code for tokens, directly with the provider.
How does the authorization code flow keep tokens safe?
- The code, not the token, crosses the browser. URLs end up in history and logs, so only a short-lived, single-use code travels there. The tokens come back on a direct server-to-server request.
- The client proves who it is. On the token request, the app sends its own secret (here with
Authorization: Basic), so a code given to the wrong app is useless. - PKCE ties the code to the app instance that asked. The app makes a random code verifier and puts only its hash, the code challenge, in the first redirect. On the token request it sends the verifier itself, which an attacker who stole the code does not have. Apps that cannot keep a secret, such as mobile apps and single-page apps, rely on it entirely.
stateties the return to the start. The app compares thestatein the callback with the one it stored, so an attacker cannot slip their own code into your browser and sign you into their account.- Tokens are limited. An access token works only for its scope and for a short time. A refresh token, if the app asked for one, gets new access tokens without bothering the user.
What do people get wrong about OAuth?
Mini example
An access token expires after an hour. If the app asked for offline access, it can get a new one with its refresh token, without sending the user through the provider again:
POST /token HTTP/1.1Host: id.exampleAuthorization: Basic bm90ZXM6czNjcmV0Content-Type: application/x-www-form-urlencoded grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA HTTP/1.1 200 OKContent-Type: application/json {"access_token":"8xLOxBtZp8","token_type":"Bearer","expires_in":3600}Quick check
An attacker reads the code from a URL in a log file. Why can't they get tokens with it?
Further reading
All steps as text
- Step 1:
A notes app offers “Sign in with Example ID”, so you don’t need yet another password. You press the button.
- Step 2:
The app never asks for your password. It sends you to the provider, with two random secrets: a
stateit keeps in a cookie, and a PKCE secret whose hash goes in the URL. - Step 3:
The browser follows the redirect to id.example. The URL says which app is asking (
client_id), what it wants (scope), and where to send you back. - Step 4:
You sign in on the provider’s own page, at id.example. Your password goes to the provider only; the notes app never sees it.
- Step 5:
The provider asks whether the notes app may see your email address. You allow it: that is the consent for the
scopethe app asked for. - Step 6:
The provider sends you back to the app with a short-lived, one-time
codeand the samestate. The code is not a token yet. - Step 7:
The browser goes to the callback address. The code and the state are in the URL, and the
login_statecookie comes along on its own.Lines 1–3
- Step 8:
The app compares the
statein the URL with the one in the cookie. They match, so this sign-in started here, not on an attacker’s page.Lines 1–3
- Step 9:
Now the app’s server calls the provider directly, not through the browser. It sends the code, its own secret in
Authorization, and the PKCE secret itself.Lines 5–10
- Step 10:
The provider checks that the code is fresh, unused and issued to this app, and that the secret’s hash matches the one from the first redirect. A stolen code alone is useless.
Line 10
- Step 11:
It answers with tokens. The
access_tokenlets the app call the provider’s API for an hour, only within the scope. Theid_tokensays who you are.Lines 12–15
- Step 12:
The
id_tokenis a signed JWT. The app checks the provider’s signature, that it was issued for this app, and that it hasn’t expired. Inside: Sara and her email.Line 15
- Step 13:
From here it is an ordinary login: the app starts its own session and sets its own cookie. The tokens stay on the app’s server.
Lines 17–19
- Step 14:
Later the app checks that your email address hasn’t changed. It calls the provider’s API with the access token in an
Authorization: Bearerheader. - Step 15:
The password goes only to the provider, the browser carries only a one-time code, and the tokens travel server to server. The app ends up with its own session.