@jdcpuwiz/zoo-kit 1.1.0 → 1.3.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 +65 -0
- package/dist/cjs/devices.d.ts +110 -0
- package/dist/cjs/devices.js +305 -0
- package/dist/cjs/generated/contract.d.ts +1 -1
- package/dist/cjs/generated/contract.js +1 -1
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +7 -1
- package/dist/cjs/people.d.ts +39 -2
- package/dist/cjs/people.js +55 -0
- package/dist/cjs/whoami.d.ts +11 -3
- package/dist/cjs/whoami.js +19 -1
- package/dist/devices.d.ts +110 -0
- package/dist/devices.js +301 -0
- package/dist/generated/contract.d.ts +1 -1
- package/dist/generated/contract.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/people.d.ts +39 -2
- package/dist/people.js +55 -0
- package/dist/whoami.d.ts +11 -3
- package/dist/whoami.js +19 -1
- package/package.json +1 -1
package/dist/people.js
CHANGED
|
@@ -32,7 +32,24 @@ export const PEOPLE_DEFAULTS = {
|
|
|
32
32
|
export const PEOPLE_PATHS = {
|
|
33
33
|
list: "/api/v1/people",
|
|
34
34
|
whoamiResolve: "/api/v1/whoami-resolve",
|
|
35
|
+
contactsSearch: "/api/v1/contacts/search",
|
|
35
36
|
};
|
|
37
|
+
function toContact(raw) {
|
|
38
|
+
if (!raw || typeof raw !== "object")
|
|
39
|
+
return null;
|
|
40
|
+
const r = raw;
|
|
41
|
+
if (typeof r.name !== "string" || !r.name)
|
|
42
|
+
return null;
|
|
43
|
+
const strs = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === "string") : []);
|
|
44
|
+
return {
|
|
45
|
+
uid: typeof r.uid === "string" ? r.uid : null,
|
|
46
|
+
name: r.name,
|
|
47
|
+
emails: strs(r.emails).map((e) => e.toLowerCase()),
|
|
48
|
+
phones: strs(r.phones),
|
|
49
|
+
org: typeof r.org === "string" ? r.org : null,
|
|
50
|
+
hasPhoto: r.hasPhoto === true,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
36
53
|
const KEYS = new Set(PERSON_KEYS);
|
|
37
54
|
/** Keep only the contract's Person keys; null for anything that is not a usable person. */
|
|
38
55
|
export function toPerson(raw) {
|
|
@@ -67,6 +84,7 @@ export function createPeople(options = {}) {
|
|
|
67
84
|
let listCache = null;
|
|
68
85
|
let listInflight = null;
|
|
69
86
|
const users = new Map();
|
|
87
|
+
const searches = new Map();
|
|
70
88
|
async function get(path) {
|
|
71
89
|
const c = config();
|
|
72
90
|
if (c.missing.length) {
|
|
@@ -154,9 +172,46 @@ export function createPeople(options = {}) {
|
|
|
154
172
|
return { ok: true, detail: `stale since ${l.loadedAt?.toISOString()}: ${l.error ?? ""}`.trim() };
|
|
155
173
|
throw new Error(`roster ${l.status}${l.error ? `: ${l.error}` : ""}`);
|
|
156
174
|
},
|
|
175
|
+
contacts: {
|
|
176
|
+
async search(q, opts = {}) {
|
|
177
|
+
const needle = (q ?? "").trim().toLowerCase();
|
|
178
|
+
if (!needle)
|
|
179
|
+
return { contacts: [], status: "fresh" };
|
|
180
|
+
const limit = Math.max(1, Math.min(50, Math.floor(opts.limit ?? 20)));
|
|
181
|
+
const key = `${limit}|${needle}`;
|
|
182
|
+
const hit = searches.get(key);
|
|
183
|
+
const age = hit ? now() - hit.at : Infinity;
|
|
184
|
+
if (hit && age < o.ttlMs)
|
|
185
|
+
return hit.result;
|
|
186
|
+
const got = await get(`${PEOPLE_PATHS.contactsSearch}?q=${encodeURIComponent(needle)}&limit=${limit}`);
|
|
187
|
+
if (got.kind === "ok") {
|
|
188
|
+
const j = got.json;
|
|
189
|
+
if (Array.isArray(j?.contacts)) {
|
|
190
|
+
const contacts = j.contacts.map(toContact).filter((c) => c !== null);
|
|
191
|
+
const result = { contacts, status: j?.status === "stale" ? "stale" : "fresh" };
|
|
192
|
+
if (searches.size > 500)
|
|
193
|
+
searches.clear();
|
|
194
|
+
searches.set(key, { result, at: now() });
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const error = got.kind === "ok" ? "malformed contacts answer" : got.kind === "not-found" ? "HTTP 404" : got.error;
|
|
199
|
+
if (hit && age <= o.staleUsernameMs)
|
|
200
|
+
return { ...hit.result, status: "stale", error };
|
|
201
|
+
return { contacts: [], status: got.kind === "unconfigured" ? "unconfigured" : "unavailable", error };
|
|
202
|
+
},
|
|
203
|
+
async byEmail(email) {
|
|
204
|
+
const e = (email ?? "").trim().toLowerCase();
|
|
205
|
+
if (!e)
|
|
206
|
+
return null;
|
|
207
|
+
const r = await api.contacts.search(e, { limit: 5 });
|
|
208
|
+
return r.contacts.find((c) => c.emails.includes(e)) ?? null;
|
|
209
|
+
},
|
|
210
|
+
},
|
|
157
211
|
clear() {
|
|
158
212
|
listCache = null;
|
|
159
213
|
users.clear();
|
|
214
|
+
searches.clear();
|
|
160
215
|
},
|
|
161
216
|
};
|
|
162
217
|
return api;
|
package/dist/whoami.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type AppIdentity, type HeaderBag, type RequestLike } from "./keys.js";
|
|
2
|
+
import type { Devices } from "./devices.js";
|
|
2
3
|
import { type People, type Person } from "./people.js";
|
|
3
4
|
export type { AppIdentity, HeaderBag, RequestLike } from "./keys.js";
|
|
4
5
|
export interface PersonIdentity {
|
|
@@ -16,6 +17,8 @@ export interface Anonymous {
|
|
|
16
17
|
kind: "anonymous";
|
|
17
18
|
/** Why — for the app's own logs. Never sent to the client by requirePerson. */
|
|
18
19
|
reason?: string;
|
|
20
|
+
/** True when the answer is "could not check" (zoo-people down) rather than "no" — requirePerson answers 503. */
|
|
21
|
+
unavailable?: boolean;
|
|
19
22
|
}
|
|
20
23
|
export type Identity = PersonIdentity | AppIdentity | Anonymous;
|
|
21
24
|
export interface AuthentikOptions {
|
|
@@ -39,7 +42,12 @@ export interface DeviceAnswer {
|
|
|
39
42
|
export interface WhoamiOptions {
|
|
40
43
|
/** Allowed app callers (source 1). */
|
|
41
44
|
apps?: readonly string[];
|
|
42
|
-
/**
|
|
45
|
+
/**
|
|
46
|
+
* zoo-people device pairing (source 2, kit 1.3.0): the Bearer is resolved as
|
|
47
|
+
* a device key through this client. Tried before `resolveDeviceKey`.
|
|
48
|
+
*/
|
|
49
|
+
devices?: Devices;
|
|
50
|
+
/** Device-key resolver (source 2, legacy) — the app's own Device table. */
|
|
43
51
|
resolveDeviceKey?: (key: string) => Promise<DeviceAnswer | null>;
|
|
44
52
|
/** The authentik header (source 3). `true` = env defaults. */
|
|
45
53
|
authentik?: boolean | AuthentikOptions;
|
|
@@ -70,10 +78,10 @@ export declare function whoami(req: RequestLike, opts?: WhoamiOptions): Promise<
|
|
|
70
78
|
* Hono: app.onError((e, c) => e instanceof ZooKitAuthError ? e.response : c.text("error", 500))
|
|
71
79
|
*/
|
|
72
80
|
export declare class ZooKitAuthError extends Error {
|
|
73
|
-
readonly status: 401 | 403;
|
|
81
|
+
readonly status: 401 | 403 | 503;
|
|
74
82
|
/** Server-side reason for the app's own logs. */
|
|
75
83
|
readonly reason: string;
|
|
76
|
-
constructor(status: 401 | 403, reason?: string);
|
|
84
|
+
constructor(status: 401 | 403 | 503, reason?: string);
|
|
77
85
|
/** A fresh Response each read (a body can only be consumed once). */
|
|
78
86
|
get response(): Response;
|
|
79
87
|
}
|
package/dist/whoami.js
CHANGED
|
@@ -102,6 +102,22 @@ export async function whoami(req, opts = {}) {
|
|
|
102
102
|
if (app)
|
|
103
103
|
return { kind: "app", app, via: "key" };
|
|
104
104
|
}
|
|
105
|
+
if (token && opts.devices) {
|
|
106
|
+
const v = await opts.devices.resolveKey(token);
|
|
107
|
+
if (v.ok) {
|
|
108
|
+
const d = v.identity;
|
|
109
|
+
// The full roster Person when the directory has it; otherwise the
|
|
110
|
+
// device answer's own person fields, so an outage of the roster list
|
|
111
|
+
// never unpairs a phone zoo-people just vouched for.
|
|
112
|
+
const full = await dir.get(d.slug).catch(() => null);
|
|
113
|
+
const p = (full ?? { ...d.person });
|
|
114
|
+
return asPerson(p, "device", { device: { ...d.device, app: d.app, status: v.status } });
|
|
115
|
+
}
|
|
116
|
+
if (v.status === 503)
|
|
117
|
+
return { kind: "anonymous", reason: v.reason, unavailable: true };
|
|
118
|
+
if (!opts.resolveDeviceKey)
|
|
119
|
+
return { kind: "anonymous", reason: v.reason };
|
|
120
|
+
}
|
|
105
121
|
if (token && opts.resolveDeviceKey) {
|
|
106
122
|
try {
|
|
107
123
|
const d = await opts.resolveDeviceKey(token);
|
|
@@ -155,7 +171,7 @@ export class ZooKitAuthError extends Error {
|
|
|
155
171
|
/** Server-side reason for the app's own logs. */
|
|
156
172
|
reason;
|
|
157
173
|
constructor(status, reason = "") {
|
|
158
|
-
super(status === 401 ? "unauthorized" : "forbidden");
|
|
174
|
+
super(status === 401 ? "unauthorized" : status === 403 ? "forbidden" : "unavailable");
|
|
159
175
|
this.name = "ZooKitAuthError";
|
|
160
176
|
this.status = status;
|
|
161
177
|
this.reason = reason;
|
|
@@ -173,6 +189,8 @@ export async function requirePerson(req, opts = {}) {
|
|
|
173
189
|
const who = await whoami(req, opts);
|
|
174
190
|
if (who.kind === "person")
|
|
175
191
|
return who;
|
|
192
|
+
if (who.kind === "anonymous" && who.unavailable)
|
|
193
|
+
throw new ZooKitAuthError(503, who.reason ?? "");
|
|
176
194
|
throw new ZooKitAuthError(401, who.kind === "anonymous" ? (who.reason ?? "") : "an app key is not a person");
|
|
177
195
|
}
|
|
178
196
|
/**
|
package/package.json
CHANGED