OAuth / OAuth2
Updated June 3, 2026OAuth2 (Open Authorization) is the industry-standard protocol for delegated authorization — it lets a user grant a third-party application access to their data on another service, without handing over their password. Before OAuth, apps like Yelp would ask users to type their Google credentials directly into Yelp's site so Yelp could scrape their contacts; OAuth2 eliminated that pattern.
Authentication vs. Authorization
Authentication (AuthN) answers "who are you?" It verifies identity. Authorization (AuthZ) answers "what are you allowed to do?" It grants permissions. OAuth2 is strictly an authorization protocol; it issues access tokens, not identities (OIDC, covered in the SSO article, layers identity on top of it).
OAuth2 is both an authentication and an authorization protocol.
Authorization Code Flow
OAuth2 actors — Resource Owner, Client, Authorization Server, Resource Server
The most common OAuth2 scenario: you are building a web app (the Client) that wants to import a user's Spotify playlists.
Resource Owner: the user. Client: your web app. Authorization Server: Spotify's login system. Resource Server: Spotify's API holding the playlist data.
Step 1 — Request: The user clicks "Import Spotify Playlists." Your app redirects the browser to Spotify's Authorization Server, including your app's client ID and the requested Scope (e.g., playlist-read) in the URL.
Step 2 — Consent: Spotify prompts the user to log in, then displays a consent screen: "This app wants to read your playlists. Allow?" The user approves. Critically, the user's password was typed into Spotify — not into your app.
Step 3 — Authorization Code: Spotify redirects the browser back to your app with a temporary, one-time-use authorization code appended to the URL.
Step 4 — Token Exchange (Back-Channel): Your server takes that code and — hidden from the browser — sends it to Spotify's Authorization Server along with your app's client secret. Spotify validates both and returns an Access Token.
Back-channel token exchange — authorization code swapped for access token server-to-server
Step 5 — Access Granted: Your app attaches the Access Token to the Authorization header of requests to Spotify's API. Spotify validates the token and returns the playlist data.
In the Authorization Code Flow, which party types the user's password into the Authorization Server?
Why use an Authorization Code?
Why didn't Spotify return the Access Token directly in Step 3? The browser is exposed — extensions or injected scripts can read URL parameters. Returning a short-lived, single-use code instead means the actual token is only ever exchanged server-to-server in Step 4, out of reach of client-side attacks.
Why does the Authorization Server return a short-lived authorization code in Step 3 instead of returning the access token directly in the browser redirect?
Scopes and Lifespans
OAuth2 tokens are deliberately constrained. A token is bound to the scopes the user approved: a token issued with playlist-read scope will be rejected if the client attempts to delete a playlist. Access tokens are short-lived — typically 15 to 60 minutes — so a stolen token has a narrow window of usefulness. Apps maintain continuous access by using a longer-lived Refresh Token to silently obtain new access tokens in the background without re-prompting the user.
An access token issued with a `playlist-read` scope can be used to delete a playlist if the client needs to.
What is the purpose of a Refresh Token in OAuth2?
Summary
OAuth2 is the industry standard for delegated authorization. It lets third-party apps access specific resources on a user's behalf without ever seeing the user's password. The Authorization Code Flow keeps tokens off the browser by exchanging them server-to-server. Scopes constrain what a token can do, and refresh tokens allow sustained access while keeping individual access tokens short-lived.
How helpful was this content?
Comments
Sign in to join the discussion
Saved on this device only
Sign in to sync progress across devices