Documentation
¶
Overview ¶
Package oauth provides an out-of-the-box OAuth authentication provider for proto-cli.
It supports both Device Code (interactive) and Authorization Code with PKCE (flag-based) flows, stores tokens as JSON with automatic refresh, and implements all cliauth interfaces.
provider := oauth.NewProvider(
oauth.WithClientID("my-client-id"),
oauth.WithEndpoints("https://auth.example.com/authorize", "https://auth.example.com/token"),
oauth.WithDeviceAuthURL("https://auth.example.com/device/code"),
oauth.WithScopes("openid", "profile", "email"),
)
Index ¶
- type Provider
- func (p *Provider) Decorate(ctx context.Context, store cliauth.AuthStore) (map[string]string, error)
- func (p *Provider) Flags() []cli.Flag
- func (p *Provider) Login(ctx context.Context, cmd *cli.Command, store cliauth.AuthStore) error
- func (p *Provider) LoginInteractive(ctx context.Context, _ io.Reader, out io.Writer, store cliauth.AuthStore) error
- func (p *Provider) Logout(ctx context.Context, store cliauth.AuthStore) error
- func (p *Provider) Status(ctx context.Context, store cliauth.AuthStore) (string, error)
- type ProviderOption
- func WithAudience(aud string) ProviderOption
- func WithBrowserOpen(fn func(url string) error) ProviderOption
- func WithClientID(id string) ProviderOption
- func WithClientSecret(secret string) ProviderOption
- func WithDeviceAuthURL(url string) ProviderOption
- func WithEndpoints(authURL, tokenURL string) ProviderOption
- func WithExpiryBuffer(d time.Duration) ProviderOption
- func WithHTTPClient(client *http.Client) ProviderOption
- func WithLoginCallback(fn func(token *oauth2.Token)) ProviderOption
- func WithLoopbackHost(host string) ProviderOption
- func WithRevocationURL(url string) ProviderOption
- func WithScopes(scopes ...string) ProviderOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider implements cliauth.InteractiveLoginProvider, cliauth.LogoutProvider, cliauth.StatusProvider, and cliauth.AuthDecorator using OAuth 2.0 flows.
func NewProvider ¶
func NewProvider(opts ...ProviderOption) *Provider
NewProvider creates a Provider configured with the given options.
func (*Provider) Decorate ¶
func (p *Provider) Decorate(ctx context.Context, store cliauth.AuthStore) (map[string]string, error)
Decorate loads the stored token, refreshes it if needed, and returns authorization metadata.
func (*Provider) LoginInteractive ¶
func (p *Provider) LoginInteractive(ctx context.Context, _ io.Reader, out io.Writer, store cliauth.AuthStore) error
LoginInteractive performs a Device Code flow, printing verification instructions to out.
type ProviderOption ¶
type ProviderOption func(*Provider)
ProviderOption configures a Provider.
func WithAudience ¶
func WithAudience(aud string) ProviderOption
WithAudience sets the audience parameter sent with authorization requests.
func WithBrowserOpen ¶
func WithBrowserOpen(fn func(url string) error) ProviderOption
WithBrowserOpen sets the function used to open a URL in the user's browser. Useful for testing or environments without a browser.
func WithClientID ¶
func WithClientID(id string) ProviderOption
WithClientID sets the OAuth client ID.
func WithClientSecret ¶
func WithClientSecret(secret string) ProviderOption
WithClientSecret sets the OAuth client secret for confidential clients.
func WithDeviceAuthURL ¶
func WithDeviceAuthURL(url string) ProviderOption
WithDeviceAuthURL sets the device authorization endpoint URL.
func WithEndpoints ¶
func WithEndpoints(authURL, tokenURL string) ProviderOption
WithEndpoints sets the authorization and token endpoint URLs.
func WithExpiryBuffer ¶
func WithExpiryBuffer(d time.Duration) ProviderOption
WithExpiryBuffer sets how far before token expiry a refresh should be triggered.
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) ProviderOption
WithHTTPClient sets a custom HTTP client (useful for testing or proxies).
func WithLoginCallback ¶
func WithLoginCallback(fn func(token *oauth2.Token)) ProviderOption
WithLoginCallback sets a function called after a successful login with the new token.
func WithLoopbackHost ¶
func WithLoopbackHost(host string) ProviderOption
WithLoopbackHost sets the host for the PKCE loopback redirect server.
func WithRevocationURL ¶
func WithRevocationURL(url string) ProviderOption
WithRevocationURL sets the token revocation endpoint URL (RFC 7009).
func WithScopes ¶
func WithScopes(scopes ...string) ProviderOption
WithScopes sets the OAuth scopes to request.