What is OAuth 2.0 and how is it used to secure REST APIs?
Learn what OAuth 2.0 is, its roles and grant flows like Authorization Code with PKCE, and how scoped access tokens secure your REST APIs.
Expected Interview Answer
OAuth 2.0 is an authorization framework that lets a user grant a third-party application limited access to their resources on another service without sharing their password. The app receives a scoped access token, which it presents to the REST API instead of the user's credentials.
OAuth 2.0 defines roles (resource owner, client, authorization server, resource server) and grant flows such as Authorization Code with PKCE for user-facing apps and Client Credentials for machine-to-machine access. The client obtains an access token (often a JWT) from the authorization server, then calls the API's resource server with 'Authorization: Bearer <token>'. The resource server validates the token and enforces the token's scopes to authorize each request, keeping delegated access limited and revocable.
- Users never share passwords with third-party apps
- Scoped tokens grant least-privilege access
- Central authorization server for issuing and revoking tokens
- Standardized flows for web, mobile, and service clients
- Decouples authentication from resource APIs
AI Mentor Explanation
OAuth 2.0 is like a team manager giving a physio a restricted access pass rather than handing over the captain's master keys. The pass, issued by the ground office, only opens the treatment room on match day. The physio never learns the captain's credentials, and the office can revoke that single pass without changing every other lock.
Step-by-Step Explanation
Step 1
Client registration
The application registers with the authorization server and receives a client id and, for confidential clients, a secret.
Step 2
User consent
The user is redirected to the authorization server, authenticates, and approves the specific scopes the app requests.
Step 3
Authorization code
The authorization server redirects back with a short-lived code; PKCE protects public clients from code interception.
Step 4
Token exchange
The client exchanges the code (plus its PKCE verifier or client secret) for an access token and optional refresh token.
Step 5
Call the API
The client sends 'Authorization: Bearer <access_token>' to the resource server on each protected request.
Step 6
Validate and enforce scopes
The resource server verifies the token and checks its scopes before serving the requested resource.
What Interviewer Expects
- That OAuth 2.0 is authorization/delegation, not authentication by itself
- The four roles: resource owner, client, authorization server, resource server
- When to use Authorization Code with PKCE versus Client Credentials
- Understanding of access tokens, refresh tokens, and scopes
- How the resource server validates tokens and enforces scopes
- Awareness that OpenID Connect adds identity on top of OAuth
Common Mistakes
- Calling OAuth an authentication protocol (that's OpenID Connect)
- Using the deprecated Implicit or Password grant for new apps
- Skipping PKCE for public/mobile clients
- Requesting overly broad scopes instead of least privilege
- Not validating token audience, issuer, and scopes on the API
Best Answer (HR Friendly)
“OAuth 2.0 lets you allow one app to access some of your data on another service without giving it your password, like handing a valet a key that only starts your car. The service gives the app a limited pass, and you can take that access away whenever you want.”
Code Example
// Middleware on the REST API (resource server)
function requireScope(scope) {
return async (req, res, next) => {
const token = (req.headers.authorization || '').replace('Bearer ', '')
try {
// Verify signature, issuer, audience and expiry
const claims = await verifyAccessToken(token, {
issuer: 'https://auth.example.com/',
audience: 'https://api.example.com'
})
const scopes = (claims.scope || '').split(' ')
if (!scopes.includes(scope)) {
return res.status(403).json({ error: 'insufficient_scope' })
}
req.user = claims
next()
} catch (err) {
res.status(401).json({ error: 'invalid_token' })
}
}
}
app.get('/api/orders', requireScope('orders:read'), listOrders)Follow-up Questions
- What problem does PKCE solve and which clients need it?
- How does OpenID Connect extend OAuth 2.0?
- When would you use the Client Credentials grant?
- What is the difference between an access token and a refresh token?
- How does the resource server validate a token issued by a separate authorization server?
MCQ Practice
1. OAuth 2.0 is fundamentally a framework for what?
OAuth 2.0 is an authorization framework for delegated access; identity is added by OpenID Connect on top of it.
2. Which grant is recommended for a single-page or mobile app?
Authorization Code with PKCE is the current recommendation for public clients; Implicit and Password grants are discouraged.
3. What does a resource server return when a valid token lacks the required scope?
A valid but under-privileged token yields 403 insufficient_scope; 401 is for missing or invalid tokens.
Flash Cards
What is OAuth 2.0? — An authorization framework for delegated, scoped access to resources without sharing the user's password.
The four OAuth roles — Resource owner, client, authorization server, and resource server.
Recommended grant for public clients — Authorization Code with PKCE.
OAuth vs OpenID Connect — OAuth 2.0 handles authorization; OpenID Connect layers on authentication/identity.
How does the API enforce access? — The resource server validates the Bearer token and checks its scopes per request.