Skip to content
Back to CurriculumLesson 11 of 30
oauth-oidcBeginner

OAuth 2.0 Core: Roles, Scopes, Consents & The Authorization Code Grant

OAuth 2.0 (RFC 6749) is a delegated authorization framework that allows third-party clients to access protected resources on behalf of a resource owner without sharing the owner's credentials. It defines four primary roles: Resource Owner, Client, Authorization Server, and Resource Server.

Interactive Visual Concept ExplainerStep-by-Step Educational Simulation
Visual Step-by-Step Concept Explaineroauth-oidc
OAuth 2.0 Core: Roles, Scopes, Consents & The Authorization Code Grant
Steps:
Resource Owner
Alice (The End User who owns data)
Client Application
CosmosApp SPA / Mobile App
Authorization Server
Google / Auth0 IdP (Issues Tokens)
Resource Server
Photos / Drive API (Accepts Bearer Tokens)
Step 1 of 6
Auto-stepping (1x)

Step 1: Client directs the user's browser to the Authorization Server's /authorize endpoint with client_id, redirect_uri, scope, and state.

Security Guarantee: OAuth 2.0 decouples resource owners from client applications through scoped, revocable access tokens.
Why This Architectural Concept Exists

Before OAuth, if you wanted an app to print photos from your social media account, you had to give the app your raw username and password. The app could now do everything your account could do, change your password, and read private messages. OAuth introduced scoped, revocable delegation.

Intuitive Mental Model

The Valet Key: Handing a valet driver your entire master keychain allows them to enter your house, open the trunk, and drive away. Handing them a valet key only allows them to drive the vehicle 5 miles per hour and park it, without unlocking the glove compartment.

Architectural System TopologyProtocol Diagram
[ Resource Owner (User) ]
       │
       ▼ (1) Grants Consent to Scopes ('photos:read')
[ Authorization Server (IdP) ] ──(2) Issues Authorization Code──► [ Client App (CosmosApp) ]
                                                                       │
                                                                       ├──(3) Trades Code for Access Token
                                                                       ▼
[ Resource Server (API) ] ◄──(4) Request with Bearer Token──────────────┘
       │
       ▼ (5) Returns Protected Photos
CosmosApp Production Milestone

CosmosApp integrates with GitHub so developers can import their repositories. Instead of asking users for their GitHub username and password, CosmosApp redirects the user to GitHub with scope=repo:read. The user reviews the consent screen, GitHub returns a short-lived authorization code, and CosmosApp exchanges it for a scoped access token.

Step-by-Step Execution Sequence
01

Step 1: Client directs the user's browser to the Authorization Server's /authorize endpoint with client_id, redirect_uri, scope, and state.

02

Step 2: Authorization Server authenticates the user and presents a consent screen describing the requested scopes.

03

Step 3: User approves consent; Authorization Server redirects the browser back to redirect_uri with a temporary authorization code.

04

Step 4: Client makes a secure back-channel POST request to the /token endpoint, submitting the authorization code and client credentials.

05

Step 5: Authorization Server validates the code and parameters, and returns an access token (and optional refresh token).

06

Step 6: Client uses the access token in the Authorization: Bearer <token> header to query the Resource Server.

Production Architecture Blueprint
// CosmosApp OAuth 2.0 Authorization URL Generator:
export function buildOAuthAuthorizeUrl(clientId: string, redirectUri: string, scopes: string[]) {
  const state = crypto.randomBytes(16).toString('hex');
  const params = new URLSearchParams({
    response_type: 'code',
    client_id: clientId,
    redirect_uri: redirectUri,
    scope: scopes.join(' '),
    state: state,
  });

  return {
    url: `https://auth.provider.com/oauth/authorize?${params.toString()}`,
    state,
  };
}
Common Architectural Misconceptions
  • ✕OAuth 2.0 is an authentication protocol (OAuth 2.0 only handles authorization/delegated access; OpenID Connect was built on top of OAuth to provide authentication).
  • ✕The authorization code and the access token are the same thing (The code is delivered via public browser redirects and is single-use; the token is exchanged via a direct back-channel connection).

Key Architectural Takeaways

  • ✓OAuth 2.0 decouples resource owners from client applications through scoped, revocable access tokens.
  • ✓Always validate the state parameter on the callback to prevent Login CSRF attacks.
  • ✓Always register exact, fully-qualified redirect URIs at the authorization server to prevent token interception.
Common Architectural Pitfall

Using OAuth 2.0 access tokens alone to identify a user without verifying an OpenID Connect ID token.

Production Best Practice Fix

Use OpenID Connect (OIDC) when authentication and user profile data are needed, not raw OAuth 2.0 access tokens.

Interactive Knowledge Check

What is the primary architectural purpose of the Authorization Code in the OAuth 2.0 flow?