OpenID Connect (OIDC) Authentication Guide

Updated: 1/15/2025Category: authentication
openidoidcoauthsecurityauthenticationjwt

OpenID Connect (OIDC) Authentication Guide#

A comprehensive guide to understanding OpenID Connect and how it builds on OAuth 2.0 to provide standardized authentication.


Table of Contents#

  1. What is OpenID Connect?
  2. OAuth 2.0 vs OpenID Connect
  3. How OpenID Connect Works
  4. ID Tokens Explained
  5. OIDC Scopes
  6. OIDC Endpoints
  7. Implementation Guide
  8. UserInfo Endpoint
  9. Token Validation
  10. Discovery & Configuration
  11. Security Considerations
  12. Common Providers
  13. Best Practices
  14. Troubleshooting

What is OpenID Connect?#

OpenID Connect (OIDC) is an identity layer built on top of OAuth 2.0. While OAuth 2.0 is designed for authorization (granting access to resources), OpenID Connect adds authentication (verifying user identity).

The Problem OIDC Solves#

Before OIDC, every OAuth provider had their own way of providing user identity:

typescript
typescript

With OIDC, there's a standardized way:

typescript
typescript

Key Benefits#

  • Standardization: Same flow for all OIDC-compliant providers
  • Built-in user info: ID tokens contain user claims (no extra API call needed)
  • Better security: Cryptographically signed tokens
  • Industry standard: Widely adopted (Google, Microsoft, Apple, Okta, Auth0, etc.)
  • Interoperability: Use any OIDC-certified library

OAuth 2.0 vs OpenID Connect#

Visual Comparison#

Loading diagram...

Side-by-Side Comparison#

Aspect OAuth 2.0 OpenID Connect
Primary Purpose Authorization Authentication
Question Answered "What can the user do?" "Who is the user?"
Token Types Access token only Access token + ID token
User Info Custom API endpoint Standardized /userinfo endpoint
Scopes Resource-specific (read:email, write:files) Identity-specific (openid, email, profile)
Token Format Opaque string (can be anything) ID token is always a JWT
User Claims Varies by provider Standardized claims (sub, email, name)
Use Cases API access, delegated permissions User login, SSO, identity verification
Specification RFC 6749 Built on OAuth 2.0 + OpenID spec

Use Case Examples#

Use OAuth 2.0 when:

  • Accessing a user's Google Drive files
  • Posting to a user's Twitter account
  • Reading a user's GitHub repositories
  • Sending emails via Gmail API

Use OpenID Connect when:

  • Logging users into your application
  • Implementing Single Sign-On (SSO)
  • Verifying user identity
  • Getting user profile information

Use Both when:

  • Sign in with Google AND access their calendar
  • Sign in with GitHub AND read their repositories
  • Sign in with Microsoft AND access OneDrive

How OpenID Connect Works#

The Complete OIDC Flow#

Loading diagram...

Step-by-Step Breakdown#

1. Authorization Request#

typescript
typescript

Key OIDC additions:

  • scope includes openid (required for OIDC)
  • nonce parameter for replay attack prevention

2. Token Exchange#

typescript
typescript

3. Decode & Verify ID Token#

typescript
typescript

ID Tokens Explained#

The ID token is the heart of OpenID Connect. It's a JSON Web Token (JWT) that contains identity information about the authenticated user.

Anatomy of an ID Token#

An ID token consists of three parts separated by dots:

text
json
json

Payload (Claims)#

json
json

Signature#

The signature ensures the token hasn't been tampered with:

text

Standard Claims#

Claim Description Example
sub Subject - unique user identifier (NEVER changes) "107265685217823649876"
iss Issuer - who created the token "https://accounts.google.com"
aud Audience - who token is intended for "your-client-id"
exp Expiration time (Unix timestamp) 1642602000
iat Issued at time (Unix timestamp) 1642598400
nonce Random value to prevent replay attacks "abc123xyz"
auth_time When user authentication occurred 1642598350
acr Authentication Context Class Reference "urn:maa:loa:1"
amr Authentication Methods References ["pwd", "mfa"]

Profile Claims#

Requested with scope=profile:

json
json

Email Claims#

Requested with scope=email:

json
json

Address Claims#

Requested with scope=address:

json
json

OIDC Scopes#

OpenID Connect defines standard scopes that request specific sets of claims.

Required Scope#

typescript
typescript

