61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const grokTokenRefreshSkew = time.Hour
|
|
|
|
type GrokTokenRefresher struct {
|
|
grokOAuthService GrokOAuthTokenService
|
|
}
|
|
|
|
func NewGrokTokenRefresher(grokOAuthService GrokOAuthTokenService) *GrokTokenRefresher {
|
|
return &GrokTokenRefresher{grokOAuthService: grokOAuthService}
|
|
}
|
|
|
|
func (r *GrokTokenRefresher) CacheKey(account *Account) string {
|
|
return GrokTokenCacheKey(account)
|
|
}
|
|
|
|
func (r *GrokTokenRefresher) CanRefresh(account *Account) bool {
|
|
return account != nil && account.Platform == PlatformGrok && account.Type == AccountTypeOAuth &&
|
|
strings.TrimSpace(account.GetGrokRefreshToken()) != ""
|
|
}
|
|
|
|
func (r *GrokTokenRefresher) NeedsRefresh(account *Account, refreshWindow time.Duration) bool {
|
|
if account == nil || strings.TrimSpace(account.GetGrokRefreshToken()) == "" {
|
|
return false
|
|
}
|
|
if strings.TrimSpace(account.GetGrokAccessToken()) == "" {
|
|
return true
|
|
}
|
|
expiresAt := account.GetCredentialAsTime("expires_at")
|
|
if expiresAt == nil {
|
|
return true
|
|
}
|
|
if refreshWindow < grokTokenRefreshSkew {
|
|
refreshWindow = grokTokenRefreshSkew
|
|
}
|
|
return time.Until(*expiresAt) < refreshWindow
|
|
}
|
|
|
|
func (r *GrokTokenRefresher) Refresh(ctx context.Context, account *Account) (map[string]any, error) {
|
|
if r == nil || r.grokOAuthService == nil {
|
|
return nil, errors.New("grok oauth service is not configured")
|
|
}
|
|
tokenInfo, err := r.grokOAuthService.RefreshAccountToken(ctx, account)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
newCredentials := r.grokOAuthService.BuildAccountCredentials(tokenInfo)
|
|
newCredentials = MergeCredentials(account.Credentials, newCredentials)
|
|
if baseURL := strings.TrimSpace(account.GetCredential("base_url")); baseURL != "" {
|
|
newCredentials["base_url"] = baseURL
|
|
}
|
|
return newCredentials, nil
|
|
}
|