auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package auth acquires the credentials chcli presents to ClickHouse.

The rest of the program only sees the Provider interface and the Credentials it returns; how a token was obtained (static value, cached OAuth session, browser login, refresh) stays inside this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Label

func Label(authType string) string

Label returns the human-readable name of an authentication type.

Types

type Credentials

type Credentials struct {
	Username string
	Password string
	Token    string

	// Identity is a display name for the authenticated principal, for
	// example the e-mail claim of an OIDC token.
	Identity string
	// Expiry is when Token stops being valid; zero if unknown or not applicable.
	Expiry time.Time
}

Credentials is what a ClickHouse connection needs to authenticate. Either Token is set (JWT / bearer authentication) or Username and Password are.

func (*Credentials) UsesToken

func (c *Credentials) UsesToken() bool

UsesToken reports whether these are token credentials.

type Endpoints

type Endpoints struct {
	Authorization string
	Token         string
	Device        string
	// Discovered is true when OIDC discovery succeeded.
	Discovered bool
}

Endpoints are the resolved provider endpoints, for diagnostics.

type FileStore

type FileStore struct {
	Dir string
}

FileStore keeps each token set in a JSON file readable only by its owner.

func (*FileStore) Delete

func (s *FileStore) Delete(key string) error

func (*FileStore) Load

func (s *FileStore) Load(key string) (*TokenSet, error)

func (*FileStore) Save

func (s *FileStore) Save(key string, ts *TokenSet) error

Save writes the token set atomically with 0600 permissions.

type JWTProvider

type JWTProvider struct {
	Token string
	// contains filtered or unexported fields
}

JWTProvider authenticates with a token supplied by the user.

func (*JWTProvider) Authenticate

func (p *JWTProvider) Authenticate(context.Context) (*Credentials, error)

type LoginRequiredError

type LoginRequiredError struct {
	Profile string
}

LoginRequiredError is returned when OAuth credentials are needed but the process is not allowed to start an interactive login.

func (*LoginRequiredError) Error

func (e *LoginRequiredError) Error() string

type OIDCConfig

type OIDCConfig struct {
	// Label names the provider in messages ("Google OAuth", "OIDC").
	Label string
	// Profile is the connection profile, used in the login hint.
	Profile string
	// CacheKey names the token cache entry.
	CacheKey string

	ClientID     string
	ClientSecret string // optional: public clients rely on PKCE alone
	Issuer       string
	// AuthURL, TokenURL and DeviceURL override the endpoints found through
	// OIDC discovery. Without an Issuer they are the only source.
	AuthURL   string
	TokenURL  string
	DeviceURL string

	Audience      string
	Scopes        []string
	UsernameClaim string
	RedirectURI   string
	Flow          string
	TokenType     string
	// AuthParams are extra authorization request parameters.
	AuthParams map[string]string
}

OIDCConfig configures the generic OAuth 2.0 / OpenID Connect provider.

type OIDCProvider

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

OIDCProvider implements browser and device logins against any OpenID Connect provider, caches the resulting tokens and refreshes them.

func (*OIDCProvider) Authenticate

func (p *OIDCProvider) Authenticate(ctx context.Context) (*Credentials, error)

Authenticate returns valid token credentials, doing the least work that achieves it: cached tokens are reused, expired ones refreshed, and only when neither is possible does an interactive login run. Non-interactive callers get a LoginRequiredError instead of a browser.

func (*OIDCProvider) Endpoints

func (p *OIDCProvider) Endpoints(ctx context.Context) (Endpoints, error)

Endpoints resolves the provider endpoints the same way a login would.

func (*OIDCProvider) Login

func (p *OIDCProvider) Login(ctx context.Context) (*Credentials, error)

Login forces the interactive flow, replacing any cached session.

func (*OIDCProvider) Logout

func (p *OIDCProvider) Logout() error

Logout removes the cached session.

func (*OIDCProvider) Status

func (p *OIDCProvider) Status() (Status, error)

Status reports on the cached session without any network access.

type Options

type Options struct {
	// Interactive permits opening a browser and waiting for the user.
	Interactive bool
	// Out receives messages for the user during login.
	Out io.Writer
	// Store persists OAuth tokens between runs.
	Store TokenStore
	// OpenBrowser opens a URL in the user's browser.
	OpenBrowser func(url string) error
	// HTTPClient is used for all requests to the identity provider.
	HTTPClient *http.Client
}

Options are the environment-dependent collaborators of a provider.

type PasswordProvider

type PasswordProvider struct {
	Username string
	Password string
}

PasswordProvider authenticates with a ClickHouse user name and password.

func (*PasswordProvider) Authenticate

func (p *PasswordProvider) Authenticate(context.Context) (*Credentials, error)

type Provider

type Provider interface {
	Authenticate(ctx context.Context) (*Credentials, error)
}

Provider yields credentials, refreshing or re-acquiring them when needed. Authenticate is called before every connection attempt and query, so implementations must be cheap when nothing has to be done.

func NewProvider

func NewProvider(r *config.Resolved, o Options) (Provider, error)

NewProvider selects and builds the provider for a resolved configuration.

func NonInteractive

func NonInteractive(provider Provider) Provider

NonInteractive returns a view of provider that never starts an interactive login: where the original would open a browser, the view fails with a LoginRequiredError. It is for background work, which must neither surprise the user with a browser window nor hold up the foreground while one is open. Cached tokens and refreshes are shared with the original.

type SessionProvider

type SessionProvider interface {
	Provider
	// Login runs the interactive login flow even if cached tokens exist.
	Login(ctx context.Context) (*Credentials, error)
	// Logout forgets all cached tokens.
	Logout() error
	// Status describes the cached session without contacting the provider.
	Status() (Status, error)
}

SessionProvider is implemented by providers that keep a login session (cached tokens) which can be inspected and ended.

type Status

type Status struct {
	LoggedIn    bool
	Identity    string
	Expiry      time.Time
	Refreshable bool
	Storage     string
}

Status describes a cached login session. It never contains token material.

type TokenSet

type TokenSet struct {
	AccessToken  string    `json:"access_token,omitempty"`
	IDToken      string    `json:"id_token,omitempty"`
	RefreshToken string    `json:"refresh_token,omitempty"`
	AccessExpiry time.Time `json:"access_expiry,omitempty"`
	IDExpiry     time.Time `json:"id_expiry,omitempty"`
	Identity     string    `json:"identity,omitempty"`

	// Fingerprint identifies the provider configuration the tokens were
	// issued for, so that editing a profile invalidates its cached session.
	Fingerprint string `json:"fingerprint"`

	// Storage says where the set was loaded from or saved to.
	Storage string `json:"-"`
}

TokenSet is a cached OAuth session.

type TokenStore

type TokenStore interface {
	// Load returns the token set stored under key, or nil if there is none.
	Load(key string) (*TokenSet, error)
	Save(key string, ts *TokenSet) error
	Delete(key string) error
}

TokenStore persists token sets between runs.

func NewTokenStore

func NewTokenStore(dir string) TokenStore

NewTokenStore returns the default store: the operating system's credential manager (macOS Keychain, Windows Credential Manager, Secret Service on Linux), falling back to owner-only files under dir when the credential manager is unavailable.

Jump to

Keyboard shortcuts

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