auth

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package auth provides auth service

Package auth provides authentication providers

Package auth provides authentication services with pluggable storage

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthConfig

type AuthConfig struct {
	BaseURL     string
	Username    string
	Password    string
	PresetToken string
	Client      *http.Client
	ClientID    string
	ClientUUID  string
	Version     string
	Timeout     time.Duration
}

AuthConfig configures the username/password provider

type AuthProvider

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

AuthProvider implements TokenProvider using username/password authentication

func NewAuthProvider

func NewAuthProvider(config AuthConfig) *AuthProvider

NewAuthProvider creates a new username/password token provider

func (*AuthProvider) FetchToken

func (p *AuthProvider) FetchToken(ctx context.Context) (string, time.Time, error)

FetchToken implements TokenProvider

func (*AuthProvider) SetPresetToken

func (p *AuthProvider) SetPresetToken(token string)

SetPresetToken sets a token to use on the next FetchToken call Useful for scenarios where you have a valid token but need to initialize the provider

func (*AuthProvider) Type

func (p *AuthProvider) Type() string

Type implements TokenProvider for AuthProvider

func (*AuthProvider) UpdateCredentials

func (p *AuthProvider) UpdateCredentials(username, password string)

UpdateCredentials updates the username and password

type Config

type Config struct {
	RefreshBuffer time.Duration
	Storage       TokenStorage // If nil, uses MemoryStorage
}

Config configures the auth manager

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults

type FileStorage

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

FileStorage implements file-based token storage

func NewFileStorage

func NewFileStorage(filePath string) (*FileStorage, error)

NewFileStorage creates a new file-based token storage

func (*FileStorage) Clear

func (s *FileStorage) Clear() error

Clear implements TokenStorage

func (*FileStorage) Retrieve

func (s *FileStorage) Retrieve() (string, time.Time, error)

Retrieve implements TokenStorage

func (*FileStorage) Store

func (s *FileStorage) Store(token string, expiry time.Time) error

Store implements TokenStorage

func (*FileStorage) Type

func (s *FileStorage) Type() string

Type implements TokenStorage

type Manager

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

Manager handles authentication token lifecycle

func NewManager

func NewManager(provider TokenProvider, config Config) *Manager

NewManager creates a new authentication manager

func (*Manager) GetToken

func (m *Manager) GetToken(ctx context.Context) (string, error)

GetToken returns a valid authentication token. if the current token is expired or about to expire, it will automatically refresh

func (*Manager) HasValidToken

func (m *Manager) HasValidToken() bool

HasValidToken returns true if the manager has a valid token

func (*Manager) InvalidateToken

func (m *Manager) InvalidateToken()

InvalidateToken marks the current token as invalid (e.g. 401)

func (*Manager) Stats

func (m *Manager) Stats() Stats

Stats returns authentication statistics

func (*Manager) TokenExpiry

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

TokenExpiry returns the current token's expiry time

type MemoryStorage

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

MemoryStorage implements in-memory token storage (default)

func NewMemoryStorage

func NewMemoryStorage() *MemoryStorage

NewMemoryStorage creates a new in-memory token storage

func (*MemoryStorage) Clear

func (s *MemoryStorage) Clear() error

Clear implements TokenStorage

func (*MemoryStorage) Retrieve

func (s *MemoryStorage) Retrieve() (string, time.Time, error)

Retrieve implements TokenStorage

func (*MemoryStorage) Store

func (s *MemoryStorage) Store(token string, expiry time.Time) error

Store implements TokenStorage

func (*MemoryStorage) Type

func (s *MemoryStorage) Type() string

Type implements TokenStorage

type StaticTokenProvider added in v0.2.1

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

StaticTokenProvider returns the same bearer token for every request.

This is intended for token-only configurations where the token is managed outside of gocmlclient (e.g. Terraform provider config). It never attempts to authenticate via username/password.

func NewStaticTokenProvider added in v0.2.1

func NewStaticTokenProvider(token string) *StaticTokenProvider

NewStaticTokenProvider returns a TokenProvider that always yields the given token.

func (*StaticTokenProvider) FetchToken added in v0.2.1

func (p *StaticTokenProvider) FetchToken(ctx context.Context) (string, time.Time, error)

FetchToken implements TokenProvider.

func (*StaticTokenProvider) Type added in v0.2.1

func (p *StaticTokenProvider) Type() string

Type implements TokenProvider.

type Stats

type Stats struct {
	HasToken         bool
	TokenExpiry      time.Time
	IsValid          bool
	TimeUntilRefresh time.Duration
	StorageType      string
}

Stats contains authentication manager statistics

type TokenProvider

type TokenProvider interface {
	// FetchToken retrieves a new authentication token
	FetchToken(ctx context.Context) (token string, expiry time.Time, err error)

	// Type returns the provider type identifier
	Type() string
}

TokenProvider defines the interface for token acquisition

type TokenStorage

type TokenStorage interface {
	// Store saves a token with its expiry time
	Store(token string, expiry time.Time) error

	// Retrieve gets the stored token and expiry time
	Retrieve() (token string, expiry time.Time, err error)

	// Clear removes the stored token
	Clear() error

	// Type returns the storage type identifier
	Type() string
}

TokenStorage defines the interface for token storage backends

type Transport

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

Transport wraps an HTTP RoundTripper to automatically add authentication

func NewTransport

func NewTransport(base http.RoundTripper, manager *Manager, skip []string) *Transport

NewTransport creates a new authenticated transport

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper

Jump to

Keyboard shortcuts

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