Without openid scope, it's just OAuth 2.0 (no ID token returned).

Standard Scopes#

typescript
typescript

Combining with OAuth Scopes#

You can mix OIDC scopes with provider-specific OAuth scopes:

typescript
typescript

Offline Access#

Request refresh token:

typescript
typescript

OIDC Endpoints#

OpenID Connect providers expose standard endpoints.

Discovery Endpoint#

The .well-known/openid-configuration endpoint returns all OIDC metadata:

typescript
typescript

Core Endpoints#

1. Authorization Endpoint#

text

Parameters:

  • client_id - Your application's client ID
  • redirect_uri - Where to send user after authorization
  • response_type - Usually code
  • scope - Must include openid + other scopes
  • state - Random value for CSRF protection
  • nonce - Random value for token replay prevention

2. Token Endpoint#

text

Parameters:

  • code - Authorization code from callback
  • client_id - Your application's client ID
  • client_secret - Your application's client secret
  • redirect_uri - Must match authorization request
  • grant_type - authorization_code

Response:

json
json

3. UserInfo Endpoint#

text

Response:

json
json

4. JWKs Endpoint#

text

Response:

json
json

Implementation Guide#

Basic OIDC Implementation#

typescript
typescript

Handling Callback#

typescript
typescript

Manual Implementation (Without Library)#

typescript
typescript

UserInfo Endpoint#

The UserInfo endpoint provides additional claims not included in the ID token.

When to Use UserInfo#

Use ID token when:

  • You only need standard claims (sub, email, name)
  • You want to minimize API calls
  • Token size is acceptable
  • Claims don't change frequently

Use UserInfo endpoint when:

  • You need claims not in ID token
  • You want the latest user information
  • Token size is a concern
  • You need custom claims specific to the provider

Making UserInfo Requests#

typescript
typescript

Comparing ID Token vs UserInfo#

typescript
typescript

Token Validation#

Proper ID token validation is critical for security.

Validation Checklist#

typescript
typescript

Using a Library#

typescript
typescript

Discovery & Configuration#

OIDC providers publish their configuration at a well-known endpoint.

Discovery Document#

typescript
typescript

Common Provider Discovery URLs#

typescript
typescript

Caching Discovery Documents#

typescript
typescript

Security Considerations#

1. Always Validate ID Tokens#

typescript
typescript

2. Use Nonce for Replay Prevention#

typescript
typescript

3. Use State for CSRF Protection#

typescript
typescript

4. Implement Token Expiration#

typescript
typescript

5. Secure Token Storage#

typescript
typescript

6. Validate Issuer#

typescript
typescript

7. Use PKCE for Public Clients#

For mobile apps or SPAs:

typescript
typescript

Common Providers#

Google#

typescript
typescript

Microsoft / Azure AD#

typescript
typescript

Apple#

typescript
typescript

Okta#

typescript
typescript

Auth0#

typescript
typescript

Best Practices#

1. Use Discovery for Configuration#

typescript
typescript

2. Request Minimal Scopes#

typescript
typescript

3. Handle Token Refresh#

typescript
typescript

4. Implement Logout#

typescript
typescript

5. Validate All Claims#

typescript
typescript

Troubleshooting#

Common Issues#

Issue: "Invalid nonce"

text

Issue: "Invalid signature"

text

Issue: "Token expired"

text

Issue: "Invalid issuer"

text

Issue: No email in claims

text

Debugging Tools#

typescript
typescript

Resources#

Official Specifications#

Libraries#

Testing Tools#

Certification#


Summary#

OpenID Connect provides:

  1. Standardization: Common protocol across all providers
  2. ID Tokens: Cryptographically signed JWTs with user identity
  3. UserInfo Endpoint: Standardized API for user information
  4. Discovery: Auto-configuration via well-known endpoints
  5. Security: Built-in protections (nonce, state, signature verification)

Key Differences from OAuth 2.0#

  • ✅ Includes openid scope → triggers OIDC flow
  • ✅ Returns ID token (JWT) → contains user claims
  • ✅ Standardized endpoints → same across providers
  • ✅ Built-in identity verification → no custom userinfo endpoints

When to Use OIDC#

  • User authentication / login
  • Single Sign-On (SSO)
  • Identity verification
  • Getting user profile information

OpenID Connect = OAuth 2.0 + Identity Layer 🔐


Happy authenticating! 🎉

Viewing as guest