token.go

 1package oauth
 2
 3import (
 4	"time"
 5)
 6
 7// Token represents an OAuth2 token from Claude Code Max.
 8type Token struct {
 9	AccessToken  string `json:"access_token"`
10	RefreshToken string `json:"refresh_token"`
11	ExpiresIn    int    `json:"expires_in"`
12	ExpiresAt    int64  `json:"expires_at"`
13}
14
15// SetExpiresAt calculates and sets the ExpiresAt field based on the current time and ExpiresIn.
16func (t *Token) SetExpiresAt() {
17	t.ExpiresAt = time.Now().Add(time.Duration(t.ExpiresIn) * time.Second).Unix()
18}
19
20// IsExpired checks if the token is expired or about to expire (within 10% of its lifetime).
21func (t *Token) IsExpired() bool {
22	return time.Now().Unix() >= (t.ExpiresAt - int64(t.ExpiresIn)/10)
23}