Documentation
¶
Index ¶
- Variables
- func ClientFromContext(ctx context.Context) (*http.Client, bool)
- func GetClient(ctx context.Context) (*http.Client, error)
- func GetClientOrExit(ctx context.Context) *http.Client
- func Login(ctx context.Context) error
- func Logout() error
- func OpenBrowser(url string) error
- func PollForToken(ctx context.Context, clientID, deviceCode string, interval int) (*oauth2.Token, error)
- func RefreshToken(ctx context.Context) error
- func Status() (bool, *oauth2.Token, error)
- func WithAuthClient(ctx context.Context) (context.Context, error)
- type AuthConfig
- type AuthMiddleware
- type DeviceFlowError
- type DeviceFlowResponse
- type FileStorage
- type GetAuthConfigFunc
- type GetClientIDFunc
- type GetClientSecretFunc
- type GetScopesFunc
- type GetTokenStorageFunc
- type KeyringStorage
- type SaveAuthConfigFunc
- type Storage
Constants ¶
This section is empty.
Variables ¶
var ( // TokenSource holds the OAuth2 token source TokenSource oauth2.TokenSource // ErrNotAuthenticated is returned when the user is not authenticated ErrNotAuthenticated = errors.New("not authenticated") )
var ( // ErrNoToken is returned when no token is found ErrNoToken = errors.New("no token found") // ErrInvalidToken is returned when the token is invalid ErrInvalidToken = errors.New("invalid token") )
Functions ¶
func ClientFromContext ¶
ClientFromContext gets the authenticated client from the context
func GetClientOrExit ¶
GetClientOrExit returns an HTTP client or exits if not authenticated
func OpenBrowser ¶
OpenBrowser attempts to open the verification URL in the default browser
func PollForToken ¶
func PollForToken(ctx context.Context, clientID, deviceCode string, interval int) (*oauth2.Token, error)
PollForToken polls GitHub for an access token using the device code
func RefreshToken ¶
RefreshToken attempts to refresh the OAuth token
Types ¶
type AuthConfig ¶
type AuthConfig struct {
// ClientID is the GitHub OAuth client ID
ClientID string
// ClientSecret is the GitHub OAuth client secret
ClientSecret string
// Scopes are the OAuth scopes to request
Scopes []string
// TokenStorage defines how to store the OAuth token
// Options: "keyring", "file", "auto"
TokenStorage string
}
AuthConfig holds the authentication configuration
type AuthMiddleware ¶
type AuthMiddleware struct {
// MaxRetries is the maximum number of retries for token refresh
MaxRetries int
// RetryDelay is the delay between retries
RetryDelay time.Duration
// contains filtered or unexported fields
}
AuthMiddleware provides middleware for authenticated requests
func NewAuthMiddleware ¶
func NewAuthMiddleware() *AuthMiddleware
NewAuthMiddleware creates a new AuthMiddleware
func (*AuthMiddleware) RoundTripper ¶
func (m *AuthMiddleware) RoundTripper(base http.RoundTripper) http.RoundTripper
RoundTripper returns an http.RoundTripper that handles authentication
type DeviceFlowError ¶
type DeviceFlowError struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
DeviceFlowError represents an error from the device flow
type DeviceFlowResponse ¶
type DeviceFlowResponse struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationURI string `json:"verification_uri"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval"`
}
DeviceFlowResponse represents the response from the device flow initiation
func InitiateDeviceFlow ¶
func InitiateDeviceFlow(clientID string, scopes []string) (*DeviceFlowResponse, error)
InitiateDeviceFlow starts the GitHub OAuth device flow
type FileStorage ¶
type FileStorage struct {
// contains filtered or unexported fields
}
FileStorage implements Storage using encrypted files
func NewFileStorage ¶
func NewFileStorage() (*FileStorage, error)
NewFileStorage creates a new FileStorage
func (*FileStorage) DeleteToken ¶
func (s *FileStorage) DeleteToken() error
DeleteToken deletes the token file
type GetAuthConfigFunc ¶
type GetAuthConfigFunc func() AuthConfig
GetAuthConfigFunc is the function type for GetAuthConfig
var GetAuthConfig GetAuthConfigFunc = func() AuthConfig { cm := config.NewConfigManager() if err := cm.Load(); err != nil { return AuthConfig{ Scopes: []string{"notifications", "repo"}, TokenStorage: "auto", } } cfg := cm.GetConfig() return AuthConfig{ ClientID: cfg.Auth.ClientID, ClientSecret: cfg.Auth.ClientSecret, Scopes: cfg.Auth.Scopes, TokenStorage: cfg.Auth.TokenStorage, } }
GetAuthConfig returns the authentication configuration from the global config
type GetClientIDFunc ¶
type GetClientIDFunc func() string
GetClientIDFunc is the function type for GetClientID
var GetClientID GetClientIDFunc = func() string { return GetAuthConfig().ClientID }
GetClientID returns the GitHub OAuth client ID
type GetClientSecretFunc ¶
type GetClientSecretFunc func() string
GetClientSecretFunc is the function type for GetClientSecret
var GetClientSecret GetClientSecretFunc = func() string { return GetAuthConfig().ClientSecret }
GetClientSecret returns the GitHub OAuth client secret
type GetScopesFunc ¶
type GetScopesFunc func() []string
GetScopesFunc is the function type for GetScopes
var GetScopes GetScopesFunc = func() []string { return GetAuthConfig().Scopes }
GetScopes returns the OAuth scopes to request
type GetTokenStorageFunc ¶
type GetTokenStorageFunc func() string
GetTokenStorageFunc is the function type for GetTokenStorage
var GetTokenStorage GetTokenStorageFunc = func() string { return GetAuthConfig().TokenStorage }
GetTokenStorage returns the token storage method
type KeyringStorage ¶
type KeyringStorage struct{}
KeyringStorage implements Storage using the system keyring
func (*KeyringStorage) DeleteToken ¶
func (s *KeyringStorage) DeleteToken() error
DeleteToken deletes the token from the system keyring
type SaveAuthConfigFunc ¶
type SaveAuthConfigFunc func(AuthConfig) error
SaveAuthConfigFunc is the function type for SaveAuthConfig
var SaveAuthConfig SaveAuthConfigFunc = func(authConfig AuthConfig) error { cm := config.NewConfigManager() if err := cm.Load(); err != nil { return err } cfg := cm.GetConfig() cfg.Auth.ClientID = authConfig.ClientID cfg.Auth.ClientSecret = authConfig.ClientSecret cfg.Auth.Scopes = authConfig.Scopes cfg.Auth.TokenStorage = authConfig.TokenStorage return cm.Save() }
SaveAuthConfig saves the authentication configuration to the global config