pairing

package
v0.58.3 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 9 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 pairing lets a second device — a tablet, a phone — reach a daemon that is otherwise bound to loopback.

NOTHING USES THIS YET. No listener is opened, no endpoint is served, and no screen offers to pair anything. It was built and tested first because it is the part that decides who gets in, and then the feature it belongs to was put on hold: nobody has asked to reach Caprock from a phone, and the three ways of arranging it differ so much in what they cost the user that choosing one before anyone wants it would be guessing. See FB-019 in .fdck/01-ledger.md.

Kept rather than deleted because the reasoning here — single-use codes, constant-time comparison, immediate revocation, never binding 0.0.0.0 — is the expensive part to get right and does not change whichever route wins.

The rule that shapes everything here is rule 4: all data stays on the machine. Nothing in this package reaches the network, registers a name, or opens a tunnel. What it does is narrower: when the user explicitly turns it on, the daemon also listens on the machine's LAN address, and a device on that same network can connect after proving it knows a code the user read off their own screen.

That is a real reduction in safety and it is stated plainly rather than smoothed over. Bound to loopback, nothing outside the machine can connect at all. Bound to the LAN, everything on that network can *try* — the guest on the home wifi, the stranger in the coworking space. So:

  • It is off until switched on, never a default, never remembered across a daemon that was started without it.
  • A device proves itself once with a short code the user reads aloud or scans, and is issued a long random token it keeps.
  • Codes are single-use and expire in minutes. A device token does not.
  • Every paired device is listed, with when it was last seen, and can be revoked one by one.

Codes are short because a person has to type them on a tablet. Short means guessable, so the code is useless after one use or five minutes, whichever comes first, and repeated wrong guesses burn it. The device token that replaces it is full length.

Index

Constants

View Source
const CodeTTL = 5 * time.Minute

CodeTTL is how long a pairing code stays usable. Long enough to walk to the other device, short enough that a code left on screen is not a standing invitation.

View Source
const MaxAttempts = 5

MaxAttempts is how many wrong guesses a code survives. A six-digit code has a million values, and this makes brute force pointless while leaving room for a person mistyping twice.

Variables

View Source
var (
	// ErrNoCode means no code is outstanding: either none was issued, or the
	// last one was used or expired.
	ErrNoCode = errors.New("no pairing code is active")
	// ErrBadCode means the code did not match. The caller must not say
	// whether the code was wrong or expired — that is one bit an attacker
	// would otherwise get for free.
	ErrBadCode = errors.New("that code is not valid")
	// ErrUnknownDevice means the token does not belong to a paired device,
	// which is also what a revoked device gets.
	ErrUnknownDevice = errors.New("this device is not paired")
)

Errors callers distinguish. Everything else is a bug.

View Source
var ErrNoLAN = errors.New("this machine has no private network address")

ErrNoLAN means the machine has no private network address to bind to — no wifi, no ethernet, or every interface is down.

Functions

func BindAddr

func BindAddr() (string, error)

BindAddr is the address to listen on for LAN access.

Deliberately not `0.0.0.0`. Binding to everything is one firewall rule away from being on the public internet, and on a machine with a public IP it is on the public internet immediately. This picks one private address instead, so the listener is reachable from the user's own network and nowhere else — even if a router is misconfigured, there is no path to a socket that is not listening.

The address is chosen from the interfaces that are up, are not loopback, and carry an RFC 1918 / RFC 4193 address. Where several qualify (wifi plus a docker bridge, say) the first non-virtual one wins, because that is the one the tablet is on.

Types

type Candidate

type Candidate struct {
	Name string
	IP   net.IP
}

Candidate is one interface address BindAddr is choosing between. Exported so the choice can be tested against machines this one is not — a laptop with a public address, a host with nothing but a docker bridge.

type Device

type Device struct {
	// ID identifies the device in the UI and for revocation. Not a secret.
	ID string `json:"id"`
	// Name is what the user sees: taken from the browser, so "iPad" or
	// "Android tablet" rather than anything the device asserts about itself.
	Name string `json:"name"`
	// Token is what the device presents on every request. Never sent to the
	// UI after the moment of pairing.
	Token string `json:"token"`
	// PairedAt and LastSeen are unix milliseconds.
	PairedAt int64 `json:"paired_at"`
	LastSeen int64 `json:"last_seen"`
}

Device is one thing that has been let in.

type Public

type Public struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	PairedAt int64  `json:"paired_at"`
	LastSeen int64  `json:"last_seen"`
}

Public is a Device without its token, for listing in the dashboard.

type Store

type Store struct {
	// Now is injectable so expiry is testable without sleeping.
	Now func() time.Time
	// contains filtered or unexported fields
}

Store holds the pairing state. The zero value is not usable; call New.

func New

func New() *Store

New returns an empty Store with no code and no devices.

func (*Store) Check

func (s *Store) Check(token string) (*Device, error)

Check validates a device token and records that the device was seen.

func (*Store) ClearCode

func (s *Store) ClearCode()

ClearCode withdraws the outstanding code, if any.

func (*Store) Code added in v0.49.0

func (s *Store) Code() string

Code returns the outstanding code, or "" when there is none or it has expired.

For showing the owner what to type into the other device — which is the only way pairing works — and for nothing else. It is never sent to anything but a loopback request, and never used to decide access: Redeem is what checks a code, in constant time, with the attempt counter behind it.

func (*Store) CodeActive

func (s *Store) CodeActive() (bool, time.Duration)

CodeActive reports whether a code is outstanding and still valid, and how long it has left. For showing a countdown, never for deciding access.

func (*Store) Devices

func (s *Store) Devices() []Public

Devices lists what is paired, without tokens, oldest first.

func (*Store) Load

func (s *Store) Load(ds []Device)

Load restores devices from disk at start-up. Codes are deliberately not persisted: an outstanding code must not survive a restart.

func (*Store) NewCode

func (s *Store) NewCode() (string, error)

NewCode issues a fresh six-digit code, replacing any outstanding one.

Replacing rather than refusing: a user who cannot find the code they were shown will press the button again, and the sensible reading of that is "the old one no longer counts", not "you already have one somewhere".

func (*Store) Redeem

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

Redeem exchanges a pairing code for a device token.

The comparison is constant-time. Six digits is small enough that timing a character-by-character compare is not far-fetched, and the cost of doing it properly is nothing.

func (*Store) Revoke

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

Revoke removes one device by id. Its token stops working immediately.

func (*Store) RevokeAll

func (s *Store) RevokeAll() int

RevokeAll unpairs everything. What the user reaches for when they no longer trust the network they are on.

func (*Store) Snapshot

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

Snapshot returns every device, tokens included, for persisting.

Jump to

Keyboard shortcuts

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