pair

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 25 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 pair is how a new device gets its own revocable token: a one-time code shown on a device already logged in, claimed by the new one. Tokens are kept only as hashes; codes only in memory.

Index

Constants

View Source
const ClaimPath = "/api/pair/claim"
View Source
const (
	CodeLife = 10 * time.Minute
)

Variables

View Source
var ErrCode = errors.New("invalid or expired code")

Functions

func CheckURL

func CheckURL(raw string) (string, error)

CheckURL accepts only a plain http(s) server address and returns it without a trailing slash.

func Fingerprint

func Fingerprint(c *x509.Certificate) string

func ParseCerts

func ParseCerts(b []byte) []*x509.Certificate

ParseCerts reads every certificate in PEM data.

func SVG

func SVG(text string) (string, error)

SVG draws a QR code, always dark on white: scanners don't read it inverted.

func Served

func Served(base string) []*x509.Certificate

Served returns the certificates an https server presents, unchecked: callers only compare them.

func Signs

func Signs(anchor *x509.Certificate, chain []*x509.Certificate) bool

Signs says whether anchor is, or signs, the served chain. The host name is checked when connecting.

func TLS

func TLS(pems ...string) *tls.Config

TLS trusts the system's authorities and the given PEM ones; nil when there are none to add.

func Terminal

func Terminal(text string) (string, error)

Terminal draws a QR code in half blocks, two modules to a character, with explicit colours so a dark and a light terminal print the same thing.

func Transport

func Transport(pems ...string) http.RoundTripper

Transport is the default one with TLS(pems...) when that adds anything, otherwise nil, which an http.Client reads as the default.

func Trusted

func Trusted(chain []*x509.Certificate) bool

Trusted says whether the system's own authorities accept the served chain.

Types

type Device

type Device struct {
	ID       string    `json:"id"`
	Name     string    `json:"name"`
	Created  time.Time `json:"created"`
	LastSeen time.Time `json:"lastSeen,omitzero"`
}

type Limiter

type Limiter struct {
	Window       time.Duration
	PerAddr, All int
	// contains filtered or unexported fields
}

Limiter counts attempts per address and overall in fixed windows.

func (*Limiter) Allow

func (l *Limiter) Allow(addr string) bool

type Mismatch

type Mismatch struct{ URL, Want, Got string }

Mismatch is a server whose certificate is not the one the pairing names.

func (Mismatch) Error

func (e Mismatch) Error() string

type Payload

type Payload struct {
	URL, Code, CA string
}

Payload is what a QR code or a pasted line carries: atc://pair?url=&code=&ca=

func Parse

func Parse(text string) (Payload, error)

func (Payload) String

func (p Payload) String() string

type Redeemed

type Redeemed struct {
	Token string `json:"token"`
	ID    string `json:"id"`
	Host  string `json:"host"`
	CA    string `json:"-"`
}

Redeemed is what claiming a pairing brings back. CA is the pinned certificate as PEM, for the caller to keep trusting; empty when the pairing pins none.

func Redeem

func Redeem(p Payload, name string) (*Redeemed, error)

type Store

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

func Open

func Open(path string) (*Store, error)

Open reads the devices file; an empty path keeps everything in memory.

func (*Store) Check

func (s *Store) Check(token string) (string, bool)

Check says which device a token belongs to and records that it was seen.

func (*Store) Claim

func (s *Store) Claim(code, name string) (string, Device, error)

Claim spends a code on a new device and returns its token, which exists nowhere after this.

func (*Store) List

func (s *Store) List() []Device

func (*Store) NewCode

func (s *Store) NewCode() (string, time.Time)

NewCode returns a code that Claim takes once within CodeLife.

func (*Store) Revoke

func (s *Store) Revoke(id string) (bool, error)

Revoke removes a device, so its token stops working, and ends what it has open right now.

func (*Store) Watch

func (s *Store) Watch(id string, stop func()) (done func())

Watch calls stop if the device is revoked, until the returned func is called; at once if it already was.

Jump to

Keyboard shortcuts

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