auth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAuthenticated = errors.New("not authenticated. Run 'spritz login' or set SPRITZ_API_KEY")

Functions

func ClearDeviceState added in v0.2.0

func ClearDeviceState(path string)

ClearDeviceState removes any pending device auth state.

func DeleteAPIKey

func DeleteAPIKey() error

func GetAPIKey

func GetAPIKey() (string, error)

func HasStoredCredentials added in v0.2.0

func HasStoredCredentials() bool

HasStoredCredentials checks for credentials in the keychain or encrypted file, ignoring the SPRITZ_API_KEY environment variable.

func LoadKeyMetadata added in v0.2.0

func LoadKeyMetadata() string

LoadKeyMetadata returns the stored key ID, or empty string if none.

func SaveDeviceState added in v0.2.0

func SaveDeviceState(path string, state *DeviceState) error

SaveDeviceState writes pending device auth state to disk.

func StoreKeyMetadata added in v0.2.0

func StoreKeyMetadata(keyID string)

StoreKeyMetadata persists the key ID so logout can offer server-side revocation.

Types

type DeviceState added in v0.2.0

type DeviceState struct {
	DeviceCode              string `json:"deviceCode"`
	UserCode                string `json:"userCode"`
	VerificationURI         string `json:"verificationUri"`
	VerificationURIComplete string `json:"verificationUriComplete"`
	Interval                int    `json:"interval"`
	ExpiresAt               string `json:"expiresAt"`
}

DeviceState is persisted between --device-start and --device-complete.

func DeviceStart added in v0.2.0

func DeviceStart(ctx context.Context, stateFile string) (*DeviceState, error)

DeviceStart initiates the device flow, persists state, and returns immediately. The caller should present the URL to the user, then later call DeviceComplete.

func LoadDeviceState added in v0.2.0

func LoadDeviceState(path string) (*DeviceState, error)

LoadDeviceState reads pending device auth state from disk. Returns nil if no pending state exists.

type DeviceTokenResponse added in v0.2.0

type DeviceTokenResponse struct {
	APIKey      string   `json:"apiKey"`
	KeyID       string   `json:"keyId"`
	Permissions []string `json:"permissions"`
	ExpiresAt   *string  `json:"expiresAt"`
	KeyName     *string  `json:"keyName"`
}

func DeviceAuth added in v0.2.0

func DeviceAuth(ctx context.Context) (*DeviceTokenResponse, error)

DeviceAuth runs the full device authorization flow (RFC 8628-style). It requests a device code, opens the browser for user approval, and polls until the user approves or the code expires. Returns the full token response so callers can store metadata.

func DeviceComplete added in v0.2.0

func DeviceComplete(ctx context.Context, stateFile string) (*DeviceTokenResponse, error)

DeviceComplete loads persisted state and polls for approval.

type LoginOptions added in v0.2.0

type LoginOptions struct {
	AllowFileStorage bool
	APIKey           string // If set, skip interactive prompts (works without TTY)
	DeviceStart      bool   // Just initiate device flow and exit
	DeviceComplete   bool   // Complete a previously started device flow
	DeviceStateFile  string // Explicit state path for two-step device flow
}

LoginOptions controls login behavior.

type LoginResult added in v0.2.0

type LoginResult struct {
	Mode                    string `json:"mode"`
	Email                   string `json:"email,omitempty"`
	FirstName               string `json:"firstName,omitempty"`
	Storage                 string `json:"storage,omitempty"`
	EnvVarActive            bool   `json:"envVarActive"`
	UserCode                string `json:"userCode,omitempty"`
	VerificationURI         string `json:"verificationUri,omitempty"`
	VerificationURIComplete string `json:"verificationUriComplete,omitempty"`
	ExpiresAt               string `json:"expiresAt,omitempty"`
	DeviceStateFile         string `json:"deviceStateFile,omitempty"`
}

func Login added in v0.2.0

func Login(ctx context.Context, opts LoginOptions) (*LoginResult, error)

Login runs the login flow. Interactive prompts require a TTY; pass APIKey in opts to authenticate non-interactively (e.g. piped envs).

type StorageMethod

type StorageMethod int
const (
	StorageEnv StorageMethod = iota
	StorageKeychain
	StorageFile
)

func GetAPIKeyWithSource added in v0.2.0

func GetAPIKeyWithSource() (string, StorageMethod, error)

func StoreAPIKey

func StoreAPIKey(apiKey string, allowFile bool) (StorageMethod, error)

StoreAPIKey stores the key in the system keychain. If the keychain is unavailable and allowFile is true, falls back to an encrypted file. If allowFile is false and the keychain fails, returns an error.

func (StorageMethod) String added in v0.2.0

func (m StorageMethod) String() string

type UserInfo added in v0.2.0

type UserInfo struct {
	Email     string `json:"email"`
	FirstName string `json:"firstName"`
}

func ValidateAPIKey added in v0.2.0

func ValidateAPIKey(apiKey string) (*UserInfo, error)

ValidateAPIKey checks the key against GET /v1/users/me and returns user info. Uses a self-contained HTTP call to avoid circular dependency with the api package.

Jump to

Keyboard shortcuts

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