Documentation
¶
Overview ¶
Package auth handles authentication and OAuth flows.
Index ¶
- Constants
- Variables
- func OpenBrowser(ctx context.Context, urlStr string) error
- type CallbackResult
- type CallbackServer
- type IntrospectResponse
- type Keychain
- type OAuthClient
- func (c *OAuthClient) AuthorizationURL(pkce *PKCEParams, redirectURI string) string
- func (c *OAuthClient) ExchangeCode(ctx context.Context, code string, pkce *PKCEParams, redirectURI string) (*Token, error)
- func (c *OAuthClient) IntrospectToken(ctx context.Context, accessToken string) (*IntrospectResponse, error)
- func (c *OAuthClient) RefreshToken(ctx context.Context, refreshToken string) (*Token, error)
- func (c *OAuthClient) RevokeToken(ctx context.Context, token string) error
- type PKCEParams
- type Token
- type TokenResponse
Constants ¶
const ( // KeyringService is the service name used in the system keychain. KeyringService = "xf" // KeyringUser is the user/account name used in the system keychain. KeyringUser = "oauth-token" )
Variables ¶
var ( // ErrAuthRequired indicates that authentication is required. ErrAuthRequired = errors.New("authentication required") // ErrAuthFailed indicates an authentication operation failed. ErrAuthFailed = errors.New("authentication failed") // ErrAuthExpired indicates the authentication token has expired. ErrAuthExpired = errors.New("authentication expired") // ErrInvalidInput indicates invalid input to an auth function. ErrInvalidInput = errors.New("invalid input") // ErrUnsupported indicates an unsupported operation or platform. ErrUnsupported = errors.New("unsupported") )
Functions ¶
Types ¶
type CallbackResult ¶
CallbackResult holds the result of the OAuth callback.
type CallbackServer ¶
type CallbackServer struct {
// contains filtered or unexported fields
}
CallbackServer handles the OAuth redirect callback from the authorization server.
func NewCallbackServer ¶
func NewCallbackServer(ctx context.Context, path string) (*CallbackServer, error)
NewCallbackServer creates a new callback server.
func (*CallbackServer) RedirectURI ¶
func (cs *CallbackServer) RedirectURI() string
RedirectURI returns the callback server's redirect URI.
func (*CallbackServer) Shutdown ¶
func (cs *CallbackServer) Shutdown(ctx context.Context) error
Shutdown gracefully shuts down the callback server.
func (*CallbackServer) Start ¶
func (cs *CallbackServer) Start()
Start begins listening for the OAuth callback.
func (*CallbackServer) WaitForCallback ¶
func (cs *CallbackServer) WaitForCallback(ctx context.Context) (*CallbackResult, error)
WaitForCallback waits for the OAuth callback result.
type IntrospectResponse ¶
type IntrospectResponse struct {
Active bool `json:"active"`
Scope string `json:"scope,omitempty"`
ClientID string `json:"client_id,omitempty"`
Username string `json:"username,omitempty"`
TokenType string `json:"token_type,omitempty"`
Exp int64 `json:"exp,omitempty"`
Iat int64 `json:"iat,omitempty"`
Sub string `json:"sub,omitempty"`
}
IntrospectResponse represents the token introspection response.
type Keychain ¶
type Keychain struct{}
Keychain manages secure token storage in the system keychain.
func (*Keychain) DeleteToken ¶
DeleteToken removes the token from the keychain.
func (*Keychain) IsAvailable ¶
IsAvailable checks if the system keychain is accessible.
type OAuthClient ¶
type OAuthClient struct {
// contains filtered or unexported fields
}
OAuthClient handles the OAuth authentication flow.
func NewOAuthClient ¶
func NewOAuthClient(cfg *config.OAuthConfig) *OAuthClient
NewOAuthClient creates a new OAuth client.
func (*OAuthClient) AuthorizationURL ¶
func (c *OAuthClient) AuthorizationURL(pkce *PKCEParams, redirectURI string) string
AuthorizationURL generates the OAuth authorization URL.
func (*OAuthClient) ExchangeCode ¶
func (c *OAuthClient) ExchangeCode(ctx context.Context, code string, pkce *PKCEParams, redirectURI string) (*Token, error)
ExchangeCode exchanges an authorization code for a token.
func (*OAuthClient) IntrospectToken ¶
func (c *OAuthClient) IntrospectToken(ctx context.Context, accessToken string) (*IntrospectResponse, error)
IntrospectToken checks the validity and metadata of a token.
func (*OAuthClient) RefreshToken ¶
RefreshToken refreshes an expired access token.
func (*OAuthClient) RevokeToken ¶
func (c *OAuthClient) RevokeToken(ctx context.Context, token string) error
RevokeToken revokes an access or refresh token.
type PKCEParams ¶
type PKCEParams struct {
// CodeVerifier is the random string used to generate the challenge.
CodeVerifier string
// CodeChallenge is the S256 hash of the verifier.
CodeChallenge string
// CodeChallengeMethod is always "S256".
CodeChallengeMethod string
// State is a random string to prevent CSRF attacks.
State string
}
PKCEParams contains the PKCE parameters for an OAuth flow.
func GeneratePKCE ¶
func GeneratePKCE() (*PKCEParams, error)
GeneratePKCE generates new PKCE parameters for an OAuth flow.
type Token ¶
type Token struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token,omitempty"`
TokenType string `json:"token_type"`
ExpiresAt time.Time `json:"expires_at"`
Scope string `json:"scope,omitempty"`
IssuedAt time.Time `json:"issued_at"`
BaseURL string `json:"base_url"`
}
Token represents an OAuth token with expiry information.
func RequireAuth ¶
RequireAuth should be called at the start of commands that require authentication.
func (*Token) IsExpiringSoon ¶
IsExpiringSoon checks if the token will expire within the given duration.
func (*Token) TimeUntilExpiry ¶
TimeUntilExpiry returns the duration until the token expires.
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"`
Scope string `json:"scope,omitempty"`
}
TokenResponse represents the OAuth token response.