OAuth 2 and OpenID Connect Flows

Topics Covered

Delegation and the OAuth Roles

The problem OAuth solves

The four roles

Clients, public and confidential

Scopes and consent

Front channel and back channel

Access tokens and their audience

Refresh tokens in OAuth

Authorisation is not authentication

Standards and profiles

Why flow choice matters

Building versus integrating

A shared vocabulary

OAuth 2 and OpenID Connect sit underneath almost every login button and API integration on the modern web. OAuth 2 is a framework for delegated authorisation: it lets a user grant an application limited access to their data on another service without handing over a password. OpenID Connect, OIDC, adds an identity layer on top so applications can also learn who the user is. Both are flexible, and most of their security problems come from choosing the wrong flow or skipping a check. This lesson explains the roles, the flows that remain recommended, the identity layer and the mistakes reviewers find most often.

The problem OAuth solves

Before OAuth, an application that wanted to read a user calendar or post to their account asked for the user password and logged in as them. That gave the application full access, forever, to everything the account could do, and the only way to revoke it was to change the password, which broke every other integration too. The password also sat in the third party database, waiting to leak. OAuth replaces this with tokens that are limited in scope and time, issued with the user consent and revocable one application at a time.

Holding the password gives an app full access with no expiry, and revoking it means changing the password for every app. An OAuth grant gives one scope, hour long tokens and per app revocation.

The four roles

OAuth defines four roles. The resource owner is usually the user, who owns the data. The client is the application that wants access. The authorisation server authenticates the user, asks for consent and issues tokens. The resource server hosts the protected API and accepts access tokens. In many deployments one company runs both the authorisation and resource servers, and in enterprise single sign on the authorisation server is an identity provider used by many internal applications. Naming the roles precisely makes it much easier to reason about who must check what.

Clients, public and confidential

Clients come in two kinds. Confidential clients run on servers and can keep a secret, so they authenticate to the authorisation server with a client secret, a private key or mutual TLS. Public clients, such as single page applications, mobile apps and desktop tools, run on devices the user controls and cannot keep a secret, because anything shipped in the app can be extracted. Public clients need flows that stay safe without a secret, which is the main reason PKCE exists. Treating a mobile app as a confidential client with an embedded secret is a common and serious design error.

A scope names a slice of access, such as calendar read or repo write. The client requests scopes, the authorisation server shows them to the user on a consent screen and the issued token carries the granted scopes. Resource servers must check scopes on every request. Good scope design is narrow and readable: users should understand what they approve, and clients should be able to ask for the minimum. Broad scopes such as full account access make consent meaningless and make every token a high value target.

The user approves at the authorisation server, the browser carries a short lived code back to the client, and the client trades it for tokens on a direct back channel call before calling the calendar API.

Front channel and back channel

OAuth flows move information over two kinds of channel. The front channel is the browser: redirects and URL parameters that pass through the user agent, where they can be observed, logged or tampered with. The back channel is a direct HTTPS call between the client and the authorisation server, which is far harder to intercept. Secure flows put only short lived, single use values such as authorisation codes on the front channel and keep tokens on the back channel. Older flows that returned tokens directly in redirects are now discouraged for exactly this reason.

Access tokens and their audience

The access token is what the client presents to the resource server. It may be an opaque string or a JWT, and the client should treat it as opaque either way, since its format belongs to the authorisation and resource servers. Each token should be issued for a specific audience, the resource server that will accept it, and should carry only the scopes needed. A token issued for one API and accepted by another, because that API never checked the audience, is one of the most common integration bugs.

Refresh tokens in OAuth

Authorisation servers can issue refresh tokens alongside access tokens, letting the client get new access tokens without involving the user. As the previous lesson explained, refresh tokens for public clients should be rotated on each use with reuse detection, and those for confidential clients should be bound to the client authentication. The authorisation server should let users and administrators see and revoke grants, which revokes the associated refresh tokens. Without that, a grant made years ago to an abandoned application can keep working indefinitely.

Authorisation is not authentication

Plain OAuth does not tell a client who the user is. An access token proves that someone granted access, not that the person currently using the client is that someone. Early sign in with integrations that treated any valid access token as proof of identity could be tricked: an attacker who obtained a token for their own account from another application could present it and log in as the victim, or a token issued to a different client could be replayed. OpenID Connect was created to give login a proper, verifiable identity assertion.

Standards and profiles

OAuth 2 is a family of specifications rather than a single document: the core framework, bearer token usage, PKCE, token introspection, revocation, device authorisation, token exchange, the security best current practice and the in progress OAuth 2.1, which consolidates the safe parts and removes the unsafe ones. Industry profiles such as FAPI tighten the rules further for financial and other high value APIs, requiring sender constrained tokens and signed requests. Teams should target OAuth 2.1 behaviour and the security best current practice even when their provider still supports older options.

Why flow choice matters

Most real OAuth vulnerabilities come from choosing a flow designed for another kind of client, leaving a deprecated flow enabled or skipping one validation step. A single page application using the implicit flow exposes tokens in URLs. A mobile app using a client secret ships that secret to every user. A server that accepts any redirect URI lets attackers steal codes. Understanding why each flow is shaped the way it is makes these mistakes easy to spot in design reviews, and the following sections walk through each in turn.

Building versus integrating

Very few teams should write their own authorisation server. Mature identity providers and open source servers implement the protocols, consent, key rotation and token lifecycle correctly and receive regular security review. The engineering work that remains is integration: registering clients with the right type and redirect URIs, choosing flows, validating tokens in resource servers and mapping identity claims to application accounts. That integration is where this lesson focuses, because it is where most teams make their mistakes.

A shared vocabulary

Reviews of OAuth designs go faster when everyone uses the same terms. A grant is the permission the user gave a client. A grant type, or flow, is the protocol exchange used to obtain tokens. An authorisation code is the short lived value returned on the front channel. A redirect URI is where the authorisation server sends the browser back. A client identifier is public, and a client secret is not. Using these words precisely in design documents prevents the confusion that leads to missing checks.

Delegation, not passwords

OAuth lets users grant limited, revocable access without sharing passwords. Know the four roles, separate public from confidential clients, design narrow scopes, keep tokens off the front channel, check audience at every resource server, let users revoke grants and use OpenID Connect, not plain OAuth, when you need to know who the user is.