liveBook · Manning

Authorization code flow

As explained in Secure APIs

The authorization code flow is an OAuth authorization flow in which a client obtains an authorization code through user authorization and exchanges it for tokens.

The authorization code flow lets a client obtain an authorization code after a user authenticates and grants access, then exchange that code for an access token. It suits confidential clients such as web servers because the client can protect a secret. The browser carries out redirects, while the server performs the token exchange. section 7.4.1 section 7.8

A typical authorization code flow proceeds as follows:

  • The application redirects the user to the authorization server with response_type=code, client_id, and redirect_uri. section 7.4.1
  • The authorization server authenticates the user, requests consent, and redirects back with an authorization code. section 8.4.1
  • The server exchanges the code, client ID, client secret, and matching redirect URI for an access token. section 8.4.1
  • PKCE strengthens the exchange by requiring a code verifier, helping prevent access-token hijacking through CSRF attacks. section 7.8

FastAPI login with Auth0

A FastAPI server exposes GET /login and redirects the user to Auth0’s authorization endpoint. The request includes response_type=code, the client ID, a configured redirect URI, and an audience. After Auth0 returns the code, GET /token sends it to the token endpoint with the client ID and protected client secret, receiving the access token. section 8.4 section 8.4.1

https://auth.example.com/authorize?response_type=code
&client_id=s6BhdRkqt3&redirect_uri=https://example.com

Use the authorization URL as a compact view of the initial request and its required parameters. section 7.4.1

How Secure APIs covers it

How deep it goes
Explains redirects, request parameters, consent, code exchange, secrets, PKCE, and a FastAPI implementation.
Examples
Uses a job portal accessing LinkedIn, Auth0 redirects, FastAPI routes, authorization URLs, and token-exchange requests.
What you can do after
Apply it via FastAPI and Auth0 by constructing a login redirect and exchanging the returned code for an access token.
How it connects
Connects OAuth 2.1 with OIDC, authorization servers, access tokens, client secrets, redirect URIs, and PKCE.

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 Secure APIs. The section links point to the parts of the book it draws on.