authtoken

package
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package authtoken provides OAuth2 client-credentials token management with automatic caching and refresh.

The client uses an RWMutex with double-check locking to efficiently cache tokens across concurrent requests, automatically refreshing expired tokens. A 30-second expiry buffer prevents race conditions during token refresh.

Basic usage:

client := authtoken.New(
    "https://auth.example.com/oauth/token",
    "client-id",
    "client-secret",
)

token, err := client.GetToken(ctx)
if err != nil {
    return err
}
// Use token for API requests

The client is safe for concurrent use. Call InvalidateToken() to force a token refresh on the next request.

Index

Constants

View Source
const (
	DefaultTimeout = 10 * time.Second
)

Variables

View Source
var (
	ErrTokenRequestFailed = errors.New("authtoken: token request failed")
	ErrNoAccessToken      = errors.New("authtoken: no access token in response")
)

Functions

This section is empty.

Types

type Client

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

func New

func New(tokenURL, clientID, clientSecret string, opts ...Option) *Client

func (*Client) GetToken

func (c *Client) GetToken(ctx context.Context) (string, error)

func (*Client) InvalidateToken

func (c *Client) InvalidateToken()

type Option

type Option func(*Client)

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

func WithTimeout

func WithTimeout(timeout time.Duration) Option

Jump to

Keyboard shortcuts

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