auth

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 15 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 auth is the daemon's device registry: it gates every WebSocket connection on a bearer a device obtained by pairing. Bearers are high-entropy random tokens, shown to a device once and stored only as a hash — a leaked devices.json cannot be replayed. Pairing the FIRST device uses a one-time bootstrap code (see otp.go). This is a process-wide security boundary, so it lives in its own package, not folded into serve.

Index

Constants

View Source
const BootstrapMaxTries = 5

BootstrapMaxTries is how many wrong guesses a bootstrap code survives before it is dropped.

The same rule and the same number as joinMaxTries, for the same reason: six digits is a million codes, the window is minutes, and an HTTP handler on a LAN answers thousands of requests a second — so without a counter the code is not a secret, it is a formality. Five is chosen for the human, not the attacker: someone reading a code off their own console does not need a sixth attempt, and a burned code is not a lockout because `nocturn pair` mints another one.

Variables

View Source
var ErrPairing = errors.New("auth: invalid or expired pairing code")

ErrPairing is returned by Pair when the bootstrap code is wrong, expired, or already spent.

Functions

This section is empty.

Types

type Class

type Class string

Class is what a device IS. It is a fact about the device, not a permission: this package stores it and never interprets it, so what a class may DO is decided in one place by whoever assembles connections (see internal/serve). Storing the category rather than the permissions is deliberate — permissions written into devices.json would freeze today's rules into every record, and widening them later would need another migration of the kind stampClasses already had to do once.

It is a separate axis from Platform: that one selects the push provider, and a browser and a speaker in the hallway could both be "web". There is deliberately no ordering — a class is a category, not a level.

The name avoids Kind and Role, both of which already mean something else here: Kind is the gate's permission dimension (and, separately, which chat store a command addresses, and which sort of notification fired), and agentkit.Role is a message author.

const (
	// ClassUnknown is the zero value, and carries the least authority. A record that failed to say
	// what it is must never be assumed trustworthy — the same reasoning as gate.RecallNever and
	// permtree.Unset.
	ClassUnknown Class = ""
	// ClassApp is a paired companion app: a screen, a lock, and a person holding it. Someone at it
	// is identified, so what it reports can be taken as that person's answer.
	ClassApp Class = "app"
	// ClassAppliance is a device with NO authenticated input path — a speaker in a hallway, a
	// doorbell, a car. Whoever is present can operate it, and none of them identified themselves, so
	// nothing it reports is anybody's consent.
	//
	// Named for that property rather than for a product: a voice satellite is one of these, but so
	// is anything else whose input nobody signed.
	ClassAppliance Class = "appliance"
	// ClassTool is a local operator tool — the `nocturn` command talking to its own daemon. It is
	// not a device anyone carries and nobody is identified at it, so it consents to nothing; what it
	// may do it may do because whoever runs it already holds the workspace on disk.
	ClassTool Class = "tool"
	// ClassWeb is a browser holding the page the daemon itself served. Like ClassApp there is a
	// screen and a person at it, and it arrives the same way — a bootstrap code printed on the
	// daemon's own stderr, or a join code relayed by an already-paired device.
	//
	// What it is NOT is reachable out of band: no push provider carries a browser, so it can only
	// answer while its tab is open. It therefore never fills the unattended role the companion app
	// exists for, which is why it is a class of its own rather than another way of spelling ClassApp.
	ClassWeb Class = "web"
	// ClassAgent is a remote AI agent — an MCP client such as Claude — calling the tools a workspace
	// exposes to it. Nobody is identified at it and its input is whatever the model on the far side
	// decided, so like an appliance it consents to nothing: what it asks to do is asked of a person.
	ClassAgent Class = "agent"
)

type Device

type Device struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Class    Class  `json:"class,omitempty"`    // what it is; decides whether it may approve
	Platform string `json:"platform,omitempty"` // ios|android|web, selects the push provider
	// BearerHash is the hex sha256 of the bearer. omitempty so that a Device blanked by List drops the
	// field entirely rather than carrying an empty one: an always-present field is one somebody
	// eventually populates. Every real record has one, so nothing legitimate is ever omitted on disk.
	BearerHash string    `json:"bearerHash,omitempty"`
	PushToken  string    `json:"pushToken,omitempty"` // APNs/FCM token, for out-of-band wake
	Added      time.Time `json:"added"`
	LastUsed   time.Time `json:"lastUsed,omitzero"`
}

Device is one paired device. The bearer itself is never stored — only its hash.

type PendingJoin

type PendingJoin struct {
	JoinID   string `json:"joinId"`
	Name     string `json:"name"`
	Platform string `json:"platform,omitempty"`
	Code     string `json:"code"`
}

PendingJoin is a second device's request awaiting confirmation, shown to an ALREADY-PAIRED device so a human can relay the code. The code is never returned to the joining device.

type PushTarget

type PushTarget struct {
	Token    string
	Platform string
}

PushTarget is a device reachable out of band: its push token and platform.

type Store

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

Store verifies bearers and pairs new devices, persisting device records (hashes only, 0600).

func New

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

New opens (or creates) a device store at path.

The directory is created here rather than left to the first write: the first write is the one that enrols a device, and a failure there leaves the daemon running with a registry it cannot persist — the least useful moment to discover a missing folder.

func (*Store) ArmBootstrap added in v0.3.0

func (s *Store) ArmBootstrap(ttl time.Duration) string

ArmBootstrap mints a fresh pairing code valid for ttl and returns it to show the operator, replacing any code already pending.

It asks nothing about the registry. WHETHER a household still needs a bootstrap code is the question "can anything already here bring a device in by itself?", and that is a question about what a class may DO — which this package deliberately does not know. The caller reads Classes, decides, and calls this. See serve.serveOn, where the decision now lives.

