Part 3: Authentication Protocols and Flows

The Day I Implemented the Wrong Flow

Early in my career, I built a React SPA that needed to call our backend API. I read "OAuth 2.0" and implemented the implicit flowβ€”because that's what the first tutorial I found suggested. Everything worked... until our security audit flagged it as a critical vulnerability. Tokens were being exposed in browser history and URL fragments.

I had to refactor to authorization code flow with PKCE. It took days to understand why one flow was secure and the other wasn't. Let me save you that pain.

OAuth 2.0 and OpenID Connect: The Foundation

MS Entra implements two critical standards:

OAuth 2.0

Purpose: Authorization (what can you access?) Result: Access tokens for calling APIs

OpenID Connect (OIDC)

Purpose: Authentication (who are you?) Result: ID tokens with user identity information

The relationship:

OpenID Connect
      β”‚
      └──── Built on top of OAuth 2.0
            β”‚
            └──── Adds identity layer

What You Get From Each

OAuth 2.0 gives you:

OIDC adds:

MS Entra Authentication Endpoints

MS Entra supports two endpoint versions:

v1.0 Endpoint (Legacy)

v2.0 Endpoint (Current)

Use v2.0 for new applications. It supports:

  • Personal Microsoft accounts

  • Better token structure

  • Incremental consent

  • PKCE support

Well-Known Configuration

MS Entra publishes its configuration at:

Real example:

OAuth 2.0 Flows in MS Entra

MS Entra supports multiple OAuth flows. Choosing the right one is critical.

Flow Selection Matrix

Let me explain each flow with real implementations.

1. Authorization Code Flow with PKCE

Best for: Single-page applications (SPAs), mobile apps, desktop apps

Why: Most secure for public clients that can't keep secrets

The Problem It Solves

Public clients (SPAs, mobile apps) can't securely store client secrets. Any secret in JavaScript or a mobile app can be extracted. PKCE (Proof Key for Code Exchange) solves this.

How PKCE Works

Complete React SPA Example

The Flow Diagram

2. Authorization Code Flow (Without PKCE)

Best for: Traditional web applications with server-side code

Why: Server can securely store client secret

Node.js Express Example

3. Client Credentials Flow

Best for: Daemon apps, background services, service-to-service communication

Why: No user involved, app authenticates as itself

When to Use

  • Scheduled jobs processing data

  • Backend service calling another backend service

  • CI/CD pipelines accessing resources

  • Monitoring/alerting systems

Implementation Example

Service-to-Service Communication

4. On-Behalf-Of (OBO) Flow

Best for: Middle-tier APIs that need to call downstream APIs on behalf of the user

Why: Maintains user context through the call chain

The Scenario

User's identity and permissions flow through the entire chain.

Implementation

OBO Token Claims

The user's identity flows through. Payment service sees who the end user is, not just the API gateway.

5. Resource Owner Password Credentials (ROPC)

⚠️ AVOID THIS FLOW

Why it exists: Legacy support for apps that can't use redirect-based flows

Why to avoid:

  • ❌ App handles user's password (security risk)

  • ❌ No MFA support

  • ❌ No Conditional Access support

  • ❌ Goes against modern security best practices

Only use if: You absolutely cannot use any other flow (very rare)

Instead, use device code flow for CLI tools or other alternatives.

6. Device Code Flow

Best for: Devices without browsers (IoT, CLI tools, smart TVs)

Why: User authenticates on a separate device

How It Works

CLI Tool Example

SAML Integration

While OAuth 2.0 is the modern standard, many enterprises still use SAML for SSO.

SAML vs OAuth 2.0

When MS Entra Uses SAML

Scenario 1: Enterprise App SSO

Scenario 2: Your App as Service Provider

SAML Configuration in MS Entra

Implementing SAML in Node.js

Flow Comparison Summary

Common Pitfalls

Pitfall 1: Using Implicit Flow

Pitfall 2: Wrong Scopes

Pitfall 3: Not Validating Tokens

Key Takeaways

  1. Use Authorization Code + PKCE for SPAs: Most secure for public clients

  2. Client Credentials for Services: No user involved

  3. On-Behalf-Of for API Chains: Maintain user context

  4. Avoid Implicit Flow: Deprecated and insecure

  5. SAML for Enterprise SSO: Still common in enterprises

  6. Always Validate Tokens: Never trust without verification

What's Next

In Part 4: Tokens and Token Management, we'll dive deep into:

  • JWT structure and claims

  • Access tokens vs ID tokens vs refresh tokens

  • Token validation best practices

  • JWKS and key rotation

  • Token lifetime management

  • Caching strategies

We'll dissect tokens to understand exactly what's inside and how to work with them securely.


Previous: Part 2: Applications and Service Principals Next: Part 4: Tokens and Token Management Back to Series Overview

Last updated