auth

package
v0.30.3 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultClientID is the default OAuth client ID for public clients
	DefaultClientID = "abctl"
	// CallbackPath is the OAuth callback path
	CallbackPath = "/callback"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client provides an HTTP client with automatic token management. Thread-safe for concurrent use by multiple goroutines/services. The mutex (mu) protects credentials during refresh operations when multiple API calls might trigger refresh simultaneously.

func NewClient

func NewClient(provider *Provider, clientID string, credentials *Credentials) *Client

NewClient creates a new authenticated HTTP client. Designed for reuse across multiple services making concurrent API calls.

func (*Client) Do

func (c *Client) Do(req *stdhttp.Request) (*stdhttp.Response, error)

Do performs an authenticated HTTP request with automatic token refresh. Thread-safe and handles concurrent requests. If token is expired, it will refresh once and retry the request.

func (*Client) GetCredentials

func (c *Client) GetCredentials() *Credentials

GetCredentials returns a copy of the current credentials

type Credentials

type Credentials struct {
	AccessToken  string    `json:"access_token"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	TokenType    string    `json:"token_type"`
	ExpiresAt    time.Time `json:"expires_at"`
}

Credentials stores authentication tokens and metadata

func CredentialsFromJSON

func CredentialsFromJSON(data []byte) (*Credentials, error)

CredentialsFromJSON deserializes credentials from JSON

func EnsureValidAuth

func EnsureValidAuth(ctx context.Context, k8sClient k8s.Client, namespace string) (*Credentials, error)

EnsureValidAuth loads credentials and refreshes if expired

func (*Credentials) IsExpired

func (c *Credentials) IsExpired() bool

IsExpired checks if the access token has expired

func (*Credentials) ToJSON

func (c *Credentials) ToJSON() ([]byte, error)

ToJSON serializes credentials to JSON

type Flow

type Flow struct {
	Provider     *Provider
	ClientID     string
	RedirectPort int
	// contains filtered or unexported fields
}

Flow manages the OAuth2/OIDC authorization flow. Uses channels and goroutines to handle async browser-based OAuth callbacks while maintaining a timeout. This pattern is necessary because we need to serve HTTP callbacks while the main thread waits for authentication to complete.

func NewFlow

func NewFlow(provider *Provider, clientID string, callbackPort int) *Flow

NewFlow creates a new OAuth flow with PKCE. The flow is designed to be reusable across different service contexts.

func (*Flow) Authenticate

func (f *Flow) Authenticate(ctx context.Context) (*Credentials, error)

Authenticate performs the OAuth flow and returns credentials. Uses goroutines and channels to handle the OAuth callback asynchronously because we need to run an HTTP server for the callback while also enforcing a timeout on the overall authentication process.

type Provider

type Provider struct {
	Issuer                string `json:"issuer"`
	AuthorizationEndpoint string `json:"authorization_endpoint"`
	TokenEndpoint         string `json:"token_endpoint"`
	UserinfoEndpoint      string `json:"userinfo_endpoint,omitempty"`
	JwksURI               string `json:"jwks_uri,omitempty"`
}

Provider represents an OAuth2/OIDC provider configuration

func DiscoverProvider

func DiscoverProvider(ctx context.Context, issuerURL string) (*Provider, error)

DiscoverProvider fetches OIDC provider configuration from well-known endpoint

type TokenResponse

type TokenResponse struct {
	AccessToken  string `json:"access_token"`
	TokenType    string `json:"token_type"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token,omitempty"`
	IDToken      string `json:"id_token,omitempty"`
}

TokenResponse represents an OAuth2 token response

func ExchangeCodeForTokens

func ExchangeCodeForTokens(ctx context.Context, provider *Provider, clientID, code, redirectURI, codeVerifier string) (*TokenResponse, error)

ExchangeCodeForTokens exchanges an authorization code for tokens

func RefreshAccessToken

func RefreshAccessToken(ctx context.Context, provider *Provider, clientID, refreshToken string) (*TokenResponse, error)

RefreshAccessToken uses a refresh token to get a new access token

Jump to

Keyboard shortcuts

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