func (*Store) BootstrapPending added in v0.3.0

func (s *Store) BootstrapPending() bool

BootstrapPending reports whether a pairing code is armed and still within its window.

It is the fact behind "which screen should a fresh client show" — enter a bootstrap code, or ask an existing device to relay a join code. Reporting that a code exists reveals nothing a caller could not learn by attempting to redeem one, and the code itself never leaves this package.

func (*Store) CapJoins added in v0.3.0

func (s *Store) CapJoins(n int)

CapJoins prunes expired joins and then keeps at most n of the rest, dropping the oldest first.

/join cannot be authenticated — the caller has nothing to authenticate with yet — so without a ceiling anyone who can reach the daemon can mint pending joins without limit and bury the household's pairing screen. Oldest-first is what keeps the flow usable for the person actually standing there: the request they just made is the one that survives the eviction.

func (*Store) Classes added in v0.3.0

func (s *Store) Classes() []Class

Classes returns the class of every enrolled device, in registry order.

It exists so a caller can decide something about the household — "is anything here able to bring a further device in?" — without this package having to learn what a class means. The answer to that question is a capability, and capabilities live in exactly one place (see serve.capabilitiesOf).

func (*Store) ConfirmJoin

func (s *Store) ConfirmJoin(joinID, code string, class Class) (string, error)

ConfirmJoin redeems a joinId and its relayed code for a new device's bearer, enrolling it as class. A wrong code is counted; past joinMaxTries the join is dropped so the 6-digit code cannot be brute-forced. An unknown, expired, or exhausted join fails with ErrPairing.

As in Pair, the class is supplied rather than decided: this package writes down what a class is, and never what it means.

func (*Store) DropPushToken added in v0.5.0

func (s *Store) DropPushToken(token string) bool

DropPushToken clears token from whichever device holds it, reporting whether one did. A push provider calls it for a token it reports as permanently invalid (the app was removed, the token rotated), so the device is not pushed to again until it registers a fresh one. The device itself stays paired.

func (*Store) Forget added in v0.3.0

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

Forget removes the device with this id and reports whether there was one.

Without it a lost phone is permanent: its bearer stays valid for as long as the registry exists, and the only remedy is editing devices.json by hand and restarting — which is not a remedy anyone finds at the moment they need it. Revocation takes effect on the next connection; a socket already open is not torn down, because the device is unreachable in the case this exists for and the next reconnect is the same moment either way.

func (*Store) Join

func (s *Store) Join(name, platform string) string

Join records a second device's request and returns its joinId — never the code. The code is minted server-side and revealed only to an already-paired device via PendingJoins; a human relays it.

func (*Store) JoinPlatform added in v0.3.0

func (s *Store) JoinPlatform(joinID string) string

JoinPlatform returns the platform recorded when joinID asked to join, or "" if there is no such open join. It reports what was stored and interprets none of it — the caller turns a platform into a class, because only the caller knows what a class means.

func (*Store) List added in v0.3.0

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

List returns every enrolled device, in registry order, with the two fields nothing outside this package should read blanked: BearerHash and PushToken.

Blanked here rather than at each call site: the hash is a secret's shadow and the push token addresses a specific handset, and a caller that has to remember to strip them will one day forget. Neither is needed to show a device list, and Lookup does the only comparison the hash exists for.

Note the loop ranges over VALUES. Blanking a copy is the whole point — taking the address of the element instead would wipe the live registry, hand out bearers nobody can authenticate with, and persist that on the next save.

func (*Store) Lookup

func (s *Store) Lookup(bearer string) (Device, bool)

Lookup returns the device owning bearer, comparing hashes in constant time.

It replaces a plain yes/no check because the answer alone is no longer enough: what a connection is allowed to do depends on the device's Class, and a caller that has to ask a second question would have to search again.

func (*Store) Mint

func (s *Store) Mint(name string, class Class) (string, error)

Mint enrols a device of the given class outright, without a code exchange, and returns its bearer (shown once, never stored).

It exists for devices that cannot take part in one. A satellite has no screen to show a code and no keyboard to enter one, so the join flow — where a device asks and an already-paired device confirms — has nothing to work with. Enrolling itself is not the alternative: a device that can do that is the hole. Instead an already-authenticated caller asks for this on its behalf, so the human authorises on something that can authorise, and the satellite only ever receives.

The CALLER is responsible for being trusted. This function checks nothing.

func (*Store) Pair

func (s *Store) Pair(code, name, platform string, class Class) (string, error)

Pair redeems the bootstrap code, registering a device named name (on platform, optional) as class and returning its bearer (shown once, never stored). The code is single-use.

The class is a parameter and not a decision made here: which class a holder is enrolled as depends on what a class MEANS, and this package deliberately does not know. The caller computes it (see serve.classFor) and this writes down the answer.

func (*Store) PendingJoins

func (s *Store) PendingJoins() []PendingJoin

PendingJoins lists the open join requests with their codes, for an already-paired device to relay to a human. Expired entries are pruned in passing.

func (*Store) PushTargets

func (s *Store) PushTargets() []PushTarget

PushTargets returns every device reachable out of band (has a push token).

func (*Store) RegisterPush

func (s *Store) RegisterPush(bearer, token, platform string) error

RegisterPush records (or, with an empty token, clears) the push token of the device owning bearer, so it can be woken out of band. A no-op if the bearer is unknown.

func (*Store) UpdateLastUsed

func (s *Store) UpdateLastUsed(bearer string)

UpdateLastUsed sets a device's LastUsed to now on connect. A no-op if the bearer is unknown (already rejected by Verify).

Jump to

Keyboard shortcuts

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