How can you revoke access tokens and refresh tokens?
Question
How can you revoke access tokens and refresh tokens?
Brief Answer
Revoking access and refresh tokens is a critical security measure to immediately invalidate them, preventing unauthorized access, especially during user logout, permission changes, or security compromises. The methods differ based on the token type and its lifecycle.
1. Revoking Access Tokens (Short-Lived)
- Authorization Server Revocation Endpoint: The primary and most explicit method. Clients send a POST request to the OAuth 2.0 compliant revocation endpoint on the Authorization Server (AS) with the token, telling the AS to invalidate it immediately.
- Resource Server Introspection: Resource Servers (RS) can verify an access token’s status in real-time by calling the AS’s introspection endpoint. If the AS indicates the token is invalid or revoked, the RS denies access, effectively preventing its use.
- Token Expiration (Implicit): Access tokens are designed to be short-lived (e.g., 5-15 minutes). While not a direct revocation, their inherent expiration acts as an implicit invalidation, limiting the window of vulnerability for a compromised token.
2. Revoking Refresh Tokens (Long-Lived)
- Database Deletion or Blacklisting: Refresh tokens are highly sensitive and stored securely by the Authorization Server, often in an encrypted database. To revoke, they are immediately deleted from the database or added to a managed blacklist.
- Impact: It’s crucial to understand that revoking a refresh token immediately invalidates ALL access tokens that were derived from it or could be derived in the future. This forces the user to re-authenticate completely.
Key Security Best Practices & When to Revoke:
- User Logout: Always revoke both access and refresh tokens upon user logout to terminate the session completely.
- Permission Changes: Revoke existing tokens if a user’s roles or permissions change to ensure they only access resources aligned with their updated privileges.
- Compromise: Immediately revoke tokens if a security breach or token compromise is suspected.
- Secure Storage: Refresh tokens must be stored with the highest level of security by the Authorization Server (e.g., encrypted fields, restricted access).
Combining explicit revocation via endpoints with implicit methods like expiration and introspection provides a robust token management strategy.
Super Brief Answer
Token revocation is essential for security to immediately invalidate tokens due to logout, compromise, or permission changes.
- Access Tokens are primarily revoked via the Authorization Server’s revocation endpoint, by resource server introspection, or implicitly by their short expiration.
- Refresh Tokens, being long-lived, are revoked by deleting them from the Authorization Server’s database or adding them to a blacklist. Revoking a refresh token invalidates all derived access tokens.
Detailed Answer
Revoking access and refresh tokens is a critical security practice in any OAuth 2.0 or OIDC implementation. It allows you to immediately invalidate tokens, preventing unauthorized access to resources, especially in cases of compromise, user logout, or permission changes. Understanding the different methods for token revocation is essential for building secure and robust authentication systems.
Direct Summary: How to Revoke Access and Refresh Tokens
Access tokens are primarily revoked by the authorization server directly, often through a dedicated revocation endpoint, or by resource servers using introspection to verify their validity in real-time. Refresh tokens, being long-lived and sensitive, are typically revoked by deleting them from the database where they are stored or by adding them to a blacklist. Both revocation methods are fundamental for preventing further unauthorized use of tokens and managing their lifecycle effectively.
Understanding Token Revocation Mechanisms
Token revocation ensures that even if a token hasn’t expired, it can be immediately invalidated due to security concerns or operational changes. This process differs slightly between access tokens and refresh tokens due to their distinct roles and lifespans.
1. Revoking Access Tokens
Access tokens are short-lived credentials used to access protected resources. Their revocation typically involves the authorization server, which issued them, or the resource server, which validates them.
-
Authorization Server Revocation (Explicit)
Authorization servers commonly expose a revocation endpoint, compliant with standards like OAuth 2.0. Clients can send a POST request to this endpoint, supplying the token they wish to revoke. Upon receiving the request, the authorization server invalidates the token, ensuring it can no longer be used for authentication or authorization. This method provides immediate and explicit control over a token’s validity.
-
Resource Server Introspection (Implicit/Verification)
Resource servers can use introspection to verify an access token’s status in real-time. They send a request to the authorization server’s introspection endpoint, and the server responds with information about the token’s validity, including whether it has been revoked or expired. If the introspection response indicates that a token is invalid or revoked, the resource server can then deny access, effectively revoking its utility for that specific request. While not direct revocation, it ensures revoked tokens cannot be used.
-
Token Expiration (Implicit)
Access tokens are designed with a short lifespan (e.g., 5-15 minutes) to minimize the impact of potential compromise. While not a direct revocation method, expiration acts as an implicit form of invalidation. Once an access token expires, a new one must be obtained, typically using a refresh token. This strategy significantly limits the window of vulnerability for a compromised access token.
2. Revoking Refresh Tokens
Refresh tokens are long-lived credentials used to obtain new access tokens without requiring the user to re-authenticate. Due to their extended validity and power, their secure handling and immediate revocation are paramount.
-
Database Deletion or Blacklisting
Refresh tokens are highly sensitive and should be treated with the same care as user passwords. They are typically stored securely on the authorization server, often in encrypted database fields. When a refresh token needs to be revoked (e.g., user logout, account compromise), it should be immediately deleted from the database or added to a blacklist managed by the authorization server. This prevents it from being used to mint new access tokens.
-
Implications of Revoking Refresh Tokens
It’s crucial to understand that revoking a refresh token invalidates all access tokens derived from it. This action has significant security benefits by cutting off all active sessions associated with that refresh token. However, it will also impact user experience by forcing the user to re-authenticate. In a real-world application, this might involve notifying the user and guiding them through the re-login process. Some systems might explore issuing multiple refresh tokens with different scopes or lifespans to minimize disruption while maintaining security.
Key Security Best Practices for Token Revocation
Beyond the technical mechanisms, implementing robust security practices around token revocation is vital:
- Revocation on Logout: It is essential to revoke both access and refresh tokens when a user logs out to prevent unauthorized access if the session is hijacked or the device is lost.
- Revocation on Permission Changes: If a user’s roles or permissions change, revoking their existing tokens ensures that they can only access resources aligned with their updated permissions, preventing privilege escalation or unauthorized access.
- Secure Storage of Refresh Tokens: Refresh tokens must be stored securely, ideally in a dedicated database table with encrypted fields. Techniques like AES-256 encryption and regular key rotation should be implemented to minimize the impact of potential key compromises. Access to this database should be strictly limited to authorized services.
Practical Implementation Examples
Integrating token revocation into an application architecture involves thoughtful design and leveraging appropriate tools and frameworks.
-
Combining Explicit and Implicit Revocation
A comprehensive token management strategy often combines both explicit and implicit revocation methods. For instance, in a microservices architecture, you might implement an authorization server with a dedicated revocation endpoint for immediate invalidation of tokens upon user logout or security events. Simultaneously, you would rely on short-lived access tokens for implicit revocation via expiration, and blacklisting for compromised refresh tokens. This layered approach provides granular control over the token lifecycle and aligns with industry best practices outlined in OAuth 2.0.
-
Implementing in C# and ASP.NET Core
In C# and ASP.NET Core environments, libraries like IdentityServer4 are commonly used for OAuth 2.0 and OIDC implementations, providing robust token management capabilities. Developers can implement custom revocation logic using interfaces like
IRevocationEndpointHandlerto integrate specific business rules for token invalidation. TheIntrospectionEndpointcan be utilized to enable resource servers to validate tokens effectively. For secure data protection, ASP.NET Core’s built-in Data Protection API offers secure encryption and key management for sensitive data like refresh tokens.
Conceptual Code Sample: Calling a Revocation Endpoint
Below is a conceptual example demonstrating how a client might send a request to a revocation endpoint. The actual implementation details, such as authentication (e.g., client_id:client_secret), will vary based on the OAuth 2.0 client type and authorization server configuration.
async function revokeToken(token, tokenTypeHint) {
const response = await fetch('YOUR_AUTH_SERVER/revoke', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + btoa('client_id:client_secret') // Use appropriate client authentication
},
body: `token=${token}&token_type_hint=${tokenTypeHint}`
});
if (response.ok) {
console.log(`${tokenTypeHint} token revoked successfully.`);
} else {
console.error(`Failed to revoke ${tokenTypeHint} token:`, response.statusText);
}
}
// Example usage:
// revokeToken('your_access_token_here', 'access_token');
// revokeToken('your_refresh_token_here', 'refresh_token');
Conclusion
Effective token revocation is a cornerstone of secure identity and access management. By understanding and implementing the various methods for invalidating access and refresh tokens—whether through explicit authorization server endpoints, real-time introspection, or secure database management—developers can significantly enhance the security posture of their applications and protect user data from unauthorized access.

