auth

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package auth handles authentication and OAuth flows.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrStoreUnavailable indicates that the credential store cannot be accessed.
	ErrStoreUnavailable = errors.New("credential storage unavailable")
	// ErrConfigMismatch indicates that stored credentials target another configuration.
	ErrConfigMismatch = errors.New("authentication configuration mismatch")

	// ErrAuthRequired indicates that authentication is required.
	ErrAuthRequired = errors.New("authentication required")

	// ErrInvalidInput indicates invalid input to an auth function.
	ErrInvalidInput = errors.New("invalid input")
)

Functions

func ErrorMessage

func ErrorMessage(err error) string

ErrorMessage removes classification sentinels from user-facing diagnostics.

func GeneratePKCE

func GeneratePKCE() (*pkceParams, error)

GeneratePKCE generates new PKCE parameters for an OAuth flow.

func NewCallbackServer

func NewCallbackServer(ctx context.Context, path string) (*callbackServer, error)

NewCallbackServer creates a new callback server.

func NewOAuthClient

func NewOAuthClient(cfg *config.OAuthConfig) *oauthClient

NewOAuthClient creates a new OAuth client.

func OpenBrowser

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

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

Types

type FileStore

type FileStore struct {
	// contains filtered or unexported fields
}

FileStore stores OAuth credentials separately from ordinary configuration. Reads reject nonregular files and check that the opened file still matches the path. Except for logout and Windows, group or other permissions are rejected. Windows relies on filesystem access controls instead of Unix mode checks. Malformed file diagnostics do not include secret input.

func (*FileStore) DeleteToken

func (s *FileStore) DeleteToken() error

DeleteToken removes a regular credential file, ignoring a missing file.

func (*FileStore) LoadToken

func (s *FileStore) LoadToken() (*Token, error)

LoadToken reads credentials after checking file permissions.

func (*FileStore) LoadTokenForLogout

func (s *FileStore) LoadTokenForLogout() (*Token, error)

LoadTokenForLogout reads a regular credential file without checking permissions.

func (*FileStore) Path

func (s *FileStore) Path() string

Path returns the credential file path.

func (*FileStore) PrepareLogin

func (s *FileStore) PrepareLogin() error

PrepareLogin rejects known storage failures before browser authentication. The temporary probe does not replace existing credentials.

func (*FileStore) SaveToken

func (s *FileStore) SaveToken(token *Token) error

SaveToken writes nonexternal credentials to a temporary file and renames it over the destination after syncing it. Existing files must pass validation.

func (*FileStore) Source

func (s *FileStore) Source() string

Source returns the credential source name.

type IntrospectResponse

type IntrospectResponse struct {
	Active   bool   `json:"active"`
	Username string `json:"username,omitempty"`
	Scope    string `json:"scope,omitempty"`
	Exp      int64  `json:"exp,omitempty"`
	Iat      int64  `json:"iat,omitempty"`
}

IntrospectResponse represents the token introspection response.

type Store

type Store interface {
	LoadToken() (*Token, error)
	SaveToken(*Token) error
	DeleteToken() error
	Source() string
}

Store is the selected credential source. Environment credentials are read-only.

func NewStore

func NewStore() (Store, error)

NewStore resolves environment credentials before the configured persistent store.

type Token

type Token struct {
	External     bool      `json:"-"`
	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 holds OAuth credentials, expiry, issue time, scope, and the issuing base URL. External tokens have no local expiry or refresh metadata and the External marker is not serialized.

func RequireAuthFrom

func RequireAuthFrom(store Store) (*Token, error)

RequireAuthFrom loads credentials and checks their configured destination.

func (*Token) IsExpired

func (t *Token) IsExpired() bool

IsExpired reports whether the expiry is less than 30 seconds away or past.

func (*Token) IsExpiringSoon

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

IsExpiringSoon reports whether the expiry is strictly before now plus within. It does not special-case external tokens or a zero expiry.

func (*Token) TimeUntilExpiry

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

TimeUntilExpiry returns the duration until the token expires.

type WritableStore

type WritableStore interface {
	Store
	PrepareLogin() error
	LoadTokenForLogout() (*Token, error)
}

WritableStore supports the persistent authentication lifecycle. Logout may read an insecure regular file solely to revoke its credentials.

func NewWritableStore

func NewWritableStore() (WritableStore, error)

NewWritableStore selects persistent storage without relying on display labels.

Jump to

Keyboard shortcuts

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