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 ¶
- func Label(authType string) string
- type Credentials
- type Endpoints
- type FileStore
- type JWTProvider
- type LoginRequiredError
- type OIDCConfig
- type OIDCProvider
- func (p *OIDCProvider) Authenticate(ctx context.Context) (*Credentials, error)
- func (p *OIDCProvider) Endpoints(ctx context.Context) (Endpoints, error)
- func (p *OIDCProvider) Login(ctx context.Context) (*Credentials, error)
- func (p *OIDCProvider) Logout() error
- func (p *OIDCProvider) Status() (Status, error)
- type Options
- type PasswordProvider
- type Provider
- type SessionProvider
- type Status
- type TokenSet
- type TokenStore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
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 ¶
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 ¶
NewProvider selects and builds the provider for a resolved configuration.
func NonInteractive ¶
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.