auth

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package auth holds the CLI's credentials: where they are kept (OS keychain, with a 0600 file as the fallback), how the bearer token is chosen for a request, and the device login flow.

Index

Constants

View Source
const (
	KindPersonal = "pat" // waniis_pat_…: a personal API token
	KindGrant    = "cli" // waniis_cli_…: a device-flow access token
	KindEnv      = "env" // WANIIS_TOKEN, never stored
)

Token kinds, from the token prefixes (DEC-152).

View Source
const (
	PrefixPersonal = "waniis_pat_"
	PrefixGrant    = "waniis_cli_"
)

Prefixes of the two token kinds.

View Source
const (
	GrantDeviceCode   = "urn:ietf:params:oauth:grant-type:device_code"
	GrantRefreshToken = "refresh_token"
)

Grant types (RFC 8628 and RFC 6749).

View Source
const ClientID = "waniis-cli"

ClientID identifies the CLI to the device flow.

View Source
const KeychainService = "waniis"

KeychainService is the service name entries are stored under in the OS keychain.

Variables

This section is empty.

Functions

func KindOf

func KindOf(token string) string

KindOf guesses the kind of a token from its prefix; unknown prefixes count as personal tokens.

Types

type ChainStore

type ChainStore struct {
	Primary  Store
	Fallback Store
	// Notify is told when the fallback is used; may be nil.
	Notify func(msg string)
}

ChainStore prefers the keychain and falls back to the file where there is no keychain (a headless Linux box, a container). Save reports which one took the credentials.

func (*ChainStore) Delete

func (c *ChainStore) Delete(host string) error

Delete removes the credentials from both.

func (*ChainStore) Load

func (c *ChainStore) Load(host string) (*Credentials, error)

Load looks in the keychain, then in the file.

func (*ChainStore) Save

func (c *ChainStore) Save(cred *Credentials) error

Save tries the keychain and falls back to the file.

type Credentials

type Credentials struct {
	Host         string    `json:"host"`
	Token        string    `json:"token"`
	RefreshToken string    `json:"refreshToken,omitempty"`
	ExpiresAt    time.Time `json:"expiresAt,omitempty"`
	Kind         string    `json:"kind"`
	UserID       string    `json:"userId,omitempty"`
	Login        string    `json:"login,omitempty"`
}

Credentials is what is stored for one host. It never leaves the credential store except as an Authorization header, and no command prints it.

type DeviceCode

type DeviceCode struct {
	DeviceCode              string `json:"device_code"`
	UserCode                string `json:"user_code"`
	VerificationURI         string `json:"verification_uri"`
	VerificationURIComplete string `json:"verification_uri_complete"`
	ExpiresIn               int    `json:"expires_in"`
	Interval                int    `json:"interval"`
}

DeviceCode is the answer to POST /auth/device/code (RFC 8628 section 3.2).

type DeviceFlow

type DeviceFlow struct {
	Client *api.Client // used anonymously
	Now    func() time.Time
	Sleep  func(context.Context, time.Duration) error
	// Show is called once with the code to present to the person (URL and user code).
	Show func(DeviceCode)
}

DeviceFlow runs the OAuth 2.0 device authorization grant against the API.

func (*DeviceFlow) Poll

func (d *DeviceFlow) Poll(ctx context.Context, dc *DeviceCode) (*Credentials, error)

Poll waits for the person to approve, at the server's interval, backing off on slow_down.

func (*DeviceFlow) Start

func (d *DeviceFlow) Start(ctx context.Context, deviceName, scope string) (*DeviceCode, error)

Start asks for a device code.

type FileStore

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

FileStore keeps credentials in a JSON file readable only by the user (mode 0600), a map by host.

func NewFileStore

func NewFileStore(dir string) *FileStore

NewFileStore builds a FileStore at dir/credentials.

func (*FileStore) Delete

func (f *FileStore) Delete(host string) error

Delete removes the host's credentials.

func (*FileStore) Load

func (f *FileStore) Load(host string) (*Credentials, error)

Load reads the host's credentials.

func (*FileStore) Save

func (f *FileStore) Save(c *Credentials) error

Save writes the host's credentials.

type KeyringStore

type KeyringStore struct{}

KeyringStore keeps credentials in the OS keychain (macOS Keychain, Windows Credential Manager, the Secret Service on Linux), one entry per host under the service "waniis".

func (KeyringStore) Delete

func (KeyringStore) Delete(host string) error

Delete removes the host's entry; a missing one is not an error.

func (KeyringStore) Load

func (KeyringStore) Load(host string) (*Credentials, error)

Load reads the host's entry.

func (KeyringStore) Save

func (KeyringStore) Save(c *Credentials) error

Save writes the host's entry.

type MemoryStore

type MemoryStore struct {
	M map[string]*Credentials
	// contains filtered or unexported fields
}

MemoryStore is an in-memory Store for tests.

func (*MemoryStore) Delete

func (m *MemoryStore) Delete(host string) error

Delete removes the host's credentials.

func (*MemoryStore) Load

func (m *MemoryStore) Load(host string) (*Credentials, error)

Load reads the host's credentials.

func (*MemoryStore) Save

func (m *MemoryStore) Save(c *Credentials) error

Save writes the host's credentials.

type Refresher

type Refresher func(ctx context.Context, refreshToken string) (*Credentials, error)

Refresher exchanges a refresh token for a new credential (the CLI grant's rotating token).

func NewRefresher

func NewRefresher(client *api.Client, now func() time.Time) Refresher

NewRefresher builds a Refresher that calls POST /auth/device/token with the refresh grant.

type Source

type Source struct {
	Host   string
	EnvTok string
	Store  Store
	Renew  Refresher
	Now    func() time.Time
	// contains filtered or unexported fields
}

Source picks the bearer token for a host: WANIIS_TOKEN wins over anything stored, then the stored credential, refreshed shortly before it expires. It implements api.TokenSource.

func (*Source) Refresh

func (s *Source) Refresh(ctx context.Context) (string, bool)

Refresh renews the stored grant after a 401. It reports whether a new token is in place.

func (*Source) Token

func (s *Source) Token(ctx context.Context) (string, error)

Token returns the token to send.

type Store

type Store interface {
	Load(host string) (*Credentials, error) // nil, nil when there is none
	Save(c *Credentials) error
	Delete(host string) error
}

Store keeps credentials by host.

type TokenResponse

type TokenResponse struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	TokenType    string `json:"token_type"`
	ExpiresIn    int    `json:"expires_in"`
	Scope        string `json:"scope"`
}

TokenResponse is the answer to POST /auth/device/token.

Jump to

Keyboard shortcuts

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