Record Class OAuth2Token

java.lang.Object
java.lang.Record
cloud.opencode.base.oauth2.OAuth2Token

public record OAuth2Token(String accessToken, String tokenType, String refreshToken, String idToken, Set<String> scopes, Instant issuedAt, Instant expiresAt) extends Record
OAuth2 Token Record OAuth2 Token 记录

Immutable record representing an OAuth2 access token response.

表示 OAuth2 访问令牌响应的不可变记录。

Features | 主要功能:

  • Access token storage - 访问令牌存储
  • Refresh token support - 刷新令牌支持
  • ID token for OIDC - OIDC 的 ID 令牌
  • Expiration tracking - 过期时间跟踪
  • Scope management - 权限范围管理

Usage Examples | 使用示例:

// Create token with builder
OAuth2Token token = OAuth2Token.builder()
    .accessToken("eyJhbGciOiJSUzI1NiIs...")
    .refreshToken("dGhpcyBpcyBhIHJlZnJlc2g...")
    .expiresIn(3600)
    .build();

// Check expiration
if (token.isExpired()) {
    // refresh token
}

// Use in HTTP request
request.setHeader("Authorization", token.toBearerHeader());

Thread Safety | 线程安全:

This class is immutable and thread-safe.

此类是不可变的,线程安全。

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

    • OAuth2Token

      public OAuth2Token(String accessToken, String tokenType, String refreshToken, String idToken, Set<String> scopes, Instant issuedAt, Instant expiresAt)
      Compact constructor with validation 带验证的紧凑构造器
  • Method Details

    • isExpired

      public boolean isExpired()
      Check if token is expired 检查令牌是否已过期
      Returns:
      true if expired | 已过期返回 true
    • isExpiringSoon

      public boolean isExpiringSoon(Duration threshold)
      Check if token is expiring soon 检查令牌是否即将过期
      Parameters:
      threshold - time threshold | 时间阈值
      Returns:
      true if expiring within threshold | 在阈值内即将过期返回 true
    • remainingTime

      public Duration remainingTime()
      Get remaining valid time 获取剩余有效时间
      Returns:
      remaining duration, or ZERO if expired | 剩余时间,已过期则返回 ZERO
    • hasRefreshToken

      public boolean hasRefreshToken()
      Check if has refresh token 检查是否有刷新令牌
      Returns:
      true if has refresh token | 有刷新令牌返回 true
    • hasIdToken

      public boolean hasIdToken()
      Check if has ID token (OIDC) 检查是否有 ID 令牌(OIDC)
      Returns:
      true if has ID token | 有 ID 令牌返回 true
    • toBearerHeader

      public String toBearerHeader()
      Get Bearer authorization header value 获取 Bearer 授权头值
      Returns:
      the authorization header value | 授权头值
    • toAuthorizationHeader

      public String toAuthorizationHeader()
      Get authorization header value with token type 获取带令牌类型的授权头值
      Returns:
      the authorization header value | 授权头值
    • builder

      public static OAuth2Token.Builder builder()
      Create a new builder 创建新的构建器
      Returns:
      the builder | 构建器
    • builder

      public static OAuth2Token.Builder builder(OAuth2Token token)
      Create a builder from existing token 从现有令牌创建构建器
      Parameters:
      token - the token to copy | 要复制的令牌
      Returns:
      the builder | 构建器
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • accessToken

      public String accessToken()
      Returns the value of the accessToken record component.
      Returns:
      the value of the accessToken record component
    • tokenType

      public String tokenType()
      Returns the value of the tokenType record component.
      Returns:
      the value of the tokenType record component
    • refreshToken

      public String refreshToken()
      Returns the value of the refreshToken record component.
      Returns:
      the value of the refreshToken record component
    • idToken

      public String idToken()
      Returns the value of the idToken record component.
      Returns:
      the value of the idToken record component
    • scopes

      public Set<String> scopes()
      Returns the value of the scopes record component.
      Returns:
      the value of the scopes record component
    • issuedAt

      public Instant issuedAt()
      Returns the value of the issuedAt record component.
      Returns:
      the value of the issuedAt record component
    • expiresAt

      public Instant expiresAt()
      Returns the value of the expiresAt record component.
      Returns:
      the value of the expiresAt record component