Documentation
¶
Overview ¶
Package oauth implements Qdrant Cloud CLI browser/device login.
The Auth0 issuer is not a separate config field. It is taken from QDRANT_CLOUD_ENDPOINT (grpc.<cluster>.qdrant.io) by:
- mapping the gRPC host to the HTTPS API host (grpc. → api.)
- fetching unauthenticated RFC 9728 protected-resource metadata
- falling back to login.<cluster>.qdrant.io when the gateway does not advertise OAuth yet
Index ¶
- Constants
- Variables
- func ScopeLabel(scope string) string
- type Client
- func (c *Client) Discover(ctx context.Context, endpoint string) (Discovery, error)
- func (c *Client) Login(ctx context.Context, opts LoginOptions) (LoginResult, error)
- func (c *Client) Logout(ctx context.Context, endpoint string) (warnings []error, err error)
- func (c *Client) Session(endpoint string) (Token, bool, error)
- func (c *Client) Token(ctx context.Context, endpoint string) (Token, error)
- func (c *Client) TokenSource(ctx context.Context, endpoint string) (oauth2.TokenSource, error)
- type Discovery
- type LoginOptions
- type LoginResult
- type MemorySecrets
- type OSKeychain
- type Options
- type SecretStore
- type Token
- type WaitCodeFunc
Constants ¶
const ( StoreAuto = "auto" StoreKeychain = "keychain" StoreFile = "file" )
Token store modes (config token_store, env QDRANT_CLOUD_TOKEN_STORE), and the value of Token.Store:
- "" or StoreAuto: the OS keychain, or credentials.yaml (0600) when no keychain is available;
- StoreKeychain: the OS keychain only; saving fails when it is not available;
- StoreFile: credentials.yaml only.
Variables ¶
var ErrNotLoggedIn = errors.New("not logged in")
ErrNotLoggedIn means there is no stored session for the endpoint.
var ErrSecretNotFound = keyring.ErrNotFound
ErrSecretNotFound is the error a SecretStore returns for an entry that does not exist.
Functions ¶
func ScopeLabel ¶
ScopeLabel is how a scope reads in a sentence: "read only" or "manage".
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client logs in to Qdrant Cloud with OAuth and keeps the session of each endpoint: discovery of the login server, the browser and device logins, refresh, and logout.
func (*Client) Discover ¶
Discover maps endpoint → API host, then prefers the gateway's unauthenticated PRM document for the issuer. If OAuth is not enabled yet, it infers login.<cluster>.
func (*Client) Login ¶
func (c *Client) Login(ctx context.Context, opts LoginOptions) (LoginResult, error)
Login runs the browser (authorization code + PKCE) or device login and stores the session.
func (*Client) Logout ¶
Logout removes endpoint's session. It is best effort: when the refresh token can be read, it is revoked first, and a failure to read or revoke it is returned as a warning, not an error, so that logout still works when the keychain entry is gone or the login server cannot be reached. err is set only when the session could not be removed.
func (*Client) Session ¶
Session returns the stored session for endpoint without its access and refresh tokens (it does not read the OS keychain), or false when there is none.
func (*Client) Token ¶
Token returns a fresh access token for endpoint's session, with the session it belongs to.
func (*Client) TokenSource ¶
TokenSource returns the access token of endpoint's session, refreshed when it is about to expire. A refreshed token is written back to the store. When the login server no longer accepts the refresh token (invalid_grant), the session is removed, so the next command asks to log in instead of trying again. It returns ErrNotLoggedIn when there is no session.
type Discovery ¶
type Discovery struct {
// Endpoint is the gRPC endpoint the setup is for.
Endpoint string `json:"endpoint"`
// Resource is the API the access token is for (its audience).
Resource string `json:"resource"`
// Issuer is the login server.
Issuer string `json:"issuer"`
// Source says where Issuer comes from.
Source string `json:"source"`
Scopes []string `json:"scopes"`
// MetadataError is why the protected-resource metadata could not be used, when Source is
// endpoint inference. The inferred values work too; this explains why they were inferred.
MetadataError string `json:"metadata_error,omitempty"`
}
Discovery is the OAuth setup for one Cloud environment.
type LoginOptions ¶
type LoginOptions struct {
Endpoint string
// ClientID overrides the built-in client id of the login server.
ClientID string
// Scope is read-only or manage (the default).
Scope string
Device bool
// LoginURL overrides the discovered login server.
LoginURL string
// OpenURL opens the login page in a browser.
OpenURL func(string) error
// Output gets the prompts: the login URL, or the device code.
Output io.Writer
}
LoginOptions configure a browser or device login.
type LoginResult ¶
type LoginResult struct {
Token Token
Discovery Discovery
// CredentialsPath is the credentials.yaml the session was written to.
CredentialsPath string
// KeychainErr is why the tokens went to credentials.yaml although the keychain was to be tried.
KeychainErr error
}
LoginResult is a completed login.
type MemorySecrets ¶
type MemorySecrets struct {
// contains filtered or unexported fields
}
MemorySecrets is an in-memory SecretStore, so that tests never read or write the keychain of the machine they run on.
func NewMemorySecrets ¶
func NewMemorySecrets() *MemorySecrets
NewMemorySecrets returns an empty MemorySecrets.
func (*MemorySecrets) Delete ¶
func (m *MemorySecrets) Delete(service, user string) error
Delete removes a secret, or returns ErrSecretNotFound.
func (*MemorySecrets) Get ¶
func (m *MemorySecrets) Get(service, user string) (string, error)
Get returns a secret, or ErrSecretNotFound.
func (*MemorySecrets) Set ¶
func (m *MemorySecrets) Set(service, user, secret string) error
Set stores secret.
type OSKeychain ¶
type OSKeychain struct{}
OSKeychain is the SecretStore of the operating system.
func (OSKeychain) Delete ¶
func (OSKeychain) Delete(service, user string) error
Delete removes a secret from the keychain, or returns ErrSecretNotFound.
func (OSKeychain) Get ¶
func (OSKeychain) Get(service, user string) (string, error)
Get reads a secret from the keychain, or returns ErrSecretNotFound.
func (OSKeychain) Set ¶
func (OSKeychain) Set(service, user, secret string) error
Set stores secret in the keychain.
type Options ¶
type Options struct {
// ConfigDir returns the directory of the config file; credentials.yaml goes next to it.
ConfigDir func() string
// TokenStore returns the token store mode: StoreAuto (the default), StoreKeychain, or StoreFile.
TokenStore func() string
// Secrets keeps the access and refresh tokens. The default is the OS keychain.
Secrets SecretStore
// HTTPClient is used for every OAuth request. The default times out after 10 seconds.
HTTPClient *http.Client
// WaitCode replaces the loopback listener of the browser login, with the redirect URL it sends.
// For tests.
WaitCode WaitCodeFunc
RedirectURL string
}
Options configure a Client. Only ConfigDir is required.
type SecretStore ¶
type SecretStore interface {
Set(service, user, secret string) error
Get(service, user string) (string, error)
Delete(service, user string) error
}
SecretStore keeps the access and refresh tokens: the OS keychain (macOS Keychain, Windows Credential Manager, or the Secret Service on Linux), or MemorySecrets in tests.
type Token ¶
type Token struct {
Endpoint string `yaml:"endpoint"`
// Store is StoreKeychain when the access and refresh tokens are in the OS keychain; credentials.yaml
// then holds only the fields that are not secret. Empty means both are in credentials.yaml.
Store string `yaml:"store,omitempty"`
AccessToken string `yaml:"access_token,omitempty"`
RefreshToken string `yaml:"refresh_token,omitempty"`
TokenType string `yaml:"token_type,omitempty"`
Expiry time.Time `yaml:"expiry,omitempty"`
// Scope is the requested scope: what qcloud asked the login server for (read-only or manage).
Scope string `yaml:"requested_scope,omitempty"`
// GrantedScope is what the user granted on the consent page (read-only or manage), which can be
// narrower than the requested scope. Qdrant Cloud enforces it on every call.
GrantedScope string `yaml:"granted_scope,omitempty"`
// Email is the user the session belongs to, for display.
Email string `yaml:"email,omitempty"`
Issuer string `yaml:"issuer,omitempty"`
ClientID string `yaml:"client_id,omitempty"`
Resource string `yaml:"resource,omitempty"`
}
Token is a stored OAuth session for one gRPC endpoint.