0Pricing
OAuth2 & OpenID Connect Deep Dive · Lezione

Token di accesso, token di refresh e scope

Comprenda quali token rilascia OAuth2, come i token di refresh mantengono attive le sessioni e come gli scope limitano ciò che un token può fare.

Token di accesso, token di refresh e scope è una lezione OAuth2 & OpenID Connect Deep Dive gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento OAuth2 & OpenID Connect Deep Dive, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso OAuth2 & OpenID Connect Deep Dive include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Domande Frequenti

La lezione «Token di accesso, token di refresh e scope» è gratuita?

Sì — il testo completo di «Token di accesso, token di refresh e scope» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso OAuth2 & OpenID Connect Deep Dive, passa a CoddyKit PRO. Il corso OAuth2 & OpenID Connect Deep Dive include 4 lezioni in totale.

Cosa imparerò in «Token di accesso, token di refresh e scope»?

Comprenda quali token rilascia OAuth2, come i token di refresh mantengono attive le sessioni e come gli scope limitano ciò che un token può fare. Eserciti OAuth2 & OpenID Connect Deep Dive con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare OAuth2 & OpenID Connect Deep Dive?

Non è richiesta alcuna esperienza precedente. OAuth2 & OpenID Connect Deep Dive su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Token di accesso, token di refresh e scope»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione OAuth2 & OpenID Connect Deep Dive?

Sì. Ogni lezione OAuth2 & OpenID Connect Deep Dive include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. OAuth2: il protocollo di delega
  2. Ruoli e terminologia di OAuth2
  3. Panoramica dei principali grant type
  4. Token di accesso, token di refresh e scope
← Torna a OAuth2 & OpenID Connect Deep Dive