0Pricing
OAuth2 & OpenID Connect Deep Dive · Lesson

Access Tokens, Refresh Tokens & Scopes

Understand the tokens OAuth2 issues, how refresh tokens keep sessions alive, and how scopes limit what a token can do.

Access Tokens, Refresh Tokens & Scopes is a free OAuth2 & OpenID Connect Deep Dive lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the OAuth2 & OpenID Connect Deep Dive learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Tokens Are the Currency of OAuth2

After a successful flow, OAuth2 hands the client a token instead of the user's credentials. The client presents this token to the resource server to access protected data.

The Access Token

An access token is a short-lived credential proving the client may call an API on the user's behalf. It is sent on each request, usually in the Authorization header.

GET /api/profile
Authorization: Bearer eyJhbGciOi...

Why Tokens Expire

Access tokens are deliberately short-lived (minutes to an hour). If one leaks, the window of misuse is small. But forcing the user to re-login constantly would be painful.

The Refresh Token

A refresh token is a longer-lived credential the client uses to obtain new access tokens without bothering the user. It is stored securely and never sent to APIs.

Refreshing in Practice

When the access token expires, the client posts the refresh token to the token endpoint and receives a fresh access token.

POST /oauth/token
grant_type=refresh_token
&refresh_token=def502...
&client_id=my-app

What Scopes Are

Scopes declare exactly what a token may do, such as read:profile or write:orders. They implement least privilege at the token level.

scope=read:profile write:orders

Requesting Scopes

The client requests scopes during authorization. The user consents, and the issued token is limited to the granted scopes, no more.

GET /authorize?response_type=code
  &client_id=my-app
  &scope=read:profile
  &redirect_uri=https://app/cb

Enforcing Scopes

The resource server checks that the token carries the scope an endpoint requires. A token with only read:profile is rejected from a write endpoint.

if (!token.getScopes().contains("write:orders")) {
    return forbidden();
}

Reference vs Self-Contained

An access token may be a random reference the server looks up, or a self-contained JWT carrying claims the server validates by signature without a lookup.

Revoking Tokens

Refresh tokens can be revoked to end a session, for example on logout or suspected compromise. This invalidates the ability to mint new access tokens.

POST /oauth/revoke
token=def502...
&token_type_hint=refresh_token

Token Storage Matters

Store tokens carefully: never in plain localStorage for sensitive apps, keep refresh tokens server-side or in secure storage, and always use HTTPS in transit.

Quick Check

An access token has expired but you do not want the user to log in again. What does the client use to get a new one?

Recap

You learned how OAuth2 tokens work:

  • Access tokens are short-lived and sent to APIs
  • Refresh tokens silently obtain new access tokens
  • Scopes enforce least privilege per token
  • Tokens can be reference-based or JWTs
  • Revocation and secure storage protect sessions

These concepts underpin every OAuth2 grant type you will study next.

Frequently asked questions

Is the “Access Tokens, Refresh Tokens & Scopes” lesson free?

Yes — the full text of “Access Tokens, Refresh Tokens & Scopes” is free to read here on the web, and the OAuth2 & OpenID Connect Deep Dive course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the OAuth2 & OpenID Connect Deep Dive course, upgrade to CoddyKit PRO.

What will I learn in “Access Tokens, Refresh Tokens & Scopes”?

Understand the tokens OAuth2 issues, how refresh tokens keep sessions alive, and how scopes limit what a token can do. You practise OAuth2 & OpenID Connect Deep Dive with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start OAuth2 & OpenID Connect Deep Dive?

No prior experience is required. OAuth2 & OpenID Connect Deep Dive on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Access Tokens, Refresh Tokens & Scopes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this OAuth2 & OpenID Connect Deep Dive lesson?

Yes. Every OAuth2 & OpenID Connect Deep Dive lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. OAuth2: The Delegation Protocol
  2. OAuth2 Roles & Terminology
  3. Core Grant Types Overview
  4. Access Tokens, Refresh Tokens & Scopes
← Back to OAuth2 & OpenID Connect Deep Dive