Card 07/ 11

ComparisonDifficulty: Intermediate2 min

Access Token vs ID Token vs Refresh Token

One is for the API, one is for the app, and one is for getting more of the first.

An OAuth and OpenID Connect flow can return three tokens, each with one audience and one job. The access token is for the API, sent on each call. The ID token is for the client, read once to learn who signed in. The refresh token is for the authorization server, exchanged for a new access token when the old one expires. Sending one where another belongs is a common and serious mistake.

The three tokens compared on who they are for, who reads them, how long they last, and where they must never go
PropertyAccess tokenID tokenRefresh token
Defined byOAuth 2.0OpenID ConnectOAuth 2.0
Sent toThe resource server, on every callNobody; the client keeps itThe authorization server's token endpoint
Read byThe APIThe clientThe authorization server
FormatOpaque or JWT, the API's choiceAlways a JWTUsually opaque
Typical lifetimeMinutes to an hourMinutes; used once at sign-inDays to months, often rotated on use
Must neverBe read by the client to identify the userBe sent to an API as an access tokenLeave the client or be sent to an API

Access token. Its audience is the API. The client should treat it as opaque even if it happens to be a JWT, because the API may change its format.

ID token. Its audience is the client, as its aud says. An API that accepts ID tokens as credentials accepts tokens that were never issued to it and carry no scopes.

Refresh token. It is the most powerful of the three, because it mints new access tokens. Keep it in the most protected storage the client has; for browser apps, current guidance is a back end that holds it, or rotation, where each use returns a new refresh token and invalidates the old.

Choose by audience. API call: the access token. Who signed in: the ID token. Access token expired: the refresh token, at the token endpoint.