@jdcpuwiz/zoo-kit 1.6.0 → 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 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)
@@ -74,17 +74,43 @@ export type DeviceVerdict = {
74
74
  export interface RedeemOptions {
75
75
  deviceName?: string;
76
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;
77
86
  }
78
- export type RedeemResult = {
87
+ interface RedeemOkBase {
79
88
  ok: true;
80
- deviceId: string;
81
89
  /** The raw key — hand it to the phone, never log or store it. */
82
90
  key: string;
83
91
  keyPrefix: string;
84
92
  name: string;
85
93
  appRole: string | null;
86
94
  person: DevicePerson;
87
- } | {
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
+ }) | {
88
114
  ok: false;
89
115
  status: number;
90
116
  error: string;
@@ -128,6 +154,9 @@ export interface DevicesOptions extends FailoverOptions {
128
154
  }
129
155
  export interface Devices {
130
156
  /** Forward a pairing code to zoo-people. Never throws. */
157
+ redeem(code: string, opts: RedeemOptions & {
158
+ allowUngranted: true;
159
+ }): Promise<RedeemAnyResult>;
131
160
  redeem(code: string, opts?: RedeemOptions): Promise<RedeemResult>;
132
161
  /** Read the device key from the request and resolve it. */
133
162
  resolve(req: RequestLike): Promise<DeviceVerdict>;
@@ -150,3 +179,4 @@ export interface Devices {
150
179
  export declare function createDevices(options?: DevicesOptions): Devices;
151
180
  /** The shared instance (env-configured). Apps that need `app`/`cookie` make their own with createDevices. */
152
181
  export declare const devices: Devices;
182
+ export {};
@@ -269,7 +269,7 @@ function createDevices(options = {}) {
269
269
  return (0, keys_js_1.bearerOf)(hdrs) ?? (options.cookie ? cookieValue(hdrs, options.cookie) : null);
270
270
  }
271
271
  return {
272
- async redeem(code, ro = {}) {
272
+ redeem: (async (code, ro = {}) => {
273
273
  const body = { code: String(code ?? "") };
274
274
  if (ro.deviceName)
275
275
  body.deviceName = ro.deviceName;
@@ -282,21 +282,32 @@ function createDevices(options = {}) {
282
282
  if (res.status !== 200)
283
283
  return { ok: false, status: res.status, error: str(j.error) || `HTTP ${res.status}` };
284
284
  const person = toPersonPart(j.person);
285
- if (!person || !str(j.deviceId) || !str(j.key))
285
+ if (!person || !str(j.key))
286
286
  return { ok: false, status: 503, error: "malformed answer" };
287
- const identity = toIdentity({ ...j, app: str(j.app) || options.app || "" });
288
- if (identity && identity.app)
289
- remember(sha256(str(j.key)), identity);
290
- return {
287
+ const base = {
291
288
  ok: true,
292
- deviceId: str(j.deviceId),
293
289
  key: str(j.key),
294
290
  keyPrefix: str(j.keyPrefix),
295
291
  name: str(j.name),
296
292
  appRole: typeof j.appRole === "string" ? j.appRole : null,
297
293
  person,
294
+ physicalDeviceId: str(j.physicalDeviceId) || null,
298
295
  };
299
- },
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
+ }),
300
311
  resolve(req) {
301
312
  const key = readKey(req.headers);
302
313
  if (!key)
@@ -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.6.0";
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.6.0";
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"];
@@ -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, type SwapResult, } 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
@@ -74,17 +74,43 @@ export type DeviceVerdict = {
74
74
  export interface RedeemOptions {
75
75
  deviceName?: string;
76
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;
77
86
  }
78
- export type RedeemResult = {
87
+ interface RedeemOkBase {
79
88
  ok: true;
80
- deviceId: string;
81
89
  /** The raw key — hand it to the phone, never log or store it. */
82
90
  key: string;
83
91
  keyPrefix: string;
84
92
  name: string;
85
93
  appRole: string | null;
86
94
  person: DevicePerson;
87
- } | {
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
+ }) | {
88
114
  ok: false;
89
115
  status: number;
90
116
  error: string;
@@ -128,6 +154,9 @@ export interface DevicesOptions extends FailoverOptions {
128
154
  }
129
155
  export interface Devices {
130
156
  /** Forward a pairing code to zoo-people. Never throws. */
157
+ redeem(code: string, opts: RedeemOptions & {
158
+ allowUngranted: true;
159
+ }): Promise<RedeemAnyResult>;
131
160
  redeem(code: string, opts?: RedeemOptions): Promise<RedeemResult>;
132
161
  /** Read the device key from the request and resolve it. */
133
162
  resolve(req: RequestLike): Promise<DeviceVerdict>;
@@ -150,3 +179,4 @@ export interface Devices {
150
179
  export declare function createDevices(options?: DevicesOptions): Devices;
151
180
  /** The shared instance (env-configured). Apps that need `app`/`cookie` make their own with createDevices. */
152
181
  export declare const devices: Devices;
182
+ export {};
package/dist/devices.js CHANGED
@@ -265,7 +265,7 @@ export function createDevices(options = {}) {
265
265
  return bearerOf(hdrs) ?? (options.cookie ? cookieValue(hdrs, options.cookie) : null);
266
266
  }
267
267
  return {
268
- async redeem(code, ro = {}) {
268
+ redeem: (async (code, ro = {}) => {
269
269
  const body = { code: String(code ?? "") };
270
270
  if (ro.deviceName)
271
271
  body.deviceName = ro.deviceName;
@@ -278,21 +278,32 @@ export function createDevices(options = {}) {
278
278
  if (res.status !== 200)
279
279
  return { ok: false, status: res.status, error: str(j.error) || `HTTP ${res.status}` };
280
280
  const person = toPersonPart(j.person);
281
- if (!person || !str(j.deviceId) || !str(j.key))
281
+ if (!person || !str(j.key))
282
282
  return { ok: false, status: 503, error: "malformed answer" };
283
- const identity = toIdentity({ ...j, app: str(j.app) || options.app || "" });
284
- if (identity && identity.app)
285
- remember(sha256(str(j.key)), identity);
286
- return {
283
+ const base = {
287
284
  ok: true,
288
- deviceId: str(j.deviceId),
289
285
  key: str(j.key),
290
286
  keyPrefix: str(j.keyPrefix),
291
287
  name: str(j.name),
292
288
  appRole: typeof j.appRole === "string" ? j.appRole : null,
293
289
  person,
290
+ physicalDeviceId: str(j.physicalDeviceId) || null,
294
291
  };
295
- },
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
+ }),
296
307
  resolve(req) {
297
308
  const key = readKey(req.headers);
298
309
  if (!key)
@@ -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.6.0";
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.6.0";
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, type SwapResult, } 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jdcpuwiz/zoo-kit",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Thin, pinned server-side client for the homelab's shared services: notify, whoami, people, health, app keys.",
5
5
  "license": "MIT",
6
6
  "type": "module",