OAuth 2.0 and delegated access: scopes, flows, and tokens explained
How OAuth lets an app act for a user without the password: the authorization code flow with PKCE, what scopes and consent mean, access versus refresh tokens, and the review points that catch common mistakes.
Diagram
Authorization code flow with PKCE
Step 1 of 4
Redirect
The app sends the user to the authorization server with a code challenge.
GET /authorize?response_type=code&client_id=notes-app &redirect_uri=https://notes.example.test/cb &scope=calendar.read&state=9c1f...&code_challenge=E9Mel...&code_challenge_method=S256
In the URL fragment
None
Not supported
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Redirect URI matching
- What fails
- Loose redirect matching sends codes to the wrong place.
- Who is affected
- Users of the app, whose codes can be captured.
- What you would notice
- Wildcard or prefix redirect URIs in client registrations.
- What you change
- Register exact URIs and reject anything else.
State and PKCE
- What fails
- state ties the response to the user’s session; PKCE ties the code to the client that asked.
- Who is affected
- Users who could be logged into an attacker’s account or have codes replayed.
- What you would notice
- Authorization requests without state or code_challenge.
- What you change
- Require PKCE for all clients and validate state on return.
Scope creep
- What fails
- Apps request more access than the feature needs, and users click approve.
- Who is affected
- Everyone whose data the scope covers.
- What you would notice
- Read-write or org-wide scopes for a read-only feature.
- What you change
- Request the narrowest scopes and add more only when a feature needs them.
Access and refresh tokens
Access tokens are short-lived and sent to APIs. Refresh tokens are long-lived and only sent to the token endpoint. Rotate refresh tokens on every use and revoke the family if an old one is replayed. OAuth 2.1 drafts fold these recommendations, plus mandatory PKCE, into the core spec.
OAuth is not login by itself
OAuth delegates access to APIs. Signing a user in is OpenID Connect’s job: it adds an ID token with issuer, audience, and nonce that the client must validate. Using an access token as proof of identity is a classic mistake.
Review checklist
Use this list for any app that integrates with an identity provider.
- Exact redirect URIs, no wildcards
- PKCE with S256 for every client
- state validated on callback
- Narrow scopes, documented per feature
- Refresh token rotation and revocation
- ID token validated for sign-in, not the access token
Check yourself
Recognition and the fix. Nothing here is a procedure.
What does PKCE prevent?
Go deeper
Specific techniques, exercises, and glossary cards for this topic.
- 01Exact redirect URI matchingThe redirect URI is where the authorization code goes. Includes the diagram and a defensive fix.
- 02Token audience and the smallest scopeA token is a capability for a specific API, for a specific set of actions, for a limited time. Includes the diagram and a defensive fix.