login

package
v0.187.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 12 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 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

View Source
const (
	ClientID = "draugr-cli"
	Scope    = "check"
)

The client a server knows `draugr login` as, and the one scope it asks for.

Variables

View Source
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.

View Source
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.

View Source
var ErrNotLoggedIn = errors.New("not logged in")

ErrNotLoggedIn is a store holding no login for the origin asked about.

Functions

func Origin

func Origin(server string) (string, error)

Origin is the key a login is stored under: the server's scheme, host and port.

func UserAgent

func UserAgent(version string) string

UserAgent is the User-Agent every request sends.

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

func NewClient(server string, device Device) (Client, error)

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

func (c Client) Revoke(ctx context.Context, token string) error

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.

func (Client) Wait

func (c Client) Wait(ctx context.Context, a Authorization) (Login, error)

Wait polls until the request is approved, denied or expires, every interval the server set and slower each time it answers slow_down. A pending request keeps polling: approval may first wait on the person signing in to the console.

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.

func (Keychain) Delete

func (Keychain) Delete(origin string) error

Delete removes the keychain's login for origin.

func (Keychain) Get

func (Keychain) Get(origin string) (Login, error)

Get returns the login the keychain holds for origin.

func (Keychain) Set

func (Keychain) Set(origin string, l Login) error

Set keeps l in the keychain under origin.

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.

func (Login) Expired

func (l Login) Expired(now time.Time) bool

Expired reports whether the token has expired at now.

func (Login) GoString

func (l Login) GoString() string

GoString is String, for %#v.

func (Login) String

func (l Login) String() string

String describes the login without its token, so a %v in an error or a test cannot print it.

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.

Jump to

Keyboard shortcuts

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