Help us shape the future of Go! Take the 2026 Go Developer Survey

auth

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultCredentialsPath

func DefaultCredentialsPath(profileName string) (string, error)

DefaultCredentialsPath returns the default location for cached CLI credentials for the given profile name. Each non-default profile gets its own file - they're logins against different identity providers/clients, so caching them together would let switching profiles silently pick up the wrong token. The default profile keeps the original, un-suffixed path for backward compatibility with credentials cached before profiles existed.

func DiscoverEndpoints

func DiscoverEndpoints(ctx context.Context, httpClient *http.Client, issuer string) (authURL, tokenURL string, err error)

DiscoverEndpoints fetches the OIDC discovery document at issuer + "/.well-known/openid-configuration" and returns its authorization and token endpoints. This lets callers configure just an issuer/base URL, as most standards-compliant identity providers support discovery, instead of every individual endpoint.

func NewState

func NewState() (string, error)

NewState generates a random CSRF state parameter for the authorization request.

func SaveToken

func SaveToken(path string, token *Token) error

SaveToken writes the token to the given path, creating parent directories as needed. The file is written with 0600 permissions since it holds secrets.

Types

type LoginOptions

type LoginOptions struct {
	AuthURL     string
	TokenURL    string
	ClientID    string
	Scopes      string
	ExtraParams map[string]string // IDP-specific extras, e.g. "resource" or "audience"
	HTTPClient  *http.Client
	OpenBrowser bool
	Timeout     time.Duration
	// CallbackPort pins the local redirect listener to a specific loopback
	// port, for identity providers (like ThunderID today) whose redirectUris
	// allow-list requires an exact match rather than a wildcard/any port. 0
	// means let the OS assign a free ephemeral port (the RFC 8252-preferred
	// default, used whenever the IDP supports it).
	CallbackPort int
}

LoginOptions configures a browser-based OAuth2 Authorization Code + PKCE login.

type PKCE

type PKCE struct {
	Verifier  string
	Challenge string
}

PKCE holds an OAuth2 PKCE (RFC 7636) verifier/challenge pair using the S256 challenge method.

func NewPKCE

func NewPKCE() (*PKCE, error)

NewPKCE generates a new PKCE verifier/challenge pair.

type Token

type Token struct {
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	TokenType    string    `json:"token_type"`
	ExpiresAt    time.Time `json:"expires_at"`
	TokenURL     string    `json:"token_url"`
	ClientID     string    `json:"client_id"`
	// ExtraParams remembers IDP-specific query params required at login (e.g.
	// ThunderID's resource=... audience binding) so a later refresh reuses
	// them automatically instead of silently dropping them.
	ExtraParams map[string]string `json:"extra_params,omitempty"`
}

Token represents a cached OAuth2 token set, along with the token endpoint and client ID needed to refresh it later without the caller re-specifying flags.

func EnsureFreshToken

func EnsureFreshToken(ctx context.Context, path string, httpClient *http.Client) (*Token, error)

EnsureFreshToken loads the cached token from path and, if it has expired, refreshes it and persists the refreshed token back to the same path.

func LoadToken

func LoadToken(path string) (*Token, error)

LoadToken reads a cached token from the given path.

func Login

func Login(ctx context.Context, opts LoginOptions) (*Token, error)

Login runs a browser-based OAuth2 Authorization Code + PKCE flow and returns the resulting token. Per RFC 8252 (OAuth for native apps), it starts a short-lived local HTTP server on a loopback, OS-assigned port to receive the redirect, opens the system browser to the authorization URL, waits for the callback, then exchanges the code for a token.

func RefreshToken

func RefreshToken(ctx context.Context, httpClient *http.Client, token *Token) (*Token, error)

RefreshToken exchanges a refresh token for a new access token, using the token endpoint and client ID recorded on the token itself.

func (*Token) Expired

func (t *Token) Expired() bool

Expired reports whether the access token has passed its expiry, with a small safety margin so a request doesn't race the real expiry.

Jump to

Keyboard shortcuts

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