Overview

Chapter 13. User authentication with OAuth 2.0

This chapter explains why OAuth 2.0 is a delegation framework for authorization, not an authentication protocol, even though it often appears in login flows. It distinguishes authentication (proving who the user is and whether they are present) from authorization (granting a client scoped access), and cautions that conflating the two leads to insecure designs. OAuth can, however, serve as a key ingredient within a broader “recipe” for authentication, where additional components supply identity assertions and context that OAuth alone does not provide.

To build authentication on top of OAuth, the chapter remaps roles: the client becomes the relying party (RP) and the authorization server plus protected resource act together as an identity provider (IdP). The user authorizes release of identity data, and the RP receives two artifacts: an access token for APIs and a separate ID token that conveys who authenticated, for whom, and under what conditions. The text catalogs common pitfalls when treating plain OAuth as authentication—such as assuming access tokens prove login, relying on API access as proof of user presence, token injection, missing audience restrictions, tampered user data, and provider-specific variations—and outlines mitigations including using the authorization code flow with state/nonce, validating signed tokens with correct audience and lifetimes, and preferring a standardized identity layer.

OpenID Connect provides that standardized layer by defining a signed ID token (a JWT with issuer, subject, audience, timestamps, and optional authentication context) delivered alongside the OAuth access token, plus a UserInfo endpoint gated by well-known scopes for profile attributes. It adds dynamic discovery and client registration so RPs can interoperate with many IdPs, remains compatible with plain OAuth, and offers advanced options like JWT-based client authentication, signed/encrypted responses, request objects, hybrid flows, and session management. The chapter closes by walking through a minimal implementation: issuing and validating ID tokens, exposing a UserInfo endpoint, and fetching/displaying user attributes—demonstrating how a few targeted additions transform an OAuth deployment into a robust, interoperable login system.

Attempting to make an authentication protocol out of OAuth, and failing
Making an authentication protocol out of OAuth, more successfully
Components of an OAuth-based authentication and identity protocol
A NASCAR-style identity provider selector
WebFinger transforms an email address into a URL
The client page showing a logged-in user
The client showing a successful login and fetch of user information

FAQ

Is OAuth 2.0 an authentication protocol?No. OAuth 2.0 is a delegation protocol for authorization. It issues access tokens for clients to call APIs on a user’s behalf, but it does not tell the client who the user is or whether the user is currently present.
If OAuth 2.0 isn’t authentication, how can I use it to authenticate users?Build an authentication protocol on top of OAuth by: - Treating the OAuth client as the relying party (RP) - Combining the authorization server and protected resource into an identity provider (IdP) - Delegating access to identity data - Adding an ID token for authentication context and using a UserInfo API for profile data OpenID Connect standardizes this pattern.
Why isn’t an access token proof that a user logged in?Because access tokens: - Are opaque to clients and intended for protected resources - Can be issued without interactive user authentication (e.g., refresh tokens, client credentials, assertions) - May be long-lived and outlast the user’s session - Can be injected if a client accepts tokens from untrusted sources
Can calling a “who am I” API with an access token prove the user is currently authenticated?Not reliably. The token might not be fresh, may have been obtained without the user present, and the user is not on the client→resource call path. Use an ID token that conveys the authentication event (issuer, audience, timestamps, nonce) and consider token freshness policy.
What are common pitfalls when using OAuth 2.0 for authentication?- Treating access tokens as proof of authentication - Treating successful API calls as proof of authentication - Allowing access token injection (especially with implicit flow) - Lacking audience restriction (accepting tokens meant for other clients) - Accepting tampered or unverified user info - Implementing a different, incompatible identity API per provider
What is an OpenID Connect ID token and how do I validate it?The ID token is a signed JWT for the RP that describes the authentication event. Validate by: - Verifying the signature with the IdP’s published key - Checking iss is trusted and aud includes your client ID - Ensuring exp/iat (and nbf if present) are valid for current time - Matching the nonce if you sent one - Optionally verifying at_hash/c_hash against the access token/code
Why use both an access token and an ID token?They have different audiences and lifecycles: - ID token → for the client (RP), short‑lived, parseable, carries authentication context - Access token → for protected resources, can remain opaque to the client, usable beyond the login moment This separation preserves privacy and reduces attack surface.
What is the OpenID Connect UserInfo endpoint and how is it used?A standard protected resource that returns the user’s profile claims. Call it with the access token (not the ID token). Access is gated by scopes: - openid: sub - profile: basic profile claims - email: email, email_verified - phone: phone_number, phone_number_verified - address: address object
How do discovery and dynamic registration enable interoperability?- Discover the issuer (via configuration or WebFinger) - Fetch OpenID Provider metadata at {issuer}/.well-known/openid-configuration to learn endpoints and jwks_uri - Perform dynamic client registration to introduce the RP to the IdP This lets one client work with many providers using a single protocol.
How do I mitigate token injection, audience confusion, and tampering?- Use the authorization code flow and accept tokens only from the token endpoint - Use state (and nonce for OpenID Connect) to prevent CSRF/replay - Validate ID token signature, iss, aud, timestamps, and hashes - Require TLS and verify server certificates - Don’t pass access tokens around internally or accept them from untrusted sources - Prefer signed (and optionally encrypted) tokens/user info

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • OAuth 2 in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • OAuth 2 in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • OAuth 2 in Action ebook for free