Course contentsShow
← Security
Lesson 760 of 3,05216. Web Security - Authentication MechanismsFree lesson

OAuth 2.0 Tokens: Access and Refresh

Access token purpose and lifetime, refresh token rotation, token storage considerations, and revocation mechanisms.

OAuth 2.0 Tokens: Access and Refresh

What you'll learn: How OAuth 2.0 uses access tokens for API authorization and refresh tokens for obtaining new credentials without re-authentication.

Two Types of Tokens

When OAuth 2.0 completes an authorization flow (like the Authorization Code Flow you've learned), it issues two distinct tokens:

Access tokens are short-lived credentials (typically 15 minutes to 1 hour) that your application presents to protected APIs. Think of them like a temporary badge at a conference—it grants you access for a limited time, then expires.

Refresh tokens are longer-lived credentials (days to months) used solely to request new access tokens. They're like a ticket you can exchange for a new badge when yours expires, without going through security screening again.

Token Lifecycle and Storage

Access tokens should be kept in memory when possible—never localStorage for browser apps (vulnerable to XSS). For mobile/native apps, secure storage mechanisms (like OS keychains) are appropriate.

Refresh tokens are more sensitive since they grant long-term access. Store them encrypted and treat compromise seriously. Many implementations use refresh token rotation: each time you use a refresh token, the server issues a new refresh token and invalidates the old one, limiting replay attack windows.

Revocation Mechanisms

OAuth 2.0 providers offer token revocation endpoints where clients can explicitly invalidate tokens during logout or when compromise is detected. Users can also revoke application access through provider dashboards (like "Connected Apps" in Google).

Servers enforce revocation by maintaining token blacklists or checking against database state. The short lifetime of access tokens means they "self-destruct" even if revocation isn't immediate.

Key Takeaway: Access tokens are short-lived credentials for API calls, while refresh tokens enable obtaining new access tokens without re-authentication—proper storage and rotation of both is critical for OAuth 2.0 security.