@voltro/protocol 0.32.0 → 0.34.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/CHANGELOG.md +2006 -0
- package/dist/auth-B6YZuSBr.js +303 -0
- package/dist/index.d.ts +1052 -27
- package/dist/index.js +267 -148
- package/dist/rest.d.ts +115 -3
- package/dist/rest.js +12 -12
- package/dist/serverErrorBus-BeN9pOpY.js +43 -0
- package/dist/session.d.ts +77 -16
- package/dist/session.js +62 -49
- package/package.json +5 -3
- package/dist/auth-CXMrvPyX.js +0 -90
- package/dist/serverErrorBus-C3JTqgIc.js +0 -157
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { Context as e, Effect as t, Schema as n } from "effect";
|
|
2
|
+
import { RpcMiddleware as r } from "@effect/rpc";
|
|
3
|
+
//#region src/scopes.ts
|
|
4
|
+
var i = "admin:full", a = "apikeys:issue:self", o = "apikeys:issue:org", s = "apikeys:issue:other", c = class extends n.TaggedError()("ScopeError", {
|
|
5
|
+
required: n.String,
|
|
6
|
+
message: n.String
|
|
7
|
+
}) {}, l = (e) => "scopes" in e && e.scopes ? e.scopes : [], u = /* @__PURE__ */ new WeakMap(), d = (e, t) => {
|
|
8
|
+
typeof e == "object" && e && u.set(e, t);
|
|
9
|
+
}, f = (e) => {
|
|
10
|
+
if (typeof e == "object" && e) {
|
|
11
|
+
let t = u.get(e);
|
|
12
|
+
if (t) return t;
|
|
13
|
+
}
|
|
14
|
+
return l(e);
|
|
15
|
+
}, p = (e, t) => {
|
|
16
|
+
let n = f(e);
|
|
17
|
+
return n.includes("admin:full") || n.includes(t);
|
|
18
|
+
}, ee = (e, t) => {
|
|
19
|
+
let n = l(e);
|
|
20
|
+
return n.includes("admin:full") || n.includes(t);
|
|
21
|
+
}, te = (e, n) => p(e, n) ? t.void : t.fail(new c({
|
|
22
|
+
required: n,
|
|
23
|
+
message: `missing required scope '${n}'`
|
|
24
|
+
})), m = (e) => typeof e.action == "string" && typeof e.resourceType == "string", h = (e) => typeof e.open == "string", g = (e) => ({ open: e }), _ = (e) => typeof e == "string" ? [e] : e, v, y = (e) => {
|
|
25
|
+
v = e;
|
|
26
|
+
}, b = (e) => v === void 0 ? e.filter((e) => (e.guards ?? []).some((e) => !m(e) && e.resource !== void 0)).map((e) => e.tag) : [], x = (e) => [
|
|
27
|
+
`${e.length} procedure${e.length === 1 ? "" : "s"} declare a per-resource guard that nothing enforces:`,
|
|
28
|
+
...e.slice(0, 10).map((e) => ` ${e}`),
|
|
29
|
+
...e.length > 10 ? [` … and ${e.length - 10} more`] : [],
|
|
30
|
+
"",
|
|
31
|
+
" `guards: [{ scope, resource }]` reads as \"may you do this to THIS resource\",",
|
|
32
|
+
" but with no resource-scope resolver registered the extractor is advisory and",
|
|
33
|
+
" the check runs against the caller's GLOBAL scopes. For an app whose",
|
|
34
|
+
" authorization is per-resource, that guard passes callers it should refuse.",
|
|
35
|
+
"",
|
|
36
|
+
" Fix it one of two ways:",
|
|
37
|
+
" • setResourceScopeResolver((req) => Effect<boolean>) — answers \"does this",
|
|
38
|
+
" subject hold `req.scope` on `req.resource`\" from your own tables.",
|
|
39
|
+
" • Drop the `resource` extractor if a global scope check is what you meant."
|
|
40
|
+
].join("\n"), S = () => v, C, ne = (e) => {
|
|
41
|
+
C = e;
|
|
42
|
+
}, w = () => C, T = (e) => new c({
|
|
43
|
+
required: "access-decision",
|
|
44
|
+
message: `${e ?? "this procedure"} declares no access decision: it has neither \`guards:\` nor \`openAccess:\`, and the app runs with \`security.defaultDeny\`. Add the scope(s) a caller must hold (\`guards: [{ scope: '…' }]\`), or declare it open on purpose with \`openAccess: '<why anyone may call this>'\`.`
|
|
45
|
+
}), E = (e, t, n) => {
|
|
46
|
+
if (!t || t.length === 0) return n?.defaultDeny === !0 ? T(n.procedure) : null;
|
|
47
|
+
for (let n of t) {
|
|
48
|
+
if (h(n)) continue;
|
|
49
|
+
if (m(n)) {
|
|
50
|
+
if (p(e, "admin:full")) continue;
|
|
51
|
+
return new c({
|
|
52
|
+
required: `${n.resourceType}:${n.action}`,
|
|
53
|
+
message: `relationship guard '${n.action}' on '${n.resourceType}' cannot be checked synchronously`
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
let t = _(n.scope);
|
|
57
|
+
if (t.length === 0) continue;
|
|
58
|
+
let r = t.filter((t) => p(e, t));
|
|
59
|
+
if (!((n.mode ?? "all") === "any" ? r.length > 0 : r.length === t.length)) {
|
|
60
|
+
let r = (n.mode ?? "all") === "any" ? t[0] : t.find((t) => !p(e, t));
|
|
61
|
+
return new c({
|
|
62
|
+
required: r,
|
|
63
|
+
message: (n.mode ?? "all") === "any" ? `missing all of required scopes [${t.join(", ")}]` : `missing required scope '${r}'`
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}, D = (e, n, r) => {
|
|
69
|
+
let i = (e) => new c({
|
|
70
|
+
required: `${n.resourceType}:${n.action}`,
|
|
71
|
+
message: e
|
|
72
|
+
});
|
|
73
|
+
if (p(e, "admin:full")) return t.succeed(null);
|
|
74
|
+
let a = C;
|
|
75
|
+
if (a === void 0) return t.succeed(i(`no policy resolver registered — cannot authorize '${n.action}' on '${n.resourceType}'`));
|
|
76
|
+
let o = n.resource(r);
|
|
77
|
+
return o === void 0 || o === "" ? t.succeed(i(`input does not identify a '${n.resourceType}' to authorize`)) : t.suspend(() => a({
|
|
78
|
+
subject: e,
|
|
79
|
+
action: n.action,
|
|
80
|
+
resourceType: n.resourceType,
|
|
81
|
+
resourceId: o
|
|
82
|
+
})).pipe(t.catchAllCause(() => t.succeed(!1)), t.map((e) => e ? null : i(`no '${n.action}' relation on ${n.resourceType}:${o}`)));
|
|
83
|
+
}, O = (e, n, r, i) => {
|
|
84
|
+
if (!n || n.length === 0) return t.succeed(E(e, n, i));
|
|
85
|
+
let a = v;
|
|
86
|
+
return !n.some(m) && (!a || !n.some((e) => e.resource !== void 0)) ? t.succeed(E(e, n, i)) : t.gen(function* () {
|
|
87
|
+
for (let i of n) {
|
|
88
|
+
if (h(i)) continue;
|
|
89
|
+
if (m(i)) {
|
|
90
|
+
let t = yield* D(e, i, r);
|
|
91
|
+
if (t) return t;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
let n = _(i.scope);
|
|
95
|
+
if (n.length === 0) continue;
|
|
96
|
+
let o = i.resource ? i.resource(r) : void 0, s = [];
|
|
97
|
+
for (let r of n) {
|
|
98
|
+
if (p(e, r)) {
|
|
99
|
+
s.push(!0);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (o !== void 0 && a !== void 0) {
|
|
103
|
+
let n = yield* t.suspend(() => a({
|
|
104
|
+
subject: e,
|
|
105
|
+
scope: r,
|
|
106
|
+
resource: o
|
|
107
|
+
})).pipe(t.catchAllCause(() => t.succeed(!1)));
|
|
108
|
+
s.push(n);
|
|
109
|
+
} else s.push(!1);
|
|
110
|
+
}
|
|
111
|
+
let l = i.mode ?? "all";
|
|
112
|
+
if (!(l === "any" ? s.some(Boolean) : s.every(Boolean))) {
|
|
113
|
+
let e = l === "any" ? n[0] : n[n.findIndex((e, t) => !s[t])];
|
|
114
|
+
return new c({
|
|
115
|
+
required: e,
|
|
116
|
+
message: l === "any" ? `missing all of required scopes [${n.join(", ")}]` : `missing required scope '${e}'` + (o === void 0 ? "" : ` on resource '${o}'`)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
});
|
|
122
|
+
}, k = n.Record({
|
|
123
|
+
key: n.String,
|
|
124
|
+
value: n.Unknown
|
|
125
|
+
}), A = n.optional(n.Array(n.String)), j = n.Struct({
|
|
126
|
+
type: n.Literal("user"),
|
|
127
|
+
id: n.String,
|
|
128
|
+
tenantId: n.String,
|
|
129
|
+
scopes: A,
|
|
130
|
+
metadata: n.optional(k)
|
|
131
|
+
}), M = n.Struct({
|
|
132
|
+
type: n.Literal("apiKey"),
|
|
133
|
+
id: n.String,
|
|
134
|
+
tenantId: n.String,
|
|
135
|
+
scopes: A,
|
|
136
|
+
metadata: n.optional(k)
|
|
137
|
+
}), N = n.Struct({
|
|
138
|
+
type: n.Literal("serviceAccount"),
|
|
139
|
+
id: n.String,
|
|
140
|
+
tenantId: n.String,
|
|
141
|
+
scopes: A,
|
|
142
|
+
metadata: n.optional(k)
|
|
143
|
+
}), P = n.Struct({
|
|
144
|
+
type: n.Literal("anonymous"),
|
|
145
|
+
id: n.Null,
|
|
146
|
+
tenantId: n.NullOr(n.String)
|
|
147
|
+
}), F = n.Struct({
|
|
148
|
+
type: n.Literal("system"),
|
|
149
|
+
id: n.String,
|
|
150
|
+
tenantId: n.Null,
|
|
151
|
+
scopes: A,
|
|
152
|
+
metadata: n.optional(k)
|
|
153
|
+
}), I = n.Union(j, M, N, P, F), L = n.Union(j.omit("scopes"), M.omit("scopes"), N.omit("scopes"), P, F.omit("scopes")), R = (e) => {
|
|
154
|
+
if (e.type === "anonymous") return e;
|
|
155
|
+
let { scopes: t, ...n } = e;
|
|
156
|
+
return n;
|
|
157
|
+
}, z = (e) => e.scopes ?? [], B = (e) => ({
|
|
158
|
+
type: "anonymous",
|
|
159
|
+
id: null,
|
|
160
|
+
tenantId: e
|
|
161
|
+
}), V = (e, t = ["admin:full"]) => ({
|
|
162
|
+
type: "system",
|
|
163
|
+
id: e,
|
|
164
|
+
tenantId: null,
|
|
165
|
+
scopes: t
|
|
166
|
+
}), H = (e, t) => {
|
|
167
|
+
let n = e;
|
|
168
|
+
return {
|
|
169
|
+
type: "serviceAccount",
|
|
170
|
+
id: e.id ?? "system",
|
|
171
|
+
tenantId: t,
|
|
172
|
+
scopes: n.scopes ?? ["admin:full"],
|
|
173
|
+
...n.metadata === void 0 ? {} : { metadata: n.metadata }
|
|
174
|
+
};
|
|
175
|
+
}, U = (e) => e.type === "system", W = class extends e.Tag("@voltro/Subject")() {}, G = class extends e.Tag("@voltro/ConnectionInfo")() {}, K = class extends r.Tag()("@voltro/AuthMiddleware", { provides: W }) {}, q = class extends r.Tag()("@voltro/ConnectionInfoMiddleware", { provides: G }) {}, J = (e) => typeof e.mountRoutes == "function", Y = (e) => Array.isArray(e) ? {
|
|
176
|
+
kind: "grant",
|
|
177
|
+
scopes: e
|
|
178
|
+
} : e, X = (e, t) => {
|
|
179
|
+
if (e.type === "anonymous") return {
|
|
180
|
+
subject: e,
|
|
181
|
+
removed: []
|
|
182
|
+
};
|
|
183
|
+
let n = z(e);
|
|
184
|
+
if (t.kind === "grant") {
|
|
185
|
+
if (t.scopes.length === 0) return {
|
|
186
|
+
subject: e,
|
|
187
|
+
removed: []
|
|
188
|
+
};
|
|
189
|
+
let r = [.../* @__PURE__ */ new Set([...n, ...t.scopes])];
|
|
190
|
+
return {
|
|
191
|
+
subject: {
|
|
192
|
+
...e,
|
|
193
|
+
scopes: r
|
|
194
|
+
},
|
|
195
|
+
removed: []
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
let r = new Set(t.scopes), i = n.filter((e) => !r.has(e));
|
|
199
|
+
return {
|
|
200
|
+
subject: {
|
|
201
|
+
...e,
|
|
202
|
+
scopes: [...r]
|
|
203
|
+
},
|
|
204
|
+
removed: i
|
|
205
|
+
};
|
|
206
|
+
}, re = 3e4, ie = 1e4, Z = (e) => `${e.type}\u0000${e.tenantId ?? ""}\u0000${e.id ?? ""}`, ae = () => {
|
|
207
|
+
if (typeof process > "u") return;
|
|
208
|
+
let e = process.env.VOLTRO_AUTH_SCOPE_CACHE_TTL_MS;
|
|
209
|
+
if (e === void 0 || e === "") return;
|
|
210
|
+
let t = Number(e);
|
|
211
|
+
return Number.isFinite(t) && t >= 0 ? t : void 0;
|
|
212
|
+
}, Q = (e = {}) => {
|
|
213
|
+
let t = e.ttlMs ?? ae() ?? 3e4, n = e.maxEntries ?? 1e4, r = e.now ?? Date.now, i = /* @__PURE__ */ new Map(), a = () => {
|
|
214
|
+
if (i.size <= n) return;
|
|
215
|
+
let e = r();
|
|
216
|
+
for (let [t, n] of i) n.freshUntil <= e && i.delete(t);
|
|
217
|
+
for (let e of i.keys()) {
|
|
218
|
+
if (i.size <= n) break;
|
|
219
|
+
i.delete(e);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
return {
|
|
223
|
+
ttlMs: t,
|
|
224
|
+
invalidate: (e) => {
|
|
225
|
+
i.delete(e);
|
|
226
|
+
},
|
|
227
|
+
invalidateAll: () => {
|
|
228
|
+
i.clear();
|
|
229
|
+
},
|
|
230
|
+
resolve: async (e, n) => {
|
|
231
|
+
let o = r();
|
|
232
|
+
if (t > 0) {
|
|
233
|
+
let t = i.get(e);
|
|
234
|
+
if (t && t.freshUntil > o) return {
|
|
235
|
+
decision: t.decision,
|
|
236
|
+
fresh: !1
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
let s = await n();
|
|
240
|
+
return t > 0 && s.kind !== "unavailable" && (i.set(e, {
|
|
241
|
+
decision: s,
|
|
242
|
+
freshUntil: o + t
|
|
243
|
+
}), a()), {
|
|
244
|
+
decision: s,
|
|
245
|
+
fresh: !0
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
}, oe = (e) => typeof e.resolve == "function", se = (e, t) => {
|
|
250
|
+
let n = t?.resolveScopes, r = n === void 0 || t?.scopeCache === !1 ? null : t?.scopeCache !== void 0 && oe(t.scopeCache) ? t.scopeCache : Q(t?.scopeCache ?? {});
|
|
251
|
+
return async (i) => {
|
|
252
|
+
let a = t?.getStore?.(), o = a === void 0 ? i : {
|
|
253
|
+
...i,
|
|
254
|
+
store: a
|
|
255
|
+
};
|
|
256
|
+
for (let i of e) {
|
|
257
|
+
let e = await i.resolve(o);
|
|
258
|
+
if (e.kind === "matched") {
|
|
259
|
+
let a = e.subject;
|
|
260
|
+
if (n !== void 0) {
|
|
261
|
+
let s = async () => Y(await n(e.subject, o)), { decision: c, fresh: l } = r === null ? {
|
|
262
|
+
decision: await s(),
|
|
263
|
+
fresh: !0
|
|
264
|
+
} : await r.resolve(Z(e.subject), s);
|
|
265
|
+
if (c.kind === "unavailable") {
|
|
266
|
+
let e = `scope resolution unavailable: ${c.reason}`;
|
|
267
|
+
throw t?.onStrategyFailed?.({
|
|
268
|
+
strategyId: i.id,
|
|
269
|
+
reason: e
|
|
270
|
+
}), new $({ reason: e });
|
|
271
|
+
}
|
|
272
|
+
let u = X(e.subject, c);
|
|
273
|
+
l && u.removed.length > 0 && t?.onScopesNarrowed?.({
|
|
274
|
+
strategyId: i.id,
|
|
275
|
+
subjectType: e.subject.type,
|
|
276
|
+
subjectId: e.subject.id,
|
|
277
|
+
removed: u.removed,
|
|
278
|
+
granted: c.scopes
|
|
279
|
+
}), a = u.subject;
|
|
280
|
+
}
|
|
281
|
+
return e.credentialExpiresAt === void 0 ? { subject: a } : {
|
|
282
|
+
subject: a,
|
|
283
|
+
credentialExpiresAt: e.credentialExpiresAt
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
if (e.kind === "failed") {
|
|
287
|
+
t?.onStrategyFailed?.({
|
|
288
|
+
strategyId: i.id,
|
|
289
|
+
reason: e.reason
|
|
290
|
+
});
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (t?.fallback) return { subject: await t.fallback(o) };
|
|
295
|
+
let s = o.headers["x-tenant"] ?? null;
|
|
296
|
+
if (s === null && t?.anonymousTenantRequired === !0) throw new $({ reason: "tenant required (x-tenant header missing)" });
|
|
297
|
+
return { subject: B(s) };
|
|
298
|
+
};
|
|
299
|
+
}, $ = class extends n.TaggedError()("Unauthenticated", { reason: n.optional(n.String) }) {}, ce = (e, t) => {
|
|
300
|
+
if (e.type === "anonymous") throw new $(t === void 0 ? {} : { reason: t });
|
|
301
|
+
};
|
|
302
|
+
//#endregion
|
|
303
|
+
export { b as A, d as B, s as C, E as D, x as E, h as F, y as H, m as I, T as L, S as M, p as N, O, ee as P, g as R, o as S, c as T, l as U, ne as V, Z as _, re as a, H as b, W as c, X as d, ce as f, Q as g, U as h, ie as i, w as j, f as k, $ as l, J as m, G as n, I as o, se as p, q as r, L as s, K as t, B as u, R as v, a as w, i as x, V as y, te as z };
|