Interface TokenManager

All Superinterfaces:
AutoCloseable

public interface TokenManager extends AutoCloseable
Token Manager Interface Token 管理接口

Manages OAuth2 tokens with automatic refresh capabilities.

管理 OAuth2 令牌,具有自动刷新功能。

Features | 主要功能:

  • Token storage and retrieval - Token 存储和检索
  • Automatic token refresh - 自动 Token 刷新
  • Token expiration handling - Token 过期处理
  • Multi-user token management - 多用户 Token 管理

Usage Examples | 使用示例:

// Create token manager
TokenManager manager = new DefaultTokenManager(tokenStore, refresher);

// Store token
manager.store("user-1", token);

// Get valid token (auto-refresh if needed)
OAuth2Token validToken = manager.getValidToken("user-1");

// Remove token
manager.remove("user-1");

Thread Safety | 线程安全:

Implementations should be thread-safe.

实现应该是线程安全的。

Since:
JDK 25, opencode-base-oauth2 V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clear all tokens 清除所有 Token
    void
    Close the token manager and release resources 关闭 Token 管理器并释放资源
    boolean
    Check if a token exists for the key 检查是否存在该键的 Token
    get(String key)
    Get a stored token 获取存储的 Token
    getOrObtain(String key, Supplier<OAuth2Token> tokenSupplier)
    Get a valid token or obtain a new one 获取有效的 Token 或获取新的
    Get a valid token, refreshing if necessary 获取有效的 Token,必要时刷新
    boolean
    Check if a valid token exists for the key 检查是否存在该键的有效 Token
    void
    Remove a token 移除 Token
    int
    Get the number of stored tokens 获取存储的 Token 数量
    void
    store(String key, OAuth2Token token)
    Store a token 存储 Token
  • Method Details

    • store

      void store(String key, OAuth2Token token)
      Store a token 存储 Token
      Parameters:
      key - the storage key (e.g., user ID) | 存储键(例如用户 ID)
      token - the token to store | 要存储的 Token
    • get

      Get a stored token 获取存储的 Token
      Parameters:
      key - the storage key | 存储键
      Returns:
      the token if found | 找到的 Token
    • getValidToken

      OAuth2Token getValidToken(String key)
      Get a valid token, refreshing if necessary 获取有效的 Token,必要时刷新

      This method will:

      • Return the stored token if it's still valid
      • Refresh the token if it's expiring soon and has a refresh token
      • Throw an exception if the token is expired and cannot be refreshed
      Parameters:
      key - the storage key | 存储键
      Returns:
      the valid token | 有效的 Token
      Throws:
      OAuth2Exception - if no token found or refresh fails | 如果未找到 Token 或刷新失败
    • getOrObtain

      OAuth2Token getOrObtain(String key, Supplier<OAuth2Token> tokenSupplier)
      Get a valid token or obtain a new one 获取有效的 Token 或获取新的

      If no token exists for the key, the supplier is called to obtain a new token.

      Parameters:
      key - the storage key | 存储键
      tokenSupplier - supplier to obtain new token if needed | 需要时获取新 Token 的提供者
      Returns:
      the valid token | 有效的 Token
    • exists

      boolean exists(String key)
      Check if a token exists for the key 检查是否存在该键的 Token
      Parameters:
      key - the storage key | 存储键
      Returns:
      true if token exists | 如果存在 Token 返回 true
    • hasValidToken

      boolean hasValidToken(String key)
      Check if a valid token exists for the key 检查是否存在该键的有效 Token
      Parameters:
      key - the storage key | 存储键
      Returns:
      true if valid token exists | 如果存在有效 Token 返回 true
    • remove

      void remove(String key)
      Remove a token 移除 Token
      Parameters:
      key - the storage key | 存储键
    • clear

      void clear()
      Clear all tokens 清除所有 Token
    • size

      int size()
      Get the number of stored tokens 获取存储的 Token 数量
      Returns:
      the token count | Token 数量
    • close

      void close()
      Close the token manager and release resources 关闭 Token 管理器并释放资源
      Specified by:
      close in interface AutoCloseable