{"modulePath":"github.com/Agent-Field/codeaf","version":"v0.8.4","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package pair\n\nPackage pair is how two machines come to trust each other without ssh, and the encrypted tunnel they talk through afterwards.\n\nTHE WHOLE STORY IN SIX LINES:\n\n\tbig-machine$ codeaf serve\n\t  this machine is reachable as  otter-lamp-42\n\t  pair a new device with code   715 302   (valid 10 minutes)\n\n\tlaptop$ codeaf chat --at otter-lamp-42\n\t  pairing with otter-lamp-42 — enter the code shown there: ______\n\t  paired. this laptop is now a key to otter-lamp-42.\n\nAfter that, \\`codeaf chat --at otter-lamp-42\\` from that laptop just opens.\n\n── the three facts this package is built on ────────────────────────────────\n\nFIRST: THE RELAY BROKERS THE INTRODUCTION AND IS NEVER TRUSTED WITH IT. The pairing runs a PAKE — a password-authenticated key exchange — over the six digits shown on the engine machine's own screen. Somebody in the middle who does not know those digits cannot learn the shared key, cannot guess at it offline, and gets exactly ONE guess per attempt, which the engine machine counts and cuts off. That is the property a plain shared secret over a brokered channel would not have, and it is why the code can be six digits instead of a paragraph.\n\nSECOND: THE PAIRING PINS KEYS, AND EVERY LATER CONNECTION IS BETWEEN PINNED KEYS. The PAKE is used once, to carry two long-term public keys past the relay safely. From then on the surface dials with a Noise handshake against the key it pinned, and the machine answers only devices whose key is in its own book. The code is never used again and cannot be replayed.\n\nTHIRD: A PAIRED DEVICE IS A HAND ON THAT MACHINE'S TOOLS. It is not a read-only window and it is not a login to a website. It opens conversations on that machine, runs the tools that machine's gate allows, and spends that machine's key — the same weight as an ssh key, and the pairing screen says so in those words before anybody types a code.\n\n── what is NOT here, said plainly ──────────────────────────────────────────\n\nTHE DEVICE KEY IS A FILE, NOT A KEYCHAIN ENTRY, AND THERE IS NO TOUCH ID. The design for this lane puts the device key in the OS keychain where there is one, so that the platform can demand a fingerprint before releasing it and codeaf never sees a biometric. That is the right design and it is not built. What is built is [Keeper](#Keeper) — the seam it goes behind — with one implementation: a file with owner-only permissions under the codeaf home directory. Every person-facing sentence in this package says \"a file on this machine\" because that is what it is, and the manual page says the same. A CAPABILITY THAT CANNOT WORK IS ABSENT, NOT BROKEN.\n\n## Constants\n\n```go\nconst CodeAttempts = 5\n```\nCodeAttempts is how many wrong codes a single code will absorb before it is thrown away.\n\nIT IS THE OTHER HALF OF WHAT MAKES SIX DIGITS ENOUGH. A PAKE gives an attacker one guess per exchange and no way to test a guess offline; a small cap on exchanges is what turns \"one guess at a time\" into \"five guesses, ever\". A million codes and five guesses is the whole of the arithmetic.\n\n```go\nconst CodeValidFor = 10 * time.Minute\n```\nCodeValidFor is how long a pairing code shown by \\`codeaf serve\\` is good for. It is quoted in the person-facing line, and there is exactly one of it.\n\n```go\nconst HandshakeWithin = 30 * time.Second\n```\nHandshakeWithin bounds every exchange in this package. A connection that has arrived but not finished proving who it is holds a slot, and a slot held for ever is the cheapest denial of service there is.\n\n```go\nconst PairingWeight = \"a paired device is a key to that machine: it opens conversations there, runs whatever that machine allows to run, and spends that machine's model key. it is the same weight as an ssh key.\"\n```\nPairingWeight is what a person reads BEFORE they type a code. It is the whole security posture of this feature in one sentence, in a person's words.\n\nA PAIRED DEVICE IS A HAND ON THAT MACHINE'S TOOLS. Saying so at the moment of the decision is not a disclaimer; it is the only moment at which the sentence can do any good.\n\n```go\nconst RelayEnv = \"CODEAF_RELAY\"\n```\nRelayEnv names the override, exported so that a sentence and the code that reads it spell it the same way.\n\n## Variables\n\n```go\nvar (\n\t// ErrNoRelay is this machine having no relay address at all.\n\tErrNoRelay = errors.New(\"pair: no relay is configured\")\n\t// ErrNotPaired is this device never having been let in to that machine.\n\tErrNotPaired = errors.New(\"pair: this device is not paired with that machine\")\n\t// ErrWrongCode is the pairing code not matching.\n\tErrWrongCode = errors.New(\"pair: that pairing code is not the one shown there\")\n\t// ErrNotThatMachine is a completed dial to something that does not hold\n\t// the key this device pinned.\n\tErrNotThatMachine = errors.New(\"pair: that is not the machine this device paired with\")\n\t// ErrNoAnswer is a handshake that got no reply at all.\n\tErrNoAnswer = errors.New(\"pair: that machine did not answer the handshake\")\n)\n```\nThe facts, so a caller can tell them apart before phrasing them.\n\n## Functions\n\n```go\nfunc Busy() error\n```\nBusy is the relay turning connections away.\n\n```go\nfunc DeviceKeyPath() string\n```\nDeviceKeyPath is the file the device key is in.\n\n```go\nfunc DevicesList(name string, paired []Paired, keeper Keeper, now time.Time) string\n```\nDevicesList is what \\`codeaf devices\\` prints on the machine that owns the work.\n\n```go\nfunc Dir() string\n```\nDir is where everything this package writes lives, moved wholesale by CODEAF\\_HOME like the rest of codeaf's state.\n\n```go\nfunc Lines(name string, code *Code) string\n```\nLines is what \\`codeaf serve\\` prints, exactly.\n\nThe wording and the spacing are the design's own, and the validity is interpolated from [CodeValidFor](#CodeValidFor) rather than typed, because a number that appears in two places drifts.\n\n```go\nfunc MachinesList(known []Known, now time.Time) string\n```\nMachinesList is what \\`codeaf devices\\` prints about the machines THIS device can reach. It is the other half of the same command, because a person asking \"what am I paired with\" means both directions and should not have to know there are two books.\n\n```go\nfunc NoAnswer(name string) error\n```\nNoAnswer is the other shape the same protection takes.\n\nA MACHINE THAT IS NOT THE ONE THIS DEVICE PAIRED WITH CANNOT EVEN READ THE FIRST MESSAGE, so it does not answer at all — and neither does a connection that dropped. The two are one event from this side and this sentence covers both truthfully, because the fact that matters is the same either way: nothing of the conversation left this device.\n\n```go\nfunc NoRelay(name string) error\n```\nNoRelay is the first sentence: nothing is configured.\n\n```go\nfunc NotConnected(name string) error\n```\nNotConnected is the third: the relay is fine and that machine is not there.\n\n```go\nfunc NotPaired(name string) error\n```\nNotPaired is the fourth: this device has never been let in.\n\n```go\nfunc NotThatMachine(name string) error\n```\nNotThatMachine is the pinned key not matching. It is the sentence that says the safety property held: the connection stopped rather than opening.\n\n```go\nfunc PairedLine(name string) string\n```\nPairedLine is what a person reads when it worked.\n\n```go\nfunc PairingPreamble(name string) string\n```\nPairingPreamble names what is about to happen.\n\n```go\nfunc PairingPrompt(name string) string\n```\nPairingPrompt is the line the code is typed after.\n\n```go\nfunc ReadCode(typed string) (string, error)\n```\nReadCode takes what a person typed and answers the six digits, or says what is wrong with it.\n\nSPACES AND DASHES ARE THROWN AWAY, because a code read aloud gets written down with whichever separator the listener prefers and neither of them is wrong.\n\n```go\nfunc Relay() string\n```\nRelay is the address this machine uses when nothing else says otherwise. Empty means none is set up.\n\n```go\nfunc RelayFile() string\n```\nRelayFile is where a relay address is kept when somebody wants it to outlive a shell.\n\n```go\nfunc RelayFor(known Known, found bool) string\n```\nRelayFor is the address to reach one machine through: the override, then the relay that machine was paired through, then the configured one.\n\n```go\nfunc RevokedLine(label string) string\n```\nRevokedLine is what a person reads when a device has been stopped.\n\n```go\nfunc Stopped() string\n```\nStopped is what a machine says to a device it no longer lets in. It travels inside the encryption, so it is this machine's own words and not the relay's.\n\n```go\nfunc ThisMachineLabel() string\n```\nThisMachineLabel is what this device calls itself when it pairs. It is the host name, because that is the word a person already uses for their laptop, and it is only ever a label — a connection is checked against a key.\n\n```go\nfunc Unreachable(service string) error\n```\nUnreachable is the second: something is configured and it is not answering.\n\n```go\nfunc WrongCode(name string) error\n```\nWrongCode is the pairing code being wrong, said with the fact that makes it worth trying again.\n\n## Types\n\n```go\ntype Book struct {\n\t// contains filtered or unexported fields\n}\n```\nBook is one of the two files, held against the disk.\n\n```go\nfunc BookAt(path string) *Book\n```\nBookAt is either book, put somewhere else — which is what tests want and nothing in the product does.\n\n```go\nfunc DeviceBook() *Book\n```\nDeviceBook is what this machine remembers about the devices it lets in.\n\n```go\nfunc MachineBook() *Book\n```\nMachineBook is what this device remembers about the machines it can reach.\n\n```go\nfunc (b *Book) Admit(one Paired) error\n```\nAdmit writes a device into this machine's book. A device that pairs twice keeps its first Since, because that is when this machine first let it in.\n\n```go\nfunc (b *Book) Allows(publicKey []byte) (Paired, bool, error)\n```\nAllows says whether a key may open a conversation on this machine, and is the one question the connection path asks of this book.\n\n```go\nfunc (b *Book) Devices() ([]Paired, error)\n```\nDevices is every device this machine lets in, oldest first.\n\n```go\nfunc (b *Book) Machine(name string) (Known, bool, error)\n```\nMachine finds one by name.\n\n```go\nfunc (b *Book) Machines() ([]Known, error)\n```\nMachines is every machine this device is paired with, oldest first.\n\n```go\nfunc (b *Book) Remember(one Known) error\n```\nRemember writes a machine into this device's book, replacing an older pairing with the same name.\n\nA RE-PAIRING REPLACES THE KEY, and that is not a hole: getting here means the person read a fresh code off that machine's own screen and the PAKE agreed. Refusing to replace would mean a machine that was rebuilt could never be reached again from a device that remembered its old key.\n\n```go\nfunc (b *Book) Revoke(label string) (Paired, error)\n```\nRevoke takes a device out of this machine's book. It matches on the label a person can see, and refuses an ambiguous one rather than guessing which of two laptops called \\`laptop\\` was meant.\n\n```go\nfunc (b *Book) RevokeAll(label string) (int, error)\n```\nRevokeAll takes every device with a label out, which is the honest answer to two laptops with the same host name.\n\n```go\nfunc (b *Book) Touch(publicKey []byte, when time.Time)\n```\nTouch records that a device connected. It is best-effort: a connection is not refused because a note about it could not be written.\n\n```go\ntype Code struct {\n\t// contains filtered or unexported fields\n}\n```\nCode is one live pairing code.\n\n```go\nfunc NewCode(now time.Time) (*Code, error)\n```\nNewCode mints one. The digits come from crypto/rand and not from the clock, a counter, or anything else a watcher could follow.\n\n```go\nfunc (c *Code) Shown() string\n```\nShown is the code as a person reads it off the screen.\n\n```go\ntype Desk struct {\n\t// Now is the clock, swapped by tests.\n\tNow func() time.Time\n\t// contains filtered or unexported fields\n}\n```\nDesk is the engine machine's live pairing code: minting it, showing it, spending its attempts, and throwing it away.\n\nTHERE IS ONE CODE AT A TIME. Two live codes would mean two independent guess budgets against the same machine, and a person only ever reads one number off the screen anyway.\n\n```go\nfunc (d *Desk) Offer() (*Code, error)\n```\nOffer is the code to show right now, minting a fresh one when the last has expired or been spent.\n\n```go\nfunc (d *Desk) Retire()\n```\nRetire throws the current code away — what a successful pairing does, so that one code pairs one device.\n\n```go\nfunc (d *Desk) Spend() (string, error)\n```\nSpend takes one attempt off the live code and answers what the exchange should be run against. A code with nothing left is gone, and the sentence says so rather than letting somebody keep guessing.\n\n```go\ntype Device struct {\n\t// contains filtered or unexported fields\n}\n```\nDevice is this machine's identity on the relay.\n\n```go\nfunc ThisDevice(keeper Keeper) (Device, error)\n```\nThisDevice is this machine's key, made on first use.\n\nMAKING ONE IS NOT AN EVENT A PERSON IS TOLD ABOUT, because it is not a decision they made: the first time anything needs this machine's name, the key that name comes from has to exist. What they are told about is the name, which is the part that means something.\n\n```go\nfunc (d Device) Name() string\n```\nName is what this machine would be reachable as, which is a fact about the key and not about whether anything is running.\n\n```go\nfunc (d Device) Private() *ecdh.PrivateKey\n```\nPrivate is the key the relay's registration challenge is answered with.\n\n```go\nfunc (d Device) Public() []byte\n```\nPublic is the 32 bytes the other end pins.\n\n```go\ntype FileKeeper struct{ Path string }\n```\nFileKeeper keeps the key in a file only its owner can read.\n\n```go\nfunc (f FileKeeper) Forget() error\n```\n\n```go\nfunc (f FileKeeper) Load() ([]byte, bool, error)\n```\n\n```go\nfunc (f FileKeeper) Save(seed []byte) error\n```\n\n```go\nfunc (f FileKeeper) Where() string\n```\nWhere is the sentence a person reads. It says a file because it is a file.\n\n```go\ntype Host struct {\n\t// Service is the relay's address.\n\tService string\n\t// Device is this machine's key.\n\tDevice Device\n\t// Devices is the book of devices this machine lets in.\n\tDevices *Book\n\t// Desk holds the live pairing code.\n\tDesk *Desk\n\t// Say is where a person-facing line goes — the name, the code, and one\n\t// line per device that arrives or is turned away. Nil says nothing.\n\tSay func(string)\n\t// Open is handed a plaintext pipe for each connection that was let in, and\n\t// owns it from then on: it runs the conversation and closes the pipe. THE\n\t// TUNNEL IS AN io.ReadWriteCloser AND NOTHING MORE, which is what lets the\n\t// caller hand it straight to internal/remote.\n\tOpen func(io.ReadWriteCloser)\n\t// Now is the clock, swapped by tests.\n\tNow func() time.Time\n}\n```\nHost is the machine side.\n\n```go\nfunc (h *Host) Run(ctx context.Context) error\n```\nRun holds the registration until the context is cancelled, redialling when the connection to the relay is lost.\n\nA LOST RELAY IS NOT A LOST MACHINE. Wifi drops, a relay is restarted, a NAT forgets — none of those are reasons for the conversations on this machine to end, so this loop backs off and comes back rather than returning. It returns only when the context ends, or when the relay refuses in a way that trying again cannot fix.\n\n```go\ntype Keeper interface {\n\t// Where is the person-facing answer to \"where is that key kept\", in a\n\t// person's words rather than a path.\n\tWhere() string\n\t// Load answers the key, or false when this machine has never made one.\n\tLoad() ([]byte, bool, error)\n\t// Save writes a newly made key.\n\tSave(seed []byte) error\n\t// Forget removes it. The machine loses its name and every pairing that\n\t// named it, which is why nothing calls this except a person who asked.\n\tForget() error\n}\n```\nKeeper is where a device key lives. It is an interface because THIS IS THE SEAM THE OS KEYCHAIN GOES BEHIND — on a Mac the key should be a keychain item whose release the platform can gate on Touch ID, so that a fingerprint unlocks a connection and codeaf never sees a biometric.\n\nTHAT IS NOT BUILT. The only implementation in this build is [FileKeeper](#FileKeeper), and [OpenKeeper](#OpenKeeper) returns it on every platform. Nothing in this package pretends otherwise: [Keeper.Where](#Keeper.Where) is the sentence a person is shown, and today it always says the key is a file.\n\n```go\nfunc OpenKeeper() Keeper\n```\nOpenKeeper is where this build keeps a device key.\n\nIT IS THE FILE KEEPER ON EVERY PLATFORM, INCLUDING MACS. See [Keeper](#Keeper).\n\n```go\ntype Known struct {\n\t// Name is what a person types after --at.\n\tName string `json:\"name\"`\n\t// Key is the machine's long-term public key, base64 raw-url. THIS IS THE\n\t// PINNED THING: a connection is to this key, and the name is only how the\n\t// relay finds it.\n\tKey string `json:\"key\"`\n\t// Service is the relay this machine was paired through, so a device that\n\t// has used two relays reaches each machine through the right one.\n\tService string `json:\"service\"`\n\t// Since is when the pairing happened.\n\tSince time.Time `json:\"since\"`\n}\n```\nKnown is one machine this device has paired with, as the surface remembers it.\n\n```go\nfunc Machines() ([]Known, error)\n```\nMachines is every machine this device can reach, for \\`codeaf devices\\` on a surface and for a door that wants to say which names it knows.\n\n```go\ntype Paired struct {\n\t// Label is what the device called itself when it paired — its host name.\n\t// It is a convenience for the person reading `codeaf devices` and is\n\t// NEVER what a connection is checked against.\n\tLabel string `json:\"label\"`\n\t// Key is the device's long-term public key, base64 raw-url. This is what a\n\t// connection is checked against.\n\tKey string `json:\"key\"`\n\t// Since is when it paired, and Seen is when it last opened a connection.\n\tSince time.Time `json:\"since\"`\n\tSeen  time.Time `json:\"seen,omitempty\"`\n}\n```\nPaired is one device this machine has let in, as the engine remembers it.\n\n```go\ntype Reach struct {\n\t// Name is what the person typed after --at.\n\tName string\n\t// Device is this device's key.\n\tDevice Device\n\t// Machines is this device's book of machines it has paired with.\n\tMachines *Book\n\t// Label is what this device calls itself; see [ThisMachineLabel].\n\tLabel string\n\t// AskCode is asked for the pairing code when this device is not yet paired\n\t// with the machine. NIL MEANS THIS DOOR CANNOT ASK — a headless run, a\n\t// script — and an unpaired machine is then refused with [NotPaired]\n\t// rather than hanging on a prompt nobody is there to answer.\n\tAskCode func(name string) (string, error)\n\t// Say is where the pairing's own lines go. Nil says nothing.\n\tSay func(string)\n\t// Now is the clock, swapped by tests.\n\tNow func() time.Time\n}\n```\nReach is one attempt to open a connection to a named machine.\n\n```go\nfunc (r Reach) Open(ctx context.Context) (*Tunnel, error)\n```\nOpen answers a live tunnel to the machine, pairing first if it has to.\n\nEVERY ROAD OUT OF HERE IS ONE SENTENCE NAMING WHAT IS WRONG. There are four ways this fails that a person cannot tell apart by looking, and errors.go keeps one sentence for each.\n\n```go\ntype Tunnel struct {\n\t// contains filtered or unexported fields\n}\n```\nTunnel is one encrypted connection.\n\n```go\nfunc (t *Tunnel) Close() error\n```\n\n```go\nfunc (t *Tunnel) Peer() []byte\n```\nPeer is the key on the other end, proved by the handshake.\n\n```go\nfunc (t *Tunnel) Read(b []byte) (int, error)\n```\nRead hands back plaintext. A record arrives whole and is drained across as many Reads as the caller needs, which is what makes this an ordinary stream rather than a message queue.\n\n```go\nfunc (t *Tunnel) Write(b []byte) (int, error)\n```\nWrite encrypts and sends. A write longer than one record is cut up, because the caller above knows nothing about records and must not have to.\n\n","path":"github.com/Agent-Field/codeaf/internal/pair","name":"pair","synopsis":"Package pair is how two machines come to trust each other without ssh, and the encrypted tunnel they talk through afterwards.","isRedistributable":true}
