auth

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// TokenSource holds the OAuth2 token source
	TokenSource oauth2.TokenSource

	// ErrNotAuthenticated is returned when the user is not authenticated
	ErrNotAuthenticated = errors.New("not authenticated")
)
View Source
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

func ClientFromContext(ctx context.Context) (*http.Client, bool)

ClientFromContext gets the authenticated client from the context

func GetClient

func GetClient(ctx context.Context) (*http.Client, error)

GetClient returns an HTTP client with the OAuth2 token

func GetClientOrExit

func GetClientOrExit(ctx context.Context) *http.Client

GetClientOrExit returns an HTTP client or exits if not authenticated

func Login

func Login(ctx context.Context) error

Login performs the GitHub OAuth2 device flow authentication

func Logout

func Logout() error

Logout removes the stored credentials

func OpenBrowser

func OpenBrowser(url string) error

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

func RefreshToken(ctx context.Context) error

RefreshToken attempts to refresh the OAuth token

func Status

func Status() (bool, *oauth2.Token, error)

Status checks the authentication status

func WithAuthClient

func WithAuthClient(ctx context.Context) (context.Context, error)

WithAuthClient adds an authenticated client to the context

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

func (*FileStorage) LoadToken

func (s *FileStorage) LoadToken() (*oauth2.Token, error)

LoadToken loads the token from an encrypted file

func (*FileStorage) SaveToken

func (s *FileStorage) SaveToken(token *oauth2.Token) error

SaveToken saves the token to an encrypted 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

func (*KeyringStorage) LoadToken

func (s *KeyringStorage) LoadToken() (*oauth2.Token, error)

LoadToken loads the token from the system keyring

func (*KeyringStorage) SaveToken

func (s *KeyringStorage) SaveToken(token *oauth2.Token) error

SaveToken saves the token to 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

type Storage

type Storage interface {
	SaveToken(token *oauth2.Token) error
	LoadToken() (*oauth2.Token, error)
	DeleteToken() error
}

Storage interface for token storage

func CreateStorage

func CreateStorage() (Storage, error)

CreateStorage creates a storage implementation based on the configuration

Jump to

Keyboard shortcuts

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