Book a 30-min callBook a call

OAuth 2.0 and OpenID Connect explained for developers: flows, tokens and PKCE

OAuth 2.0 and OpenID Connect for developers: the authorization code flow with PKCE, access, ID and refresh tokens, JWT checks, device flow, storage and logout.

A user circle, a client box, an authorization server box with a padlock and an API box, with a key passing from the server to the client and on to the API.
Written by
BYD Tech
Published
Topic
Deep dives
Reading time
15 min read

Questions people ask

What is the difference between OAuth 2.0 and OpenID Connect?

OAuth 2.0 is delegated authorization: it gives an app an access token to call an API on a user’s behalf. OpenID Connect is an identity layer on top of OAuth that also returns an ID token, a signed JWT saying who the user is and how they signed in. Use OIDC for login, OAuth scopes for API access.

Do single-page apps need PKCE?

Yes. A single-page app is a public client, and RFC 9700 requires PKCE for public clients. Better still, RFC 10017 strongly recommends a backend-for-frontend for business and sensitive apps, so the tokens never reach the browser at all.

Why not store tokens in localStorage?

Any script running on the page can read localStorage, so one injected script can steal every token there. RFC 10017 notes that no browser storage fully stops theft once an attacker runs code in the page, which is why a backend-for-frontend with an HttpOnly session cookie is the stronger design.

Can I use an ID token to call my API?

No. An ID token is addressed to the client, so its audience is the client ID, not your API. Send the access token to the API, and have the API check that the access token’s audience names the API.

What is refresh token rotation?

Each time a client uses a refresh token, the server returns a new one and invalidates the old one. If an old token is ever presented again, the server knows two parties hold copies and revokes the active token for that grant, forcing a fresh sign-in.

Building something like this?