oidctoken

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package oidctoken mints short-lived bearer tokens from durable OAuth 2.0 and OpenID Connect client inputs.

A backend that calls a third-party API on a user's behalf usually holds a long-lived credential (a client secret, an automation user's password, or a refresh token) and needs a short-lived access token for each call. This package turns the former into the latter with standard facilities only:

  1. read the issuer's /.well-known/openid-configuration document,
  2. check that the advertised issuer matches the configured one,
  3. use the advertised token endpoint and client authentication methods,
  4. request a token with one of the standard grants, and
  5. derive the expiry from expires_in or, failing that, the JWT exp claim.

It is provider-neutral: nothing here knows about a particular identity provider or a particular resource server. Manager adds a per-source cache that renews shortly before expiry and collapses concurrent mints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expiration

func Expiration(token string, expiresIn json.RawMessage, now time.Time) time.Time

Expiration derives when a token expires from the response's expires_in (a number or a numeric string) and, when that is absent, from the exp claim of a JWT. It returns the zero time when neither is available.

Types

type ClientAuthMethod

type ClientAuthMethod string

ClientAuthMethod is how the client authenticates to the token endpoint.

const (
	// ClientAuthAuto chooses from the methods discovery advertises: basic
	// when supported, else post, and none when there is no client secret.
	ClientAuthAuto ClientAuthMethod = "auto"
	// ClientAuthSecretBasic sends the client ID and secret as HTTP basic auth.
	ClientAuthSecretBasic ClientAuthMethod = "client_secret_basic"
	// ClientAuthSecretPost sends the client ID and secret in the form body.
	ClientAuthSecretPost ClientAuthMethod = "client_secret_post"
	// ClientAuthNone sends only the client ID, for a public client.
	ClientAuthNone ClientAuthMethod = "none"
)

type Grant

type Grant string

Grant is an OAuth 2.0 grant type this package can request.

const (
	// GrantClientCredentials authenticates the client itself: a service or
	// machine-to-machine identity with a client secret.
	GrantClientCredentials Grant = "client_credentials"
	// GrantPassword is the resource owner password grant. It is a legacy flow
	// kept for providers that still require it; prefer the other two.
	GrantPassword Grant = "password"
	// GrantRefreshToken renews an access token from a refresh token obtained
	// through an interactive flow elsewhere.
	GrantRefreshToken Grant = "refresh_token"
)

type Manager

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

Manager caches minted tokens per source and renews them shortly before they expire. Concurrent requests for the same source share one mint. It is safe for concurrent use and sized for a process: sources that are never requested again stay cached until the process exits.

func NewManager

func NewManager() *Manager

NewManager returns an empty Manager.

func (*Manager) Forget

func (m *Manager) Forget(source Source)

Forget drops any cached token for the source, forcing the next Token call to mint. Use it after the resource server rejects a token before expiry.

func (*Manager) Token

func (m *Manager) Token(ctx context.Context, source Source) (Token, error)

Token returns a valid token for the source, minting one when the cache has none or the cached one is about to expire.

type Source

type Source struct {
	Grant        Grant
	Issuer       string
	ClientID     string
	ClientSecret string
	Username     string
	Password     string
	RefreshToken string
	// Scopes is the space-separated scope parameter, sent as given.
	Scopes string
	// TokenField defaults to TokenFieldAccess when empty.
	TokenField TokenField
	// ClientAuthMethod defaults to ClientAuthAuto when empty.
	ClientAuthMethod ClientAuthMethod
	// InsecureSkipVerify disables TLS verification for discovery and the
	// token endpoint only. It says nothing about the resource server the
	// minted token is sent to.
	InsecureSkipVerify bool
}

Source is the durable input a token is minted from. Everything except the secrets is safe to persist as metadata; ClientSecret, Password, and RefreshToken must be stored sealed.

func (Source) Validate

func (s Source) Validate() error

Validate reports whether the source has what its grant needs.

type Token

type Token struct {
	Value        string
	RefreshToken string
	ExpiresAt    time.Time
}

Token is a minted bearer. RefreshToken is set when the provider returned one, which for the refresh grant may be a rotated replacement the caller must persist. A zero ExpiresAt means the provider gave no expiry and the token cannot be cached.

func Mint

func Mint(ctx context.Context, source Source) (Token, error)

Mint requests one token from the source's issuer. It performs discovery on every call; use Manager to cache.

type TokenField

type TokenField string

TokenField selects which token from the response is used as the bearer.

const (
	// TokenFieldAccess sends the access token. This is the default and what
	// almost every resource server expects.
	TokenFieldAccess TokenField = "access_token"
	// TokenFieldID sends the ID token, for a resource server configured to
	// accept one.
	TokenFieldID TokenField = "id_token"
)

Directories

Path Synopsis
Package oidctokentest provides a fake OpenID provider for tests of code that mints tokens with package oidctoken.
Package oidctokentest provides a fake OpenID provider for tests of code that mints tokens with package oidctoken.

Jump to

Keyboard shortcuts

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