pairing

package
v0.107.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 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.

It is the store behind LAN access (ADR-029): `caprock up --lan` or the Settings screen's Show a code opens a second listener, internal/api's gate checks every request from the network against the tokens kept here, and the status screen issues codes and lists devices. It was built and tested before that feature, because it is the part that decides who gets in.

A token makes a device a reader, not the owner — unless the owner, at the machine, gives that one device the controller role (ADR-034). What each role may do is decided in internal/api (pairedDeviceRoutes, controllerRoutes), not here: this package answers "which device is this, and which role did the owner give it", never "what may it do".

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 (
	// RoleViewer reads figures and nothing else (ADR-029).
	RoleViewer = "viewer"
	// RoleController may also start sessions in known projects, type into the
	// sessions Caprock started, stop them, and answer task approvals.
	RoleController = "controller"
)

The roles a paired device can hold (ADR-034).

A device is paired as a viewer and stays one until the owner, on the machine Caprock runs on, makes it a controller. An empty Role is a viewer: every devices.json written before roles existed reads as "viewer", which is what those devices were.

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.

func ValidRole added in v0.71.0

func ValidRole(r string) bool

ValidRole reports whether r names a role a device may be given.

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"`
	// Role is RoleViewer or RoleController; empty reads as a viewer. Set only
	// by the owner on loopback (SetRole), never by the device: pairing always
	// makes a viewer.
	Role string `json:"role,omitempty"`
}

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"`
	// Role is always set here: "viewer" or "controller".
	Role string `json:"role"`
}

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.

It returns a copy: the role can change under a request in flight (the owner revokes control while the phone is typing), and a pointer into the store would be read without the lock. A caller that needs the role *now* — a terminal socket that stays open for an hour — asks again.

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) RoleOf added in v0.71.0

func (s *Store) RoleOf(token string) string

RoleOf is the current role of the device holding token, or "" when the token is unknown or revoked. It does not touch LastSeen: it is asked once per keystroke on an open terminal, which is not the device being seen again.

func (*Store) SetRole added in v0.71.0

func (s *Store) SetRole(id, role string) bool

SetRole gives one device, by id, a role. It reports false when there is no such device or the role is not one ValidRole accepts. Takes effect on that device's next request, and on its next keystroke into an open terminal.

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