refresh

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package refresh provides automatic token refresh functionality.

This package implements background token monitoring and automatic refresh to ensure tokens remain valid without user intervention. It monitors token expiration times and proactively refreshes tokens before they expire.

Features:

  • Background goroutine for expiration monitoring
  • Configurable refresh threshold (default: 5 minutes before expiry)
  • Automatic token refresh using OAuth client
  • Token storage via callback function
  • Refresh failure handling with re-authentication callbacks
  • Graceful shutdown support

Example usage:

import (
    "github.com/AINative-studio/ainative-code/internal/auth/refresh"
    "github.com/AINative-studio/ainative-code/internal/auth/oauth"
)

// Create OAuth client
oauthClient := oauth.NewClient(config)

// Create refresh manager
manager := refresh.NewManager(refresh.Config{
    OAuthClient:   oauthClient,
    TokenStore:    storeTokenFunc,
    OnRefreshFail: handleRefreshFailure,
})

// Start monitoring with current tokens
manager.Start(ctx, tokens)

// Manager automatically refreshes tokens before expiry
// Call Stop() for graceful shutdown
defer manager.Stop()

Index

Constants

View Source
const (
	// DefaultRefreshThreshold is the time before expiration to trigger refresh
	DefaultRefreshThreshold = 5 * time.Minute

	// DefaultCheckInterval is how often to check token expiration
	DefaultCheckInterval = 1 * time.Minute

	// MinRefreshThreshold is the minimum allowed refresh threshold
	MinRefreshThreshold = 1 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// OAuthClient is the OAuth client for token refresh
	OAuthClient *oauth.Client

	// TokenStore is called to store refreshed tokens
	TokenStore TokenStoreFunc

	// OnRefreshFail is called when refresh fails
	OnRefreshFail RefreshFailFunc

	// RefreshThreshold is the time before expiry to trigger refresh
	// Default: 5 minutes
	RefreshThreshold time.Duration

	// CheckInterval is how often to check expiration
	// Default: 1 minute
	CheckInterval time.Duration
}

Config represents the configuration for the refresh manager.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages automatic token refresh.

func NewManager

func NewManager(config Config) *Manager

NewManager creates a new token refresh manager.

func (*Manager) ForceRefresh

func (m *Manager) ForceRefresh(ctx context.Context) error

ForceRefresh immediately refreshes the tokens regardless of expiration time.

func (*Manager) GetExpiresAt

func (m *Manager) GetExpiresAt() time.Time

GetExpiresAt returns the token expiration time.

func (*Manager) GetLastRefreshAt

func (m *Manager) GetLastRefreshAt() time.Time

GetLastRefreshAt returns the time of the last successful refresh.

func (*Manager) GetRefreshStatus

func (m *Manager) GetRefreshStatus() *RefreshStatus

GetRefreshStatus returns the current refresh status.

func (*Manager) GetTokens

func (m *Manager) GetTokens() *jwt.TokenPair

GetTokens returns the current tokens.

func (*Manager) IsRunning

func (m *Manager) IsRunning() bool

IsRunning returns whether the manager is currently running.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, tokens *jwt.TokenPair) error

Start begins monitoring and refreshing tokens.

func (*Manager) Stop

func (m *Manager) Stop()

Stop gracefully stops the refresh manager.

func (*Manager) UpdateTokens

func (m *Manager) UpdateTokens(tokens *jwt.TokenPair) error

UpdateTokens manually updates the tokens (useful after re-authentication).

type RefreshFailFunc

type RefreshFailFunc func(err error) bool

RefreshFailFunc is called when token refresh fails. It should return true if the manager should attempt re-authentication.

type RefreshStatus

type RefreshStatus struct {
	IsRunning        bool
	ExpiresAt        time.Time
	RefreshAt        time.Time
	LastRefreshAt    time.Time
	TimeUntilExpiry  time.Duration
	TimeUntilRefresh time.Duration
	NeedsRefresh     bool
}

RefreshStatus represents the current refresh status.

type TokenStoreFunc

type TokenStoreFunc func(tokens *jwt.TokenPair) error

TokenStoreFunc is called to store refreshed tokens.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL