@jdcpuwiz/zoo-kit 1.5.1 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/dist/cjs/devices.d.ts +55 -2
- package/dist/cjs/devices.js +46 -8
- package/dist/cjs/generated/contract.d.ts +1 -1
- package/dist/cjs/generated/contract.js +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/devices.d.ts +55 -2
- package/dist/devices.js +46 -8
- package/dist/generated/contract.d.ts +1 -1
- package/dist/generated/contract.js +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -212,6 +212,12 @@ export const devices = createDevices({ app: "zoo-health" /* , cookie: "ZSB_DEVIC
|
|
|
212
212
|
const r = await devices.redeem(body.code, { deviceName: body.name, platform: "ios" });
|
|
213
213
|
if (!r.ok) return Response.json({ error: r.error }, { status: r.status }); // 404/409/410/429/503
|
|
214
214
|
return Response.json({ key: r.key }); // hand to the phone; never log it
|
|
215
|
+
// r.granted is true here. A one-pairing (physical-device) code for a device
|
|
216
|
+
// this app is NOT switched on for comes back { ok:false, status:409,
|
|
217
|
+
// error:"not-granted" } — tell the phone "ask Wiz to turn <App> on" (1.7.0).
|
|
218
|
+
// A SHARED pair door that must hand back the device's zd_ key regardless
|
|
219
|
+
// (home-stack /api/pair) passes { allowUngranted: true }: then the answer is
|
|
220
|
+
// ok:true with granted:false and deviceId:null — never write a row for it.
|
|
215
221
|
|
|
216
222
|
// any device-authenticated route
|
|
217
223
|
const v = await devices.resolve(req); // Bearer (or the legacy cookie)
|
|
@@ -375,3 +381,6 @@ npm pack --dry-run # dist/, README.md, LICENSE, package.json only
|
|
|
375
381
|
npm run test:tarball # pack, install in a temp dir: require, import, tsc
|
|
376
382
|
node scripts/tarball-test.mjs @jdcpuwiz/zoo-kit@X.Y.Z # the same three checks on a published version
|
|
377
383
|
```
|
|
384
|
+
|
|
385
|
+
### devices.swap() (1.6.0, C2016)
|
|
386
|
+
`devices.swap(legacyKey)` trades a live legacy per-app key (hs_, zq_, …) for the device's shared `zd_` key on the SAME physical device (zoo-people ≥0.27, `POST /api/v1/devices/swap`). One-shot to the primary only. Returns `{ok:true, key, physicalDeviceId, deviceName, deviceId, granted, appRole, rotated, person}` or `{ok:false, status, error}` (401 = the legacy key, its device or person is not live). Hand `key` to the phone once; the legacy key keeps working. Python: `create_devices().swap_key(legacy_key)` (py 1.3.0).
|
package/dist/cjs/devices.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export declare const DEVICES_PATHS: {
|
|
|
4
4
|
readonly redeem: "/api/v1/devices/redeem";
|
|
5
5
|
readonly resolve: "/api/v1/devices/resolve";
|
|
6
6
|
readonly selfRevoke: "/api/v1/devices/self-revoke";
|
|
7
|
+
readonly swap: "/api/v1/devices/swap";
|
|
7
8
|
};
|
|
8
9
|
/** The conventional path an app mounts revokeHandler() on. */
|
|
9
10
|
export declare const DEVICE_REVOKE_PATH = "/api/internal/devices/revoked";
|
|
@@ -73,16 +74,62 @@ export type DeviceVerdict = {
|
|
|
73
74
|
export interface RedeemOptions {
|
|
74
75
|
deviceName?: string;
|
|
75
76
|
platform?: "ios" | "macos" | "web";
|
|
77
|
+
/**
|
|
78
|
+
* C2104: a shared pair door (home-stack's /api/pair, which every MKKit app
|
|
79
|
+
* uses) must hand back the device-wide `zd_` key even when the door's OWN
|
|
80
|
+
* app is not switched on for that device. With this set, such an answer is
|
|
81
|
+
* `ok: true, granted: false, deviceId: null`. Without it (the default) the
|
|
82
|
+
* same answer is `{ ok: false, status: 409, error: "not-granted" }`, so a
|
|
83
|
+
* per-app door never writes a shadow row with a null id.
|
|
84
|
+
*/
|
|
85
|
+
allowUngranted?: boolean;
|
|
76
86
|
}
|
|
77
|
-
|
|
87
|
+
interface RedeemOkBase {
|
|
78
88
|
ok: true;
|
|
79
|
-
deviceId: string;
|
|
80
89
|
/** The raw key — hand it to the phone, never log or store it. */
|
|
81
90
|
key: string;
|
|
82
91
|
keyPrefix: string;
|
|
83
92
|
name: string;
|
|
84
93
|
appRole: string | null;
|
|
85
94
|
person: DevicePerson;
|
|
95
|
+
/** The physical device (one-pairing codes); null on a legacy per-app answer. */
|
|
96
|
+
physicalDeviceId: string | null;
|
|
97
|
+
}
|
|
98
|
+
export type RedeemResult = (RedeemOkBase & {
|
|
99
|
+
deviceId: string;
|
|
100
|
+
granted: true;
|
|
101
|
+
}) | {
|
|
102
|
+
ok: false;
|
|
103
|
+
status: number;
|
|
104
|
+
error: string;
|
|
105
|
+
};
|
|
106
|
+
/** redeem(code, { allowUngranted: true }) — deviceId is null when not granted. */
|
|
107
|
+
export type RedeemAnyResult = (RedeemOkBase & {
|
|
108
|
+
deviceId: string;
|
|
109
|
+
granted: true;
|
|
110
|
+
}) | (RedeemOkBase & {
|
|
111
|
+
deviceId: null;
|
|
112
|
+
granted: false;
|
|
113
|
+
}) | {
|
|
114
|
+
ok: false;
|
|
115
|
+
status: number;
|
|
116
|
+
error: string;
|
|
117
|
+
};
|
|
118
|
+
/** devices.swap(): a live legacy per-app key traded for the device's shared `zd_` key (C2016). */
|
|
119
|
+
export type SwapResult = {
|
|
120
|
+
ok: true;
|
|
121
|
+
/** The raw `zd_` key — hand it to the phone ONCE, never log or store it. */
|
|
122
|
+
key: string;
|
|
123
|
+
keyPrefix: string;
|
|
124
|
+
physicalDeviceId: string;
|
|
125
|
+
deviceName: string;
|
|
126
|
+
/** This app's grant id on the device (null when this app is not switched on there). */
|
|
127
|
+
deviceId: string | null;
|
|
128
|
+
granted: boolean;
|
|
129
|
+
appRole: string | null;
|
|
130
|
+
/** How many older shared keys this swap retired (one live `zd_` per device). */
|
|
131
|
+
rotated: number;
|
|
132
|
+
person: DevicePerson;
|
|
86
133
|
} | {
|
|
87
134
|
ok: false;
|
|
88
135
|
status: number;
|
|
@@ -107,6 +154,9 @@ export interface DevicesOptions extends FailoverOptions {
|
|
|
107
154
|
}
|
|
108
155
|
export interface Devices {
|
|
109
156
|
/** Forward a pairing code to zoo-people. Never throws. */
|
|
157
|
+
redeem(code: string, opts: RedeemOptions & {
|
|
158
|
+
allowUngranted: true;
|
|
159
|
+
}): Promise<RedeemAnyResult>;
|
|
110
160
|
redeem(code: string, opts?: RedeemOptions): Promise<RedeemResult>;
|
|
111
161
|
/** Read the device key from the request and resolve it. */
|
|
112
162
|
resolve(req: RequestLike): Promise<DeviceVerdict>;
|
|
@@ -117,6 +167,8 @@ export interface Devices {
|
|
|
117
167
|
ok: boolean;
|
|
118
168
|
status: number;
|
|
119
169
|
}>;
|
|
170
|
+
/** Trade a live legacy per-app key for the device's shared `zd_` key (zoo-people ≥0.27). Never throws. */
|
|
171
|
+
swap(legacyKey: string): Promise<SwapResult>;
|
|
120
172
|
/** Drop every cached answer for this device and refuse it from now on. */
|
|
121
173
|
evict(deviceId: string): boolean;
|
|
122
174
|
/** The route handler for zoo-people's revoke push (mount on DEVICE_REVOKE_PATH). */
|
|
@@ -127,3 +179,4 @@ export interface Devices {
|
|
|
127
179
|
export declare function createDevices(options?: DevicesOptions): Devices;
|
|
128
180
|
/** The shared instance (env-configured). Apps that need `app`/`cookie` make their own with createDevices. */
|
|
129
181
|
export declare const devices: Devices;
|
|
182
|
+
export {};
|
package/dist/cjs/devices.js
CHANGED
|
@@ -39,6 +39,7 @@ exports.DEVICES_PATHS = {
|
|
|
39
39
|
redeem: "/api/v1/devices/redeem",
|
|
40
40
|
resolve: "/api/v1/devices/resolve",
|
|
41
41
|
selfRevoke: "/api/v1/devices/self-revoke",
|
|
42
|
+
swap: "/api/v1/devices/swap",
|
|
42
43
|
};
|
|
43
44
|
/** The conventional path an app mounts revokeHandler() on. */
|
|
44
45
|
exports.DEVICE_REVOKE_PATH = "/api/internal/devices/revoked";
|
|
@@ -174,6 +175,7 @@ function createDevices(options = {}) {
|
|
|
174
175
|
}
|
|
175
176
|
// Primary-first failover is safe for resolve (read-only). Redeem is one-shot
|
|
176
177
|
// and goes to the primary only: a redeem that timed out may have landed.
|
|
178
|
+
// Swap is one-shot too (it mints a key and may rotate the older one).
|
|
177
179
|
const urls = path === exports.DEVICES_PATHS.resolve ? c.urls : c.urls.slice(0, 1);
|
|
178
180
|
const out = await (0, http_js_1.failoverFetch)(urls, path, { method: "POST", headers: { Authorization: `Bearer ${c.key}`, "Content-Type": "application/json" }, body: JSON.stringify(body) }, failover);
|
|
179
181
|
if (out.kind === "failed")
|
|
@@ -267,7 +269,7 @@ function createDevices(options = {}) {
|
|
|
267
269
|
return (0, keys_js_1.bearerOf)(hdrs) ?? (options.cookie ? cookieValue(hdrs, options.cookie) : null);
|
|
268
270
|
}
|
|
269
271
|
return {
|
|
270
|
-
async
|
|
272
|
+
redeem: (async (code, ro = {}) => {
|
|
271
273
|
const body = { code: String(code ?? "") };
|
|
272
274
|
if (ro.deviceName)
|
|
273
275
|
body.deviceName = ro.deviceName;
|
|
@@ -280,21 +282,32 @@ function createDevices(options = {}) {
|
|
|
280
282
|
if (res.status !== 200)
|
|
281
283
|
return { ok: false, status: res.status, error: str(j.error) || `HTTP ${res.status}` };
|
|
282
284
|
const person = toPersonPart(j.person);
|
|
283
|
-
if (!person || !str(j.
|
|
285
|
+
if (!person || !str(j.key))
|
|
284
286
|
return { ok: false, status: 503, error: "malformed answer" };
|
|
285
|
-
const
|
|
286
|
-
if (identity && identity.app)
|
|
287
|
-
remember(sha256(str(j.key)), identity);
|
|
288
|
-
return {
|
|
287
|
+
const base = {
|
|
289
288
|
ok: true,
|
|
290
|
-
deviceId: str(j.deviceId),
|
|
291
289
|
key: str(j.key),
|
|
292
290
|
keyPrefix: str(j.keyPrefix),
|
|
293
291
|
name: str(j.name),
|
|
294
292
|
appRole: typeof j.appRole === "string" ? j.appRole : null,
|
|
295
293
|
person,
|
|
294
|
+
physicalDeviceId: str(j.physicalDeviceId) || null,
|
|
296
295
|
};
|
|
297
|
-
|
|
296
|
+
if (!str(j.deviceId)) {
|
|
297
|
+
// C2104: a one-pairing code for a device this app is not switched on for.
|
|
298
|
+
// Only a physical answer (physicalDeviceId present) may legitimately lack
|
|
299
|
+
// a deviceId; anything else is still malformed.
|
|
300
|
+
if (!base.physicalDeviceId)
|
|
301
|
+
return { ok: false, status: 503, error: "malformed answer" };
|
|
302
|
+
if (!ro.allowUngranted)
|
|
303
|
+
return { ok: false, status: 409, error: "not-granted" };
|
|
304
|
+
return { ...base, deviceId: null, granted: false };
|
|
305
|
+
}
|
|
306
|
+
const identity = toIdentity({ ...j, app: str(j.app) || options.app || "" });
|
|
307
|
+
if (identity && identity.app)
|
|
308
|
+
remember(sha256(str(j.key)), identity);
|
|
309
|
+
return { ...base, deviceId: str(j.deviceId), granted: true };
|
|
310
|
+
}),
|
|
298
311
|
resolve(req) {
|
|
299
312
|
const key = readKey(req.headers);
|
|
300
313
|
if (!key)
|
|
@@ -315,6 +328,31 @@ function createDevices(options = {}) {
|
|
|
315
328
|
cache.delete(sha256(key));
|
|
316
329
|
return { ok: res.status === 200, status: res.status };
|
|
317
330
|
},
|
|
331
|
+
async swap(legacyKey) {
|
|
332
|
+
if (!legacyKey)
|
|
333
|
+
return { ok: false, status: 401, error: "no key" };
|
|
334
|
+
const res = await post(exports.DEVICES_PATHS.swap, { key: legacyKey });
|
|
335
|
+
if (res.kind === "failed")
|
|
336
|
+
return { ok: false, status: 503, error: res.error === "unconfigured" ? "unconfigured" : "unavailable" };
|
|
337
|
+
const j = (res.json ?? {});
|
|
338
|
+
if (res.status !== 200)
|
|
339
|
+
return { ok: false, status: res.status, error: str(j.error) || `HTTP ${res.status}` };
|
|
340
|
+
const person = toPersonPart(j.person);
|
|
341
|
+
if (!person || !str(j.key) || !str(j.physicalDeviceId))
|
|
342
|
+
return { ok: false, status: 503, error: "malformed answer" };
|
|
343
|
+
return {
|
|
344
|
+
ok: true,
|
|
345
|
+
key: str(j.key),
|
|
346
|
+
keyPrefix: str(j.keyPrefix),
|
|
347
|
+
physicalDeviceId: str(j.physicalDeviceId),
|
|
348
|
+
deviceName: str(j.deviceName),
|
|
349
|
+
deviceId: str(j.deviceId) || null,
|
|
350
|
+
granted: j.granted === true,
|
|
351
|
+
appRole: typeof j.appRole === "string" ? j.appRole : null,
|
|
352
|
+
rotated: typeof j.rotated === "number" ? j.rotated : 0,
|
|
353
|
+
person,
|
|
354
|
+
};
|
|
355
|
+
},
|
|
318
356
|
evict,
|
|
319
357
|
revokeHandler() {
|
|
320
358
|
const callers = options.revokeCallers ?? ["zoo-people"];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** The kit's own version — reported as `kit` by the health handler. */
|
|
2
|
-
export declare const KIT_VERSION = "1.
|
|
2
|
+
export declare const KIT_VERSION = "1.7.0";
|
|
3
3
|
/** The exact keys a v1 Person may carry; anything else is dropped. */
|
|
4
4
|
export declare const PERSON_KEYS: readonly ["authentikUsername", "dob", "enabled", "haEntityId", "headingColor", "id", "kind", "name", "photoUrl", "role", "slug", "sortOrder", "traccarDeviceId"];
|
|
5
5
|
export declare const NOTIFY_PRIORITIES: readonly ["passive", "active", "time-sensitive", "critical"];
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.SLUG_PATTERN = exports.NOTIFY_PRIORITIES = exports.PERSON_KEYS = exports.KIT_VERSION = void 0;
|
|
6
6
|
/** The kit's own version — reported as `kit` by the health handler. */
|
|
7
|
-
exports.KIT_VERSION = "1.
|
|
7
|
+
exports.KIT_VERSION = "1.7.0";
|
|
8
8
|
/** The exact keys a v1 Person may carry; anything else is dropped. */
|
|
9
9
|
exports.PERSON_KEYS = ["authentikUsername", "dob", "enabled", "haEntityId", "headingColor", "id", "kind", "name", "photoUrl", "role", "slug", "sortOrder", "traccarDeviceId"];
|
|
10
10
|
exports.NOTIFY_PRIORITIES = ["passive", "active", "time-sensitive", "critical"];
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export { ENV, appKeyEnv, appKeyForEnv, appUrlsEnv, checkConfig, envSuffix, type
|
|
|
13
13
|
export { CATEGORIES, CATEGORY_ALIASES, CATEGORY_SLUGS, canonicalCategory, customCategory, isCanonicalCategory, isCategoryAlias, toCategory, type CategoryAlias, type CategoryInput, type CustomCategory, type NotifyCategory, } from "./categories.js";
|
|
14
14
|
export { NOTIFY_DEFAULTS, NOTIFY_PATH, dedupeKey, notify, notifyConfigured, type NotifyAction, type NotifyDryRun, type NotifyFailed, type NotifyInput, type NotifyOptions, type NotifyPriority, type NotifyResult, type NotifySent, } from "./notify.js";
|
|
15
15
|
export { PEOPLE_DEFAULTS, PEOPLE_PATHS, createPeople, people, toPerson, type Contact, type Contacts, type ContactsResult, type ContactsStatus, type People, type PeopleClient, type SignatureResult, type PeopleList, type PeopleOptions, type PeopleStatus, type Person, type ResolveStatus, type ResolvedUsername, } from "./people.js";
|
|
16
|
-
export { DEVICES_DEFAULTS, DEVICES_PATHS, DEVICE_REVOKE_PATH, createDevices, devices, type DeviceIdentity, type DeviceInfo, type DevicePerson, type DeviceStatus, type DeviceVerdict, type Devices, type DevicesOptions, type RedeemOptions, type RedeemResult, } from "./devices.js";
|
|
16
|
+
export { DEVICES_DEFAULTS, DEVICES_PATHS, DEVICE_REVOKE_PATH, createDevices, devices, type DeviceIdentity, type DeviceInfo, type DevicePerson, type DeviceStatus, type DeviceVerdict, type Devices, type DevicesOptions, type RedeemOptions, type RedeemResult, type RedeemAnyResult, type SwapResult, } from "./devices.js";
|
|
17
17
|
export { ZooKitAuthError, cameThroughGate, gateSecretOk, readAuthentikUsername, requireApp, requirePerson, whoami, type Anonymous, type AuthentikOptions, type DeviceAnswer, type Identity, type PersonIdentity, type SessionAnswer, type WhoamiOptions, } from "./whoami.js";
|
|
18
18
|
export { ZooKitConfigError, bearerOf, callApp, checkApp, safeEqual, type AppIdentity, type AppVerdict, type CallAppOptions, type HeaderBag, type RequestLike, } from "./keys.js";
|
|
19
19
|
export { healthBody, healthHandler, type CheckFn, type CheckResult, type HealthBody, type HealthOptions } from "./health.js";
|
package/dist/devices.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export declare const DEVICES_PATHS: {
|
|
|
4
4
|
readonly redeem: "/api/v1/devices/redeem";
|
|
5
5
|
readonly resolve: "/api/v1/devices/resolve";
|
|
6
6
|
readonly selfRevoke: "/api/v1/devices/self-revoke";
|
|
7
|
+
readonly swap: "/api/v1/devices/swap";
|
|
7
8
|
};
|
|
8
9
|
/** The conventional path an app mounts revokeHandler() on. */
|
|
9
10
|
export declare const DEVICE_REVOKE_PATH = "/api/internal/devices/revoked";
|
|
@@ -73,16 +74,62 @@ export type DeviceVerdict = {
|
|
|
73
74
|
export interface RedeemOptions {
|
|
74
75
|
deviceName?: string;
|
|
75
76
|
platform?: "ios" | "macos" | "web";
|
|
77
|
+
/**
|
|
78
|
+
* C2104: a shared pair door (home-stack's /api/pair, which every MKKit app
|
|
79
|
+
* uses) must hand back the device-wide `zd_` key even when the door's OWN
|
|
80
|
+
* app is not switched on for that device. With this set, such an answer is
|
|
81
|
+
* `ok: true, granted: false, deviceId: null`. Without it (the default) the
|
|
82
|
+
* same answer is `{ ok: false, status: 409, error: "not-granted" }`, so a
|
|
83
|
+
* per-app door never writes a shadow row with a null id.
|
|
84
|
+
*/
|
|
85
|
+
allowUngranted?: boolean;
|
|
76
86
|
}
|
|
77
|
-
|
|
87
|
+
interface RedeemOkBase {
|
|
78
88
|
ok: true;
|
|
79
|
-
deviceId: string;
|
|
80
89
|
/** The raw key — hand it to the phone, never log or store it. */
|
|
81
90
|
key: string;
|
|
82
91
|
keyPrefix: string;
|
|
83
92
|
name: string;
|
|
84
93
|
appRole: string | null;
|
|
85
94
|
person: DevicePerson;
|
|
95
|
+
/** The physical device (one-pairing codes); null on a legacy per-app answer. */
|
|
96
|
+
physicalDeviceId: string | null;
|
|
97
|
+
}
|
|
98
|
+
export type RedeemResult = (RedeemOkBase & {
|
|
99
|
+
deviceId: string;
|
|
100
|
+
granted: true;
|
|
101
|
+
}) | {
|
|
102
|
+
ok: false;
|
|
103
|
+
status: number;
|
|
104
|
+
error: string;
|
|
105
|
+
};
|
|
106
|
+
/** redeem(code, { allowUngranted: true }) — deviceId is null when not granted. */
|
|
107
|
+
export type RedeemAnyResult = (RedeemOkBase & {
|
|
108
|
+
deviceId: string;
|
|
109
|
+
granted: true;
|
|
110
|
+
}) | (RedeemOkBase & {
|
|
111
|
+
deviceId: null;
|
|
112
|
+
granted: false;
|
|
113
|
+
}) | {
|
|
114
|
+
ok: false;
|
|
115
|
+
status: number;
|
|
116
|
+
error: string;
|
|
117
|
+
};
|
|
118
|
+
/** devices.swap(): a live legacy per-app key traded for the device's shared `zd_` key (C2016). */
|
|
119
|
+
export type SwapResult = {
|
|
120
|
+
ok: true;
|
|
121
|
+
/** The raw `zd_` key — hand it to the phone ONCE, never log or store it. */
|
|
122
|
+
key: string;
|
|
123
|
+
keyPrefix: string;
|
|
124
|
+
physicalDeviceId: string;
|
|
125
|
+
deviceName: string;
|
|
126
|
+
/** This app's grant id on the device (null when this app is not switched on there). */
|
|
127
|
+
deviceId: string | null;
|
|
128
|
+
granted: boolean;
|
|
129
|
+
appRole: string | null;
|
|
130
|
+
/** How many older shared keys this swap retired (one live `zd_` per device). */
|
|
131
|
+
rotated: number;
|
|
132
|
+
person: DevicePerson;
|
|
86
133
|
} | {
|
|
87
134
|
ok: false;
|
|
88
135
|
status: number;
|
|
@@ -107,6 +154,9 @@ export interface DevicesOptions extends FailoverOptions {
|
|
|
107
154
|
}
|
|
108
155
|
export interface Devices {
|
|
109
156
|
/** Forward a pairing code to zoo-people. Never throws. */
|
|
157
|
+
redeem(code: string, opts: RedeemOptions & {
|
|
158
|
+
allowUngranted: true;
|
|
159
|
+
}): Promise<RedeemAnyResult>;
|
|
110
160
|
redeem(code: string, opts?: RedeemOptions): Promise<RedeemResult>;
|
|
111
161
|
/** Read the device key from the request and resolve it. */
|
|
112
162
|
resolve(req: RequestLike): Promise<DeviceVerdict>;
|
|
@@ -117,6 +167,8 @@ export interface Devices {
|
|
|
117
167
|
ok: boolean;
|
|
118
168
|
status: number;
|
|
119
169
|
}>;
|
|
170
|
+
/** Trade a live legacy per-app key for the device's shared `zd_` key (zoo-people ≥0.27). Never throws. */
|
|
171
|
+
swap(legacyKey: string): Promise<SwapResult>;
|
|
120
172
|
/** Drop every cached answer for this device and refuse it from now on. */
|
|
121
173
|
evict(deviceId: string): boolean;
|
|
122
174
|
/** The route handler for zoo-people's revoke push (mount on DEVICE_REVOKE_PATH). */
|
|
@@ -127,3 +179,4 @@ export interface Devices {
|
|
|
127
179
|
export declare function createDevices(options?: DevicesOptions): Devices;
|
|
128
180
|
/** The shared instance (env-configured). Apps that need `app`/`cookie` make their own with createDevices. */
|
|
129
181
|
export declare const devices: Devices;
|
|
182
|
+
export {};
|
package/dist/devices.js
CHANGED
|
@@ -35,6 +35,7 @@ export const DEVICES_PATHS = {
|
|
|
35
35
|
redeem: "/api/v1/devices/redeem",
|
|
36
36
|
resolve: "/api/v1/devices/resolve",
|
|
37
37
|
selfRevoke: "/api/v1/devices/self-revoke",
|
|
38
|
+
swap: "/api/v1/devices/swap",
|
|
38
39
|
};
|
|
39
40
|
/** The conventional path an app mounts revokeHandler() on. */
|
|
40
41
|
export const DEVICE_REVOKE_PATH = "/api/internal/devices/revoked";
|
|
@@ -170,6 +171,7 @@ export function createDevices(options = {}) {
|
|
|
170
171
|
}
|
|
171
172
|
// Primary-first failover is safe for resolve (read-only). Redeem is one-shot
|
|
172
173
|
// and goes to the primary only: a redeem that timed out may have landed.
|
|
174
|
+
// Swap is one-shot too (it mints a key and may rotate the older one).
|
|
173
175
|
const urls = path === DEVICES_PATHS.resolve ? c.urls : c.urls.slice(0, 1);
|
|
174
176
|
const out = await failoverFetch(urls, path, { method: "POST", headers: { Authorization: `Bearer ${c.key}`, "Content-Type": "application/json" }, body: JSON.stringify(body) }, failover);
|
|
175
177
|
if (out.kind === "failed")
|
|
@@ -263,7 +265,7 @@ export function createDevices(options = {}) {
|
|
|
263
265
|
return bearerOf(hdrs) ?? (options.cookie ? cookieValue(hdrs, options.cookie) : null);
|
|
264
266
|
}
|
|
265
267
|
return {
|
|
266
|
-
async
|
|
268
|
+
redeem: (async (code, ro = {}) => {
|
|
267
269
|
const body = { code: String(code ?? "") };
|
|
268
270
|
if (ro.deviceName)
|
|
269
271
|
body.deviceName = ro.deviceName;
|
|
@@ -276,21 +278,32 @@ export function createDevices(options = {}) {
|
|
|
276
278
|
if (res.status !== 200)
|
|
277
279
|
return { ok: false, status: res.status, error: str(j.error) || `HTTP ${res.status}` };
|
|
278
280
|
const person = toPersonPart(j.person);
|
|
279
|
-
if (!person || !str(j.
|
|
281
|
+
if (!person || !str(j.key))
|
|
280
282
|
return { ok: false, status: 503, error: "malformed answer" };
|
|
281
|
-
const
|
|
282
|
-
if (identity && identity.app)
|
|
283
|
-
remember(sha256(str(j.key)), identity);
|
|
284
|
-
return {
|
|
283
|
+
const base = {
|
|
285
284
|
ok: true,
|
|
286
|
-
deviceId: str(j.deviceId),
|
|
287
285
|
key: str(j.key),
|
|
288
286
|
keyPrefix: str(j.keyPrefix),
|
|
289
287
|
name: str(j.name),
|
|
290
288
|
appRole: typeof j.appRole === "string" ? j.appRole : null,
|
|
291
289
|
person,
|
|
290
|
+
physicalDeviceId: str(j.physicalDeviceId) || null,
|
|
292
291
|
};
|
|
293
|
-
|
|
292
|
+
if (!str(j.deviceId)) {
|
|
293
|
+
// C2104: a one-pairing code for a device this app is not switched on for.
|
|
294
|
+
// Only a physical answer (physicalDeviceId present) may legitimately lack
|
|
295
|
+
// a deviceId; anything else is still malformed.
|
|
296
|
+
if (!base.physicalDeviceId)
|
|
297
|
+
return { ok: false, status: 503, error: "malformed answer" };
|
|
298
|
+
if (!ro.allowUngranted)
|
|
299
|
+
return { ok: false, status: 409, error: "not-granted" };
|
|
300
|
+
return { ...base, deviceId: null, granted: false };
|
|
301
|
+
}
|
|
302
|
+
const identity = toIdentity({ ...j, app: str(j.app) || options.app || "" });
|
|
303
|
+
if (identity && identity.app)
|
|
304
|
+
remember(sha256(str(j.key)), identity);
|
|
305
|
+
return { ...base, deviceId: str(j.deviceId), granted: true };
|
|
306
|
+
}),
|
|
294
307
|
resolve(req) {
|
|
295
308
|
const key = readKey(req.headers);
|
|
296
309
|
if (!key)
|
|
@@ -311,6 +324,31 @@ export function createDevices(options = {}) {
|
|
|
311
324
|
cache.delete(sha256(key));
|
|
312
325
|
return { ok: res.status === 200, status: res.status };
|
|
313
326
|
},
|
|
327
|
+
async swap(legacyKey) {
|
|
328
|
+
if (!legacyKey)
|
|
329
|
+
return { ok: false, status: 401, error: "no key" };
|
|
330
|
+
const res = await post(DEVICES_PATHS.swap, { key: legacyKey });
|
|
331
|
+
if (res.kind === "failed")
|
|
332
|
+
return { ok: false, status: 503, error: res.error === "unconfigured" ? "unconfigured" : "unavailable" };
|
|
333
|
+
const j = (res.json ?? {});
|
|
334
|
+
if (res.status !== 200)
|
|
335
|
+
return { ok: false, status: res.status, error: str(j.error) || `HTTP ${res.status}` };
|
|
336
|
+
const person = toPersonPart(j.person);
|
|
337
|
+
if (!person || !str(j.key) || !str(j.physicalDeviceId))
|
|
338
|
+
return { ok: false, status: 503, error: "malformed answer" };
|
|
339
|
+
return {
|
|
340
|
+
ok: true,
|
|
341
|
+
key: str(j.key),
|
|
342
|
+
keyPrefix: str(j.keyPrefix),
|
|
343
|
+
physicalDeviceId: str(j.physicalDeviceId),
|
|
344
|
+
deviceName: str(j.deviceName),
|
|
345
|
+
deviceId: str(j.deviceId) || null,
|
|
346
|
+
granted: j.granted === true,
|
|
347
|
+
appRole: typeof j.appRole === "string" ? j.appRole : null,
|
|
348
|
+
rotated: typeof j.rotated === "number" ? j.rotated : 0,
|
|
349
|
+
person,
|
|
350
|
+
};
|
|
351
|
+
},
|
|
314
352
|
evict,
|
|
315
353
|
revokeHandler() {
|
|
316
354
|
const callers = options.revokeCallers ?? ["zoo-people"];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** The kit's own version — reported as `kit` by the health handler. */
|
|
2
|
-
export declare const KIT_VERSION = "1.
|
|
2
|
+
export declare const KIT_VERSION = "1.7.0";
|
|
3
3
|
/** The exact keys a v1 Person may carry; anything else is dropped. */
|
|
4
4
|
export declare const PERSON_KEYS: readonly ["authentikUsername", "dob", "enabled", "haEntityId", "headingColor", "id", "kind", "name", "photoUrl", "role", "slug", "sortOrder", "traccarDeviceId"];
|
|
5
5
|
export declare const NOTIFY_PRIORITIES: readonly ["passive", "active", "time-sensitive", "critical"];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// GENERATED by packages/zoo-kit/scripts/generate.mjs from contracts/v1/*.v1.yaml + packages/zoo-kit/package.json.
|
|
2
2
|
// Do not edit — change the contract and run `npm run gen`.
|
|
3
3
|
/** The kit's own version — reported as `kit` by the health handler. */
|
|
4
|
-
export const KIT_VERSION = "1.
|
|
4
|
+
export const KIT_VERSION = "1.7.0";
|
|
5
5
|
/** The exact keys a v1 Person may carry; anything else is dropped. */
|
|
6
6
|
export const PERSON_KEYS = ["authentikUsername", "dob", "enabled", "haEntityId", "headingColor", "id", "kind", "name", "photoUrl", "role", "slug", "sortOrder", "traccarDeviceId"];
|
|
7
7
|
export const NOTIFY_PRIORITIES = ["passive", "active", "time-sensitive", "critical"];
|
package/dist/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export { ENV, appKeyEnv, appKeyForEnv, appUrlsEnv, checkConfig, envSuffix, type
|
|
|
13
13
|
export { CATEGORIES, CATEGORY_ALIASES, CATEGORY_SLUGS, canonicalCategory, customCategory, isCanonicalCategory, isCategoryAlias, toCategory, type CategoryAlias, type CategoryInput, type CustomCategory, type NotifyCategory, } from "./categories.js";
|
|
14
14
|
export { NOTIFY_DEFAULTS, NOTIFY_PATH, dedupeKey, notify, notifyConfigured, type NotifyAction, type NotifyDryRun, type NotifyFailed, type NotifyInput, type NotifyOptions, type NotifyPriority, type NotifyResult, type NotifySent, } from "./notify.js";
|
|
15
15
|
export { PEOPLE_DEFAULTS, PEOPLE_PATHS, createPeople, people, toPerson, type Contact, type Contacts, type ContactsResult, type ContactsStatus, type People, type PeopleClient, type SignatureResult, type PeopleList, type PeopleOptions, type PeopleStatus, type Person, type ResolveStatus, type ResolvedUsername, } from "./people.js";
|
|
16
|
-
export { DEVICES_DEFAULTS, DEVICES_PATHS, DEVICE_REVOKE_PATH, createDevices, devices, type DeviceIdentity, type DeviceInfo, type DevicePerson, type DeviceStatus, type DeviceVerdict, type Devices, type DevicesOptions, type RedeemOptions, type RedeemResult, } from "./devices.js";
|
|
16
|
+
export { DEVICES_DEFAULTS, DEVICES_PATHS, DEVICE_REVOKE_PATH, createDevices, devices, type DeviceIdentity, type DeviceInfo, type DevicePerson, type DeviceStatus, type DeviceVerdict, type Devices, type DevicesOptions, type RedeemOptions, type RedeemResult, type RedeemAnyResult, type SwapResult, } from "./devices.js";
|
|
17
17
|
export { ZooKitAuthError, cameThroughGate, gateSecretOk, readAuthentikUsername, requireApp, requirePerson, whoami, type Anonymous, type AuthentikOptions, type DeviceAnswer, type Identity, type PersonIdentity, type SessionAnswer, type WhoamiOptions, } from "./whoami.js";
|
|
18
18
|
export { ZooKitConfigError, bearerOf, callApp, checkApp, safeEqual, type AppIdentity, type AppVerdict, type CallAppOptions, type HeaderBag, type RequestLike, } from "./keys.js";
|
|
19
19
|
export { healthBody, healthHandler, type CheckFn, type CheckResult, type HealthBody, type HealthOptions } from "./health.js";
|
package/package.json
CHANGED