auth

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package auth finds, stores and forgets the API token (design §4).

A token comes from QUANTIC_TOKEN, the system keyring, or a file, in that order. Each is kept per Quantic host, so a token for a local Phoenix server never replaces the one for quantic.finance.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not in the keyring")

ErrNotFound is what a Keyring returns when it holds nothing for that service and user. Any other error means the keyring itself failed.

View Source
var ErrNotSignedIn = errors.New("not signed in")

ErrNotSignedIn means there is no token for the host anywhere.

Functions

This section is empty.

Types

type Keyring

type Keyring interface {
	Get(service, user string) (string, error)
	Set(service, user, secret string) error
	Delete(service, user string) error
}

Keyring is the part of a system keyring the CLI uses: one secret per service and user. SystemKeyring is the real one; tests use their own.

type Saved

type Saved struct {
	Source     Source
	KeyringErr error
}

Saved says where Save put a token. KeyringErr is why it isn't in the keyring, when it went to the file instead.

type Secret

type Secret struct {
	// contains filtered or unexported fields
}

Secret is a token that can't be printed by accident; only Reveal gives the token, and only the code that sets the Authorization header calls it.

encoding/json, and log/slog's JSON handler, can't see an unexported field, so to them a Secret is {}. fmt can: it reads unexported fields through reflection. What stops it is the pointer below.

func ParseSecret

func ParseSecret(s string) (Secret, error)

ParseSecret checks that s looks like a token, after trimming the newline a file or a paste leaves. It never puts s in its error.

func (Secret) Format

func (Secret) Format(f fmt.State, _ rune)

Format is fmt's hook for every verb, so an error message that includes a Secret says [redacted] rather than an address. fmt can't call it through an unexported field of another struct; there, the pointer shows instead.

func (Secret) IsZero

func (s Secret) IsZero() bool

IsZero reports whether there is no token.

func (Secret) Reveal

func (s Secret) Reveal() string

Reveal returns the token itself, for the Authorization header.

type Source

type Source string

Source says where a token came from, for messages and `auth status`.

const (
	FromEnv     Source = "QUANTIC_TOKEN"
	FromKeyring Source = "keyring"
	FromFile    Source = "file"
)

type Store

type Store struct {
	// Env is QUANTIC_TOKEN's value; set, it wins over anything stored.
	Env string

	Keyring Keyring

	// File is where tokens go when the keyring can't take them (design §4):
	// $XDG_CONFIG_HOME/quantic/tokens.json, readable only by its owner.
	File string
}

Store finds and keeps tokens, one per Quantic host ("quantic.finance", "localhost:4000").

func (*Store) Delete

func (s *Store) Delete(host string) ([]Source, error)

Delete forgets host's token in the keyring and the file, and returns where it was. QUANTIC_TOKEN is the caller's to unset.

func (*Store) Load

func (s *Store) Load(host string) (Token, error)

Load finds the token for host: QUANTIC_TOKEN, then the keyring, then the file. With none, it returns ErrNotSignedIn.

func (*Store) Save

func (s *Store) Save(host string, secret Secret) (Saved, error)

Save keeps secret for host in the keyring or, if the keyring fails, in the file. Either way, no other copy for host is left behind to be found first.

type SystemKeyring

type SystemKeyring struct{}

SystemKeyring is the operating system's keyring: the Secret Service on Linux (GNOME Keyring, KWallet), the Keychain on macOS, the Credential Manager on Windows.

func (SystemKeyring) Delete

func (SystemKeyring) Delete(service, user string) error

func (SystemKeyring) Get

func (SystemKeyring) Get(service, user string) (string, error)

func (SystemKeyring) Set

func (SystemKeyring) Set(service, user, secret string) error

type Token

type Token struct {
	Secret Secret
	Source Source
}

Token is a token and where it came from.

Directories

Path Synopsis
Package authtest has keyrings for tests, so that no test ever reaches the keyring of the machine it runs on.
Package authtest has keyrings for tests, so that no test ever reaches the keyring of the machine it runs on.

Jump to

Keyboard shortcuts

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