Refresh Token & Access Token

brandon·2025년 6월 19일

jwt

목록 보기
2/5

Access Token

An access token is like a digital key that grants an application temporary access to specific protected resources (like user data, APIs, or services) on a server.

Key characteristics of an Access Token:

  • Purpose: Primarily used for authorization. When an application needs to access a user's resources, it sends the access token with its request. The server then validates the token to ensure the application has the necessary permissions.
  • Short-Lived: For security reasons, access tokens typically have a short lifespan (e.g., minutes to a few hours). This limits the potential damage if an access token is compromised, as it will quickly expire and become invalid.
  • Bearer Token (Common Type): Often, access tokens are "bearer tokens," meaning anyone who possesses the token can use it. This is why their short lifespan is crucial.
  • Opaque (Often): Access tokens are often "opaque," meaning their internal structure isn't meant to be parsed or understood by the client application. The server is the one that validates and interprets the token.
  • Stateless (Often): If they are JSON Web Tokens (JWTs), they are self-contained and don't require the server to store session information, making them stateless.
  • Transmission: Usually sent in the HTTP Authorization header of API requests (e.g., Authorization: Bearer <access_token>).

Refresh Token

A refresh token is a special, long-lived token used to obtain new access tokens after the current access token expires, without requiring the user to re-enter their credentials.

Key characteristics of a Refresh Token:

  • Purpose: Its main job is to "refresh" or renew access tokens. It's not directly used to access protected resources.
  • Long-Lived: Refresh tokens have a significantly longer lifespan than access tokens (e.g., days, weeks, or even months). This allows users to remain logged in for extended periods without frequent re-authentication.
  • Secure Storage: Because of their long lifespan and ability to grant new access tokens, refresh tokens must be stored very securely on the client side (e.g., in HttpOnly cookies for web applications, or secure storage for mobile apps) and are typically tracked and managed by the authorization server.
  • One-Time Use (Often): In many secure implementations, a refresh token is invalidated after it's used to obtain a new access token, and a new refresh token is issued. This is called "refresh token rotation" and further enhances security.
  • Used with Authorization Server: When an access token expires, the client sends the refresh token to the authorization server's token endpoint to request a new access token (and often a new refresh token as well).

How They Work Together (The Flow)

  1. Initial Authentication: A user logs into an application (e.g., with username and password).
  2. Token Issuance: Upon successful authentication, the authorization server issues both an access token and a refresh token to the client application.
  3. Resource Access: The client uses the access token to make requests to protected resources. The server validates the access token with each request.
  4. Access Token Expiration: When the access token expires, the client can no longer use it to access resources.
  5. Token Refresh: Instead of prompting the user to log in again, the client sends the refresh token to the authorization server.
  6. New Tokens: If the refresh token is valid, the authorization server issues a new access token (and often a new refresh token) to the client.
  7. Continued Access: The client can then use the new access token to continue accessing resources without interrupting the user's session.

Why Use Both?

  • Enhanced Security: By having short-lived access tokens, the window of opportunity for an attacker to exploit a stolen token is significantly reduced. Even if an access token is compromised, it will quickly expire.
  • Improved User Experience: Long-lived refresh tokens allow users to stay logged in for extended periods without needing to repeatedly enter their credentials, leading to a smoother user experience.
  • Separation of Concerns: Access tokens are for authorizing immediate access to resources, while refresh tokens are for managing long-term sessions and re-issuing access. This separation helps to limit the scope of each token's power.

In summary, access tokens are like temporary entry passes to a building, while refresh tokens are like special VIP cards that let you get a new entry pass without going through the main security check every time. This dual-token system balances security with user convenience in modern authentication flows.

profile
everything happens for a reason

0개의 댓글