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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classOAuth2Token Builder OAuth2Token 构建器 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the value of theaccessTokenrecord component.static OAuth2Token.Builderbuilder()Create a new builder 创建新的构建器static OAuth2Token.Builderbuilder(OAuth2Token token) Create a builder from existing token 从现有令牌创建构建器final booleanIndicates whether some other object is "equal to" this one.Returns the value of theexpiresAtrecord component.final inthashCode()Returns a hash code value for this object.booleanCheck if has ID token (OIDC) 检查是否有 ID 令牌(OIDC)booleanCheck if has refresh token 检查是否有刷新令牌idToken()Returns the value of theidTokenrecord component.booleanCheck if token is expired 检查令牌是否已过期booleanisExpiringSoon(Duration threshold) Check if token is expiring soon 检查令牌是否即将过期issuedAt()Returns the value of theissuedAtrecord component.Returns the value of therefreshTokenrecord component.Get remaining valid time 获取剩余有效时间scopes()Returns the value of thescopesrecord component.Get authorization header value with token type 获取带令牌类型的授权头值Get Bearer authorization header value 获取 Bearer 授权头值Returns the value of thetokenTyperecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
OAuth2Token
-
-
Method Details
-
isExpired
public boolean isExpired()Check if token is expired 检查令牌是否已过期- Returns:
- true if expired | 已过期返回 true
-
isExpiringSoon
Check if token is expiring soon 检查令牌是否即将过期- Parameters:
threshold- time threshold | 时间阈值- Returns:
- true if expiring within threshold | 在阈值内即将过期返回 true
-
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
Get Bearer authorization header value 获取 Bearer 授权头值- Returns:
- the authorization header value | 授权头值
-
toAuthorizationHeader
Get authorization header value with token type 获取带令牌类型的授权头值- Returns:
- the authorization header value | 授权头值
-
builder
-
builder
Create a builder from existing token 从现有令牌创建构建器- Parameters:
token- the token to copy | 要复制的令牌- Returns:
- the builder | 构建器
-
toString
-
hashCode
-
equals
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 withObjects::equals(Object,Object). -
accessToken
Returns the value of theaccessTokenrecord component.- Returns:
- the value of the
accessTokenrecord component
-
tokenType
Returns the value of thetokenTyperecord component.- Returns:
- the value of the
tokenTyperecord component
-
refreshToken
Returns the value of therefreshTokenrecord component.- Returns:
- the value of the
refreshTokenrecord component
-
idToken
Returns the value of theidTokenrecord component.- Returns:
- the value of the
idTokenrecord component
-
scopes
-
issuedAt
Returns the value of theissuedAtrecord component.- Returns:
- the value of the
issuedAtrecord component
-
expiresAt
Returns the value of theexpiresAtrecord component.- Returns:
- the value of the
expiresAtrecord component
-