Documentation
¶
Overview ¶
Package login holds a person's short-lived, check-only token for a Draugr server: the OAuth 2.0 device authorization grant (RFC 8628) that issues it, the revocation (RFC 7009) that ends it, and the OS keychain that keeps it between runs.
The token is kept in the keychain and nowhere else. A file holding it would be readable by anything running as the person, would be copied by backups and dotfile repositories, and would outlive the sign-in that produced it, which are the properties a token in a shell profile already has and this exists to replace.
Index ¶
Constants ¶
const ( ClientID = "draugr-cli" Scope = "check" )
The client a server knows `draugr login` as, and the one scope it asks for.
Variables ¶
var ( // ErrExpired is a code that expired before anybody approved it. ErrExpired = errors.New("the code expired before it was approved") // ErrDenied is a request somebody denied. ErrDenied = errors.New("the request was denied") // ErrInvalidClient is a server that does not know the client `draugr login` presents. ErrInvalidClient = errors.New("the server does not accept " + ClientID + " as a client (invalid_client)") )
Errors a poll ends with, besides an *OAuthError carrying the server's own description.
var ErrNoKeychain = errors.New("the OS keychain is not available")
ErrNoKeychain is a machine with no OS keychain Draugr can reach. A *KeychainError matches it.
var ErrNotLoggedIn = errors.New("not logged in")
ErrNotLoggedIn is a store holding no login for the origin asked about.
Functions ¶
Types ¶
type Authorization ¶
type Authorization 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"`
}
Authorization is the server's answer to a device authorization request (RFC 8628 §3.2).
func (Authorization) Lifetime ¶
func (a Authorization) Lifetime() time.Duration
Lifetime is how long the code stays valid.
type Client ¶
type Client struct {
// Server is the server's address, without a trailing slash.
Server string
// Device describes this machine.
Device Device
// HTTP sends the requests. Nil is a client with a timeout that follows no redirect.
HTTP *http.Client
// Sleep waits between polls, returning early with the context's error. Nil waits on a timer.
Sleep func(ctx context.Context, d time.Duration) error
// Now is the clock a login's expiry is computed from. Nil is time.Now.
Now func() time.Time
}
Client speaks the device authorization grant and revocation to one server.
func NewClient ¶
NewClient is a client for server, which must be https, or http to this machine: the token comes back over this connection.
func (Client) Authorize ¶
func (c Client) Authorize(ctx context.Context) (Authorization, error)
Authorize asks the server for a device code and the code a person enters to approve it.
func (Client) Revoke ¶
Revoke tells the server to stop accepting token (RFC 7009). The server answers success for a token that is unknown, revoked or expired, so this succeeds whether or not it was still live.
type Device ¶
type Device struct {
// Name is the raw hostname.
Name string
// Version is the Draugr version.
Version string
}
Device describes the machine asking for a login. The server displays it on the approval page and decides nothing by it.
type Keychain ¶
type Keychain struct{}
Keychain is the Store the OS keychain backs.
type KeychainError ¶
type KeychainError struct {
Err error
}
KeychainError is a keychain that could not be read or written.
func (*KeychainError) Error ¶
func (e *KeychainError) Error() string
func (*KeychainError) Is ¶
func (e *KeychainError) Is(target error) bool
Is makes the error match ErrNoKeychain.
func (*KeychainError) Unwrap ¶
func (e *KeychainError) Unwrap() error
Unwrap returns the keychain's own error.
type Login ¶
type Login struct {
// Server is the address `draugr login` was given, without a trailing slash.
Server string `json:"server"`
// Token is the bearer token. Never logged and never printed.
Token string `json:"token"`
// Email and Organization say whose login it is, as the server answered.
Email string `json:"email"`
Organization string `json:"organization"`
// Issued is when the token arrived, and Expires when the server stops accepting it.
Issued time.Time `json:"issued"`
Expires time.Time `json:"expires"`
}
Login is a token a server issued to a person, and whose it is.
type OAuthError ¶
type OAuthError struct {
Code string `json:"error"`
Description string `json:"error_description"`
// Status is the HTTP status line, for an answer that was not in OAuth's shape.
Status string `json:"-"`
}
OAuthError is an error answer in OAuth's shape.
func (*OAuthError) Error ¶
func (e *OAuthError) Error() string
type Store ¶
type Store interface {
// Get returns the login for an origin, or an error matching ErrNotLoggedIn.
Get(origin string) (Login, error)
// Set keeps a login for an origin, replacing any it held.
Set(origin string, l Login) error
// Delete removes the login for an origin, or returns an error matching ErrNotLoggedIn.
Delete(origin string) error
}
Store keeps logins, one for each server origin.