token

package
v0.0.0-...-9f7285a Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoNewTokenResolved = errors.New("no new token resolved")
View Source
var ErrNoValidTokenResolved = errors.New("no valid token returned")

Functions

This section is empty.

Types

type BasicTokenStore

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

BasicTokenStore provides feature to refresh token and return cached token. BasicTokenStore memory the expired tokens and it calls resolvers in order to get new token after MarkTokenExpired called.

func NewBasicTokenStore

func NewBasicTokenStore(tokenType string, resolver TokenResolver) *BasicTokenStore

func (*BasicTokenStore) Digest

func (b *BasicTokenStore) Digest() string

Digest implements TokenStore.

func (*BasicTokenStore) GetToken

func (b *BasicTokenStore) GetToken(ctx context.Context) (*Token, error)

GetToken implements TokenStore.

func (*BasicTokenStore) GetType

func (b *BasicTokenStore) GetType() string

func (*BasicTokenStore) IsTokenValidityAssured

func (b *BasicTokenStore) IsTokenValidityAssured(ctx context.Context) bool

func (*BasicTokenStore) RefreshToken

func (b *BasicTokenStore) RefreshToken(ctx context.Context) error

RefreshToken implements TokenStore. A token store can be referenced from multiple http client and these can call RefreshToken multiple times in parallel just after token expiration. RefreshToken() will ignore the refresh request when it can't acquire the write lock and wait until the lock to be acquired.

type MockErrorTokenResolver

type MockErrorTokenResolver struct{}

func NewMockErrorTokenResolver

func NewMockErrorTokenResolver() *MockErrorTokenResolver

func (*MockErrorTokenResolver) Resolve

func (m *MockErrorTokenResolver) Resolve(ctx context.Context) (*Token, error)

Resolve implements TokenResolver.

type MultiTokenResolver

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

func NewMultiTokenResolver

func NewMultiTokenResolver(resolvers ...TokenResolver) *MultiTokenResolver

func (*MultiTokenResolver) Resolve

func (m *MultiTokenResolver) Resolve(ctx context.Context) (*Token, error)

Resolve implements TokenResolver.

type MultiTokenStoreRefresher

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

MultiTokenStoreRefresher implements the TokenRefresher to refresh tokens in multiple stores. This rotate the next token store to be refreshed but it will ignore if the token is assured to be alive from the expiry time.

func NewMultiTokenStoreRefresher

func NewMultiTokenStoreRefresher(tokenStores ...TokenStore) *MultiTokenStoreRefresher

func (*MultiTokenStoreRefresher) Refresh

Refresh implements TokenRefresher.

type NopTokenRefresher

type NopTokenRefresher struct {
}

func (*NopTokenRefresher) Refresh

func (n *NopTokenRefresher) Refresh(ctx context.Context) error

Refresh implements TokenRefresher.

type OnceTokenResolver

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

OnceTokenResolver resolves the token from somewhere only onetime. This is mainly used for resolving token from CLI arguments or environment variables.

func NewOnceTokenResolver

func NewOnceTokenResolver(resolver func() string) *OnceTokenResolver

func (*OnceTokenResolver) Resolve

func (e *OnceTokenResolver) Resolve(ctx context.Context) (*Token, error)

Resolve implements TokenResolver.

type SpyTokenResolver

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

func NewSpyTokenResolver

func NewSpyTokenResolver(tokenResponse ...*Token) *SpyTokenResolver

func NewSpyTokenResolverWithDelay

func NewSpyTokenResolverWithDelay(delayInMillisecond int, tokenResponse ...*Token) *SpyTokenResolver

func (*SpyTokenResolver) Resolve

func (m *SpyTokenResolver) Resolve(ctx context.Context) (*Token, error)

type Token

type Token struct {
	// RawToken is the actual token in string representation.
	RawToken string
	// ValidAtLeastUntil holds the expiration time when the information is available.
	// The token refreshers won't refresh token if this value exists and later than now.
	// The default value will be `January 1, year 1, 00:00:00.000000000 UTC` and it is earlier than the possible time.Now().
	// The default value naturally means the token is expired.
	ValidAtLeastUntil time.Time
}

Token contains the token string like access token.

func New

func New(rawToken string) *Token

New instanciate a new Token without expiration time.

func NewWithExpiry

func NewWithExpiry(rawToken string, validAtLeastUntil time.Time) *Token

NewWithExpiry instanciate a new Token from the raw token string and expiration time.

type TokenRefresher

type TokenRefresher interface {
	Refresh(ctx context.Context) error
}

type TokenResolver

type TokenResolver interface {
	// Resolve returns the token newly resolved.
	Resolve(ctx context.Context) (*Token, error)
}

TokenResolver resolve the new token

type TokenStore

type TokenStore interface {
	task.CachableDependency
	GetType() string
	// GetToken returns the current token. It can come from cache or newly resolved from TokenResolver.
	GetToken(ctx context.Context) (*Token, error)
	// IsTokenValidityAssured returns true if this token is assured to be valid now.
	// When a permission error happens, refresher may attempt to refresh tokens. The refresh is only needed when the token can be expired(when we don't know the expiration time) or the token is actually expired.
	IsTokenValidityAssured(ctx context.Context) bool
	// RefreshToken refreshes the token returned from GetToken()
	RefreshToken(ctx context.Context) error
}

Jump to

Keyboard shortcuts

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