5 May 2025

OAuth 2.0 and OpenID Connect (OIDC)

Goated video: https://www.youtube.com/watch?v=996OiexHze0
PPT: https://drive.google.com/file/d/1UyPqnrGnCCJ7PeIY-rDV-3tRprIEprBB/view?usp=sharing

 +--------+                               +---------------+
 |        |--(A)- Authorization Request ->|   Resource    |
 |        |                               |     Owner     |
 |        |<-(B)-- Authorization Grant ---|               |
 |        |                               +---------------+
 |        |
 |        |                               +---------------+
 |        |--(C)-- Authorization Grant -->| Authorization |
 | Client |                               |     Server    |
 |        |<-(D)----- Access Token -------|               |
 |        |                               +---------------+
 |        |
 |        |                               +---------------+
 |        |--(E)----- Access Token ------>|    Resource   |
 |        |                               |     Server    |
 |        |<-(F)--- Protected Resource ---|               |
 +--------+                               +---------------+

Authorization vs. Authentication

Authentication:

Authorization:

Analogy:

Aspect Authentication Authorization
Purpose Verifies identity Grants or restricts access
When it happens Before authorization After authentication
Mechanisms Passwords, biometrics, MFA Role-based access control (RBAC), permissions
Example Logging into a system Accessing specific files or features

Authorization Flow

Backchannel and Front Channel

Back Channel Auth Flow


OAuth 2.0 Authorization Code Flow

Forward Channel (Less Secure)

  1. The user clicks “Sign in with Google” on the client application.
  2. The client app redirects the user’s browser to Google’s authorization server.
  3. The user logs in and consents to the requested permissions.
  4. Google redirects the user’s browser back to the client app with an authorization code in the URL (redirect URI or callback).

Risk: The authorization code is exposed to the browser and could be intercepted. However, this alone is not useful because an authorization code alone cannot be used to access resources. An access token is required, which can only be obtained by providing both the authorization code and a client secret.

Back Channel (More Secure)

  1. The client app takes the authorization code and sends it directly to Google’s token endpoint via a POST request (over HTTPS).
  2. The POST request is encrypted with HTTPS, and the client app authenticates itself using a client secret.
  3. Google responds with an access token, which is securely transmitted over HTTPS and never exposed to the browser.
POST /token HTTP/1.1
Host: authorization-server.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=AUTH_CODE
&redirect_uri=https://client-app.com/callback
&client_id=CLIENT_ID
&client_secret=CLIENT_SECRET

OAuth 2.0 is for Authorization, Not Authentication

Why OAuth 2.0 Is Not for Authentication

1. Lack of Identity Verification

2. Misuse for Authentication

3. Inconsistency in Authentication Implementation

Let’s consider an example:


OpenID Connect (OIDC)

To solve the problem of needing user identity verification, OIDC adds an authentication layer on top of OAuth 2.0.

OIDC Enhancements

  1. ID Tokens (JWTs)

    • OIDC introduces ID tokens, which are JSON Web Tokens (JWTs) containing user identity information (e.g., name, email).
    • These tokens are signed and can be verified to ensure the user’s identity.
  2. Standardized User Info

    • OIDC provides a standard way to retrieve user information using the /userinfo endpoint.
  3. Authentication Flow

    • OIDC follows OAuth 2.0 flows but adds an ID token alongside the access token.

OIDC vs. OAuth 2.0

Step OAuth 2.0 OpenID Connect (OIDC)
Token Issued Access token Access token + ID token
Identity Information Not included in the access token Included in the ID token
User Info Retrieval Requires additional API call to /userinfo ID token contains user info; /userinfo is optional
Use Case Accessing Google Drive or Gmail Logging into a website using “Sign in with Google”

OIDC Flow

Reason why oauth isnt enough for authentication and Combination of oauth +oidc is the way to go

Why Is the ID Token Trustworthy?

What If the Attacker Steals Both Tokens?

Yes, if the attacker steals both the Access Token and ID Token simultaneously, they can access the app for the ID Token’s lifetime (5-10 mins).

However, this risk is minimized by:

Refresh Tokens

Purpose of Refresh Tokens

Key Points

When Is a Refresh Token Used?

Benefits of Refresh Tokens

Even when an access token expires, the user remains logged in, but API calls will fail until a new token is obtained.