auth

package
v2.10.23 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package auth obtains service-to-service access tokens from Keycloak using the OAuth2 client_credentials grant.

Tokens are cached per audience. A cache hit never takes a lock and never waits on the network: once a token is warm, callers are served from an atomic read while refreshes happen in the background. Concurrent cold fetches for the same audience collapse into a single request; different audiences never block each other.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoToken = errors.New("keycloak returned no access token")

ErrNoToken reports that Keycloak returned a response without an access token.

Functions

This section is empty.

Types

type Options added in v2.8.28

type Options struct {
	// TokenURL is the Keycloak token endpoint, taken from the rotator-managed
	// secret (token_endpoint).
	TokenURL string

	// ClientID and ClientSecret authenticate this service against Keycloak.
	ClientID     string
	ClientSecret string

	// HTTPClient is used for token requests. Defaults to a client with a 5s
	// timeout.
	HTTPClient *http.Client

	// Now defaults to clock.GlobalClock.Now.
	Now func() time.Time
}

Options configures a TokenSource.

type TokenSource added in v2.8.28

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

TokenSource issues and caches client_credentials tokens for one Keycloak client. It is safe for concurrent use and must be created with NewTokenSource.

func NewTokenSource added in v2.8.28

func NewTokenSource(ctx context.Context, opts Options) *TokenSource

NewTokenSource returns a TokenSource. ctx parents background refreshes, so pass the application context, not a request context.

func (*TokenSource) Token added in v2.8.28

func (t *TokenSource) Token(ctx context.Context, audience string) (string, error)

Token returns an access token valid for audience. A warm token is returned without blocking; a token inside its refresh window is returned immediately while a refresh runs in the background. Only a cold or expired token makes the caller wait, and then only one caller per audience actually calls Keycloak.

type Transport

type Transport struct {
	// Source issues the tokens. Required.
	Source *TokenSource

	// Audience is the callee's Keycloak client id, which becomes the token's
	// aud claim. Required.
	Audience string

	// Next defaults to http.DefaultTransport.
	Next http.RoundTripper
}

Transport adds a client-credentials bearer token for one audience to every request it forwards. One Transport per callee audience; all of them should share a single TokenSource so the token cache is shared too.

func (*Transport) RoundTrip

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

RoundTrip attaches the bearer to a clone of the request, because RoundTrip must not modify the request it is given.

Jump to

Keyboard shortcuts

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