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 ¶
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).
const ( PrefixPersonal = "waniis_pat_" PrefixGrant = "waniis_cli_" )
Prefixes of the two token kinds.
const ( GrantDeviceCode = "urn:ietf:params:oauth:grant-type:device_code" GrantRefreshToken = "refresh_token" )
Grant types (RFC 8628 and RFC 6749).
const ClientID = "waniis-cli"
ClientID identifies the CLI to the device flow.
const KeychainService = "waniis"
KeychainService is the service name entries are stored under in the OS keychain.
Variables ¶
This section is empty.
Functions ¶
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 ¶
NewFileStore builds a FileStore at dir/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).
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.
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.