auth

package
v0.1.7-alpha.6 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package auth handles authentication and OAuth flows.

Index

Constants

View Source
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

View Source
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

func OpenBrowser

func OpenBrowser(ctx context.Context, urlStr string) error

OpenBrowser opens a URL in the user's default browser.

Types

type CallbackResult

type CallbackResult struct {
	Code  string
	State string
	Error string
}

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 NewKeychain

func NewKeychain() *Keychain

NewKeychain creates a new Keychain instance.

func (*Keychain) DeleteToken

func (k *Keychain) DeleteToken() error

DeleteToken removes the token from the keychain.

func (*Keychain) IsAvailable

func (k *Keychain) IsAvailable() bool

IsAvailable checks if the system keychain is accessible.

func (*Keychain) LoadToken

func (k *Keychain) LoadToken() (*Token, error)

LoadToken retrieves the stored token from the keychain.

func (*Keychain) SaveToken

func (k *Keychain) SaveToken(token *Token) error

SaveToken stores a token in the keychain.

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

func (c *OAuthClient) RefreshToken(ctx context.Context, refreshToken string) (*Token, error)

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

func RequireAuth() (*Token, error)

RequireAuth should be called at the start of commands that require authentication.

func (*Token) IsExpired

func (t *Token) IsExpired() bool

IsExpired checks if the token has expired, accounting for clock skew.

func (*Token) IsExpiringSoon

func (t *Token) IsExpiringSoon(within time.Duration) bool

IsExpiringSoon checks if the token will expire within the given duration.

func (*Token) TimeUntilExpiry

func (t *Token) TimeUntilExpiry() time.Duration

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.

Jump to

Keyboard shortcuts

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