48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Credential represents an Anthropic API credential loaded from a JSON file.
|
|
type Credential struct {
|
|
ID string
|
|
Email string
|
|
AccessToken string
|
|
RefreshToken string
|
|
ExpiresAt time.Time
|
|
FilePath string
|
|
CooldownUntil time.Time
|
|
nextRefreshAfter time.Time
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// IsOnCooldown returns true if the credential is currently on cooldown.
|
|
func (c *Credential) IsOnCooldown() bool {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return time.Now().Before(c.CooldownUntil)
|
|
}
|
|
|
|
// SetCooldown puts the credential on cooldown for the given duration.
|
|
func (c *Credential) SetCooldown(duration time.Duration) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.CooldownUntil = time.Now().Add(duration)
|
|
}
|
|
|
|
// ClearCooldown removes any active cooldown on the credential.
|
|
func (c *Credential) ClearCooldown() {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.CooldownUntil = time.Time{}
|
|
}
|
|
|
|
// Token returns the current access token.
|
|
func (c *Credential) Token() string {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return c.AccessToken
|
|
}
|