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:
- The person at the kernel issues a code (
adk-rust pair,POST /api/kernel/pairing): twelve characters shown asABCD-EFGH-2345(60 bits), valid for ten minutes, naming the scopes the device gets. The store keeps the code’s SHA-256 only. - 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. - The device sends the token as
Authorization: Bearerover 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 throughguard_devices; without the guard,DeviceTokenExtractorrefuses the decision.
| Scope | Routes (under /api) |
|---|---|
DeviceScope::Read | GET /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::Decide | POST /approvals/{id}/decide, low and medium risk; high risk waits for confirming it is the person (spec 3.3 §7.2) |
DeviceScope::Stop | POST /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.
| Backend | Type | Feature |
|---|---|---|
| In-memory | InMemoryDeviceStore | — |
| SQLite | SqliteDeviceStore | sqlite (default) |
| PostgreSQL | PostgresDeviceStore | postgres |
| Redis | RedisDeviceStore | redis |
§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§
- Claimed
Device - A device just paired (spec 3.3 §10 K2): the only time its token is seen.
- Device
Attachment - One thing kept beside a device or a person (spec 3.3 §5.4, §8.2): a record of
kindunderkey, which the surface that wrote it reads back. The store never readsrecord. - Device
Record - A paired device as the store keeps it (spec 3.3 §10 K2): its token’s hash, never the token.
- Device
Token Extractor - 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. - Device
View - A paired device as
GET /kernel/deviceslists it (spec 3.3 §10 K2): no token, no hash. - InMemory
Device Store - A
DeviceStorein 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. - Issue
Code - What
adk-rust pairasks for (spec 3.3 §10 K2): a code for a device acting forowner. - Issued
Code - 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. - Pairing
Code - An issued pairing code as the store keeps it (spec 3.3 §10 K2): its hash, never the code.
- Postgres
Device Store - A
DeviceStorein PostgreSQL, shared by every replica (spec 3.3 §10 K2, featurepostgres). - Redis
Device Store - A
DeviceStorein Redis, shared by every replica (spec 3.3 §10 K2, featureredis). - Sqlite
Device Store - A
DeviceStorein SQLite (spec 3.3 §10 K2, featuresqlite).
Enums§
- Attachment
Holder - Who an attachment belongs to.
- Device
Kind - 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.
- Device
Scope - What a device may do (spec 3.3 §10 K2): answer decisions, read, or stop everything.
- Device
State - 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,terminalortray(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§
- Device
Store - 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!, featureconformance).
Functions§
- device_
scope_ for - The scope a device needs for
methodonpath, orNonewhen the route is closed to devices (spec 3.3 §7.2, §10 K2, S-I10).pathmay carry the/apiprefix and a query.
Type Aliases§
- Device
Routes - 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.Nonecloses a route to devices.