Skip to content
AI Engineer Guide.

concept · basics · intermediate · concept 7 of 24

OAuth & OIDC

0 of 24 complete

Why it matters

Delegating access without sharing a password requires a protocol with several parties: the client, the authorisation server and the resource server. Each has a different trust level, and the tokens are scoped to a party and a purpose. Implementations fail when they conflate those purposes, for example by accepting an access token as an identity assertion, or by shipping a client secret in a mobile app that anyone can decompile.

Mental model

It is a coat check: you get a ticket for your coat, and the ticket is only good at that counter. The analogy breaks because OAuth has several kinds of ticket with different audiences, and presenting the wrong kind at the wrong counter is exactly how implementations get breached.

How it works

Mechanism, step by stepInteractive
  1. 1Choose the flow by client type: authorisation code with proof key exchange for public clients, and never the implicit flow.
  2. 2Request the minimum scopes, and treat scope as the authorisation contract with the resource server.
  3. 3Validate the ID token signature, issuer, audience, expiry and nonce before trusting any claim.
  4. 4Keep access tokens opaque to the client, and send them only to the resource server they were issued for.
  5. 5Rotate refresh tokens on use and revoke the family when a reused token is detected.
  6. 6Validate redirect URIs exactly, and reject any that are not pre-registered.

Step 1 of 6

Choose the flow by client type: authorisation code with proof key exchange for public clients, and never the implicit flow.

Step 1/6 · step 1

Figure. The mechanism in this page's own steps. The figure adds sequence, not new claims.

What to notice. A step can be read alone, but the order is the mechanism. The control lets you stop on any one.

Text version

The page's steps: 1. Choose the flow by client type: authorisation code with proof key exchange for public cli… 2. Request the minimum scopes, and treat scope as the authorisation contract with the resour… 3. Validate the ID token signature, issuer, audience, expiry and nonce before trusting any c… 4. Keep access tokens opaque to the client, and send them only to the resource server they w… 5. Rotate refresh tokens on use and revoke the family when a reused token is detected. 6. Validate redirect URIs exactly, and reject any that are not pre-registered.

  1. Choose the flow by client type: authorisation code with proof key exchange for public clients, and never the implicit flow.
  2. Request the minimum scopes, and treat scope as the authorisation contract with the resource server.
  3. Validate the ID token signature, issuer, audience, expiry and nonce before trusting any claim.
  4. Keep access tokens opaque to the client, and send them only to the resource server they were issued for.
  5. Rotate refresh tokens on use and revoke the family when a reused token is detected.
  6. Validate redirect URIs exactly, and reject any that are not pre-registered.

Trade-offs

ChoiceGood forCostFails when
Authorisation code with proof keySafe for public and confidential clientsMore round trips and state handlingThe client cannot open a browser
Client credentialsMachine-to-machine accessNo user contextThe action must be attributed to a person
Short access tokenLimited exposureMore refresh trafficThe client cannot store a refresh token securely
Long-lived tokenFewer moving partsA stolen token is usable for a long timeThe token is held in a browser or device
Trade-off mapInteractive

Good for

Safe for public and confidential clients

Cost

More round trips and state handling

Fails when

The client cannot open a browser

  1. The client cannot open a browser
  2. The action must be attributed to a person
  3. The client cannot store a refresh token securely
  4. The token is held in a browser or device

Figure. Each choice with what it buys, what it costs, and the case where it loses.

What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.

Text version

Four trade-offs: Authorisation code with proof key is good for Safe for public and confidential clients, costs More round trips and state handling, and fails when The client cannot open a browser. Client credentials is good for Machine-to-machine access, costs No user context, and fails when The action must be attributed to a person. Short access token is good for Limited exposure, costs More refresh traffic, and fails when The client cannot store a refresh token securely. Long-lived token is good for Fewer moving parts, costs A stolen token is usable for a long time, and fails when The token is held in a browser or device

Failure modes

  • Access token used as identity. A token minted for an API is accepted as proof of who the user is. Debug: list every place identity is read and confirm it comes from an ID token or a userinfo call.
  • Missing state or nonce. An attacker can inject a code or replay a token into the callback. Debug: assert the authorisation request includes state and nonce and that both are verified.
  • Open redirect URI. A wildcard redirect sends codes to an attacker-controlled host. Debug: diff registered redirect URIs against exact matches and fail on any pattern.

In production

Monitor token issuance, refresh failures and reuse detection, and alert on a spike in reuse, which usually means a token was stolen. Keep the signing keys rotated with an overlap so validation never fails during a rollover.

Common misconceptions

People think: OAuth is a login protocol.

Actually: OAuth delegates access; identity comes from OpenID Connect and the ID token.

People think: A client secret makes a mobile app confidential.

Actually: Anything shipped to a device can be extracted, which is why public clients rely on proof key exchange instead of a secret.

Interview angle

Separate the two tokens by audience and purpose, pick the flow from the client type, and list the validation checks. Mention refresh rotation and exact redirect matching. A login button described as OAuth is not an answer.

Sources