Overview

12 Single Sign On (SSO) using OAuth2 and OpenID Connect

Modern enterprises and consumer apps benefit from Single Sign-On, where a user authenticates once and seamlessly accesses multiple applications. The chapter motivates this with employee and customer scenarios, then shows how to decouple authentication from individual apps by delegating it to a dedicated Identity Provider. Centralizing authentication improves usability and reduces credential sprawl, while enabling consistent, stronger controls. Applications rely on the Identity Provider to verify identity and issue tokens that act like access cards, which other services can use to authorize requests.

The chapter frames SSO with OAuth 2 and OpenID Connect, introducing the four main actors: user, client, Identity Provider, and resource server. It explains the authorization code flow: redirect the user to authenticate, return a one-time authorization code, exchange it for an access token, then call the backend with that token. Tokens are commonly JWTs signed with rotating asymmetric keys so resource servers can validate integrity and issuer authenticity via public keys. It contrasts this with token introspection (ask the Identity Provider each time) and notes its coupling, latency, and exposure trade-offs. It also clarifies token types and lifetimes, why the implicit flow is deprecated due to leakage risks, and how OpenID Connect adds an ID token alongside the access token.

Finally, the chapter applies these concepts to an Acme Inc. system using a dedicated Identity Provider and standard client configuration: defining users and clients, allowed grant types, redirect URIs, and scopes. It demonstrates the end-to-end path—discovering endpoints, initiating the authorization request, authenticating, obtaining the authorization code, exchanging it at the token endpoint, and inspecting the resulting JWT claims (such as subject, audience, issuer, and expiration) identified by a key ID for verification. It also covers the client credentials flow for service-to-service calls (for example, payments), where no end user is involved and a service authenticates directly to obtain a token for backend-to-backend requests.

Jeanny uses multiple apps as part of her daily work routine, requiring her to authenticate into each one individually every day.
Patrick shops online, listens to music on various platforms, and purchases tickets for shows and movies, all using different apps and websites. Since he already has a Gmail account, he prefers to authenticate across these services using his Gmail credentials.
Before accessing an app, the Identity Provider (IdP) authenticates the user. The IdP handles the authentication process, while the other apps rely on it to provide the necessary details for authorizing the user to use the specific features they offer.
Authenticating using a known social media account. Patrick uses his Google credentials to sign into multiple apps. In this case, Google acts as the IdP. The other apps Patrick uses are designed to let users, like him, authenticate with their Google credentials. Generally, apps offer multiple authentication options, including social media platforms and email services.
The user authenticates with the IdP, which then issues a token. This token serves as proof of the user's identity and successful authentication.
The authorization code grant type. The user verifies their identity to access and work with data managed by the app’s backend.
Verifying the authenticity of a signed token. When the resource server receives a signed token, it uses a public key to validate the token. The resource server obtains this public key from the IdP, allowing it to verify that the token is valid.
Token Introspection. When a token isn’t cryptographically signed, the resource server must directly query the IdP to validate the token. This process is known as token introspection.
Suppose Acme Inc. needs to issue payments. In this scenario, our app must communicate with a specialized service known as a payment service provider. This provider must verify that the requests are indeed coming from Acme Inc. and not another service. Additionally, it needs to ensure that the data has not been altered during transmission.
Acme Inc. authenticates against an Identity Provider (IdP) and obtains a token. The app then uses this token to send requests to another service, which authorizes them.

Summary

  • Single Sign-On (SSO) simplifies user authentication by allowing employees and customers to use a single set of credentials for multiple applications.
  • OAuth2 and OpenID Connect are protocols used to implement SSO by separating authentication responsibilities into an Identity Provider (IdP).
  • Among the authentication flows the OAuth 2 specification describes the most used ones are:
    • The authorization code grant type is the most common, used when user interaction is required.
    • The client credentials grant type is used when one application needs to authenticate another without user involvement.
  • Using OAuth 2/OpenID Connect implies multiple actors in the authentication flow:
    • The user (e.g., employee or customer)
    • The client (app or service used)
    • The IdP (also known as authorization server) – a service offering authentication capabilities to multiple clients.
    • The resource server - app backend that handles data and authorization
  • Tokens are central to OAuth2 and OpenID Connect:
    • Access tokens which are used for authorization
    • ID tokens which contain user information
  • JWTs (JSON Web Tokens) are commonly used for token formatting and include a header (metadata), a payload (user/client details), and a signature for validation.
  • Token validation involves cryptographic methods or token introspection, ensuring secure communication between applications.

FAQ

What is Single Sign-On (SSO) and why is it useful?SSO lets a user authenticate once with a trusted Identity Provider (IdP) and then access multiple applications without logging in again. It reduces password fatigue, streamlines access for employees using many internal apps, and enables customers to reuse existing accounts (for example, Google) across services.
What is an Identity Provider (IdP)? Is it the same as an Authorization Server?An IdP is a dedicated application that authenticates users and issues tokens as proof. Other apps rely on it to verify identity and drive authorization decisions. Some people call it an Authorization Server; this chapter consistently uses the term IdP.
Who are the main actors in OAuth 2.0 and OpenID Connect?- User: the person using the app - Client: the application the user interacts with (web or mobile) - Identity Provider (IdP): authenticates users and issues tokens - Resource Server: the backend API that enforces authorization and serves data
How does the authorization code grant type work?- The user asks the client to access protected data. - The client redirects the user to the IdP’s login page. - The user authenticates at the IdP. - The IdP redirects back with a one-time authorization code. - The client exchanges that code with the IdP for an access token. - The client calls the resource server, presenting the access token. - The resource server validates the token and returns the data.
Why is the implicit grant type deprecated?It returns the access token via a browser redirect URL, which risks token exposure: - Tokens can leak through browser history or plugins. - URLs might be logged. - URLs can be mistakenly forwarded. The authorization code exchange avoids sending the access token in the redirect URL.
How does a resource server validate an access token?Commonly, the IdP signs tokens with a private key and publishes matching public keys. The resource server: - Reads the token’s key ID (kid), - Fetches the corresponding public key from the IdP, - Verifies the signature to ensure authenticity and integrity. If tokens are opaque (not signed/self-describing), the resource server can use token introspection by asking the IdP directly if the token is valid.
What are access tokens and ID tokens, and how are they used?- Access token: presented to the resource server to authorize API calls. - ID token: carries identity details about the authenticated user. With OpenID Connect, both are typically JWTs and serve complementary roles.
What’s the difference between opaque and non-opaque (JWT) tokens?- Non-opaque (e.g., JWT): self-contained; include header and payload claims; can be validated locally with the IdP’s public key. - Opaque: carry no readable claims; must be validated via token introspection with the IdP. Opaque tokens increase coupling and latency but can centralize validation.
How long do access tokens last, and what limits misuse if they’re stolen?Access tokens are short-lived (often an hour or less). Short lifetimes reduce the window of misuse. Signed tokens prevent tampering, and avoiding token delivery via browser URLs (using the code exchange) helps prevent leakage.
When should I use the client credentials grant type?Use it for machine-to-machine scenarios with no user involved (for example, an internal service calling a payment provider). The client authenticates to the IdP, obtains an access token, and uses it to call the resource server, which authorizes the request.

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
  • Software Security for Developers 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
  • Software Security for Developers ebook for free