Skip to main content

Lesson 4 of 10

OAuth 2.0 & OpenID Connect

OAuth 2.0 is the industry standard framework for delegated authorization - it allows users to grant applications access to their data without sharing passwords. OpenID Connect (OIDC) adds an identity layer on top of OAuth 2.0. Both have common implementation vulnerabilities.

50 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

OAuth 2.0 Core Concepts

OAuth 2.0 Core Concepts
# OAuth 2.0 - The actors and their roles

Resource Owner:   The user (Alice) who owns the data
Client:           The application requesting access (your app)
Authorization Server: Issues access tokens after user consent (Google, GitHub)
Resource Server:  Holds the protected data (Google APIs, GitHub API)

# Authorization Code Flow (most secure - use for web/mobile apps)

Step 1 - Client → Authorization Server:
  GET https://accounts.google.com/oauth2/auth
    ?client_id=YOUR_CLIENT_ID
    &redirect_uri=https://yourapp.com/callback
    &scope=email profile
    &response_type=code
    &state=RANDOM_CSRF_TOKEN    # ← CRITICAL: prevent CSRF attack

Step 2 - User logs in to Google, grants consent
  Google redirects to: https://yourapp.com/callback?code=AUTH_CODE&state=SAME_TOKEN

Step 3 - Client validates state, exchanges code for token:
  POST https://oauth2.googleapis.com/token
    grant_type=authorization_code
    &code=AUTH_CODE
    &client_id=CLIENT_ID
    &client_secret=CLIENT_SECRET  # Never exposed to browser!
    &redirect_uri=https://yourapp.com/callback

Step 4 - Receive tokens:
  access_token: for API calls (short-lived, 1 hour)
  refresh_token: for getting new access tokens (long-lived)
  id_token: JWT identifying the user (OpenID Connect)

OAuth 2.0 Vulnerabilities

  • Missing state parameter: OAuth without state parameter allows CSRF - attacker can link victim's account to attacker's OAuth identity. Always generate and validate a random state value
  • Open redirect in redirect_uri: If the authorization server doesn't strictly validate redirect_uri, attacker can redirect authorization code to their server. Attacker's URI: https://yourapp.com/callback?redirect=https://evil.com. Always register exact redirect URIs
  • Token leakage via Referer: If redirect_uri contains sensitive tokens and the callback page loads third-party resources, the authorization code may leak in Referer header. Use state parameter, not implicit flow
  • Implicit flow vulnerabilities: The implicit flow (response_type=token) returns access tokens directly in the URL fragment - visible in browser history, logs, Referer headers. Deprecated in OAuth 2.1. Always use Authorization Code flow with PKCE
  • Scope validation: Always validate that the requested scopes match what was granted. Apps should request minimal scopes needed

PKCE - Proof Key for Code Exchange

PKCE - Proof Key for Code Exchange
# PKCE (RFC 7636) - Required for public clients (mobile, SPA)
# Prevents authorization code interception attacks

import hashlib, base64, secrets

# 1. Generate code verifier (random 43-128 char string)
code_verifier = secrets.token_urlsafe(64)

# 2. Create code challenge
code_challenge = base64.urlsafe_b64encode(
    hashlib.sha256(code_verifier.encode()).digest()
).rstrip(b'=').decode()

# 3. Include in authorization request
auth_url = (
    f"https://auth.server.com/authorize"
    f"?code_challenge={code_challenge}"
    f"&code_challenge_method=S256"
    f"&response_type=code"
    f"&client_id=CLIENT_ID"
    f"&redirect_uri=..."
)

# 4. When exchanging code for token, include code_verifier
# Server verifies: SHA256(code_verifier) == code_challenge
# → Authorization code cannot be used without knowing the verifier
# → Intercepted code is useless to attacker

Common Mistakes

  • Not validating the state parameter - this is a CSRF vulnerability in your OAuth flow; always validate state matches the value you sent
  • Using implicit flow for SPAs - use Authorization Code + PKCE instead; implicit flow has been deprecated
  • Over-requesting scopes - request only the permissions you actually need; users (and security reviewers) are suspicious of apps requesting read:all when they only need email

Tip

Tip

Practice OAuth 20 OpenID Connect in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).

Practice Task

Note

Practice Task - (1) Write a working example of OAuth 20 OpenID Connect from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with OAuth 20 OpenID Connect is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.

Key Takeaways

  • Missing state parameter: OAuth without state parameter allows CSRF - attacker can link victim's account to attacker's OAuth identity. Always generate and validate a random state value
  • Open redirect in redirect_uri: If the authorization server doesn't strictly validate redirect_uri, attacker can redirect authorization code to their server. Attacker's URI: https://yourapp.com/callback?redirect=https://evil.com. Always register exact redirect URIs
  • Token leakage via Referer: If redirect_uri contains sensitive tokens and the callback page loads third-party resources, the authorization code may leak in Referer header. Use state parameter, not implicit flow
  • Not validating the state parameter - this is a CSRF vulnerability in your OAuth flow; always validate state matches the value you sent
  • Using implicit flow for SPAs - use Authorization Code + PKCE instead; implicit flow has been deprecated
  • Over-requesting scopes - request only the permissions you actually need; users (and security reviewers) are suspicious of apps requesting read:all when they only need email

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/10 done