@middag-io/licensing 0.1.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 +72 -0
- package/dist/build/index.d.ts +47 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +113 -0
- package/dist/build/index.js.map +1 -0
- package/dist/client/index.d.ts +74 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +912 -0
- package/dist/client/index.js.map +1 -0
- package/dist/contract/index.d.ts +138 -0
- package/dist/contract/index.d.ts.map +1 -0
- package/dist/contract/index.js +33 -0
- package/dist/contract/index.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/package.json +48 -0
|
@@ -0,0 +1,912 @@
|
|
|
1
|
+
import { CONTRACT_VERSION } from "../contract/index.js";
|
|
2
|
+
//#region node_modules/jose/dist/webapi/lib/buffer_utils.js
|
|
3
|
+
var encoder = new TextEncoder();
|
|
4
|
+
var decoder = new TextDecoder();
|
|
5
|
+
function concat(...buffers) {
|
|
6
|
+
const size = buffers.reduce((acc, { length }) => acc + length, 0);
|
|
7
|
+
const buf = new Uint8Array(size);
|
|
8
|
+
let i = 0;
|
|
9
|
+
for (const buffer of buffers) {
|
|
10
|
+
buf.set(buffer, i);
|
|
11
|
+
i += buffer.length;
|
|
12
|
+
}
|
|
13
|
+
return buf;
|
|
14
|
+
}
|
|
15
|
+
function encode(string) {
|
|
16
|
+
const bytes = new Uint8Array(string.length);
|
|
17
|
+
for (let i = 0; i < string.length; i++) {
|
|
18
|
+
const code = string.charCodeAt(i);
|
|
19
|
+
if (code > 127) throw new TypeError("non-ASCII string encountered in encode()");
|
|
20
|
+
bytes[i] = code;
|
|
21
|
+
}
|
|
22
|
+
return bytes;
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region node_modules/jose/dist/webapi/lib/base64.js
|
|
26
|
+
function decodeBase64(encoded) {
|
|
27
|
+
if (Uint8Array.fromBase64) return Uint8Array.fromBase64(encoded);
|
|
28
|
+
const binary = atob(encoded);
|
|
29
|
+
const bytes = new Uint8Array(binary.length);
|
|
30
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
31
|
+
return bytes;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region node_modules/jose/dist/webapi/util/base64url.js
|
|
35
|
+
function decode(input) {
|
|
36
|
+
if (Uint8Array.fromBase64) return Uint8Array.fromBase64(typeof input === "string" ? input : decoder.decode(input), { alphabet: "base64url" });
|
|
37
|
+
let encoded = input;
|
|
38
|
+
if (encoded instanceof Uint8Array) encoded = decoder.decode(encoded);
|
|
39
|
+
encoded = encoded.replace(/-/g, "+").replace(/_/g, "/");
|
|
40
|
+
try {
|
|
41
|
+
return decodeBase64(encoded);
|
|
42
|
+
} catch {
|
|
43
|
+
throw new TypeError("The input to be decoded is not correctly encoded.");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region node_modules/jose/dist/webapi/lib/crypto_key.js
|
|
48
|
+
var unusable = (name, prop = "algorithm.name") => /* @__PURE__ */ new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`);
|
|
49
|
+
var isAlgorithm = (algorithm, name) => algorithm.name === name;
|
|
50
|
+
function getHashLength(hash) {
|
|
51
|
+
return parseInt(hash.name.slice(4), 10);
|
|
52
|
+
}
|
|
53
|
+
function checkHashLength(algorithm, expected) {
|
|
54
|
+
if (getHashLength(algorithm.hash) !== expected) throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
55
|
+
}
|
|
56
|
+
function getNamedCurve(alg) {
|
|
57
|
+
switch (alg) {
|
|
58
|
+
case "ES256": return "P-256";
|
|
59
|
+
case "ES384": return "P-384";
|
|
60
|
+
case "ES512": return "P-521";
|
|
61
|
+
default: throw new Error("unreachable");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function checkUsage(key, usage) {
|
|
65
|
+
if (usage && !key.usages.includes(usage)) throw new TypeError(`CryptoKey does not support this operation, its usages must include ${usage}.`);
|
|
66
|
+
}
|
|
67
|
+
function checkSigCryptoKey(key, alg, usage) {
|
|
68
|
+
switch (alg) {
|
|
69
|
+
case "HS256":
|
|
70
|
+
case "HS384":
|
|
71
|
+
case "HS512":
|
|
72
|
+
if (!isAlgorithm(key.algorithm, "HMAC")) throw unusable("HMAC");
|
|
73
|
+
checkHashLength(key.algorithm, parseInt(alg.slice(2), 10));
|
|
74
|
+
break;
|
|
75
|
+
case "RS256":
|
|
76
|
+
case "RS384":
|
|
77
|
+
case "RS512":
|
|
78
|
+
if (!isAlgorithm(key.algorithm, "RSASSA-PKCS1-v1_5")) throw unusable("RSASSA-PKCS1-v1_5");
|
|
79
|
+
checkHashLength(key.algorithm, parseInt(alg.slice(2), 10));
|
|
80
|
+
break;
|
|
81
|
+
case "PS256":
|
|
82
|
+
case "PS384":
|
|
83
|
+
case "PS512":
|
|
84
|
+
if (!isAlgorithm(key.algorithm, "RSA-PSS")) throw unusable("RSA-PSS");
|
|
85
|
+
checkHashLength(key.algorithm, parseInt(alg.slice(2), 10));
|
|
86
|
+
break;
|
|
87
|
+
case "Ed25519":
|
|
88
|
+
case "EdDSA":
|
|
89
|
+
if (!isAlgorithm(key.algorithm, "Ed25519")) throw unusable("Ed25519");
|
|
90
|
+
break;
|
|
91
|
+
case "ML-DSA-44":
|
|
92
|
+
case "ML-DSA-65":
|
|
93
|
+
case "ML-DSA-87":
|
|
94
|
+
if (!isAlgorithm(key.algorithm, alg)) throw unusable(alg);
|
|
95
|
+
break;
|
|
96
|
+
case "ES256":
|
|
97
|
+
case "ES384":
|
|
98
|
+
case "ES512": {
|
|
99
|
+
if (!isAlgorithm(key.algorithm, "ECDSA")) throw unusable("ECDSA");
|
|
100
|
+
const expected = getNamedCurve(alg);
|
|
101
|
+
if (key.algorithm.namedCurve !== expected) throw unusable(expected, "algorithm.namedCurve");
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
default: throw new TypeError("CryptoKey does not support this operation");
|
|
105
|
+
}
|
|
106
|
+
checkUsage(key, usage);
|
|
107
|
+
}
|
|
108
|
+
//#endregion
|
|
109
|
+
//#region node_modules/jose/dist/webapi/lib/invalid_key_input.js
|
|
110
|
+
function message(msg, actual, ...types) {
|
|
111
|
+
types = types.filter(Boolean);
|
|
112
|
+
if (types.length > 2) {
|
|
113
|
+
const last = types.pop();
|
|
114
|
+
msg += `one of type ${types.join(", ")}, or ${last}.`;
|
|
115
|
+
} else if (types.length === 2) msg += `one of type ${types[0]} or ${types[1]}.`;
|
|
116
|
+
else msg += `of type ${types[0]}.`;
|
|
117
|
+
if (actual == null) msg += ` Received ${actual}`;
|
|
118
|
+
else if (typeof actual === "function" && actual.name) msg += ` Received function ${actual.name}`;
|
|
119
|
+
else if (typeof actual === "object" && actual != null) {
|
|
120
|
+
if (actual.constructor?.name) msg += ` Received an instance of ${actual.constructor.name}`;
|
|
121
|
+
}
|
|
122
|
+
return msg;
|
|
123
|
+
}
|
|
124
|
+
var invalidKeyInput = (actual, ...types) => message("Key must be ", actual, ...types);
|
|
125
|
+
var withAlg = (alg, actual, ...types) => message(`Key for the ${alg} algorithm must be `, actual, ...types);
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region node_modules/jose/dist/webapi/util/errors.js
|
|
128
|
+
var JOSEError = class extends Error {
|
|
129
|
+
static code = "ERR_JOSE_GENERIC";
|
|
130
|
+
code = "ERR_JOSE_GENERIC";
|
|
131
|
+
constructor(message, options) {
|
|
132
|
+
super(message, options);
|
|
133
|
+
this.name = this.constructor.name;
|
|
134
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
var JOSEAlgNotAllowed = class extends JOSEError {
|
|
138
|
+
static code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
139
|
+
code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
140
|
+
};
|
|
141
|
+
var JOSENotSupported = class extends JOSEError {
|
|
142
|
+
static code = "ERR_JOSE_NOT_SUPPORTED";
|
|
143
|
+
code = "ERR_JOSE_NOT_SUPPORTED";
|
|
144
|
+
};
|
|
145
|
+
var JWSInvalid = class extends JOSEError {
|
|
146
|
+
static code = "ERR_JWS_INVALID";
|
|
147
|
+
code = "ERR_JWS_INVALID";
|
|
148
|
+
};
|
|
149
|
+
var JWSSignatureVerificationFailed = class extends JOSEError {
|
|
150
|
+
static code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
151
|
+
code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
152
|
+
constructor(message = "signature verification failed", options) {
|
|
153
|
+
super(message, options);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region node_modules/jose/dist/webapi/lib/is_key_like.js
|
|
158
|
+
var isCryptoKey = (key) => {
|
|
159
|
+
if (key?.[Symbol.toStringTag] === "CryptoKey") return true;
|
|
160
|
+
try {
|
|
161
|
+
return key instanceof CryptoKey;
|
|
162
|
+
} catch {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
var isKeyObject = (key) => key?.[Symbol.toStringTag] === "KeyObject";
|
|
167
|
+
var isKeyLike = (key) => isCryptoKey(key) || isKeyObject(key);
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region node_modules/jose/dist/webapi/lib/helpers.js
|
|
170
|
+
function decodeBase64url(value, label, ErrorClass) {
|
|
171
|
+
try {
|
|
172
|
+
return decode(value);
|
|
173
|
+
} catch {
|
|
174
|
+
throw new ErrorClass(`Failed to base64url decode the ${label}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region node_modules/jose/dist/webapi/lib/type_checks.js
|
|
179
|
+
var isObjectLike = (value) => typeof value === "object" && value !== null;
|
|
180
|
+
function isObject(input) {
|
|
181
|
+
if (!isObjectLike(input) || Object.prototype.toString.call(input) !== "[object Object]") return false;
|
|
182
|
+
if (Object.getPrototypeOf(input) === null) return true;
|
|
183
|
+
let proto = input;
|
|
184
|
+
while (Object.getPrototypeOf(proto) !== null) proto = Object.getPrototypeOf(proto);
|
|
185
|
+
return Object.getPrototypeOf(input) === proto;
|
|
186
|
+
}
|
|
187
|
+
function isDisjoint(...headers) {
|
|
188
|
+
const sources = headers.filter(Boolean);
|
|
189
|
+
if (sources.length === 0 || sources.length === 1) return true;
|
|
190
|
+
let acc;
|
|
191
|
+
for (const header of sources) {
|
|
192
|
+
const parameters = Object.keys(header);
|
|
193
|
+
if (!acc || acc.size === 0) {
|
|
194
|
+
acc = new Set(parameters);
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
for (const parameter of parameters) {
|
|
198
|
+
if (acc.has(parameter)) return false;
|
|
199
|
+
acc.add(parameter);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
var isJWK = (key) => isObject(key) && typeof key.kty === "string";
|
|
205
|
+
var isPrivateJWK = (key) => key.kty !== "oct" && (key.kty === "AKP" && typeof key.priv === "string" || typeof key.d === "string");
|
|
206
|
+
var isPublicJWK = (key) => key.kty !== "oct" && key.d === void 0 && key.priv === void 0;
|
|
207
|
+
var isSecretJWK = (key) => key.kty === "oct" && typeof key.k === "string";
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region node_modules/jose/dist/webapi/lib/signing.js
|
|
210
|
+
function checkKeyLength(alg, key) {
|
|
211
|
+
if (alg.startsWith("RS") || alg.startsWith("PS")) {
|
|
212
|
+
const { modulusLength } = key.algorithm;
|
|
213
|
+
if (typeof modulusLength !== "number" || modulusLength < 2048) throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function subtleAlgorithm(alg, algorithm) {
|
|
217
|
+
const hash = `SHA-${alg.slice(-3)}`;
|
|
218
|
+
switch (alg) {
|
|
219
|
+
case "HS256":
|
|
220
|
+
case "HS384":
|
|
221
|
+
case "HS512": return {
|
|
222
|
+
hash,
|
|
223
|
+
name: "HMAC"
|
|
224
|
+
};
|
|
225
|
+
case "PS256":
|
|
226
|
+
case "PS384":
|
|
227
|
+
case "PS512": return {
|
|
228
|
+
hash,
|
|
229
|
+
name: "RSA-PSS",
|
|
230
|
+
saltLength: parseInt(alg.slice(-3), 10) >> 3
|
|
231
|
+
};
|
|
232
|
+
case "RS256":
|
|
233
|
+
case "RS384":
|
|
234
|
+
case "RS512": return {
|
|
235
|
+
hash,
|
|
236
|
+
name: "RSASSA-PKCS1-v1_5"
|
|
237
|
+
};
|
|
238
|
+
case "ES256":
|
|
239
|
+
case "ES384":
|
|
240
|
+
case "ES512": return {
|
|
241
|
+
hash,
|
|
242
|
+
name: "ECDSA",
|
|
243
|
+
namedCurve: algorithm.namedCurve
|
|
244
|
+
};
|
|
245
|
+
case "Ed25519":
|
|
246
|
+
case "EdDSA": return { name: "Ed25519" };
|
|
247
|
+
case "ML-DSA-44":
|
|
248
|
+
case "ML-DSA-65":
|
|
249
|
+
case "ML-DSA-87": return { name: alg };
|
|
250
|
+
default: throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async function getSigKey(alg, key, usage) {
|
|
254
|
+
if (key instanceof Uint8Array) {
|
|
255
|
+
if (!alg.startsWith("HS")) throw new TypeError(invalidKeyInput(key, "CryptoKey", "KeyObject", "JSON Web Key"));
|
|
256
|
+
return crypto.subtle.importKey("raw", key, {
|
|
257
|
+
hash: `SHA-${alg.slice(-3)}`,
|
|
258
|
+
name: "HMAC"
|
|
259
|
+
}, false, [usage]);
|
|
260
|
+
}
|
|
261
|
+
checkSigCryptoKey(key, alg, usage);
|
|
262
|
+
return key;
|
|
263
|
+
}
|
|
264
|
+
async function verify(alg, key, signature, data) {
|
|
265
|
+
const cryptoKey = await getSigKey(alg, key, "verify");
|
|
266
|
+
checkKeyLength(alg, cryptoKey);
|
|
267
|
+
const algorithm = subtleAlgorithm(alg, cryptoKey.algorithm);
|
|
268
|
+
try {
|
|
269
|
+
return await crypto.subtle.verify(algorithm, cryptoKey, signature, data);
|
|
270
|
+
} catch {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
//#endregion
|
|
275
|
+
//#region node_modules/jose/dist/webapi/lib/jwk_to_key.js
|
|
276
|
+
var unsupportedAlg = "Invalid or unsupported JWK \"alg\" (Algorithm) Parameter value";
|
|
277
|
+
function subtleMapping(jwk) {
|
|
278
|
+
let algorithm;
|
|
279
|
+
let keyUsages;
|
|
280
|
+
switch (jwk.kty) {
|
|
281
|
+
case "AKP":
|
|
282
|
+
switch (jwk.alg) {
|
|
283
|
+
case "ML-DSA-44":
|
|
284
|
+
case "ML-DSA-65":
|
|
285
|
+
case "ML-DSA-87":
|
|
286
|
+
algorithm = { name: jwk.alg };
|
|
287
|
+
keyUsages = jwk.priv ? ["sign"] : ["verify"];
|
|
288
|
+
break;
|
|
289
|
+
default: throw new JOSENotSupported(unsupportedAlg);
|
|
290
|
+
}
|
|
291
|
+
break;
|
|
292
|
+
case "RSA":
|
|
293
|
+
switch (jwk.alg) {
|
|
294
|
+
case "PS256":
|
|
295
|
+
case "PS384":
|
|
296
|
+
case "PS512":
|
|
297
|
+
algorithm = {
|
|
298
|
+
name: "RSA-PSS",
|
|
299
|
+
hash: `SHA-${jwk.alg.slice(-3)}`
|
|
300
|
+
};
|
|
301
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
302
|
+
break;
|
|
303
|
+
case "RS256":
|
|
304
|
+
case "RS384":
|
|
305
|
+
case "RS512":
|
|
306
|
+
algorithm = {
|
|
307
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
308
|
+
hash: `SHA-${jwk.alg.slice(-3)}`
|
|
309
|
+
};
|
|
310
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
311
|
+
break;
|
|
312
|
+
case "RSA-OAEP":
|
|
313
|
+
case "RSA-OAEP-256":
|
|
314
|
+
case "RSA-OAEP-384":
|
|
315
|
+
case "RSA-OAEP-512":
|
|
316
|
+
algorithm = {
|
|
317
|
+
name: "RSA-OAEP",
|
|
318
|
+
hash: `SHA-${parseInt(jwk.alg.slice(-3), 10) || 1}`
|
|
319
|
+
};
|
|
320
|
+
keyUsages = jwk.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
|
|
321
|
+
break;
|
|
322
|
+
default: throw new JOSENotSupported(unsupportedAlg);
|
|
323
|
+
}
|
|
324
|
+
break;
|
|
325
|
+
case "EC":
|
|
326
|
+
switch (jwk.alg) {
|
|
327
|
+
case "ES256":
|
|
328
|
+
case "ES384":
|
|
329
|
+
case "ES512":
|
|
330
|
+
algorithm = {
|
|
331
|
+
name: "ECDSA",
|
|
332
|
+
namedCurve: {
|
|
333
|
+
ES256: "P-256",
|
|
334
|
+
ES384: "P-384",
|
|
335
|
+
ES512: "P-521"
|
|
336
|
+
}[jwk.alg]
|
|
337
|
+
};
|
|
338
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
339
|
+
break;
|
|
340
|
+
case "ECDH-ES":
|
|
341
|
+
case "ECDH-ES+A128KW":
|
|
342
|
+
case "ECDH-ES+A192KW":
|
|
343
|
+
case "ECDH-ES+A256KW":
|
|
344
|
+
algorithm = {
|
|
345
|
+
name: "ECDH",
|
|
346
|
+
namedCurve: jwk.crv
|
|
347
|
+
};
|
|
348
|
+
keyUsages = jwk.d ? ["deriveBits"] : [];
|
|
349
|
+
break;
|
|
350
|
+
default: throw new JOSENotSupported(unsupportedAlg);
|
|
351
|
+
}
|
|
352
|
+
break;
|
|
353
|
+
case "OKP":
|
|
354
|
+
switch (jwk.alg) {
|
|
355
|
+
case "Ed25519":
|
|
356
|
+
case "EdDSA":
|
|
357
|
+
algorithm = { name: "Ed25519" };
|
|
358
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
359
|
+
break;
|
|
360
|
+
case "ECDH-ES":
|
|
361
|
+
case "ECDH-ES+A128KW":
|
|
362
|
+
case "ECDH-ES+A192KW":
|
|
363
|
+
case "ECDH-ES+A256KW":
|
|
364
|
+
algorithm = { name: jwk.crv };
|
|
365
|
+
keyUsages = jwk.d ? ["deriveBits"] : [];
|
|
366
|
+
break;
|
|
367
|
+
default: throw new JOSENotSupported(unsupportedAlg);
|
|
368
|
+
}
|
|
369
|
+
break;
|
|
370
|
+
default: throw new JOSENotSupported("Invalid or unsupported JWK \"kty\" (Key Type) Parameter value");
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
algorithm,
|
|
374
|
+
keyUsages
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
async function jwkToKey(jwk) {
|
|
378
|
+
if (!jwk.alg) throw new TypeError("\"alg\" argument is required when \"jwk.alg\" is not present");
|
|
379
|
+
const { algorithm, keyUsages } = subtleMapping(jwk);
|
|
380
|
+
const keyData = { ...jwk };
|
|
381
|
+
if (keyData.kty !== "AKP") delete keyData.alg;
|
|
382
|
+
delete keyData.use;
|
|
383
|
+
return crypto.subtle.importKey("jwk", keyData, algorithm, jwk.ext ?? (jwk.d || jwk.priv ? false : true), jwk.key_ops ?? keyUsages);
|
|
384
|
+
}
|
|
385
|
+
//#endregion
|
|
386
|
+
//#region node_modules/jose/dist/webapi/lib/normalize_key.js
|
|
387
|
+
var unusableForAlg = "given KeyObject instance cannot be used for this algorithm";
|
|
388
|
+
var cache;
|
|
389
|
+
var handleJWK = async (key, jwk, alg, freeze = false) => {
|
|
390
|
+
cache ||= /* @__PURE__ */ new WeakMap();
|
|
391
|
+
let cached = cache.get(key);
|
|
392
|
+
if (cached?.[alg]) return cached[alg];
|
|
393
|
+
const cryptoKey = await jwkToKey({
|
|
394
|
+
...jwk,
|
|
395
|
+
alg
|
|
396
|
+
});
|
|
397
|
+
if (freeze) Object.freeze(key);
|
|
398
|
+
if (!cached) cache.set(key, { [alg]: cryptoKey });
|
|
399
|
+
else cached[alg] = cryptoKey;
|
|
400
|
+
return cryptoKey;
|
|
401
|
+
};
|
|
402
|
+
var handleKeyObject = (keyObject, alg) => {
|
|
403
|
+
cache ||= /* @__PURE__ */ new WeakMap();
|
|
404
|
+
let cached = cache.get(keyObject);
|
|
405
|
+
if (cached?.[alg]) return cached[alg];
|
|
406
|
+
const isPublic = keyObject.type === "public";
|
|
407
|
+
const extractable = isPublic ? true : false;
|
|
408
|
+
let cryptoKey;
|
|
409
|
+
if (keyObject.asymmetricKeyType === "x25519") {
|
|
410
|
+
switch (alg) {
|
|
411
|
+
case "ECDH-ES":
|
|
412
|
+
case "ECDH-ES+A128KW":
|
|
413
|
+
case "ECDH-ES+A192KW":
|
|
414
|
+
case "ECDH-ES+A256KW": break;
|
|
415
|
+
default: throw new TypeError(unusableForAlg);
|
|
416
|
+
}
|
|
417
|
+
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, isPublic ? [] : ["deriveBits"]);
|
|
418
|
+
}
|
|
419
|
+
if (keyObject.asymmetricKeyType === "ed25519") {
|
|
420
|
+
if (alg !== "EdDSA" && alg !== "Ed25519") throw new TypeError(unusableForAlg);
|
|
421
|
+
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, [isPublic ? "verify" : "sign"]);
|
|
422
|
+
}
|
|
423
|
+
switch (keyObject.asymmetricKeyType) {
|
|
424
|
+
case "ml-dsa-44":
|
|
425
|
+
case "ml-dsa-65":
|
|
426
|
+
case "ml-dsa-87":
|
|
427
|
+
if (alg !== keyObject.asymmetricKeyType.toUpperCase()) throw new TypeError(unusableForAlg);
|
|
428
|
+
cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, [isPublic ? "verify" : "sign"]);
|
|
429
|
+
}
|
|
430
|
+
if (keyObject.asymmetricKeyType === "rsa") {
|
|
431
|
+
let hash;
|
|
432
|
+
switch (alg) {
|
|
433
|
+
case "RSA-OAEP":
|
|
434
|
+
hash = "SHA-1";
|
|
435
|
+
break;
|
|
436
|
+
case "RS256":
|
|
437
|
+
case "PS256":
|
|
438
|
+
case "RSA-OAEP-256":
|
|
439
|
+
hash = "SHA-256";
|
|
440
|
+
break;
|
|
441
|
+
case "RS384":
|
|
442
|
+
case "PS384":
|
|
443
|
+
case "RSA-OAEP-384":
|
|
444
|
+
hash = "SHA-384";
|
|
445
|
+
break;
|
|
446
|
+
case "RS512":
|
|
447
|
+
case "PS512":
|
|
448
|
+
case "RSA-OAEP-512":
|
|
449
|
+
hash = "SHA-512";
|
|
450
|
+
break;
|
|
451
|
+
default: throw new TypeError(unusableForAlg);
|
|
452
|
+
}
|
|
453
|
+
if (alg.startsWith("RSA-OAEP")) return keyObject.toCryptoKey({
|
|
454
|
+
name: "RSA-OAEP",
|
|
455
|
+
hash
|
|
456
|
+
}, extractable, isPublic ? ["encrypt"] : ["decrypt"]);
|
|
457
|
+
cryptoKey = keyObject.toCryptoKey({
|
|
458
|
+
name: alg.startsWith("PS") ? "RSA-PSS" : "RSASSA-PKCS1-v1_5",
|
|
459
|
+
hash
|
|
460
|
+
}, extractable, [isPublic ? "verify" : "sign"]);
|
|
461
|
+
}
|
|
462
|
+
if (keyObject.asymmetricKeyType === "ec") {
|
|
463
|
+
const namedCurve = new Map([
|
|
464
|
+
["prime256v1", "P-256"],
|
|
465
|
+
["secp384r1", "P-384"],
|
|
466
|
+
["secp521r1", "P-521"]
|
|
467
|
+
]).get(keyObject.asymmetricKeyDetails?.namedCurve);
|
|
468
|
+
if (!namedCurve) throw new TypeError(unusableForAlg);
|
|
469
|
+
const expectedCurve = {
|
|
470
|
+
ES256: "P-256",
|
|
471
|
+
ES384: "P-384",
|
|
472
|
+
ES512: "P-521"
|
|
473
|
+
};
|
|
474
|
+
if (expectedCurve[alg] && namedCurve === expectedCurve[alg]) cryptoKey = keyObject.toCryptoKey({
|
|
475
|
+
name: "ECDSA",
|
|
476
|
+
namedCurve
|
|
477
|
+
}, extractable, [isPublic ? "verify" : "sign"]);
|
|
478
|
+
if (alg.startsWith("ECDH-ES")) cryptoKey = keyObject.toCryptoKey({
|
|
479
|
+
name: "ECDH",
|
|
480
|
+
namedCurve
|
|
481
|
+
}, extractable, isPublic ? [] : ["deriveBits"]);
|
|
482
|
+
}
|
|
483
|
+
if (!cryptoKey) throw new TypeError(unusableForAlg);
|
|
484
|
+
if (!cached) cache.set(keyObject, { [alg]: cryptoKey });
|
|
485
|
+
else cached[alg] = cryptoKey;
|
|
486
|
+
return cryptoKey;
|
|
487
|
+
};
|
|
488
|
+
async function normalizeKey(key, alg) {
|
|
489
|
+
if (key instanceof Uint8Array) return key;
|
|
490
|
+
if (isCryptoKey(key)) return key;
|
|
491
|
+
if (isKeyObject(key)) {
|
|
492
|
+
if (key.type === "secret") return key.export();
|
|
493
|
+
if ("toCryptoKey" in key && typeof key.toCryptoKey === "function") try {
|
|
494
|
+
return handleKeyObject(key, alg);
|
|
495
|
+
} catch (err) {
|
|
496
|
+
if (err instanceof TypeError) throw err;
|
|
497
|
+
}
|
|
498
|
+
return handleJWK(key, key.export({ format: "jwk" }), alg);
|
|
499
|
+
}
|
|
500
|
+
if (isJWK(key)) {
|
|
501
|
+
if (key.k) return decode(key.k);
|
|
502
|
+
return handleJWK(key, key, alg, true);
|
|
503
|
+
}
|
|
504
|
+
throw new Error("unreachable");
|
|
505
|
+
}
|
|
506
|
+
//#endregion
|
|
507
|
+
//#region node_modules/jose/dist/webapi/key/import.js
|
|
508
|
+
async function importJWK(jwk, alg, options) {
|
|
509
|
+
if (!isObject(jwk)) throw new TypeError("JWK must be an object");
|
|
510
|
+
let ext;
|
|
511
|
+
alg ??= jwk.alg;
|
|
512
|
+
ext ??= options?.extractable ?? jwk.ext;
|
|
513
|
+
switch (jwk.kty) {
|
|
514
|
+
case "oct":
|
|
515
|
+
if (typeof jwk.k !== "string" || !jwk.k) throw new TypeError("missing \"k\" (Key Value) Parameter value");
|
|
516
|
+
return decode(jwk.k);
|
|
517
|
+
case "RSA":
|
|
518
|
+
if ("oth" in jwk && jwk.oth !== void 0) throw new JOSENotSupported("RSA JWK \"oth\" (Other Primes Info) Parameter value is not supported");
|
|
519
|
+
return jwkToKey({
|
|
520
|
+
...jwk,
|
|
521
|
+
alg,
|
|
522
|
+
ext
|
|
523
|
+
});
|
|
524
|
+
case "AKP":
|
|
525
|
+
if (typeof jwk.alg !== "string" || !jwk.alg) throw new TypeError("missing \"alg\" (Algorithm) Parameter value");
|
|
526
|
+
if (alg !== void 0 && alg !== jwk.alg) throw new TypeError("JWK alg and alg option value mismatch");
|
|
527
|
+
return jwkToKey({
|
|
528
|
+
...jwk,
|
|
529
|
+
ext
|
|
530
|
+
});
|
|
531
|
+
case "EC":
|
|
532
|
+
case "OKP": return jwkToKey({
|
|
533
|
+
...jwk,
|
|
534
|
+
alg,
|
|
535
|
+
ext
|
|
536
|
+
});
|
|
537
|
+
default: throw new JOSENotSupported("Unsupported \"kty\" (Key Type) Parameter value");
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
//#endregion
|
|
541
|
+
//#region node_modules/jose/dist/webapi/lib/validate_crit.js
|
|
542
|
+
function validateCrit(Err, recognizedDefault, recognizedOption, protectedHeader, joseHeader) {
|
|
543
|
+
if (joseHeader.crit !== void 0 && protectedHeader?.crit === void 0) throw new Err("\"crit\" (Critical) Header Parameter MUST be integrity protected");
|
|
544
|
+
if (!protectedHeader || protectedHeader.crit === void 0) return /* @__PURE__ */ new Set();
|
|
545
|
+
if (!Array.isArray(protectedHeader.crit) || protectedHeader.crit.length === 0 || protectedHeader.crit.some((input) => typeof input !== "string" || input.length === 0)) throw new Err("\"crit\" (Critical) Header Parameter MUST be an array of non-empty strings when present");
|
|
546
|
+
let recognized;
|
|
547
|
+
if (recognizedOption !== void 0) recognized = new Map([...Object.entries(recognizedOption), ...recognizedDefault.entries()]);
|
|
548
|
+
else recognized = recognizedDefault;
|
|
549
|
+
for (const parameter of protectedHeader.crit) {
|
|
550
|
+
if (!recognized.has(parameter)) throw new JOSENotSupported(`Extension Header Parameter "${parameter}" is not recognized`);
|
|
551
|
+
if (joseHeader[parameter] === void 0) throw new Err(`Extension Header Parameter "${parameter}" is missing`);
|
|
552
|
+
if (recognized.get(parameter) && protectedHeader[parameter] === void 0) throw new Err(`Extension Header Parameter "${parameter}" MUST be integrity protected`);
|
|
553
|
+
}
|
|
554
|
+
return new Set(protectedHeader.crit);
|
|
555
|
+
}
|
|
556
|
+
//#endregion
|
|
557
|
+
//#region node_modules/jose/dist/webapi/lib/validate_algorithms.js
|
|
558
|
+
function validateAlgorithms(option, algorithms) {
|
|
559
|
+
if (algorithms !== void 0 && (!Array.isArray(algorithms) || algorithms.some((s) => typeof s !== "string"))) throw new TypeError(`"${option}" option must be an array of strings`);
|
|
560
|
+
if (!algorithms) return;
|
|
561
|
+
return new Set(algorithms);
|
|
562
|
+
}
|
|
563
|
+
//#endregion
|
|
564
|
+
//#region node_modules/jose/dist/webapi/lib/check_key_type.js
|
|
565
|
+
var tag = (key) => key?.[Symbol.toStringTag];
|
|
566
|
+
var jwkMatchesOp = (alg, key, usage) => {
|
|
567
|
+
if (key.use !== void 0) {
|
|
568
|
+
let expected;
|
|
569
|
+
switch (usage) {
|
|
570
|
+
case "sign":
|
|
571
|
+
case "verify":
|
|
572
|
+
expected = "sig";
|
|
573
|
+
break;
|
|
574
|
+
case "encrypt":
|
|
575
|
+
case "decrypt":
|
|
576
|
+
expected = "enc";
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
if (key.use !== expected) throw new TypeError(`Invalid key for this operation, its "use" must be "${expected}" when present`);
|
|
580
|
+
}
|
|
581
|
+
if (key.alg !== void 0 && key.alg !== alg) throw new TypeError(`Invalid key for this operation, its "alg" must be "${alg}" when present`);
|
|
582
|
+
if (Array.isArray(key.key_ops)) {
|
|
583
|
+
let expectedKeyOp;
|
|
584
|
+
switch (true) {
|
|
585
|
+
case usage === "sign" || usage === "verify":
|
|
586
|
+
case alg === "dir":
|
|
587
|
+
case alg.includes("CBC-HS"):
|
|
588
|
+
expectedKeyOp = usage;
|
|
589
|
+
break;
|
|
590
|
+
case alg.startsWith("PBES2"):
|
|
591
|
+
expectedKeyOp = "deriveBits";
|
|
592
|
+
break;
|
|
593
|
+
case /^A\d{3}(?:GCM)?(?:KW)?$/.test(alg):
|
|
594
|
+
if (!alg.includes("GCM") && alg.endsWith("KW")) expectedKeyOp = usage === "encrypt" ? "wrapKey" : "unwrapKey";
|
|
595
|
+
else expectedKeyOp = usage;
|
|
596
|
+
break;
|
|
597
|
+
case usage === "encrypt" && alg.startsWith("RSA"):
|
|
598
|
+
expectedKeyOp = "wrapKey";
|
|
599
|
+
break;
|
|
600
|
+
case usage === "decrypt":
|
|
601
|
+
expectedKeyOp = alg.startsWith("RSA") ? "unwrapKey" : "deriveBits";
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
if (expectedKeyOp && key.key_ops?.includes?.(expectedKeyOp) === false) throw new TypeError(`Invalid key for this operation, its "key_ops" must include "${expectedKeyOp}" when present`);
|
|
605
|
+
}
|
|
606
|
+
return true;
|
|
607
|
+
};
|
|
608
|
+
var symmetricTypeCheck = (alg, key, usage) => {
|
|
609
|
+
if (key instanceof Uint8Array) return;
|
|
610
|
+
if (isJWK(key)) {
|
|
611
|
+
if (isSecretJWK(key) && jwkMatchesOp(alg, key, usage)) return;
|
|
612
|
+
throw new TypeError(`JSON Web Key for symmetric algorithms must have JWK "kty" (Key Type) equal to "oct" and the JWK "k" (Key Value) present`);
|
|
613
|
+
}
|
|
614
|
+
if (!isKeyLike(key)) throw new TypeError(withAlg(alg, key, "CryptoKey", "KeyObject", "JSON Web Key", "Uint8Array"));
|
|
615
|
+
if (key.type !== "secret") throw new TypeError(`${tag(key)} instances for symmetric algorithms must be of type "secret"`);
|
|
616
|
+
};
|
|
617
|
+
var asymmetricTypeCheck = (alg, key, usage) => {
|
|
618
|
+
if (isJWK(key)) switch (usage) {
|
|
619
|
+
case "decrypt":
|
|
620
|
+
case "sign":
|
|
621
|
+
if (isPrivateJWK(key) && jwkMatchesOp(alg, key, usage)) return;
|
|
622
|
+
throw new TypeError(`JSON Web Key for this operation must be a private JWK`);
|
|
623
|
+
case "encrypt":
|
|
624
|
+
case "verify":
|
|
625
|
+
if (isPublicJWK(key) && jwkMatchesOp(alg, key, usage)) return;
|
|
626
|
+
throw new TypeError(`JSON Web Key for this operation must be a public JWK`);
|
|
627
|
+
}
|
|
628
|
+
if (!isKeyLike(key)) throw new TypeError(withAlg(alg, key, "CryptoKey", "KeyObject", "JSON Web Key"));
|
|
629
|
+
if (key.type === "secret") throw new TypeError(`${tag(key)} instances for asymmetric algorithms must not be of type "secret"`);
|
|
630
|
+
if (key.type === "public") switch (usage) {
|
|
631
|
+
case "sign": throw new TypeError(`${tag(key)} instances for asymmetric algorithm signing must be of type "private"`);
|
|
632
|
+
case "decrypt": throw new TypeError(`${tag(key)} instances for asymmetric algorithm decryption must be of type "private"`);
|
|
633
|
+
}
|
|
634
|
+
if (key.type === "private") switch (usage) {
|
|
635
|
+
case "verify": throw new TypeError(`${tag(key)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
636
|
+
case "encrypt": throw new TypeError(`${tag(key)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
function checkKeyType(alg, key, usage) {
|
|
640
|
+
switch (alg.substring(0, 2)) {
|
|
641
|
+
case "A1":
|
|
642
|
+
case "A2":
|
|
643
|
+
case "di":
|
|
644
|
+
case "HS":
|
|
645
|
+
case "PB":
|
|
646
|
+
symmetricTypeCheck(alg, key, usage);
|
|
647
|
+
break;
|
|
648
|
+
default: asymmetricTypeCheck(alg, key, usage);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
//#endregion
|
|
652
|
+
//#region node_modules/jose/dist/webapi/jws/flattened/verify.js
|
|
653
|
+
async function flattenedVerify(jws, key, options) {
|
|
654
|
+
if (!isObject(jws)) throw new JWSInvalid("Flattened JWS must be an object");
|
|
655
|
+
if (jws.protected === void 0 && jws.header === void 0) throw new JWSInvalid("Flattened JWS must have either of the \"protected\" or \"header\" members");
|
|
656
|
+
if (jws.protected !== void 0 && typeof jws.protected !== "string") throw new JWSInvalid("JWS Protected Header incorrect type");
|
|
657
|
+
if (jws.payload === void 0) throw new JWSInvalid("JWS Payload missing");
|
|
658
|
+
if (typeof jws.signature !== "string") throw new JWSInvalid("JWS Signature missing or incorrect type");
|
|
659
|
+
if (jws.header !== void 0 && !isObject(jws.header)) throw new JWSInvalid("JWS Unprotected Header incorrect type");
|
|
660
|
+
let parsedProt = {};
|
|
661
|
+
if (jws.protected) try {
|
|
662
|
+
const protectedHeader = decode(jws.protected);
|
|
663
|
+
parsedProt = JSON.parse(decoder.decode(protectedHeader));
|
|
664
|
+
} catch {
|
|
665
|
+
throw new JWSInvalid("JWS Protected Header is invalid");
|
|
666
|
+
}
|
|
667
|
+
if (!isDisjoint(parsedProt, jws.header)) throw new JWSInvalid("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
|
|
668
|
+
const joseHeader = {
|
|
669
|
+
...parsedProt,
|
|
670
|
+
...jws.header
|
|
671
|
+
};
|
|
672
|
+
const extensions = validateCrit(JWSInvalid, new Map([["b64", true]]), options?.crit, parsedProt, joseHeader);
|
|
673
|
+
let b64 = true;
|
|
674
|
+
if (extensions.has("b64")) {
|
|
675
|
+
b64 = parsedProt.b64;
|
|
676
|
+
if (typeof b64 !== "boolean") throw new JWSInvalid("The \"b64\" (base64url-encode payload) Header Parameter must be a boolean");
|
|
677
|
+
}
|
|
678
|
+
const { alg } = joseHeader;
|
|
679
|
+
if (typeof alg !== "string" || !alg) throw new JWSInvalid("JWS \"alg\" (Algorithm) Header Parameter missing or invalid");
|
|
680
|
+
const algorithms = options && validateAlgorithms("algorithms", options.algorithms);
|
|
681
|
+
if (algorithms && !algorithms.has(alg)) throw new JOSEAlgNotAllowed("\"alg\" (Algorithm) Header Parameter value not allowed");
|
|
682
|
+
if (b64) {
|
|
683
|
+
if (typeof jws.payload !== "string") throw new JWSInvalid("JWS Payload must be a string");
|
|
684
|
+
} else if (typeof jws.payload !== "string" && !(jws.payload instanceof Uint8Array)) throw new JWSInvalid("JWS Payload must be a string or an Uint8Array instance");
|
|
685
|
+
let resolvedKey = false;
|
|
686
|
+
if (typeof key === "function") {
|
|
687
|
+
key = await key(parsedProt, jws);
|
|
688
|
+
resolvedKey = true;
|
|
689
|
+
}
|
|
690
|
+
checkKeyType(alg, key, "verify");
|
|
691
|
+
const data = concat(jws.protected !== void 0 ? encode(jws.protected) : new Uint8Array(), encode("."), typeof jws.payload === "string" ? b64 ? encode(jws.payload) : encoder.encode(jws.payload) : jws.payload);
|
|
692
|
+
const signature = decodeBase64url(jws.signature, "signature", JWSInvalid);
|
|
693
|
+
const k = await normalizeKey(key, alg);
|
|
694
|
+
if (!await verify(alg, k, signature, data)) throw new JWSSignatureVerificationFailed();
|
|
695
|
+
let payload;
|
|
696
|
+
if (b64) payload = decodeBase64url(jws.payload, "payload", JWSInvalid);
|
|
697
|
+
else if (typeof jws.payload === "string") payload = encoder.encode(jws.payload);
|
|
698
|
+
else payload = jws.payload;
|
|
699
|
+
const result = { payload };
|
|
700
|
+
if (jws.protected !== void 0) result.protectedHeader = parsedProt;
|
|
701
|
+
if (jws.header !== void 0) result.unprotectedHeader = jws.header;
|
|
702
|
+
if (resolvedKey) return {
|
|
703
|
+
...result,
|
|
704
|
+
key: k
|
|
705
|
+
};
|
|
706
|
+
return result;
|
|
707
|
+
}
|
|
708
|
+
//#endregion
|
|
709
|
+
//#region node_modules/jose/dist/webapi/jws/compact/verify.js
|
|
710
|
+
async function compactVerify(jws, key, options) {
|
|
711
|
+
if (jws instanceof Uint8Array) jws = decoder.decode(jws);
|
|
712
|
+
if (typeof jws !== "string") throw new JWSInvalid("Compact JWS must be a string or Uint8Array");
|
|
713
|
+
const { 0: protectedHeader, 1: payload, 2: signature, length } = jws.split(".");
|
|
714
|
+
if (length !== 3) throw new JWSInvalid("Invalid Compact JWS");
|
|
715
|
+
const verified = await flattenedVerify({
|
|
716
|
+
payload,
|
|
717
|
+
protected: protectedHeader,
|
|
718
|
+
signature
|
|
719
|
+
}, key, options);
|
|
720
|
+
const result = {
|
|
721
|
+
payload: verified.payload,
|
|
722
|
+
protectedHeader: verified.protectedHeader
|
|
723
|
+
};
|
|
724
|
+
if (typeof key === "function") return {
|
|
725
|
+
...result,
|
|
726
|
+
key: verified.key
|
|
727
|
+
};
|
|
728
|
+
return result;
|
|
729
|
+
}
|
|
730
|
+
//#endregion
|
|
731
|
+
//#region src/client/index.ts
|
|
732
|
+
/**
|
|
733
|
+
* Runtime loader for @middag-io/licensing.
|
|
734
|
+
*
|
|
735
|
+
* Runs inside the consuming host (Moodle, WordPress, ...). The browser never
|
|
736
|
+
* calls the worker's HMAC-protected `/v1/protected/manifest` endpoint — the
|
|
737
|
+
* PHP host bridge does that, persists the `ManifestResponse`, and serves it
|
|
738
|
+
* to the browser at `manifest_url` (per ADR-012 §"Server-side host bridge").
|
|
739
|
+
*
|
|
740
|
+
* This loader:
|
|
741
|
+
*
|
|
742
|
+
* 1. Fetches the manifest JSON from the host-served URL (or via a caller-
|
|
743
|
+
* supplied `fetchManifest` for embedded use cases).
|
|
744
|
+
* 2. Verifies the JWS signature against the worker's JWKS endpoint
|
|
745
|
+
* `${workerOrigin}/api/protected/jwks` (cached in-memory per kid).
|
|
746
|
+
* 3. Validates the decoded payload against the bootstrap context
|
|
747
|
+
* (`install_id`, `host_origin`, `host_type`, `product`, `contract_version`)
|
|
748
|
+
* so a manifest swapped between installs is rejected client-side.
|
|
749
|
+
* 4. Loads a requested module by fetching its CDN URL, recomputing the
|
|
750
|
+
* SRI hash against the bytes, and dynamic-importing from a Blob URL.
|
|
751
|
+
* Native `import()` does not yet take an integrity attribute, so we
|
|
752
|
+
* compute SRI ourselves before handing the bytes to the JS engine.
|
|
753
|
+
*/
|
|
754
|
+
var JWKS_PATH = "/api/protected/jwks";
|
|
755
|
+
var JwksCache = class {
|
|
756
|
+
fetcher;
|
|
757
|
+
endpoint;
|
|
758
|
+
staticDoc;
|
|
759
|
+
byKid = /* @__PURE__ */ new Map();
|
|
760
|
+
constructor(fetcher, endpoint, staticDoc) {
|
|
761
|
+
this.fetcher = fetcher;
|
|
762
|
+
this.endpoint = endpoint;
|
|
763
|
+
this.staticDoc = staticDoc;
|
|
764
|
+
}
|
|
765
|
+
async resolve(kid) {
|
|
766
|
+
const cached = this.byKid.get(kid);
|
|
767
|
+
if (cached) return cached;
|
|
768
|
+
const jwk = (await this.loadDocument()).keys.find((k) => k.kid === kid);
|
|
769
|
+
if (!jwk) throw new LicensingError("jwks_kid_not_found", `manifest signing kid "${kid}" not present in JWKS`);
|
|
770
|
+
const key = await importJWK(jwk, "EdDSA");
|
|
771
|
+
this.byKid.set(kid, key);
|
|
772
|
+
return key;
|
|
773
|
+
}
|
|
774
|
+
async loadDocument() {
|
|
775
|
+
if (this.staticDoc) return this.staticDoc;
|
|
776
|
+
if (!this.endpoint) throw new LicensingError("jwks_endpoint_missing", "workerOrigin (or static jwks) must be supplied to verify the manifest");
|
|
777
|
+
const res = await this.fetcher(this.endpoint, { headers: { accept: "application/json" } });
|
|
778
|
+
if (!res.ok) throw new LicensingError("jwks_fetch_failed", `JWKS fetch failed: ${res.status} ${res.statusText}`);
|
|
779
|
+
const doc = await res.json();
|
|
780
|
+
if (!doc || !Array.isArray(doc.keys)) throw new LicensingError("jwks_malformed", "JWKS response did not contain a keys array");
|
|
781
|
+
return doc;
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
var LicensingError = class extends Error {
|
|
785
|
+
code;
|
|
786
|
+
constructor(code, message) {
|
|
787
|
+
super(message);
|
|
788
|
+
this.code = code;
|
|
789
|
+
this.name = "LicensingError";
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
function nowSec() {
|
|
793
|
+
return Math.floor(Date.now() / 1e3);
|
|
794
|
+
}
|
|
795
|
+
async function fetchManifestDefault(fetcher, url) {
|
|
796
|
+
const res = await fetcher(url, { headers: { accept: "application/json" } });
|
|
797
|
+
if (!res.ok) throw new LicensingError("manifest_fetch_failed", `manifest fetch failed: ${res.status} ${res.statusText}`);
|
|
798
|
+
return await res.json();
|
|
799
|
+
}
|
|
800
|
+
function decodeJwsHeader(jws) {
|
|
801
|
+
const dot = jws.indexOf(".");
|
|
802
|
+
if (dot <= 0) throw new LicensingError("jws_malformed", "JWS missing header segment");
|
|
803
|
+
const headerB64 = jws.slice(0, dot);
|
|
804
|
+
const json = atob(headerB64.replace(/-/g, "+").replace(/_/g, "/"));
|
|
805
|
+
const header = JSON.parse(json);
|
|
806
|
+
if (typeof header.alg !== "string" || typeof header.kid !== "string") throw new LicensingError("jws_header_invalid", "JWS header missing alg or kid");
|
|
807
|
+
return {
|
|
808
|
+
alg: header.alg,
|
|
809
|
+
kid: header.kid,
|
|
810
|
+
typ: header.typ
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
function assertEqual(field, actual, expected) {
|
|
814
|
+
if (actual !== expected) throw new LicensingError("manifest_field_mismatch", `manifest ${field} does not match bootstrap (got ${JSON.stringify(actual)}, expected ${JSON.stringify(expected)})`);
|
|
815
|
+
}
|
|
816
|
+
async function verifyAndValidate(options, jwks, response) {
|
|
817
|
+
const header = decodeJwsHeader(response.jws);
|
|
818
|
+
if (header.alg !== "EdDSA") throw new LicensingError("jws_alg_unsupported", `unsupported JWS alg "${header.alg}" — expected EdDSA`);
|
|
819
|
+
if (header.kid !== response.signing_kid) throw new LicensingError("jws_kid_mismatch", "JWS header kid does not match response signing_kid");
|
|
820
|
+
const key = await jwks.resolve(header.kid);
|
|
821
|
+
let verified;
|
|
822
|
+
try {
|
|
823
|
+
verified = await compactVerify(response.jws, key);
|
|
824
|
+
} catch (err) {
|
|
825
|
+
throw new LicensingError("jws_signature_invalid", `JWS verification failed: ${err.message}`);
|
|
826
|
+
}
|
|
827
|
+
const payload = JSON.parse(new TextDecoder().decode(verified.payload));
|
|
828
|
+
const skew = options.clockSkewSeconds ?? 60;
|
|
829
|
+
const now = nowSec();
|
|
830
|
+
if (payload.bundle_expires_at + skew < now) throw new LicensingError("manifest_expired", `manifest expired at ${payload.bundle_expires_at} (now=${now})`);
|
|
831
|
+
assertEqual("install_id", payload.install_id, options.installId);
|
|
832
|
+
assertEqual("host_origin", payload.host_origin, options.hostOrigin);
|
|
833
|
+
assertEqual("host_type", payload.host_type, options.hostType);
|
|
834
|
+
assertEqual("product", payload.product, options.product);
|
|
835
|
+
assertEqual("contract_version", payload.contract_version, CONTRACT_VERSION);
|
|
836
|
+
return {
|
|
837
|
+
payload,
|
|
838
|
+
response
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
async function loadModuleBytes(fetcher, url, integrity) {
|
|
842
|
+
const res = await fetcher(url, { headers: { accept: "application/javascript, text/javascript" } });
|
|
843
|
+
if (!res.ok) throw new LicensingError("module_fetch_failed", `module fetch failed for ${url}: ${res.status} ${res.statusText}`);
|
|
844
|
+
const bytes = await res.arrayBuffer();
|
|
845
|
+
await assertSri(bytes, integrity, url);
|
|
846
|
+
return bytes;
|
|
847
|
+
}
|
|
848
|
+
async function assertSri(bytes, integrity, url) {
|
|
849
|
+
const match = /^sha384-(.+)$/.exec(integrity);
|
|
850
|
+
if (!match) throw new LicensingError("module_integrity_unsupported", `unsupported integrity hash format for ${url}: ${integrity}`);
|
|
851
|
+
const expected = match[1];
|
|
852
|
+
const digest = await crypto.subtle.digest("SHA-384", bytes);
|
|
853
|
+
if (bytesToBase64(new Uint8Array(digest)) !== expected) throw new LicensingError("module_integrity_mismatch", `module bytes did not match SRI for ${url}`);
|
|
854
|
+
}
|
|
855
|
+
function bytesToBase64(bytes) {
|
|
856
|
+
let bin = "";
|
|
857
|
+
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
|
|
858
|
+
return btoa(bin);
|
|
859
|
+
}
|
|
860
|
+
async function createLicensingClient(options) {
|
|
861
|
+
const fetcher = options.fetch ?? globalThis.fetch?.bind(globalThis);
|
|
862
|
+
if (!fetcher) throw new LicensingError("fetch_unavailable", "no global fetch and no options.fetch supplied");
|
|
863
|
+
const jwks = new JwksCache(fetcher, options.workerOrigin ? `${options.workerOrigin.replace(/\/$/, "")}${JWKS_PATH}` : void 0, options.jwks);
|
|
864
|
+
const fetchManifestFn = async () => {
|
|
865
|
+
if (options.fetchManifest) return options.fetchManifest();
|
|
866
|
+
if (!options.manifestUrl) throw new LicensingError("manifest_source_missing", "either manifestUrl or fetchManifest must be supplied");
|
|
867
|
+
return fetchManifestDefault(fetcher, options.manifestUrl);
|
|
868
|
+
};
|
|
869
|
+
let current = await verifyAndValidate(options, jwks, await fetchManifestFn());
|
|
870
|
+
const moduleCache = /* @__PURE__ */ new Map();
|
|
871
|
+
async function loadModule(name) {
|
|
872
|
+
if (moduleCache.has(name)) return moduleCache.get(name);
|
|
873
|
+
const mod = current.payload.modules.find((m) => m.name === name);
|
|
874
|
+
if (!mod) throw new LicensingError("module_not_authorized", `module "${name}" is not present in the current manifest`);
|
|
875
|
+
const bytes = await loadModuleBytes(fetcher, mod.url, mod.integrity);
|
|
876
|
+
const blob = new Blob([bytes], { type: "application/javascript" });
|
|
877
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
878
|
+
try {
|
|
879
|
+
const imported = await import(
|
|
880
|
+
/* @vite-ignore */
|
|
881
|
+
blobUrl
|
|
882
|
+
);
|
|
883
|
+
moduleCache.set(name, imported);
|
|
884
|
+
return imported;
|
|
885
|
+
} finally {
|
|
886
|
+
URL.revokeObjectURL(blobUrl);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
async function refresh() {
|
|
890
|
+
const next = await verifyAndValidate(options, jwks, await fetchManifestFn());
|
|
891
|
+
current = next;
|
|
892
|
+
moduleCache.clear();
|
|
893
|
+
return next.payload;
|
|
894
|
+
}
|
|
895
|
+
return {
|
|
896
|
+
get manifest() {
|
|
897
|
+
return current.payload;
|
|
898
|
+
},
|
|
899
|
+
get response() {
|
|
900
|
+
return current.response;
|
|
901
|
+
},
|
|
902
|
+
has(moduleName) {
|
|
903
|
+
return current.payload.modules.some((m) => m.name === moduleName);
|
|
904
|
+
},
|
|
905
|
+
loadModule,
|
|
906
|
+
refresh
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
//#endregion
|
|
910
|
+
export { CONTRACT_VERSION, LicensingError, createLicensingClient };
|
|
911
|
+
|
|
912
|
+
//# sourceMappingURL=index.js.map
|