Skip to main content

Module pairing

Module pairing 

Source
Expand description

Device pairing (spec 3.3 §10 K2, §11 S-I7): short-lived pairing codes, then scoped, expiring, revocable device tokens, stored hashed.

A phone, a tray or a terminal on another machine reaches the kernel’s control plane with a device token instead of the operator’s own credentials:

  1. The person at the kernel issues a code (adk-rust pair, POST /api/kernel/pairing): twelve characters shown as ABCD-EFGH-2345 (60 bits), valid for ten minutes, naming the scopes the device gets. The store keeps the code’s SHA-256 only.
  2. The device claims it (POST /api/kernel/pairing/claim, the one route that needs no caller, since the code is the credential) and receives its token once: adkd_<id>_<secret>. The store keeps the secret’s SHA-256 only. A claim takes the code, so each code pairs one device. Every claim is counted in the store before its code is looked up, so replicas share the count: after twenty claims without a success, every waiting code is cancelled and claims are refused until the next code is issued.
  3. The device sends the token as Authorization: Bearer over HTTP. Each request is admitted only to the routes its scopes open (device_scope_for), as the person who issued the code, with that person’s approval groups, until the token expires or is revoked (adk-rust pair revoke, POST /api/kernel/devices/{id}/revoke). An event stream a device holds open ends when the device is revoked or its pairing ends: at once for a revocation on the same kernel, within five seconds for one on another replica. A decision a device answers is checked for its risk and recorded by the device guard in front of the routes: the kernel’s own HTTP listener runs it, and a server’s app runs it through guard_devices; without the guard, DeviceTokenExtractor refuses the decision.
ScopeRoutes (under /api)
DeviceScope::ReadGET /kernel/health, GET /kernel/processes, GET /kernel/events, POST /kernel/attach/{session}, GET /kernel/agenda, GET /approvals, GET /approvals/{id}; GET /kernel/settings[/{partition}], GET /kernel/away, POST /kernel/away/seen, GET /kernel/away/lines/{seq}, GET /kernel/memory, GET /kernel/record, GET /kernel/record/check (spec 3.3 §10 K3–K5); GET /kernel/agents[/{id}] (spec 3.3 §10 K7)
DeviceScope::DecidePOST /approvals/{id}/decide, low and medium risk; high risk waits for confirming it is the person (spec 3.3 §7.2)
DeviceScope::StopPOST /kernel/freeze

Every other route refuses a device token, unfreeze included: resuming happens only in the web home or on a paired local terminal (S-I10). Pairing never widens the bind: the loopback default and the rule that a non-loopback bind needs a token or SSO stay as they are (K-I18, S-I7).

A server opens its own routes to devices with Pairing::with_device_routes, a DeviceRoutes map consulted after device_scope_for, so the kernel never names a surface’s paths. The guard checks the risk of, and records, the kernel’s own decide route only; a decide route of the server’s map is admitted for the server’s engine, which enforces what each surface may decide (spec 3.3 §7.2) and records the answer.

Every device has a DeviceKind — phone, terminal or tray — set by whoever issued its code, never by the claim: a request carries it in DEVICE_KIND_METADATA. A device paired without one is a remote terminal. Surfaces keep what belongs to a device or a person beside it as DeviceAttachments (a phone’s push subscription, a passkey); revoking a device removes its attachments.

BackendTypeFeature
In-memoryInMemoryDeviceStore—
SQLiteSqliteDeviceStoresqlite (default)
PostgreSQLPostgresDeviceStorepostgres
RedisRedisDeviceStoreredis

§Example

use std::sync::Arc;
use adk_kernel::pairing::{DeviceScope, InMemoryDeviceStore, IssueCode, Pairing};

let pairing = Pairing::new(Arc::new(InMemoryDeviceStore::new()));
let issued = pairing
    .issue(IssueCode::new("ana", ["operator"]).with_scopes([DeviceScope::Read]).with_name("Ana's phone"))
    .await?;
let claimed = pairing.claim(&issued.code, None).await?;
let device = pairing.authenticate(&claimed.token).await?;
assert_eq!(device.owner, "ana");
assert!(pairing.claim(&issued.code, None).await.is_err(), "a code pairs one device");

Structs§

ClaimedDevice
A device just paired (spec 3.3 §10 K2): the only time its token is seen.
DeviceAttachment
One thing kept beside a device or a person (spec 3.3 §5.4, §8.2): a record of kind under key, which the surface that wrote it reads back. The store never reads record.
DeviceRecord
A paired device as the store keeps it (spec 3.3 §10 K2): its token’s hash, never the token.
DeviceTokenExtractor
Admits device tokens as their devices (spec 3.3 §10 K2), each only to the routes its scopes open, and every other request through fallback.
DeviceView
A paired device as GET /kernel/devices lists it (spec 3.3 §10 K2): no token, no hash.
InMemoryDeviceStore
A DeviceStore in process memory, for tests and a kernel with no durable store (spec 3.3 §10 K2). Its devices end with the process, so they must pair again after a restart.
IssueCode
What adk-rust pair asks for (spec 3.3 §10 K2): a code for a device acting for owner.
IssuedCode
A code just issued (spec 3.3 §10 K2): the only time the code itself is seen.
Pairing
Device pairing over a DeviceStore (spec 3.3 §10 K2, S-I7): issue and claim codes, authenticate and authorize device tokens, list and revoke devices.
PairingCode
An issued pairing code as the store keeps it (spec 3.3 §10 K2): its hash, never the code.
PostgresDeviceStore
A DeviceStore in PostgreSQL, shared by every replica (spec 3.3 §10 K2, feature postgres).
RedisDeviceStore
A DeviceStore in Redis, shared by every replica (spec 3.3 §10 K2, feature redis).
SqliteDeviceStore
A DeviceStore in SQLite (spec 3.3 §10 K2, feature sqlite).

Enums§

AttachmentHolder
Who an attachment belongs to.
DeviceKind
What a paired device is (spec 3.3 §7.2, §10 K2): it picks the row of §7.2 a device’s answers take. Whoever issues the pairing code sets it; the claim never does. A device paired without one is a remote terminal.
DeviceScope
What a device may do (spec 3.3 §10 K2): answer decisions, read, or stop everything.
DeviceState
Whether a device’s token works (spec 3.3 §10 K2).

Constants§

DEVICE_ID_METADATA
The request-context metadata key naming the device a request came from.
DEVICE_KIND_METADATA
The request-context metadata key with the device’s kind: phone, terminal or tray (spec 3.3 §7.2, §10 K2).
DEVICE_NAME_METADATA
The request-context metadata key with the device’s name.
DEVICE_SCOPES_METADATA
The request-context metadata key with the device’s scopes, comma-separated.

Traits§

DeviceStore
Where paired devices and waiting pairing codes are kept (spec 3.3 §10 K2, S-I7), on the backends the kernel’s other stores use, with one conformance suite (device_store_conformance_tests!, feature conformance).

Functions§

device_scope_for
The scope a device needs for method on path, or None when the route is closed to devices (spec 3.3 §7.2, §10 K2, S-I10). path may carry the /api prefix and a query.

Type Aliases§

DeviceRoutes
The device scope a server’s own routes need, consulted after the kernel’s device_scope_for: the server names its surface routes, so the kernel never does. None closes a route to devices.