liveBook · Manning

Authorization code flow

As explained in Spring Security in Action, Second Edition

The authorization code grant type is an OAuth flow in which a client obtains a token through an authorization code, commonly after user authentication.

The authorization code flow authenticates a user through a separate authorization server before the client receives an access token for its backend requests. It is the most commonly used grant type for user authentication. section 13.3 section 13.3.1

The flow proceeds in a browser and then through a token request:

  • Redirect the user from the client to the authorization server's login page. section 13.3.1
  • After successful authentication, redirect the browser back with a unique authorization code. section 13.3.1
  • Exchange the code for an access token; the client verifies its identity with credentials during this request. section 13.3.1
  • Use the access token when requesting authorized data from the resource server. section 13.3.1

Invoice application

Mary opens an invoices web or mobile application without being logged in. The application redirects her to central authentication; after login, it receives a code, exchanges it for a token, and requests her invoices. section 13.3.1

Figure 13.9 The authorization code grant type. The user is required to sign in. After login, the authorization server issues an authorization code to the client, which the client then utilizes to obtain an access token. This access token enables the client to have its requests authorized by the resource server.

Follow the sequence diagram to distinguish browser redirects from token requests and resource-server calls. section 13.3.1

How Spring Security in Action, Second Edition covers it

How deep it goes
Traces redirects, code issuance, credentialed token exchange, and API authorization, while treating PKCE as supplementary protection.
Examples
Uses a web or mobile invoices application in which Mary signs in before viewing company invoices.
What you can do after
Apply it via a client that exchanges a returned authorization code for an access token and calls protected backend endpoints.
How it connects
Connects OAuth 2, OpenID Connect, authorization servers, resource servers, access tokens, client credentials, PKCE, and refresh tokens.

Also explained in

Compare how 8 books explain authorization code flow →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.