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:
- read the issuer's /.well-known/openid-configuration document,
- check that the advertised issuer matches the configured one,
- use the advertised token endpoint and client authentication methods,
- request a token with one of the standard grants, and
- 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 ¶
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.
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.
type Token ¶
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.
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. |