Stack OverflowSrikanth SrinivasExplainer
Never Share Your Password Again — The OAuth 2.0 Solution
Summary
OAuth 2.0 solves the password anti-pattern, where third-party apps required user passwords and gained full account access, by letting users authenticate only with a trusted service that issues a scoped, time-limited, revocable token. The Authorization Code with PKCE flow uses five steps: the app redirects the user to the authorization server, the user authenticates on that server’s domain, the server returns a short-lived code, the app exchanges that code server-to-server for a token, and the token is used for API calls. Four critical parameters—state for CSRF protection, code_challenge and code_verifier for PKCE, redirect_uri to prevent hijacking, and the short-lived code to limit reuse—defend against specific attacks. Scopes bound access to permissions like calendar.read, so a breached app only exposes bounded access rather than full account control. The protocol is secure enough to run every Sign in with Google button on the internet.
Context
Before OAuth 2.0, third-party apps required users to share their actual passwords, a practice called the password anti-pattern. This gave apps full account access, made revocation impossible without changing the password everywhere, and exposed all accounts if the app was breached.
Approach / What changed
The post explains the Authorization Code with PKCE flow, a five-step protocol where the app redirects the user to the authorization server, the user authenticates on the server’s domain, the server returns a short-lived code, the app exchanges that code server-to-server for a token, and the token is used for API calls. The post details four security-critical parameters: state for CSRF protection, code_challenge and code_verifier for PKCE, redirect_uri to prevent code hijacking, and the short-lived code to limit reuse.
Takeaways
OAuth 2.0 eliminates the password anti-pattern by letting users authenticate only with the trusted service, which issues a token the app never sees the password.
The Authorization Code with PKCE flow uses four security parameters: state for CSRF, code_challenge for PKCE, redirect_uri to prevent hijacking, and a short-lived code to limit reuse.
Scopes restrict a token to specific permissions, so a breached app only exposes bounded access rather than full account control.
FAQ
What does the state parameter protect against?
The state parameter is a random value the app generates and sends in the authorize request. The app checks that the same value returns on the redirect. Without it, an attacker could trick a signed-in user into completing the attacker's OAuth flow, binding the attacker's account to the user's app session.
How does PKCE prevent authorization code interception?
PKCE uses a random code_verifier that the app hashes with SHA-256 to produce a code_challenge sent in the initial request. When exchanging the code, the app presents the original code_verifier. An attacker who intercepts the authorization code without the matching code_verifier cannot use it.
How do scopes limit what a token can do?
The scope parameter limits the token to specific permissions, such as calendar.read. The app can only read the calendar; it cannot delete events, send email, access contacts, or post on the user's behalf. If the app is breached, the attacker's access is bounded to that scope.
Source and freshness
- Source
- stackoverflow.blog
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.