@mission_sciences/provider-sdk 0.1.1

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.
@@ -0,0 +1,3276 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __typeError = (msg) => {
3
+ throw TypeError(msg);
4
+ };
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
8
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
9
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
10
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
11
+ var _a, _b, _jwks, _cached, _url, _timeoutDuration, _cooldownDuration, _cacheMaxAge, _jwksTimestamp, _pendingFetch, _headers, _customFetch, _local, _cache;
12
+ class SDKError extends Error {
13
+ constructor(message2, code, statusCode) {
14
+ super(message2);
15
+ this.code = code;
16
+ this.statusCode = statusCode;
17
+ this.name = "SDKError";
18
+ if (Error.captureStackTrace) {
19
+ Error.captureStackTrace(this, SDKError);
20
+ }
21
+ }
22
+ }
23
+ class JWTParser {
24
+ /**
25
+ * Decode JWT payload without verification
26
+ * @param token - JWT token string
27
+ * @returns Decoded payload
28
+ */
29
+ static decode(token) {
30
+ if (!token || typeof token !== "string") {
31
+ throw new SDKError("Invalid JWT token format", "INVALID_TOKEN");
32
+ }
33
+ const parts = token.split(".");
34
+ if (parts.length !== 3) {
35
+ throw new SDKError("Malformed JWT token - expected 3 parts", "MALFORMED_TOKEN");
36
+ }
37
+ try {
38
+ const payload = parts[1];
39
+ const decoded = this.base64UrlDecode(payload);
40
+ return JSON.parse(decoded);
41
+ } catch (error) {
42
+ throw new SDKError(
43
+ `Failed to decode JWT payload: ${error instanceof Error ? error.message : "Unknown error"}`,
44
+ "DECODE_ERROR"
45
+ );
46
+ }
47
+ }
48
+ /**
49
+ * Decode JWT header
50
+ * @param token - JWT token string
51
+ * @returns Decoded header
52
+ */
53
+ static decodeHeader(token) {
54
+ if (!token || typeof token !== "string") {
55
+ throw new SDKError("Invalid JWT token format", "INVALID_TOKEN");
56
+ }
57
+ const parts = token.split(".");
58
+ if (parts.length !== 3) {
59
+ throw new SDKError("Malformed JWT token - expected 3 parts", "MALFORMED_TOKEN");
60
+ }
61
+ try {
62
+ const header = parts[0];
63
+ const decoded = this.base64UrlDecode(header);
64
+ return JSON.parse(decoded);
65
+ } catch (error) {
66
+ throw new SDKError(
67
+ `Failed to decode JWT header: ${error instanceof Error ? error.message : "Unknown error"}`,
68
+ "DECODE_ERROR"
69
+ );
70
+ }
71
+ }
72
+ /**
73
+ * Extract specific claim from JWT
74
+ * @param token - JWT token string
75
+ * @param claim - Claim name to extract
76
+ * @returns Claim value
77
+ */
78
+ static extractClaim(token, claim) {
79
+ const payload = this.decode(token);
80
+ return payload[claim];
81
+ }
82
+ /**
83
+ * Check if JWT is expired (client-side only, not authoritative)
84
+ * @param token - JWT token string
85
+ * @returns True if token is expired
86
+ */
87
+ static isExpired(token) {
88
+ const payload = this.decode(token);
89
+ const exp = payload.exp;
90
+ if (!exp) {
91
+ return false;
92
+ }
93
+ return Date.now() >= exp * 1e3;
94
+ }
95
+ /**
96
+ * Get time remaining until expiration
97
+ * @param token - JWT token string
98
+ * @returns Seconds remaining (0 if expired)
99
+ */
100
+ static getTimeRemaining(token) {
101
+ const payload = this.decode(token);
102
+ const exp = payload.exp;
103
+ if (!exp) {
104
+ return 0;
105
+ }
106
+ const remaining = exp - Math.floor(Date.now() / 1e3);
107
+ return Math.max(0, remaining);
108
+ }
109
+ /**
110
+ * Base64 URL decode
111
+ * @param str - Base64 URL encoded string
112
+ * @returns Decoded string
113
+ */
114
+ static base64UrlDecode(str) {
115
+ let base64 = str.replace(/-/g, "+").replace(/_/g, "/");
116
+ const padding = base64.length % 4;
117
+ if (padding) {
118
+ base64 += "=".repeat(4 - padding);
119
+ }
120
+ try {
121
+ if (typeof Buffer !== "undefined") {
122
+ return Buffer.from(base64, "base64").toString("utf-8");
123
+ } else if (typeof atob !== "undefined") {
124
+ return atob(base64);
125
+ } else {
126
+ throw new Error("No base64 decoding available");
127
+ }
128
+ } catch (error) {
129
+ throw new SDKError(
130
+ "Invalid base64 encoding",
131
+ "ENCODING_ERROR"
132
+ );
133
+ }
134
+ }
135
+ }
136
+ const encoder = new TextEncoder();
137
+ const decoder = new TextDecoder();
138
+ function concat(...buffers) {
139
+ const size = buffers.reduce((acc, { length }) => acc + length, 0);
140
+ const buf = new Uint8Array(size);
141
+ let i = 0;
142
+ for (const buffer of buffers) {
143
+ buf.set(buffer, i);
144
+ i += buffer.length;
145
+ }
146
+ return buf;
147
+ }
148
+ function decodeBase64(encoded) {
149
+ if (Uint8Array.fromBase64) {
150
+ return Uint8Array.fromBase64(encoded);
151
+ }
152
+ const binary = atob(encoded);
153
+ const bytes = new Uint8Array(binary.length);
154
+ for (let i = 0; i < binary.length; i++) {
155
+ bytes[i] = binary.charCodeAt(i);
156
+ }
157
+ return bytes;
158
+ }
159
+ function decode(input) {
160
+ if (Uint8Array.fromBase64) {
161
+ return Uint8Array.fromBase64(typeof input === "string" ? input : decoder.decode(input), {
162
+ alphabet: "base64url"
163
+ });
164
+ }
165
+ let encoded = input;
166
+ if (encoded instanceof Uint8Array) {
167
+ encoded = decoder.decode(encoded);
168
+ }
169
+ encoded = encoded.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
170
+ try {
171
+ return decodeBase64(encoded);
172
+ } catch {
173
+ throw new TypeError("The input to be decoded is not correctly encoded.");
174
+ }
175
+ }
176
+ class JOSEError extends Error {
177
+ constructor(message2, options) {
178
+ super(message2, options);
179
+ __publicField(this, "code", "ERR_JOSE_GENERIC");
180
+ this.name = this.constructor.name;
181
+ Error.captureStackTrace?.(this, this.constructor);
182
+ }
183
+ }
184
+ __publicField(JOSEError, "code", "ERR_JOSE_GENERIC");
185
+ class JWTClaimValidationFailed extends JOSEError {
186
+ constructor(message2, payload, claim = "unspecified", reason = "unspecified") {
187
+ super(message2, { cause: { claim, reason, payload } });
188
+ __publicField(this, "code", "ERR_JWT_CLAIM_VALIDATION_FAILED");
189
+ __publicField(this, "claim");
190
+ __publicField(this, "reason");
191
+ __publicField(this, "payload");
192
+ this.claim = claim;
193
+ this.reason = reason;
194
+ this.payload = payload;
195
+ }
196
+ }
197
+ __publicField(JWTClaimValidationFailed, "code", "ERR_JWT_CLAIM_VALIDATION_FAILED");
198
+ class JWTExpired extends JOSEError {
199
+ constructor(message2, payload, claim = "unspecified", reason = "unspecified") {
200
+ super(message2, { cause: { claim, reason, payload } });
201
+ __publicField(this, "code", "ERR_JWT_EXPIRED");
202
+ __publicField(this, "claim");
203
+ __publicField(this, "reason");
204
+ __publicField(this, "payload");
205
+ this.claim = claim;
206
+ this.reason = reason;
207
+ this.payload = payload;
208
+ }
209
+ }
210
+ __publicField(JWTExpired, "code", "ERR_JWT_EXPIRED");
211
+ class JOSEAlgNotAllowed extends JOSEError {
212
+ constructor() {
213
+ super(...arguments);
214
+ __publicField(this, "code", "ERR_JOSE_ALG_NOT_ALLOWED");
215
+ }
216
+ }
217
+ __publicField(JOSEAlgNotAllowed, "code", "ERR_JOSE_ALG_NOT_ALLOWED");
218
+ class JOSENotSupported extends JOSEError {
219
+ constructor() {
220
+ super(...arguments);
221
+ __publicField(this, "code", "ERR_JOSE_NOT_SUPPORTED");
222
+ }
223
+ }
224
+ __publicField(JOSENotSupported, "code", "ERR_JOSE_NOT_SUPPORTED");
225
+ class JWSInvalid extends JOSEError {
226
+ constructor() {
227
+ super(...arguments);
228
+ __publicField(this, "code", "ERR_JWS_INVALID");
229
+ }
230
+ }
231
+ __publicField(JWSInvalid, "code", "ERR_JWS_INVALID");
232
+ class JWTInvalid extends JOSEError {
233
+ constructor() {
234
+ super(...arguments);
235
+ __publicField(this, "code", "ERR_JWT_INVALID");
236
+ }
237
+ }
238
+ __publicField(JWTInvalid, "code", "ERR_JWT_INVALID");
239
+ class JWKSInvalid extends JOSEError {
240
+ constructor() {
241
+ super(...arguments);
242
+ __publicField(this, "code", "ERR_JWKS_INVALID");
243
+ }
244
+ }
245
+ __publicField(JWKSInvalid, "code", "ERR_JWKS_INVALID");
246
+ class JWKSNoMatchingKey extends JOSEError {
247
+ constructor(message2 = "no applicable key found in the JSON Web Key Set", options) {
248
+ super(message2, options);
249
+ __publicField(this, "code", "ERR_JWKS_NO_MATCHING_KEY");
250
+ }
251
+ }
252
+ __publicField(JWKSNoMatchingKey, "code", "ERR_JWKS_NO_MATCHING_KEY");
253
+ class JWKSMultipleMatchingKeys extends (_b = JOSEError, _a = Symbol.asyncIterator, _b) {
254
+ constructor(message2 = "multiple matching keys found in the JSON Web Key Set", options) {
255
+ super(message2, options);
256
+ __publicField(this, _a);
257
+ __publicField(this, "code", "ERR_JWKS_MULTIPLE_MATCHING_KEYS");
258
+ }
259
+ }
260
+ __publicField(JWKSMultipleMatchingKeys, "code", "ERR_JWKS_MULTIPLE_MATCHING_KEYS");
261
+ class JWKSTimeout extends JOSEError {
262
+ constructor(message2 = "request timed out", options) {
263
+ super(message2, options);
264
+ __publicField(this, "code", "ERR_JWKS_TIMEOUT");
265
+ }
266
+ }
267
+ __publicField(JWKSTimeout, "code", "ERR_JWKS_TIMEOUT");
268
+ class JWSSignatureVerificationFailed extends JOSEError {
269
+ constructor(message2 = "signature verification failed", options) {
270
+ super(message2, options);
271
+ __publicField(this, "code", "ERR_JWS_SIGNATURE_VERIFICATION_FAILED");
272
+ }
273
+ }
274
+ __publicField(JWSSignatureVerificationFailed, "code", "ERR_JWS_SIGNATURE_VERIFICATION_FAILED");
275
+ function unusable(name, prop = "algorithm.name") {
276
+ return new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`);
277
+ }
278
+ function isAlgorithm(algorithm, name) {
279
+ return algorithm.name === name;
280
+ }
281
+ function getHashLength(hash) {
282
+ return parseInt(hash.name.slice(4), 10);
283
+ }
284
+ function getNamedCurve(alg) {
285
+ switch (alg) {
286
+ case "ES256":
287
+ return "P-256";
288
+ case "ES384":
289
+ return "P-384";
290
+ case "ES512":
291
+ return "P-521";
292
+ default:
293
+ throw new Error("unreachable");
294
+ }
295
+ }
296
+ function checkUsage(key, usage) {
297
+ if (!key.usages.includes(usage)) {
298
+ throw new TypeError(`CryptoKey does not support this operation, its usages must include ${usage}.`);
299
+ }
300
+ }
301
+ function checkSigCryptoKey(key, alg, usage) {
302
+ switch (alg) {
303
+ case "HS256":
304
+ case "HS384":
305
+ case "HS512": {
306
+ if (!isAlgorithm(key.algorithm, "HMAC"))
307
+ throw unusable("HMAC");
308
+ const expected = parseInt(alg.slice(2), 10);
309
+ const actual = getHashLength(key.algorithm.hash);
310
+ if (actual !== expected)
311
+ throw unusable(`SHA-${expected}`, "algorithm.hash");
312
+ break;
313
+ }
314
+ case "RS256":
315
+ case "RS384":
316
+ case "RS512": {
317
+ if (!isAlgorithm(key.algorithm, "RSASSA-PKCS1-v1_5"))
318
+ throw unusable("RSASSA-PKCS1-v1_5");
319
+ const expected = parseInt(alg.slice(2), 10);
320
+ const actual = getHashLength(key.algorithm.hash);
321
+ if (actual !== expected)
322
+ throw unusable(`SHA-${expected}`, "algorithm.hash");
323
+ break;
324
+ }
325
+ case "PS256":
326
+ case "PS384":
327
+ case "PS512": {
328
+ if (!isAlgorithm(key.algorithm, "RSA-PSS"))
329
+ throw unusable("RSA-PSS");
330
+ const expected = parseInt(alg.slice(2), 10);
331
+ const actual = getHashLength(key.algorithm.hash);
332
+ if (actual !== expected)
333
+ throw unusable(`SHA-${expected}`, "algorithm.hash");
334
+ break;
335
+ }
336
+ case "Ed25519":
337
+ case "EdDSA": {
338
+ if (!isAlgorithm(key.algorithm, "Ed25519"))
339
+ throw unusable("Ed25519");
340
+ break;
341
+ }
342
+ case "ML-DSA-44":
343
+ case "ML-DSA-65":
344
+ case "ML-DSA-87": {
345
+ if (!isAlgorithm(key.algorithm, alg))
346
+ throw unusable(alg);
347
+ break;
348
+ }
349
+ case "ES256":
350
+ case "ES384":
351
+ case "ES512": {
352
+ if (!isAlgorithm(key.algorithm, "ECDSA"))
353
+ throw unusable("ECDSA");
354
+ const expected = getNamedCurve(alg);
355
+ const actual = key.algorithm.namedCurve;
356
+ if (actual !== expected)
357
+ throw unusable(expected, "algorithm.namedCurve");
358
+ break;
359
+ }
360
+ default:
361
+ throw new TypeError("CryptoKey does not support this operation");
362
+ }
363
+ checkUsage(key, usage);
364
+ }
365
+ function message(msg, actual, ...types) {
366
+ types = types.filter(Boolean);
367
+ if (types.length > 2) {
368
+ const last = types.pop();
369
+ msg += `one of type ${types.join(", ")}, or ${last}.`;
370
+ } else if (types.length === 2) {
371
+ msg += `one of type ${types[0]} or ${types[1]}.`;
372
+ } else {
373
+ msg += `of type ${types[0]}.`;
374
+ }
375
+ if (actual == null) {
376
+ msg += ` Received ${actual}`;
377
+ } else if (typeof actual === "function" && actual.name) {
378
+ msg += ` Received function ${actual.name}`;
379
+ } else if (typeof actual === "object" && actual != null) {
380
+ if (actual.constructor?.name) {
381
+ msg += ` Received an instance of ${actual.constructor.name}`;
382
+ }
383
+ }
384
+ return msg;
385
+ }
386
+ const invalidKeyInput = (actual, ...types) => {
387
+ return message("Key must be ", actual, ...types);
388
+ };
389
+ function withAlg(alg, actual, ...types) {
390
+ return message(`Key for the ${alg} algorithm must be `, actual, ...types);
391
+ }
392
+ function isCryptoKey(key) {
393
+ return key?.[Symbol.toStringTag] === "CryptoKey";
394
+ }
395
+ function isKeyObject(key) {
396
+ return key?.[Symbol.toStringTag] === "KeyObject";
397
+ }
398
+ const isKeyLike = (key) => {
399
+ return isCryptoKey(key) || isKeyObject(key);
400
+ };
401
+ const isDisjoint = (...headers) => {
402
+ const sources = headers.filter(Boolean);
403
+ if (sources.length === 0 || sources.length === 1) {
404
+ return true;
405
+ }
406
+ let acc;
407
+ for (const header of sources) {
408
+ const parameters = Object.keys(header);
409
+ if (!acc || acc.size === 0) {
410
+ acc = new Set(parameters);
411
+ continue;
412
+ }
413
+ for (const parameter of parameters) {
414
+ if (acc.has(parameter)) {
415
+ return false;
416
+ }
417
+ acc.add(parameter);
418
+ }
419
+ }
420
+ return true;
421
+ };
422
+ function isObjectLike(value) {
423
+ return typeof value === "object" && value !== null;
424
+ }
425
+ const isObject = (input) => {
426
+ if (!isObjectLike(input) || Object.prototype.toString.call(input) !== "[object Object]") {
427
+ return false;
428
+ }
429
+ if (Object.getPrototypeOf(input) === null) {
430
+ return true;
431
+ }
432
+ let proto = input;
433
+ while (Object.getPrototypeOf(proto) !== null) {
434
+ proto = Object.getPrototypeOf(proto);
435
+ }
436
+ return Object.getPrototypeOf(input) === proto;
437
+ };
438
+ const checkKeyLength = (alg, key) => {
439
+ if (alg.startsWith("RS") || alg.startsWith("PS")) {
440
+ const { modulusLength } = key.algorithm;
441
+ if (typeof modulusLength !== "number" || modulusLength < 2048) {
442
+ throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
443
+ }
444
+ }
445
+ };
446
+ function subtleMapping(jwk) {
447
+ let algorithm;
448
+ let keyUsages;
449
+ switch (jwk.kty) {
450
+ case "AKP": {
451
+ switch (jwk.alg) {
452
+ case "ML-DSA-44":
453
+ case "ML-DSA-65":
454
+ case "ML-DSA-87":
455
+ algorithm = { name: jwk.alg };
456
+ keyUsages = jwk.priv ? ["sign"] : ["verify"];
457
+ break;
458
+ default:
459
+ throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
460
+ }
461
+ break;
462
+ }
463
+ case "RSA": {
464
+ switch (jwk.alg) {
465
+ case "PS256":
466
+ case "PS384":
467
+ case "PS512":
468
+ algorithm = { name: "RSA-PSS", hash: `SHA-${jwk.alg.slice(-3)}` };
469
+ keyUsages = jwk.d ? ["sign"] : ["verify"];
470
+ break;
471
+ case "RS256":
472
+ case "RS384":
473
+ case "RS512":
474
+ algorithm = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${jwk.alg.slice(-3)}` };
475
+ keyUsages = jwk.d ? ["sign"] : ["verify"];
476
+ break;
477
+ case "RSA-OAEP":
478
+ case "RSA-OAEP-256":
479
+ case "RSA-OAEP-384":
480
+ case "RSA-OAEP-512":
481
+ algorithm = {
482
+ name: "RSA-OAEP",
483
+ hash: `SHA-${parseInt(jwk.alg.slice(-3), 10) || 1}`
484
+ };
485
+ keyUsages = jwk.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
486
+ break;
487
+ default:
488
+ throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
489
+ }
490
+ break;
491
+ }
492
+ case "EC": {
493
+ switch (jwk.alg) {
494
+ case "ES256":
495
+ algorithm = { name: "ECDSA", namedCurve: "P-256" };
496
+ keyUsages = jwk.d ? ["sign"] : ["verify"];
497
+ break;
498
+ case "ES384":
499
+ algorithm = { name: "ECDSA", namedCurve: "P-384" };
500
+ keyUsages = jwk.d ? ["sign"] : ["verify"];
501
+ break;
502
+ case "ES512":
503
+ algorithm = { name: "ECDSA", namedCurve: "P-521" };
504
+ keyUsages = jwk.d ? ["sign"] : ["verify"];
505
+ break;
506
+ case "ECDH-ES":
507
+ case "ECDH-ES+A128KW":
508
+ case "ECDH-ES+A192KW":
509
+ case "ECDH-ES+A256KW":
510
+ algorithm = { name: "ECDH", namedCurve: jwk.crv };
511
+ keyUsages = jwk.d ? ["deriveBits"] : [];
512
+ break;
513
+ default:
514
+ throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
515
+ }
516
+ break;
517
+ }
518
+ case "OKP": {
519
+ switch (jwk.alg) {
520
+ case "Ed25519":
521
+ case "EdDSA":
522
+ algorithm = { name: "Ed25519" };
523
+ keyUsages = jwk.d ? ["sign"] : ["verify"];
524
+ break;
525
+ case "ECDH-ES":
526
+ case "ECDH-ES+A128KW":
527
+ case "ECDH-ES+A192KW":
528
+ case "ECDH-ES+A256KW":
529
+ algorithm = { name: jwk.crv };
530
+ keyUsages = jwk.d ? ["deriveBits"] : [];
531
+ break;
532
+ default:
533
+ throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
534
+ }
535
+ break;
536
+ }
537
+ default:
538
+ throw new JOSENotSupported('Invalid or unsupported JWK "kty" (Key Type) Parameter value');
539
+ }
540
+ return { algorithm, keyUsages };
541
+ }
542
+ const importJWK$1 = async (jwk) => {
543
+ if (!jwk.alg) {
544
+ throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
545
+ }
546
+ const { algorithm, keyUsages } = subtleMapping(jwk);
547
+ const keyData = { ...jwk };
548
+ if (keyData.kty !== "AKP") {
549
+ delete keyData.alg;
550
+ }
551
+ delete keyData.use;
552
+ return crypto.subtle.importKey("jwk", keyData, algorithm, jwk.ext ?? (jwk.d || jwk.priv ? false : true), jwk.key_ops ?? keyUsages);
553
+ };
554
+ async function importJWK(jwk, alg, options) {
555
+ if (!isObject(jwk)) {
556
+ throw new TypeError("JWK must be an object");
557
+ }
558
+ let ext;
559
+ alg ?? (alg = jwk.alg);
560
+ ext ?? (ext = jwk.ext);
561
+ switch (jwk.kty) {
562
+ case "oct":
563
+ if (typeof jwk.k !== "string" || !jwk.k) {
564
+ throw new TypeError('missing "k" (Key Value) Parameter value');
565
+ }
566
+ return decode(jwk.k);
567
+ case "RSA":
568
+ if ("oth" in jwk && jwk.oth !== void 0) {
569
+ throw new JOSENotSupported('RSA JWK "oth" (Other Primes Info) Parameter value is not supported');
570
+ }
571
+ return importJWK$1({ ...jwk, alg, ext });
572
+ case "AKP": {
573
+ if (typeof jwk.alg !== "string" || !jwk.alg) {
574
+ throw new TypeError('missing "alg" (Algorithm) Parameter value');
575
+ }
576
+ if (alg !== void 0 && alg !== jwk.alg) {
577
+ throw new TypeError("JWK alg and alg option value mismatch");
578
+ }
579
+ return importJWK$1({ ...jwk, ext });
580
+ }
581
+ case "EC":
582
+ case "OKP":
583
+ return importJWK$1({ ...jwk, alg, ext });
584
+ default:
585
+ throw new JOSENotSupported('Unsupported "kty" (Key Type) Parameter value');
586
+ }
587
+ }
588
+ const validateCrit = (Err, recognizedDefault, recognizedOption, protectedHeader, joseHeader) => {
589
+ if (joseHeader.crit !== void 0 && protectedHeader?.crit === void 0) {
590
+ throw new Err('"crit" (Critical) Header Parameter MUST be integrity protected');
591
+ }
592
+ if (!protectedHeader || protectedHeader.crit === void 0) {
593
+ return /* @__PURE__ */ new Set();
594
+ }
595
+ if (!Array.isArray(protectedHeader.crit) || protectedHeader.crit.length === 0 || protectedHeader.crit.some((input) => typeof input !== "string" || input.length === 0)) {
596
+ throw new Err('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
597
+ }
598
+ let recognized;
599
+ if (recognizedOption !== void 0) {
600
+ recognized = new Map([...Object.entries(recognizedOption), ...recognizedDefault.entries()]);
601
+ } else {
602
+ recognized = recognizedDefault;
603
+ }
604
+ for (const parameter of protectedHeader.crit) {
605
+ if (!recognized.has(parameter)) {
606
+ throw new JOSENotSupported(`Extension Header Parameter "${parameter}" is not recognized`);
607
+ }
608
+ if (joseHeader[parameter] === void 0) {
609
+ throw new Err(`Extension Header Parameter "${parameter}" is missing`);
610
+ }
611
+ if (recognized.get(parameter) && protectedHeader[parameter] === void 0) {
612
+ throw new Err(`Extension Header Parameter "${parameter}" MUST be integrity protected`);
613
+ }
614
+ }
615
+ return new Set(protectedHeader.crit);
616
+ };
617
+ const validateAlgorithms = (option, algorithms) => {
618
+ if (algorithms !== void 0 && (!Array.isArray(algorithms) || algorithms.some((s) => typeof s !== "string"))) {
619
+ throw new TypeError(`"${option}" option must be an array of strings`);
620
+ }
621
+ if (!algorithms) {
622
+ return void 0;
623
+ }
624
+ return new Set(algorithms);
625
+ };
626
+ function isJWK(key) {
627
+ return isObject(key) && typeof key.kty === "string";
628
+ }
629
+ function isPrivateJWK(key) {
630
+ return key.kty !== "oct" && (key.kty === "AKP" && typeof key.priv === "string" || typeof key.d === "string");
631
+ }
632
+ function isPublicJWK(key) {
633
+ return key.kty !== "oct" && typeof key.d === "undefined" && typeof key.priv === "undefined";
634
+ }
635
+ function isSecretJWK(key) {
636
+ return key.kty === "oct" && typeof key.k === "string";
637
+ }
638
+ let cache;
639
+ const handleJWK = async (key, jwk, alg, freeze = false) => {
640
+ cache || (cache = /* @__PURE__ */ new WeakMap());
641
+ let cached = cache.get(key);
642
+ if (cached?.[alg]) {
643
+ return cached[alg];
644
+ }
645
+ const cryptoKey = await importJWK$1({ ...jwk, alg });
646
+ if (freeze)
647
+ Object.freeze(key);
648
+ if (!cached) {
649
+ cache.set(key, { [alg]: cryptoKey });
650
+ } else {
651
+ cached[alg] = cryptoKey;
652
+ }
653
+ return cryptoKey;
654
+ };
655
+ const handleKeyObject = (keyObject, alg) => {
656
+ cache || (cache = /* @__PURE__ */ new WeakMap());
657
+ let cached = cache.get(keyObject);
658
+ if (cached?.[alg]) {
659
+ return cached[alg];
660
+ }
661
+ const isPublic = keyObject.type === "public";
662
+ const extractable = isPublic ? true : false;
663
+ let cryptoKey;
664
+ if (keyObject.asymmetricKeyType === "x25519") {
665
+ switch (alg) {
666
+ case "ECDH-ES":
667
+ case "ECDH-ES+A128KW":
668
+ case "ECDH-ES+A192KW":
669
+ case "ECDH-ES+A256KW":
670
+ break;
671
+ default:
672
+ throw new TypeError("given KeyObject instance cannot be used for this algorithm");
673
+ }
674
+ cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, isPublic ? [] : ["deriveBits"]);
675
+ }
676
+ if (keyObject.asymmetricKeyType === "ed25519") {
677
+ if (alg !== "EdDSA" && alg !== "Ed25519") {
678
+ throw new TypeError("given KeyObject instance cannot be used for this algorithm");
679
+ }
680
+ cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, [
681
+ isPublic ? "verify" : "sign"
682
+ ]);
683
+ }
684
+ switch (keyObject.asymmetricKeyType) {
685
+ case "ml-dsa-44":
686
+ case "ml-dsa-65":
687
+ case "ml-dsa-87": {
688
+ if (alg !== keyObject.asymmetricKeyType.toUpperCase()) {
689
+ throw new TypeError("given KeyObject instance cannot be used for this algorithm");
690
+ }
691
+ cryptoKey = keyObject.toCryptoKey(keyObject.asymmetricKeyType, extractable, [
692
+ isPublic ? "verify" : "sign"
693
+ ]);
694
+ }
695
+ }
696
+ if (keyObject.asymmetricKeyType === "rsa") {
697
+ let hash;
698
+ switch (alg) {
699
+ case "RSA-OAEP":
700
+ hash = "SHA-1";
701
+ break;
702
+ case "RS256":
703
+ case "PS256":
704
+ case "RSA-OAEP-256":
705
+ hash = "SHA-256";
706
+ break;
707
+ case "RS384":
708
+ case "PS384":
709
+ case "RSA-OAEP-384":
710
+ hash = "SHA-384";
711
+ break;
712
+ case "RS512":
713
+ case "PS512":
714
+ case "RSA-OAEP-512":
715
+ hash = "SHA-512";
716
+ break;
717
+ default:
718
+ throw new TypeError("given KeyObject instance cannot be used for this algorithm");
719
+ }
720
+ if (alg.startsWith("RSA-OAEP")) {
721
+ return keyObject.toCryptoKey({
722
+ name: "RSA-OAEP",
723
+ hash
724
+ }, extractable, isPublic ? ["encrypt"] : ["decrypt"]);
725
+ }
726
+ cryptoKey = keyObject.toCryptoKey({
727
+ name: alg.startsWith("PS") ? "RSA-PSS" : "RSASSA-PKCS1-v1_5",
728
+ hash
729
+ }, extractable, [isPublic ? "verify" : "sign"]);
730
+ }
731
+ if (keyObject.asymmetricKeyType === "ec") {
732
+ const nist = /* @__PURE__ */ new Map([
733
+ ["prime256v1", "P-256"],
734
+ ["secp384r1", "P-384"],
735
+ ["secp521r1", "P-521"]
736
+ ]);
737
+ const namedCurve = nist.get(keyObject.asymmetricKeyDetails?.namedCurve);
738
+ if (!namedCurve) {
739
+ throw new TypeError("given KeyObject instance cannot be used for this algorithm");
740
+ }
741
+ if (alg === "ES256" && namedCurve === "P-256") {
742
+ cryptoKey = keyObject.toCryptoKey({
743
+ name: "ECDSA",
744
+ namedCurve
745
+ }, extractable, [isPublic ? "verify" : "sign"]);
746
+ }
747
+ if (alg === "ES384" && namedCurve === "P-384") {
748
+ cryptoKey = keyObject.toCryptoKey({
749
+ name: "ECDSA",
750
+ namedCurve
751
+ }, extractable, [isPublic ? "verify" : "sign"]);
752
+ }
753
+ if (alg === "ES512" && namedCurve === "P-521") {
754
+ cryptoKey = keyObject.toCryptoKey({
755
+ name: "ECDSA",
756
+ namedCurve
757
+ }, extractable, [isPublic ? "verify" : "sign"]);
758
+ }
759
+ if (alg.startsWith("ECDH-ES")) {
760
+ cryptoKey = keyObject.toCryptoKey({
761
+ name: "ECDH",
762
+ namedCurve
763
+ }, extractable, isPublic ? [] : ["deriveBits"]);
764
+ }
765
+ }
766
+ if (!cryptoKey) {
767
+ throw new TypeError("given KeyObject instance cannot be used for this algorithm");
768
+ }
769
+ if (!cached) {
770
+ cache.set(keyObject, { [alg]: cryptoKey });
771
+ } else {
772
+ cached[alg] = cryptoKey;
773
+ }
774
+ return cryptoKey;
775
+ };
776
+ const normalizeKey = async (key, alg) => {
777
+ if (key instanceof Uint8Array) {
778
+ return key;
779
+ }
780
+ if (isCryptoKey(key)) {
781
+ return key;
782
+ }
783
+ if (isKeyObject(key)) {
784
+ if (key.type === "secret") {
785
+ return key.export();
786
+ }
787
+ if ("toCryptoKey" in key && typeof key.toCryptoKey === "function") {
788
+ try {
789
+ return handleKeyObject(key, alg);
790
+ } catch (err) {
791
+ if (err instanceof TypeError) {
792
+ throw err;
793
+ }
794
+ }
795
+ }
796
+ let jwk = key.export({ format: "jwk" });
797
+ return handleJWK(key, jwk, alg);
798
+ }
799
+ if (isJWK(key)) {
800
+ if (key.k) {
801
+ return decode(key.k);
802
+ }
803
+ return handleJWK(key, key, alg, true);
804
+ }
805
+ throw new Error("unreachable");
806
+ };
807
+ const tag = (key) => key?.[Symbol.toStringTag];
808
+ const jwkMatchesOp = (alg, key, usage) => {
809
+ if (key.use !== void 0) {
810
+ let expected;
811
+ switch (usage) {
812
+ case "sign":
813
+ case "verify":
814
+ expected = "sig";
815
+ break;
816
+ case "encrypt":
817
+ case "decrypt":
818
+ expected = "enc";
819
+ break;
820
+ }
821
+ if (key.use !== expected) {
822
+ throw new TypeError(`Invalid key for this operation, its "use" must be "${expected}" when present`);
823
+ }
824
+ }
825
+ if (key.alg !== void 0 && key.alg !== alg) {
826
+ throw new TypeError(`Invalid key for this operation, its "alg" must be "${alg}" when present`);
827
+ }
828
+ if (Array.isArray(key.key_ops)) {
829
+ let expectedKeyOp;
830
+ switch (true) {
831
+ case usage === "verify":
832
+ case alg === "dir":
833
+ case alg.includes("CBC-HS"):
834
+ expectedKeyOp = usage;
835
+ break;
836
+ case alg.startsWith("PBES2"):
837
+ expectedKeyOp = "deriveBits";
838
+ break;
839
+ case /^A\d{3}(?:GCM)?(?:KW)?$/.test(alg):
840
+ if (!alg.includes("GCM") && alg.endsWith("KW")) {
841
+ expectedKeyOp = "unwrapKey";
842
+ } else {
843
+ expectedKeyOp = usage;
844
+ }
845
+ break;
846
+ case usage === "encrypt":
847
+ expectedKeyOp = "wrapKey";
848
+ break;
849
+ case usage === "decrypt":
850
+ expectedKeyOp = alg.startsWith("RSA") ? "unwrapKey" : "deriveBits";
851
+ break;
852
+ }
853
+ if (expectedKeyOp && key.key_ops?.includes?.(expectedKeyOp) === false) {
854
+ throw new TypeError(`Invalid key for this operation, its "key_ops" must include "${expectedKeyOp}" when present`);
855
+ }
856
+ }
857
+ return true;
858
+ };
859
+ const symmetricTypeCheck = (alg, key, usage) => {
860
+ if (key instanceof Uint8Array)
861
+ return;
862
+ if (isJWK(key)) {
863
+ if (isSecretJWK(key) && jwkMatchesOp(alg, key, usage))
864
+ return;
865
+ 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`);
866
+ }
867
+ if (!isKeyLike(key)) {
868
+ throw new TypeError(withAlg(alg, key, "CryptoKey", "KeyObject", "JSON Web Key", "Uint8Array"));
869
+ }
870
+ if (key.type !== "secret") {
871
+ throw new TypeError(`${tag(key)} instances for symmetric algorithms must be of type "secret"`);
872
+ }
873
+ };
874
+ const asymmetricTypeCheck = (alg, key, usage) => {
875
+ if (isJWK(key)) {
876
+ switch (usage) {
877
+ case "decrypt":
878
+ case "sign":
879
+ if (isPrivateJWK(key) && jwkMatchesOp(alg, key, usage))
880
+ return;
881
+ throw new TypeError(`JSON Web Key for this operation be a private JWK`);
882
+ case "encrypt":
883
+ case "verify":
884
+ if (isPublicJWK(key) && jwkMatchesOp(alg, key, usage))
885
+ return;
886
+ throw new TypeError(`JSON Web Key for this operation be a public JWK`);
887
+ }
888
+ }
889
+ if (!isKeyLike(key)) {
890
+ throw new TypeError(withAlg(alg, key, "CryptoKey", "KeyObject", "JSON Web Key"));
891
+ }
892
+ if (key.type === "secret") {
893
+ throw new TypeError(`${tag(key)} instances for asymmetric algorithms must not be of type "secret"`);
894
+ }
895
+ if (key.type === "public") {
896
+ switch (usage) {
897
+ case "sign":
898
+ throw new TypeError(`${tag(key)} instances for asymmetric algorithm signing must be of type "private"`);
899
+ case "decrypt":
900
+ throw new TypeError(`${tag(key)} instances for asymmetric algorithm decryption must be of type "private"`);
901
+ }
902
+ }
903
+ if (key.type === "private") {
904
+ switch (usage) {
905
+ case "verify":
906
+ throw new TypeError(`${tag(key)} instances for asymmetric algorithm verifying must be of type "public"`);
907
+ case "encrypt":
908
+ throw new TypeError(`${tag(key)} instances for asymmetric algorithm encryption must be of type "public"`);
909
+ }
910
+ }
911
+ };
912
+ const checkKeyType = (alg, key, usage) => {
913
+ const symmetric = alg.startsWith("HS") || alg === "dir" || alg.startsWith("PBES2") || /^A(?:128|192|256)(?:GCM)?(?:KW)?$/.test(alg) || /^A(?:128|192|256)CBC-HS(?:256|384|512)$/.test(alg);
914
+ if (symmetric) {
915
+ symmetricTypeCheck(alg, key, usage);
916
+ } else {
917
+ asymmetricTypeCheck(alg, key, usage);
918
+ }
919
+ };
920
+ const subtleAlgorithm = (alg, algorithm) => {
921
+ const hash = `SHA-${alg.slice(-3)}`;
922
+ switch (alg) {
923
+ case "HS256":
924
+ case "HS384":
925
+ case "HS512":
926
+ return { hash, name: "HMAC" };
927
+ case "PS256":
928
+ case "PS384":
929
+ case "PS512":
930
+ return { hash, name: "RSA-PSS", saltLength: parseInt(alg.slice(-3), 10) >> 3 };
931
+ case "RS256":
932
+ case "RS384":
933
+ case "RS512":
934
+ return { hash, name: "RSASSA-PKCS1-v1_5" };
935
+ case "ES256":
936
+ case "ES384":
937
+ case "ES512":
938
+ return { hash, name: "ECDSA", namedCurve: algorithm.namedCurve };
939
+ case "Ed25519":
940
+ case "EdDSA":
941
+ return { name: "Ed25519" };
942
+ case "ML-DSA-44":
943
+ case "ML-DSA-65":
944
+ case "ML-DSA-87":
945
+ return { name: alg };
946
+ default:
947
+ throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
948
+ }
949
+ };
950
+ const getSignKey = async (alg, key, usage) => {
951
+ if (key instanceof Uint8Array) {
952
+ if (!alg.startsWith("HS")) {
953
+ throw new TypeError(invalidKeyInput(key, "CryptoKey", "KeyObject", "JSON Web Key"));
954
+ }
955
+ return crypto.subtle.importKey("raw", key, { hash: `SHA-${alg.slice(-3)}`, name: "HMAC" }, false, [usage]);
956
+ }
957
+ checkSigCryptoKey(key, alg, usage);
958
+ return key;
959
+ };
960
+ const verify = async (alg, key, signature, data) => {
961
+ const cryptoKey = await getSignKey(alg, key, "verify");
962
+ checkKeyLength(alg, cryptoKey);
963
+ const algorithm = subtleAlgorithm(alg, cryptoKey.algorithm);
964
+ try {
965
+ return await crypto.subtle.verify(algorithm, cryptoKey, signature, data);
966
+ } catch {
967
+ return false;
968
+ }
969
+ };
970
+ async function flattenedVerify(jws, key, options) {
971
+ if (!isObject(jws)) {
972
+ throw new JWSInvalid("Flattened JWS must be an object");
973
+ }
974
+ if (jws.protected === void 0 && jws.header === void 0) {
975
+ throw new JWSInvalid('Flattened JWS must have either of the "protected" or "header" members');
976
+ }
977
+ if (jws.protected !== void 0 && typeof jws.protected !== "string") {
978
+ throw new JWSInvalid("JWS Protected Header incorrect type");
979
+ }
980
+ if (jws.payload === void 0) {
981
+ throw new JWSInvalid("JWS Payload missing");
982
+ }
983
+ if (typeof jws.signature !== "string") {
984
+ throw new JWSInvalid("JWS Signature missing or incorrect type");
985
+ }
986
+ if (jws.header !== void 0 && !isObject(jws.header)) {
987
+ throw new JWSInvalid("JWS Unprotected Header incorrect type");
988
+ }
989
+ let parsedProt = {};
990
+ if (jws.protected) {
991
+ try {
992
+ const protectedHeader = decode(jws.protected);
993
+ parsedProt = JSON.parse(decoder.decode(protectedHeader));
994
+ } catch {
995
+ throw new JWSInvalid("JWS Protected Header is invalid");
996
+ }
997
+ }
998
+ if (!isDisjoint(parsedProt, jws.header)) {
999
+ throw new JWSInvalid("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
1000
+ }
1001
+ const joseHeader = {
1002
+ ...parsedProt,
1003
+ ...jws.header
1004
+ };
1005
+ const extensions = validateCrit(JWSInvalid, /* @__PURE__ */ new Map([["b64", true]]), options?.crit, parsedProt, joseHeader);
1006
+ let b64 = true;
1007
+ if (extensions.has("b64")) {
1008
+ b64 = parsedProt.b64;
1009
+ if (typeof b64 !== "boolean") {
1010
+ throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
1011
+ }
1012
+ }
1013
+ const { alg } = joseHeader;
1014
+ if (typeof alg !== "string" || !alg) {
1015
+ throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
1016
+ }
1017
+ const algorithms = options && validateAlgorithms("algorithms", options.algorithms);
1018
+ if (algorithms && !algorithms.has(alg)) {
1019
+ throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
1020
+ }
1021
+ if (b64) {
1022
+ if (typeof jws.payload !== "string") {
1023
+ throw new JWSInvalid("JWS Payload must be a string");
1024
+ }
1025
+ } else if (typeof jws.payload !== "string" && !(jws.payload instanceof Uint8Array)) {
1026
+ throw new JWSInvalid("JWS Payload must be a string or an Uint8Array instance");
1027
+ }
1028
+ let resolvedKey = false;
1029
+ if (typeof key === "function") {
1030
+ key = await key(parsedProt, jws);
1031
+ resolvedKey = true;
1032
+ }
1033
+ checkKeyType(alg, key, "verify");
1034
+ const data = concat(encoder.encode(jws.protected ?? ""), encoder.encode("."), typeof jws.payload === "string" ? encoder.encode(jws.payload) : jws.payload);
1035
+ let signature;
1036
+ try {
1037
+ signature = decode(jws.signature);
1038
+ } catch {
1039
+ throw new JWSInvalid("Failed to base64url decode the signature");
1040
+ }
1041
+ const k = await normalizeKey(key, alg);
1042
+ const verified = await verify(alg, k, signature, data);
1043
+ if (!verified) {
1044
+ throw new JWSSignatureVerificationFailed();
1045
+ }
1046
+ let payload;
1047
+ if (b64) {
1048
+ try {
1049
+ payload = decode(jws.payload);
1050
+ } catch {
1051
+ throw new JWSInvalid("Failed to base64url decode the payload");
1052
+ }
1053
+ } else if (typeof jws.payload === "string") {
1054
+ payload = encoder.encode(jws.payload);
1055
+ } else {
1056
+ payload = jws.payload;
1057
+ }
1058
+ const result = { payload };
1059
+ if (jws.protected !== void 0) {
1060
+ result.protectedHeader = parsedProt;
1061
+ }
1062
+ if (jws.header !== void 0) {
1063
+ result.unprotectedHeader = jws.header;
1064
+ }
1065
+ if (resolvedKey) {
1066
+ return { ...result, key: k };
1067
+ }
1068
+ return result;
1069
+ }
1070
+ async function compactVerify(jws, key, options) {
1071
+ if (jws instanceof Uint8Array) {
1072
+ jws = decoder.decode(jws);
1073
+ }
1074
+ if (typeof jws !== "string") {
1075
+ throw new JWSInvalid("Compact JWS must be a string or Uint8Array");
1076
+ }
1077
+ const { 0: protectedHeader, 1: payload, 2: signature, length } = jws.split(".");
1078
+ if (length !== 3) {
1079
+ throw new JWSInvalid("Invalid Compact JWS");
1080
+ }
1081
+ const verified = await flattenedVerify({ payload, protected: protectedHeader, signature }, key, options);
1082
+ const result = { payload: verified.payload, protectedHeader: verified.protectedHeader };
1083
+ if (typeof key === "function") {
1084
+ return { ...result, key: verified.key };
1085
+ }
1086
+ return result;
1087
+ }
1088
+ const epoch = (date) => Math.floor(date.getTime() / 1e3);
1089
+ const minute = 60;
1090
+ const hour = minute * 60;
1091
+ const day = hour * 24;
1092
+ const week = day * 7;
1093
+ const year = day * 365.25;
1094
+ const REGEX = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i;
1095
+ const secs = (str) => {
1096
+ const matched = REGEX.exec(str);
1097
+ if (!matched || matched[4] && matched[1]) {
1098
+ throw new TypeError("Invalid time period format");
1099
+ }
1100
+ const value = parseFloat(matched[2]);
1101
+ const unit = matched[3].toLowerCase();
1102
+ let numericDate;
1103
+ switch (unit) {
1104
+ case "sec":
1105
+ case "secs":
1106
+ case "second":
1107
+ case "seconds":
1108
+ case "s":
1109
+ numericDate = Math.round(value);
1110
+ break;
1111
+ case "minute":
1112
+ case "minutes":
1113
+ case "min":
1114
+ case "mins":
1115
+ case "m":
1116
+ numericDate = Math.round(value * minute);
1117
+ break;
1118
+ case "hour":
1119
+ case "hours":
1120
+ case "hr":
1121
+ case "hrs":
1122
+ case "h":
1123
+ numericDate = Math.round(value * hour);
1124
+ break;
1125
+ case "day":
1126
+ case "days":
1127
+ case "d":
1128
+ numericDate = Math.round(value * day);
1129
+ break;
1130
+ case "week":
1131
+ case "weeks":
1132
+ case "w":
1133
+ numericDate = Math.round(value * week);
1134
+ break;
1135
+ default:
1136
+ numericDate = Math.round(value * year);
1137
+ break;
1138
+ }
1139
+ if (matched[1] === "-" || matched[4] === "ago") {
1140
+ return -numericDate;
1141
+ }
1142
+ return numericDate;
1143
+ };
1144
+ const normalizeTyp = (value) => {
1145
+ if (value.includes("/")) {
1146
+ return value.toLowerCase();
1147
+ }
1148
+ return `application/${value.toLowerCase()}`;
1149
+ };
1150
+ const checkAudiencePresence = (audPayload, audOption) => {
1151
+ if (typeof audPayload === "string") {
1152
+ return audOption.includes(audPayload);
1153
+ }
1154
+ if (Array.isArray(audPayload)) {
1155
+ return audOption.some(Set.prototype.has.bind(new Set(audPayload)));
1156
+ }
1157
+ return false;
1158
+ };
1159
+ function validateClaimsSet(protectedHeader, encodedPayload, options = {}) {
1160
+ let payload;
1161
+ try {
1162
+ payload = JSON.parse(decoder.decode(encodedPayload));
1163
+ } catch {
1164
+ }
1165
+ if (!isObject(payload)) {
1166
+ throw new JWTInvalid("JWT Claims Set must be a top-level JSON object");
1167
+ }
1168
+ const { typ } = options;
1169
+ if (typ && (typeof protectedHeader.typ !== "string" || normalizeTyp(protectedHeader.typ) !== normalizeTyp(typ))) {
1170
+ throw new JWTClaimValidationFailed('unexpected "typ" JWT header value', payload, "typ", "check_failed");
1171
+ }
1172
+ const { requiredClaims = [], issuer, subject, audience, maxTokenAge } = options;
1173
+ const presenceCheck = [...requiredClaims];
1174
+ if (maxTokenAge !== void 0)
1175
+ presenceCheck.push("iat");
1176
+ if (audience !== void 0)
1177
+ presenceCheck.push("aud");
1178
+ if (subject !== void 0)
1179
+ presenceCheck.push("sub");
1180
+ if (issuer !== void 0)
1181
+ presenceCheck.push("iss");
1182
+ for (const claim of new Set(presenceCheck.reverse())) {
1183
+ if (!(claim in payload)) {
1184
+ throw new JWTClaimValidationFailed(`missing required "${claim}" claim`, payload, claim, "missing");
1185
+ }
1186
+ }
1187
+ if (issuer && !(Array.isArray(issuer) ? issuer : [issuer]).includes(payload.iss)) {
1188
+ throw new JWTClaimValidationFailed('unexpected "iss" claim value', payload, "iss", "check_failed");
1189
+ }
1190
+ if (subject && payload.sub !== subject) {
1191
+ throw new JWTClaimValidationFailed('unexpected "sub" claim value', payload, "sub", "check_failed");
1192
+ }
1193
+ if (audience && !checkAudiencePresence(payload.aud, typeof audience === "string" ? [audience] : audience)) {
1194
+ throw new JWTClaimValidationFailed('unexpected "aud" claim value', payload, "aud", "check_failed");
1195
+ }
1196
+ let tolerance;
1197
+ switch (typeof options.clockTolerance) {
1198
+ case "string":
1199
+ tolerance = secs(options.clockTolerance);
1200
+ break;
1201
+ case "number":
1202
+ tolerance = options.clockTolerance;
1203
+ break;
1204
+ case "undefined":
1205
+ tolerance = 0;
1206
+ break;
1207
+ default:
1208
+ throw new TypeError("Invalid clockTolerance option type");
1209
+ }
1210
+ const { currentDate } = options;
1211
+ const now = epoch(currentDate || /* @__PURE__ */ new Date());
1212
+ if ((payload.iat !== void 0 || maxTokenAge) && typeof payload.iat !== "number") {
1213
+ throw new JWTClaimValidationFailed('"iat" claim must be a number', payload, "iat", "invalid");
1214
+ }
1215
+ if (payload.nbf !== void 0) {
1216
+ if (typeof payload.nbf !== "number") {
1217
+ throw new JWTClaimValidationFailed('"nbf" claim must be a number', payload, "nbf", "invalid");
1218
+ }
1219
+ if (payload.nbf > now + tolerance) {
1220
+ throw new JWTClaimValidationFailed('"nbf" claim timestamp check failed', payload, "nbf", "check_failed");
1221
+ }
1222
+ }
1223
+ if (payload.exp !== void 0) {
1224
+ if (typeof payload.exp !== "number") {
1225
+ throw new JWTClaimValidationFailed('"exp" claim must be a number', payload, "exp", "invalid");
1226
+ }
1227
+ if (payload.exp <= now - tolerance) {
1228
+ throw new JWTExpired('"exp" claim timestamp check failed', payload, "exp", "check_failed");
1229
+ }
1230
+ }
1231
+ if (maxTokenAge) {
1232
+ const age = now - payload.iat;
1233
+ const max = typeof maxTokenAge === "number" ? maxTokenAge : secs(maxTokenAge);
1234
+ if (age - tolerance > max) {
1235
+ throw new JWTExpired('"iat" claim timestamp check failed (too far in the past)', payload, "iat", "check_failed");
1236
+ }
1237
+ if (age < 0 - tolerance) {
1238
+ throw new JWTClaimValidationFailed('"iat" claim timestamp check failed (it should be in the past)', payload, "iat", "check_failed");
1239
+ }
1240
+ }
1241
+ return payload;
1242
+ }
1243
+ async function jwtVerify(jwt, key, options) {
1244
+ const verified = await compactVerify(jwt, key, options);
1245
+ if (verified.protectedHeader.crit?.includes("b64") && verified.protectedHeader.b64 === false) {
1246
+ throw new JWTInvalid("JWTs MUST NOT use unencoded payload");
1247
+ }
1248
+ const payload = validateClaimsSet(verified.protectedHeader, verified.payload, options);
1249
+ const result = { payload, protectedHeader: verified.protectedHeader };
1250
+ if (typeof key === "function") {
1251
+ return { ...result, key: verified.key };
1252
+ }
1253
+ return result;
1254
+ }
1255
+ function getKtyFromAlg(alg) {
1256
+ switch (typeof alg === "string" && alg.slice(0, 2)) {
1257
+ case "RS":
1258
+ case "PS":
1259
+ return "RSA";
1260
+ case "ES":
1261
+ return "EC";
1262
+ case "Ed":
1263
+ return "OKP";
1264
+ case "ML":
1265
+ return "AKP";
1266
+ default:
1267
+ throw new JOSENotSupported('Unsupported "alg" value for a JSON Web Key Set');
1268
+ }
1269
+ }
1270
+ function isJWKSLike(jwks) {
1271
+ return jwks && typeof jwks === "object" && Array.isArray(jwks.keys) && jwks.keys.every(isJWKLike);
1272
+ }
1273
+ function isJWKLike(key) {
1274
+ return isObject(key);
1275
+ }
1276
+ class LocalJWKSet {
1277
+ constructor(jwks) {
1278
+ __privateAdd(this, _jwks);
1279
+ __privateAdd(this, _cached, /* @__PURE__ */ new WeakMap());
1280
+ if (!isJWKSLike(jwks)) {
1281
+ throw new JWKSInvalid("JSON Web Key Set malformed");
1282
+ }
1283
+ __privateSet(this, _jwks, structuredClone(jwks));
1284
+ }
1285
+ jwks() {
1286
+ return __privateGet(this, _jwks);
1287
+ }
1288
+ async getKey(protectedHeader, token) {
1289
+ const { alg, kid } = { ...protectedHeader, ...token?.header };
1290
+ const kty = getKtyFromAlg(alg);
1291
+ const candidates = __privateGet(this, _jwks).keys.filter((jwk2) => {
1292
+ let candidate = kty === jwk2.kty;
1293
+ if (candidate && typeof kid === "string") {
1294
+ candidate = kid === jwk2.kid;
1295
+ }
1296
+ if (candidate && (typeof jwk2.alg === "string" || kty === "AKP")) {
1297
+ candidate = alg === jwk2.alg;
1298
+ }
1299
+ if (candidate && typeof jwk2.use === "string") {
1300
+ candidate = jwk2.use === "sig";
1301
+ }
1302
+ if (candidate && Array.isArray(jwk2.key_ops)) {
1303
+ candidate = jwk2.key_ops.includes("verify");
1304
+ }
1305
+ if (candidate) {
1306
+ switch (alg) {
1307
+ case "ES256":
1308
+ candidate = jwk2.crv === "P-256";
1309
+ break;
1310
+ case "ES384":
1311
+ candidate = jwk2.crv === "P-384";
1312
+ break;
1313
+ case "ES512":
1314
+ candidate = jwk2.crv === "P-521";
1315
+ break;
1316
+ case "Ed25519":
1317
+ case "EdDSA":
1318
+ candidate = jwk2.crv === "Ed25519";
1319
+ break;
1320
+ }
1321
+ }
1322
+ return candidate;
1323
+ });
1324
+ const { 0: jwk, length } = candidates;
1325
+ if (length === 0) {
1326
+ throw new JWKSNoMatchingKey();
1327
+ }
1328
+ if (length !== 1) {
1329
+ const error = new JWKSMultipleMatchingKeys();
1330
+ const _cached2 = __privateGet(this, _cached);
1331
+ error[Symbol.asyncIterator] = async function* () {
1332
+ for (const jwk2 of candidates) {
1333
+ try {
1334
+ yield await importWithAlgCache(_cached2, jwk2, alg);
1335
+ } catch {
1336
+ }
1337
+ }
1338
+ };
1339
+ throw error;
1340
+ }
1341
+ return importWithAlgCache(__privateGet(this, _cached), jwk, alg);
1342
+ }
1343
+ }
1344
+ _jwks = new WeakMap();
1345
+ _cached = new WeakMap();
1346
+ async function importWithAlgCache(cache2, jwk, alg) {
1347
+ const cached = cache2.get(jwk) || cache2.set(jwk, {}).get(jwk);
1348
+ if (cached[alg] === void 0) {
1349
+ const key = await importJWK({ ...jwk, ext: true }, alg);
1350
+ if (key instanceof Uint8Array || key.type !== "public") {
1351
+ throw new JWKSInvalid("JSON Web Key Set members must be public keys");
1352
+ }
1353
+ cached[alg] = key;
1354
+ }
1355
+ return cached[alg];
1356
+ }
1357
+ function createLocalJWKSet(jwks) {
1358
+ const set = new LocalJWKSet(jwks);
1359
+ const localJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
1360
+ Object.defineProperties(localJWKSet, {
1361
+ jwks: {
1362
+ value: () => structuredClone(set.jwks()),
1363
+ enumerable: false,
1364
+ configurable: false,
1365
+ writable: false
1366
+ }
1367
+ });
1368
+ return localJWKSet;
1369
+ }
1370
+ function isCloudflareWorkers() {
1371
+ return typeof WebSocketPair !== "undefined" || typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers" || typeof EdgeRuntime !== "undefined" && EdgeRuntime === "vercel";
1372
+ }
1373
+ let USER_AGENT;
1374
+ if (typeof navigator === "undefined" || !navigator.userAgent?.startsWith?.("Mozilla/5.0 ")) {
1375
+ const NAME = "jose";
1376
+ const VERSION = "v6.1.0";
1377
+ USER_AGENT = `${NAME}/${VERSION}`;
1378
+ }
1379
+ const customFetch = Symbol();
1380
+ async function fetchJwks(url, headers, signal, fetchImpl = fetch) {
1381
+ const response = await fetchImpl(url, {
1382
+ method: "GET",
1383
+ signal,
1384
+ redirect: "manual",
1385
+ headers
1386
+ }).catch((err) => {
1387
+ if (err.name === "TimeoutError") {
1388
+ throw new JWKSTimeout();
1389
+ }
1390
+ throw err;
1391
+ });
1392
+ if (response.status !== 200) {
1393
+ throw new JOSEError("Expected 200 OK from the JSON Web Key Set HTTP response");
1394
+ }
1395
+ try {
1396
+ return await response.json();
1397
+ } catch {
1398
+ throw new JOSEError("Failed to parse the JSON Web Key Set HTTP response as JSON");
1399
+ }
1400
+ }
1401
+ const jwksCache = Symbol();
1402
+ function isFreshJwksCache(input, cacheMaxAge) {
1403
+ if (typeof input !== "object" || input === null) {
1404
+ return false;
1405
+ }
1406
+ if (!("uat" in input) || typeof input.uat !== "number" || Date.now() - input.uat >= cacheMaxAge) {
1407
+ return false;
1408
+ }
1409
+ if (!("jwks" in input) || !isObject(input.jwks) || !Array.isArray(input.jwks.keys) || !Array.prototype.every.call(input.jwks.keys, isObject)) {
1410
+ return false;
1411
+ }
1412
+ return true;
1413
+ }
1414
+ class RemoteJWKSet {
1415
+ constructor(url, options) {
1416
+ __privateAdd(this, _url);
1417
+ __privateAdd(this, _timeoutDuration);
1418
+ __privateAdd(this, _cooldownDuration);
1419
+ __privateAdd(this, _cacheMaxAge);
1420
+ __privateAdd(this, _jwksTimestamp);
1421
+ __privateAdd(this, _pendingFetch);
1422
+ __privateAdd(this, _headers);
1423
+ __privateAdd(this, _customFetch);
1424
+ __privateAdd(this, _local);
1425
+ __privateAdd(this, _cache);
1426
+ if (!(url instanceof URL)) {
1427
+ throw new TypeError("url must be an instance of URL");
1428
+ }
1429
+ __privateSet(this, _url, new URL(url.href));
1430
+ __privateSet(this, _timeoutDuration, typeof options?.timeoutDuration === "number" ? options?.timeoutDuration : 5e3);
1431
+ __privateSet(this, _cooldownDuration, typeof options?.cooldownDuration === "number" ? options?.cooldownDuration : 3e4);
1432
+ __privateSet(this, _cacheMaxAge, typeof options?.cacheMaxAge === "number" ? options?.cacheMaxAge : 6e5);
1433
+ __privateSet(this, _headers, new Headers(options?.headers));
1434
+ if (USER_AGENT && !__privateGet(this, _headers).has("User-Agent")) {
1435
+ __privateGet(this, _headers).set("User-Agent", USER_AGENT);
1436
+ }
1437
+ if (!__privateGet(this, _headers).has("accept")) {
1438
+ __privateGet(this, _headers).set("accept", "application/json");
1439
+ __privateGet(this, _headers).append("accept", "application/jwk-set+json");
1440
+ }
1441
+ __privateSet(this, _customFetch, options?.[customFetch]);
1442
+ if (options?.[jwksCache] !== void 0) {
1443
+ __privateSet(this, _cache, options?.[jwksCache]);
1444
+ if (isFreshJwksCache(options?.[jwksCache], __privateGet(this, _cacheMaxAge))) {
1445
+ __privateSet(this, _jwksTimestamp, __privateGet(this, _cache).uat);
1446
+ __privateSet(this, _local, createLocalJWKSet(__privateGet(this, _cache).jwks));
1447
+ }
1448
+ }
1449
+ }
1450
+ pendingFetch() {
1451
+ return !!__privateGet(this, _pendingFetch);
1452
+ }
1453
+ coolingDown() {
1454
+ return typeof __privateGet(this, _jwksTimestamp) === "number" ? Date.now() < __privateGet(this, _jwksTimestamp) + __privateGet(this, _cooldownDuration) : false;
1455
+ }
1456
+ fresh() {
1457
+ return typeof __privateGet(this, _jwksTimestamp) === "number" ? Date.now() < __privateGet(this, _jwksTimestamp) + __privateGet(this, _cacheMaxAge) : false;
1458
+ }
1459
+ jwks() {
1460
+ return __privateGet(this, _local)?.jwks();
1461
+ }
1462
+ async getKey(protectedHeader, token) {
1463
+ if (!__privateGet(this, _local) || !this.fresh()) {
1464
+ await this.reload();
1465
+ }
1466
+ try {
1467
+ return await __privateGet(this, _local).call(this, protectedHeader, token);
1468
+ } catch (err) {
1469
+ if (err instanceof JWKSNoMatchingKey) {
1470
+ if (this.coolingDown() === false) {
1471
+ await this.reload();
1472
+ return __privateGet(this, _local).call(this, protectedHeader, token);
1473
+ }
1474
+ }
1475
+ throw err;
1476
+ }
1477
+ }
1478
+ async reload() {
1479
+ if (__privateGet(this, _pendingFetch) && isCloudflareWorkers()) {
1480
+ __privateSet(this, _pendingFetch, void 0);
1481
+ }
1482
+ __privateGet(this, _pendingFetch) || __privateSet(this, _pendingFetch, fetchJwks(__privateGet(this, _url).href, __privateGet(this, _headers), AbortSignal.timeout(__privateGet(this, _timeoutDuration)), __privateGet(this, _customFetch)).then((json) => {
1483
+ __privateSet(this, _local, createLocalJWKSet(json));
1484
+ if (__privateGet(this, _cache)) {
1485
+ __privateGet(this, _cache).uat = Date.now();
1486
+ __privateGet(this, _cache).jwks = json;
1487
+ }
1488
+ __privateSet(this, _jwksTimestamp, Date.now());
1489
+ __privateSet(this, _pendingFetch, void 0);
1490
+ }).catch((err) => {
1491
+ __privateSet(this, _pendingFetch, void 0);
1492
+ throw err;
1493
+ }));
1494
+ await __privateGet(this, _pendingFetch);
1495
+ }
1496
+ }
1497
+ _url = new WeakMap();
1498
+ _timeoutDuration = new WeakMap();
1499
+ _cooldownDuration = new WeakMap();
1500
+ _cacheMaxAge = new WeakMap();
1501
+ _jwksTimestamp = new WeakMap();
1502
+ _pendingFetch = new WeakMap();
1503
+ _headers = new WeakMap();
1504
+ _customFetch = new WeakMap();
1505
+ _local = new WeakMap();
1506
+ _cache = new WeakMap();
1507
+ function createRemoteJWKSet(url, options) {
1508
+ const set = new RemoteJWKSet(url, options);
1509
+ const remoteJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
1510
+ Object.defineProperties(remoteJWKSet, {
1511
+ coolingDown: {
1512
+ get: () => set.coolingDown(),
1513
+ enumerable: true,
1514
+ configurable: false
1515
+ },
1516
+ fresh: {
1517
+ get: () => set.fresh(),
1518
+ enumerable: true,
1519
+ configurable: false
1520
+ },
1521
+ reload: {
1522
+ value: () => set.reload(),
1523
+ enumerable: true,
1524
+ configurable: false,
1525
+ writable: false
1526
+ },
1527
+ reloading: {
1528
+ get: () => set.pendingFetch(),
1529
+ enumerable: true,
1530
+ configurable: false
1531
+ },
1532
+ jwks: {
1533
+ value: () => set.jwks(),
1534
+ enumerable: true,
1535
+ configurable: false,
1536
+ writable: false
1537
+ }
1538
+ });
1539
+ return remoteJWKSet;
1540
+ }
1541
+ class Logger {
1542
+ constructor(debug = false, prefix = "[MarketplaceSDK]") {
1543
+ this.debug = debug;
1544
+ this.prefix = prefix;
1545
+ }
1546
+ /**
1547
+ * Log debug message (only if debug enabled)
1548
+ */
1549
+ log(...args) {
1550
+ if (this.debug) {
1551
+ console.log(this.prefix, ...args);
1552
+ }
1553
+ }
1554
+ /**
1555
+ * Log info message (only if debug enabled)
1556
+ */
1557
+ info(...args) {
1558
+ if (this.debug) {
1559
+ console.info(this.prefix, ...args);
1560
+ }
1561
+ }
1562
+ /**
1563
+ * Log warning message (always shown)
1564
+ */
1565
+ warn(...args) {
1566
+ console.warn(this.prefix, ...args);
1567
+ }
1568
+ /**
1569
+ * Log error message (always shown)
1570
+ */
1571
+ error(...args) {
1572
+ console.error(this.prefix, ...args);
1573
+ }
1574
+ }
1575
+ class JWKSValidator {
1576
+ constructor(jwksUri, debug = false) {
1577
+ this.jwksUri = jwksUri;
1578
+ this.logger = new Logger(debug, "[JWKSValidator]");
1579
+ this.jwks = createRemoteJWKSet(new URL(this.jwksUri));
1580
+ this.logger.info("Initialized with JWKS URI:", this.jwksUri);
1581
+ }
1582
+ /**
1583
+ * Verify JWT signature using JWKS public key
1584
+ * @param token - JWT token to verify
1585
+ * @param expectedIssuer - Expected issuer (default: 'generalwisdom.com')
1586
+ * @param expectedApplicationId - Optional application ID to validate
1587
+ * @returns Decoded and verified JWT claims
1588
+ */
1589
+ async verify(token, expectedIssuer = "generalwisdom.com", expectedApplicationId) {
1590
+ this.logger.log("Verifying JWT signature...");
1591
+ try {
1592
+ const result = await jwtVerify(token, this.jwks, {
1593
+ issuer: expectedIssuer,
1594
+ algorithms: ["RS256"]
1595
+ });
1596
+ const payload = result.payload;
1597
+ const requiredClaims = [
1598
+ "sessionId",
1599
+ "userId",
1600
+ "orgId",
1601
+ "applicationId",
1602
+ "exp",
1603
+ "iat"
1604
+ ];
1605
+ for (const claim of requiredClaims) {
1606
+ if (!(claim in payload)) {
1607
+ throw new SDKError(
1608
+ `Missing required claim: ${claim}`,
1609
+ "MISSING_CLAIM"
1610
+ );
1611
+ }
1612
+ }
1613
+ if (expectedApplicationId && payload.applicationId !== expectedApplicationId) {
1614
+ this.logger.error(
1615
+ `Application ID mismatch: expected ${expectedApplicationId}, got ${payload.applicationId}`
1616
+ );
1617
+ throw new SDKError(
1618
+ "Token is for a different application",
1619
+ "APPLICATION_MISMATCH"
1620
+ );
1621
+ }
1622
+ const claims = {
1623
+ sessionId: payload.sessionId,
1624
+ applicationId: payload.applicationId,
1625
+ userId: payload.userId,
1626
+ orgId: payload.orgId,
1627
+ startTime: payload.startTime,
1628
+ durationMinutes: payload.durationMinutes,
1629
+ iat: payload.iat,
1630
+ exp: payload.exp,
1631
+ iss: payload.iss,
1632
+ sub: payload.sub
1633
+ };
1634
+ this.logger.log("JWT verified successfully");
1635
+ return claims;
1636
+ } catch (error) {
1637
+ this.logger.error("JWT verification failed:", error);
1638
+ if (error instanceof SDKError) {
1639
+ throw error;
1640
+ }
1641
+ const errorMessage = error instanceof Error ? error.message : "Unknown error";
1642
+ const errorName = error instanceof Error ? error.name : "Error";
1643
+ if (errorName === "JWTExpired" || errorMessage.includes("expired")) {
1644
+ throw new SDKError("Session expired", "SESSION_EXPIRED");
1645
+ }
1646
+ if (errorName === "JWSSignatureVerificationFailed") {
1647
+ throw new SDKError("Invalid JWT signature", "INVALID_SIGNATURE");
1648
+ }
1649
+ if (errorName === "JWTClaimValidationFailed") {
1650
+ throw new SDKError(`JWT claim validation failed: ${errorMessage}`, "INVALID_CLAIM");
1651
+ }
1652
+ throw new SDKError(
1653
+ `JWT verification failed: ${errorMessage}`,
1654
+ "VERIFICATION_FAILED"
1655
+ );
1656
+ }
1657
+ }
1658
+ /**
1659
+ * Update JWKS URI
1660
+ * @param jwksUri - New JWKS URI
1661
+ */
1662
+ updateJwksUri(jwksUri) {
1663
+ this.jwksUri = jwksUri;
1664
+ this.jwks = createRemoteJWKSet(new URL(this.jwksUri));
1665
+ this.logger.info("Updated JWKS URI:", this.jwksUri);
1666
+ }
1667
+ }
1668
+ class TimerManager {
1669
+ constructor(durationSeconds, warningThresholdSeconds = 300, events = {}, debug = false) {
1670
+ this.intervalId = null;
1671
+ this.warningShown = false;
1672
+ this.remainingSeconds = durationSeconds;
1673
+ this.warningThreshold = warningThresholdSeconds;
1674
+ this.events = events;
1675
+ this.logger = new Logger(debug, "[TimerManager]");
1676
+ this.logger.log("Initialized with duration:", durationSeconds, "seconds");
1677
+ }
1678
+ /**
1679
+ * Start countdown timer
1680
+ */
1681
+ start() {
1682
+ if (this.intervalId !== null) {
1683
+ this.logger.warn("Timer already running");
1684
+ return;
1685
+ }
1686
+ this.logger.log("Starting timer with", this.remainingSeconds, "seconds remaining");
1687
+ this.intervalId = window.setInterval(() => {
1688
+ this.remainingSeconds--;
1689
+ if (!this.warningShown && this.remainingSeconds <= this.warningThreshold && this.remainingSeconds > 0) {
1690
+ this.warningShown = true;
1691
+ this.logger.warn("Warning threshold reached:", this.remainingSeconds, "seconds remaining");
1692
+ this.events.onSessionWarning?.({
1693
+ remainingSeconds: this.remainingSeconds
1694
+ });
1695
+ }
1696
+ if (this.remainingSeconds <= 0) {
1697
+ this.logger.warn("Session expired");
1698
+ this.stop();
1699
+ this.events.onSessionEnd?.();
1700
+ }
1701
+ }, 1e3);
1702
+ }
1703
+ /**
1704
+ * Stop timer
1705
+ */
1706
+ stop() {
1707
+ if (this.intervalId !== null) {
1708
+ clearInterval(this.intervalId);
1709
+ this.intervalId = null;
1710
+ this.logger.log("Timer stopped");
1711
+ }
1712
+ }
1713
+ /**
1714
+ * Pause timer
1715
+ */
1716
+ pause() {
1717
+ this.stop();
1718
+ this.logger.log("Timer paused at:", this.remainingSeconds, "seconds");
1719
+ }
1720
+ /**
1721
+ * Resume timer
1722
+ */
1723
+ resume() {
1724
+ if (this.intervalId === null && this.remainingSeconds > 0) {
1725
+ this.start();
1726
+ this.logger.log("Timer resumed at:", this.remainingSeconds, "seconds");
1727
+ }
1728
+ }
1729
+ /**
1730
+ * Get remaining time in seconds
1731
+ */
1732
+ getRemainingSeconds() {
1733
+ return this.remainingSeconds;
1734
+ }
1735
+ /**
1736
+ * Get formatted time string (MM:SS)
1737
+ */
1738
+ getFormattedTime() {
1739
+ const minutes = Math.floor(this.remainingSeconds / 60);
1740
+ const seconds = this.remainingSeconds % 60;
1741
+ return `${minutes}:${seconds.toString().padStart(2, "0")}`;
1742
+ }
1743
+ /**
1744
+ * Get formatted time string with hours (HH:MM:SS)
1745
+ */
1746
+ getFormattedTimeWithHours() {
1747
+ const hours = Math.floor(this.remainingSeconds / 3600);
1748
+ const minutes = Math.floor(this.remainingSeconds % 3600 / 60);
1749
+ const seconds = this.remainingSeconds % 60;
1750
+ if (hours > 0) {
1751
+ return `${hours}:${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
1752
+ }
1753
+ return this.getFormattedTime();
1754
+ }
1755
+ /**
1756
+ * Check if timer is running
1757
+ */
1758
+ isRunning() {
1759
+ return this.intervalId !== null;
1760
+ }
1761
+ /**
1762
+ * Check if warning has been shown
1763
+ */
1764
+ hasWarningBeenShown() {
1765
+ return this.warningShown;
1766
+ }
1767
+ /**
1768
+ * Update remaining time (useful for syncing with server)
1769
+ */
1770
+ updateRemainingTime(seconds) {
1771
+ this.remainingSeconds = seconds;
1772
+ this.logger.log("Remaining time updated to:", seconds, "seconds");
1773
+ }
1774
+ }
1775
+ class HeartbeatManager {
1776
+ constructor(sessionId, apiEndpoint, token, onSync, onError, heartbeatIntervalSeconds = 30, debug = false) {
1777
+ this.sessionId = sessionId;
1778
+ this.apiEndpoint = apiEndpoint;
1779
+ this.token = token;
1780
+ this.onSync = onSync;
1781
+ this.onError = onError;
1782
+ this.intervalId = null;
1783
+ this.failureCount = 0;
1784
+ this.maxFailures = 3;
1785
+ this.isEnabled = false;
1786
+ this.heartbeatInterval = heartbeatIntervalSeconds * 1e3;
1787
+ this.logger = new Logger(debug, "[HeartbeatManager]");
1788
+ this.logger.log("Initialized with", heartbeatIntervalSeconds, "second interval");
1789
+ }
1790
+ /**
1791
+ * Start sending heartbeats
1792
+ */
1793
+ start() {
1794
+ if (this.intervalId !== null) {
1795
+ this.logger.warn("Heartbeat already running");
1796
+ return;
1797
+ }
1798
+ this.isEnabled = true;
1799
+ this.logger.log("Starting heartbeat for session:", this.sessionId);
1800
+ this.sendHeartbeat();
1801
+ this.intervalId = window.setInterval(() => {
1802
+ this.sendHeartbeat();
1803
+ }, this.heartbeatInterval);
1804
+ }
1805
+ /**
1806
+ * Stop sending heartbeats
1807
+ */
1808
+ stop() {
1809
+ if (this.intervalId !== null) {
1810
+ clearInterval(this.intervalId);
1811
+ this.intervalId = null;
1812
+ this.isEnabled = false;
1813
+ this.logger.log("Heartbeat stopped");
1814
+ }
1815
+ }
1816
+ /**
1817
+ * Send a single heartbeat to backend
1818
+ */
1819
+ async sendHeartbeat() {
1820
+ if (!this.isEnabled) {
1821
+ return;
1822
+ }
1823
+ this.logger.log("Sending heartbeat...");
1824
+ try {
1825
+ const response = await fetch(
1826
+ `${this.apiEndpoint}/sessions/${this.sessionId}/heartbeat`,
1827
+ {
1828
+ method: "POST",
1829
+ headers: {
1830
+ "Authorization": `Bearer ${this.token}`,
1831
+ "Content-Type": "application/json"
1832
+ },
1833
+ body: JSON.stringify({
1834
+ timestamp: Date.now(),
1835
+ active: true
1836
+ })
1837
+ }
1838
+ );
1839
+ if (!response.ok) {
1840
+ throw new SDKError(
1841
+ `Heartbeat failed with status ${response.status}`,
1842
+ "HEARTBEAT_FAILED",
1843
+ response.status
1844
+ );
1845
+ }
1846
+ const data = await response.json();
1847
+ this.failureCount = 0;
1848
+ if (typeof data.remaining_seconds === "number") {
1849
+ this.logger.log("Server reports", data.remaining_seconds, "seconds remaining");
1850
+ this.onSync?.(data.remaining_seconds);
1851
+ }
1852
+ this.logger.log("Heartbeat acknowledged");
1853
+ } catch (error) {
1854
+ this.failureCount++;
1855
+ this.logger.error("Heartbeat failed:", error, `(${this.failureCount}/${this.maxFailures})`);
1856
+ if (this.failureCount >= this.maxFailures) {
1857
+ this.logger.error("Max heartbeat failures reached, stopping");
1858
+ this.stop();
1859
+ const sdkError = error instanceof SDKError ? error : new SDKError(
1860
+ error instanceof Error ? error.message : "Heartbeat failed",
1861
+ "HEARTBEAT_ERROR"
1862
+ );
1863
+ this.onError?.(sdkError);
1864
+ }
1865
+ }
1866
+ }
1867
+ /**
1868
+ * Check if heartbeat is running
1869
+ */
1870
+ isRunning() {
1871
+ return this.intervalId !== null;
1872
+ }
1873
+ /**
1874
+ * Get failure count
1875
+ */
1876
+ getFailureCount() {
1877
+ return this.failureCount;
1878
+ }
1879
+ /**
1880
+ * Update heartbeat interval
1881
+ */
1882
+ updateInterval(seconds) {
1883
+ const wasRunning = this.isRunning();
1884
+ if (wasRunning) {
1885
+ this.stop();
1886
+ }
1887
+ this.heartbeatInterval = seconds * 1e3;
1888
+ this.logger.log("Heartbeat interval updated to", seconds, "seconds");
1889
+ if (wasRunning) {
1890
+ this.start();
1891
+ }
1892
+ }
1893
+ }
1894
+ class TabSyncManager {
1895
+ constructor(sessionId, onMessage, debug = false) {
1896
+ this.sessionId = sessionId;
1897
+ this.onMessage = onMessage;
1898
+ this.channel = null;
1899
+ this.isMaster = false;
1900
+ this.handleStorageEvent = (event) => {
1901
+ if (event.key === this.storageKey && event.newValue) {
1902
+ try {
1903
+ const data = JSON.parse(event.newValue);
1904
+ this.handleMessage(data);
1905
+ } catch (error) {
1906
+ this.logger.error("Failed to parse storage event:", error);
1907
+ }
1908
+ }
1909
+ };
1910
+ this.storageKey = `gw_session_sync_${sessionId}`;
1911
+ this.logger = new Logger(debug, "[TabSyncManager]");
1912
+ this.initialize();
1913
+ }
1914
+ /**
1915
+ * Initialize sync mechanism
1916
+ */
1917
+ initialize() {
1918
+ if (typeof BroadcastChannel !== "undefined") {
1919
+ this.logger.log("Using BroadcastChannel for tab sync");
1920
+ this.channel = new BroadcastChannel(`gw-session-${this.sessionId}`);
1921
+ this.channel.onmessage = (event) => {
1922
+ this.handleMessage(event.data);
1923
+ };
1924
+ } else {
1925
+ this.logger.log("Using localStorage for tab sync (fallback)");
1926
+ window.addEventListener("storage", this.handleStorageEvent);
1927
+ }
1928
+ this.electMaster();
1929
+ }
1930
+ /**
1931
+ * Broadcast message to other tabs
1932
+ */
1933
+ broadcast(type, data) {
1934
+ const message2 = {
1935
+ type,
1936
+ sessionId: this.sessionId,
1937
+ timestamp: Date.now(),
1938
+ ...data
1939
+ };
1940
+ if (this.channel) {
1941
+ this.channel.postMessage(message2);
1942
+ this.logger.log("Broadcasted:", type);
1943
+ } else {
1944
+ localStorage.setItem(this.storageKey, JSON.stringify(message2));
1945
+ this.logger.log("Broadcasted via localStorage:", type);
1946
+ }
1947
+ }
1948
+ /**
1949
+ * Handle incoming message
1950
+ */
1951
+ handleMessage(data) {
1952
+ if (data.sessionId !== this.sessionId) {
1953
+ return;
1954
+ }
1955
+ this.logger.log("Received message:", data.type);
1956
+ this.onMessage(data);
1957
+ }
1958
+ /**
1959
+ * Elect this tab as master if appropriate
1960
+ * Master tab is responsible for heartbeats
1961
+ */
1962
+ electMaster() {
1963
+ const masterKey = `gw_session_master_${this.sessionId}`;
1964
+ const existingMaster = localStorage.getItem(masterKey);
1965
+ if (!existingMaster) {
1966
+ this.isMaster = true;
1967
+ localStorage.setItem(masterKey, Date.now().toString());
1968
+ this.logger.log("Elected as master tab");
1969
+ setInterval(() => {
1970
+ if (this.isMaster) {
1971
+ localStorage.setItem(masterKey, Date.now().toString());
1972
+ }
1973
+ }, 5e3);
1974
+ window.addEventListener("beforeunload", () => {
1975
+ if (this.isMaster) {
1976
+ localStorage.removeItem(masterKey);
1977
+ this.logger.log("Removed master status");
1978
+ }
1979
+ });
1980
+ } else {
1981
+ this.logger.log("Another tab is master");
1982
+ setInterval(() => {
1983
+ const masterTimestamp = localStorage.getItem(masterKey);
1984
+ if (masterTimestamp) {
1985
+ const age = Date.now() - parseInt(masterTimestamp);
1986
+ if (age > 1e4) {
1987
+ this.logger.warn("Master tab appears dead, becoming master");
1988
+ this.isMaster = true;
1989
+ localStorage.setItem(masterKey, Date.now().toString());
1990
+ }
1991
+ }
1992
+ }, 5e3);
1993
+ }
1994
+ }
1995
+ /**
1996
+ * Check if this tab is the master
1997
+ */
1998
+ isMasterTab() {
1999
+ return this.isMaster;
2000
+ }
2001
+ /**
2002
+ * Cleanup and destroy
2003
+ */
2004
+ destroy() {
2005
+ if (this.channel) {
2006
+ this.channel.close();
2007
+ this.channel = null;
2008
+ } else {
2009
+ window.removeEventListener("storage", this.handleStorageEvent);
2010
+ }
2011
+ localStorage.removeItem(this.storageKey);
2012
+ if (this.isMaster) {
2013
+ localStorage.removeItem(`gw_session_master_${this.sessionId}`);
2014
+ }
2015
+ this.logger.log("Tab sync destroyed");
2016
+ }
2017
+ }
2018
+ const lightTheme = {
2019
+ colors: {
2020
+ // Background
2021
+ background: "hsl(0 0% 100%)",
2022
+ foreground: "hsl(222.2 84% 4.9%)",
2023
+ // Card
2024
+ card: "hsl(0 0% 100%)",
2025
+ cardForeground: "hsl(222.2 84% 4.9%)",
2026
+ // Popover
2027
+ popover: "hsl(0 0% 100%)",
2028
+ popoverForeground: "hsl(222.2 84% 4.9%)",
2029
+ // Primary (brand blue)
2030
+ primary: "hsl(221.2 83.2% 53.3%)",
2031
+ primaryForeground: "hsl(210 40% 98%)",
2032
+ // Secondary (light gray)
2033
+ secondary: "hsl(210 40% 96.1%)",
2034
+ secondaryForeground: "hsl(222.2 47.4% 11.2%)",
2035
+ // Muted
2036
+ muted: "hsl(210 40% 96.1%)",
2037
+ mutedForeground: "hsl(215.4 16.3% 46.9%)",
2038
+ // Accent
2039
+ accent: "hsl(210 40% 96.1%)",
2040
+ accentForeground: "hsl(222.2 47.4% 11.2%)",
2041
+ // Destructive (red)
2042
+ destructive: "hsl(0 84.2% 60.2%)",
2043
+ destructiveForeground: "hsl(210 40% 98%)",
2044
+ // Success (green)
2045
+ success: "hsl(142 76% 36%)",
2046
+ // Tailwind green-600 equivalent
2047
+ successForeground: "hsl(0 0% 100%)",
2048
+ // Border and input
2049
+ border: "hsl(214.3 31.8% 91.4%)",
2050
+ input: "hsl(214.3 31.8% 91.4%)",
2051
+ ring: "hsl(221.2 83.2% 53.3%)"
2052
+ },
2053
+ typography: {
2054
+ fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif',
2055
+ fontSize: {
2056
+ xs: "12px",
2057
+ sm: "14px",
2058
+ base: "16px",
2059
+ lg: "18px",
2060
+ xl: "20px",
2061
+ "2xl": "24px"
2062
+ },
2063
+ fontWeight: {
2064
+ normal: "400",
2065
+ medium: "500",
2066
+ semibold: "600",
2067
+ bold: "700"
2068
+ },
2069
+ lineHeight: {
2070
+ tight: "1.25",
2071
+ normal: "1.5",
2072
+ relaxed: "1.75"
2073
+ }
2074
+ },
2075
+ spacing: {
2076
+ borderRadius: {
2077
+ sm: "4px",
2078
+ // calc(0.5rem - 4px)
2079
+ md: "6px",
2080
+ // calc(0.5rem - 2px)
2081
+ lg: "8px"
2082
+ // 0.5rem
2083
+ },
2084
+ padding: {
2085
+ sm: "8px",
2086
+ md: "16px",
2087
+ lg: "24px",
2088
+ xl: "32px"
2089
+ },
2090
+ gap: {
2091
+ sm: "8px",
2092
+ md: "12px",
2093
+ lg: "16px"
2094
+ }
2095
+ }
2096
+ };
2097
+ const darkTheme = {
2098
+ colors: {
2099
+ // Background
2100
+ background: "hsl(222.2 84% 4.9%)",
2101
+ foreground: "hsl(210 40% 98%)",
2102
+ // Card
2103
+ card: "hsl(222.2 84% 4.9%)",
2104
+ cardForeground: "hsl(210 40% 98%)",
2105
+ // Popover
2106
+ popover: "hsl(222.2 84% 4.9%)",
2107
+ popoverForeground: "hsl(210 40% 98%)",
2108
+ // Primary (lighter blue for dark mode)
2109
+ primary: "hsl(217.2 91.2% 59.8%)",
2110
+ primaryForeground: "hsl(222.2 47.4% 11.2%)",
2111
+ // Secondary (dark gray)
2112
+ secondary: "hsl(217.2 32.6% 17.5%)",
2113
+ secondaryForeground: "hsl(210 40% 98%)",
2114
+ // Muted
2115
+ muted: "hsl(217.2 32.6% 17.5%)",
2116
+ mutedForeground: "hsl(215 20.2% 65.1%)",
2117
+ // Accent
2118
+ accent: "hsl(217.2 32.6% 17.5%)",
2119
+ accentForeground: "hsl(210 40% 98%)",
2120
+ // Destructive (darker red for dark mode)
2121
+ destructive: "hsl(0 62.8% 30.6%)",
2122
+ destructiveForeground: "hsl(210 40% 98%)",
2123
+ // Success (darker green for dark mode)
2124
+ success: "hsl(142 71% 45%)",
2125
+ // Tailwind green-500 equivalent
2126
+ successForeground: "hsl(0 0% 100%)",
2127
+ // Border and input
2128
+ border: "hsl(217.2 32.6% 17.5%)",
2129
+ input: "hsl(217.2 32.6% 17.5%)",
2130
+ ring: "hsl(224.3 76.3% 48%)"
2131
+ },
2132
+ typography: {
2133
+ fontFamily: '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif',
2134
+ fontSize: {
2135
+ xs: "12px",
2136
+ sm: "14px",
2137
+ base: "16px",
2138
+ lg: "18px",
2139
+ xl: "20px",
2140
+ "2xl": "24px"
2141
+ },
2142
+ fontWeight: {
2143
+ normal: "400",
2144
+ medium: "500",
2145
+ semibold: "600",
2146
+ bold: "700"
2147
+ },
2148
+ lineHeight: {
2149
+ tight: "1.25",
2150
+ normal: "1.5",
2151
+ relaxed: "1.75"
2152
+ }
2153
+ },
2154
+ spacing: {
2155
+ borderRadius: {
2156
+ sm: "4px",
2157
+ md: "6px",
2158
+ lg: "8px"
2159
+ },
2160
+ padding: {
2161
+ sm: "8px",
2162
+ md: "16px",
2163
+ lg: "24px",
2164
+ xl: "32px"
2165
+ },
2166
+ gap: {
2167
+ sm: "8px",
2168
+ md: "12px",
2169
+ lg: "16px"
2170
+ }
2171
+ }
2172
+ };
2173
+ function getTheme(prefersDark) {
2174
+ if (prefersDark !== void 0) {
2175
+ return prefersDark ? darkTheme : lightTheme;
2176
+ }
2177
+ if (typeof window !== "undefined" && window.matchMedia) {
2178
+ const isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
2179
+ return isDarkMode ? darkTheme : lightTheme;
2180
+ }
2181
+ return lightTheme;
2182
+ }
2183
+ function generateCSSVariables(theme) {
2184
+ return `
2185
+ --gw-background: ${theme.colors.background};
2186
+ --gw-foreground: ${theme.colors.foreground};
2187
+ --gw-card: ${theme.colors.card};
2188
+ --gw-card-foreground: ${theme.colors.cardForeground};
2189
+ --gw-primary: ${theme.colors.primary};
2190
+ --gw-primary-foreground: ${theme.colors.primaryForeground};
2191
+ --gw-secondary: ${theme.colors.secondary};
2192
+ --gw-secondary-foreground: ${theme.colors.secondaryForeground};
2193
+ --gw-muted: ${theme.colors.muted};
2194
+ --gw-muted-foreground: ${theme.colors.mutedForeground};
2195
+ --gw-accent: ${theme.colors.accent};
2196
+ --gw-accent-foreground: ${theme.colors.accentForeground};
2197
+ --gw-destructive: ${theme.colors.destructive};
2198
+ --gw-destructive-foreground: ${theme.colors.destructiveForeground};
2199
+ --gw-success: ${theme.colors.success};
2200
+ --gw-success-foreground: ${theme.colors.successForeground};
2201
+ --gw-border: ${theme.colors.border};
2202
+ --gw-input: ${theme.colors.input};
2203
+ --gw-ring: ${theme.colors.ring};
2204
+ `.trim();
2205
+ }
2206
+ class WarningModal {
2207
+ constructor(themeMode = "light", customStyles) {
2208
+ this.modal = null;
2209
+ this.legacyStyles = null;
2210
+ this.updateInterval = null;
2211
+ this.timeDisplay = null;
2212
+ this.startTime = 0;
2213
+ this.initialSeconds = 0;
2214
+ this.onEndCallback = void 0;
2215
+ const prefersDark = themeMode === "dark" || themeMode === "auto" && this.detectDarkMode();
2216
+ this.theme = getTheme(prefersDark);
2217
+ if (customStyles) {
2218
+ this.legacyStyles = {
2219
+ backgroundColor: customStyles.backgroundColor || "#ffffff",
2220
+ textColor: customStyles.textColor || "#333333",
2221
+ primaryColor: customStyles.primaryColor || "#007bff",
2222
+ borderRadius: customStyles.borderRadius || "8px",
2223
+ fontFamily: customStyles.fontFamily || '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif'
2224
+ };
2225
+ }
2226
+ }
2227
+ detectDarkMode() {
2228
+ if (typeof window !== "undefined" && window.matchMedia) {
2229
+ return window.matchMedia("(prefers-color-scheme: dark)").matches;
2230
+ }
2231
+ return false;
2232
+ }
2233
+ /**
2234
+ * Show warning modal
2235
+ */
2236
+ show(options) {
2237
+ this.hide();
2238
+ this.modal = document.createElement("div");
2239
+ this.modal.id = "gw-session-warning-modal";
2240
+ this.modal.style.cssText = `
2241
+ position: fixed;
2242
+ top: 0;
2243
+ left: 0;
2244
+ width: 100%;
2245
+ height: 100%;
2246
+ background-color: rgba(0, 0, 0, 0.5);
2247
+ display: flex;
2248
+ align-items: center;
2249
+ justify-content: center;
2250
+ z-index: 99999;
2251
+ font-family: ${this.legacyStyles?.fontFamily || this.theme.typography.fontFamily};
2252
+ `;
2253
+ const content = document.createElement("div");
2254
+ const bgColor = this.legacyStyles?.backgroundColor || this.theme.colors.card;
2255
+ const textColor = this.legacyStyles?.textColor || this.theme.colors.cardForeground;
2256
+ const borderRadius = this.legacyStyles?.borderRadius || this.theme.spacing.borderRadius.lg;
2257
+ content.style.cssText = `
2258
+ background-color: ${bgColor};
2259
+ color: ${textColor};
2260
+ border-radius: ${borderRadius};
2261
+ padding: ${this.theme.spacing.padding.lg};
2262
+ max-width: 400px;
2263
+ width: 90%;
2264
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
2265
+ border: 1px solid ${this.theme.colors.border};
2266
+ `;
2267
+ this.initialSeconds = options.remainingSeconds;
2268
+ this.startTime = Date.now();
2269
+ this.onEndCallback = options.onEnd;
2270
+ const minutes = Math.floor(options.remainingSeconds / 60);
2271
+ const seconds = options.remainingSeconds % 60;
2272
+ content.innerHTML = `
2273
+ <h2 style="margin: 0 0 ${this.theme.spacing.padding.md} 0; font-size: ${this.theme.typography.fontSize.xl}; font-weight: ${this.theme.typography.fontWeight.semibold}; color: ${textColor};">
2274
+ ⏱️ Time Running Low
2275
+ </h2>
2276
+ <p style="margin: 0 0 ${this.theme.spacing.padding.lg} 0; font-size: ${this.theme.typography.fontSize.base}; line-height: ${this.theme.typography.lineHeight.normal}; color: ${this.theme.colors.mutedForeground};">
2277
+ Your session will expire in <strong id="gw-time-display" style="color: ${textColor};">${minutes}:${seconds.toString().padStart(2, "0")}</strong>.
2278
+ </p>
2279
+ <div style="display: flex; gap: ${this.theme.spacing.gap.md}; justify-content: flex-end;">
2280
+ <button id="gw-dismiss-btn" style="
2281
+ padding: 10px 20px;
2282
+ background-color: ${this.theme.colors.secondary};
2283
+ color: ${this.theme.colors.secondaryForeground};
2284
+ border: 1px solid ${this.theme.colors.border};
2285
+ border-radius: ${this.theme.spacing.borderRadius.sm};
2286
+ font-size: ${this.theme.typography.fontSize.sm};
2287
+ font-weight: ${this.theme.typography.fontWeight.medium};
2288
+ cursor: pointer;
2289
+ transition: all 0.2s;
2290
+ font-family: ${this.theme.typography.fontFamily};
2291
+ ">
2292
+ Continue
2293
+ </button>
2294
+ ${options.onExtend ? `<button id="gw-extend-btn" style="
2295
+ padding: 10px 20px;
2296
+ background-color: ${this.theme.colors.success};
2297
+ color: ${this.theme.colors.successForeground};
2298
+ border: none;
2299
+ border-radius: ${this.theme.spacing.borderRadius.sm};
2300
+ font-size: ${this.theme.typography.fontSize.sm};
2301
+ font-weight: ${this.theme.typography.fontWeight.medium};
2302
+ cursor: pointer;
2303
+ transition: all 0.2s;
2304
+ font-family: ${this.theme.typography.fontFamily};
2305
+ ">
2306
+ Extend Session
2307
+ </button>` : ""}
2308
+ ${options.onEnd ? `<button id="gw-end-btn" style="
2309
+ padding: 10px 20px;
2310
+ background-color: ${this.theme.colors.destructive};
2311
+ color: ${this.theme.colors.destructiveForeground};
2312
+ border: none;
2313
+ border-radius: ${this.theme.spacing.borderRadius.sm};
2314
+ font-size: ${this.theme.typography.fontSize.sm};
2315
+ font-weight: ${this.theme.typography.fontWeight.medium};
2316
+ cursor: pointer;
2317
+ transition: all 0.2s;
2318
+ font-family: ${this.theme.typography.fontFamily};
2319
+ ">
2320
+ End Session
2321
+ </button>` : ""}
2322
+ </div>
2323
+ `;
2324
+ this.modal.appendChild(content);
2325
+ document.body.appendChild(this.modal);
2326
+ const extendBtn = document.getElementById("gw-extend-btn");
2327
+ if (extendBtn && options.onExtend) {
2328
+ extendBtn.addEventListener("click", () => {
2329
+ options.onExtend?.();
2330
+ this.hide();
2331
+ });
2332
+ }
2333
+ const dismissBtn = document.getElementById("gw-dismiss-btn");
2334
+ if (dismissBtn) {
2335
+ dismissBtn.addEventListener("click", () => {
2336
+ this.hide();
2337
+ });
2338
+ }
2339
+ const endBtn = document.getElementById("gw-end-btn");
2340
+ if (endBtn && options.onEnd) {
2341
+ endBtn.addEventListener("click", () => {
2342
+ options.onEnd?.();
2343
+ });
2344
+ }
2345
+ const buttons = content.querySelectorAll("button");
2346
+ buttons.forEach((button) => {
2347
+ button.addEventListener("mouseenter", () => {
2348
+ button.style.opacity = "0.9";
2349
+ });
2350
+ button.addEventListener("mouseleave", () => {
2351
+ button.style.opacity = "1";
2352
+ });
2353
+ button.addEventListener("focus", () => {
2354
+ button.style.outline = `2px solid ${this.theme.colors.ring}`;
2355
+ button.style.outlineOffset = "2px";
2356
+ });
2357
+ button.addEventListener("blur", () => {
2358
+ button.style.outline = "none";
2359
+ });
2360
+ });
2361
+ this.timeDisplay = document.getElementById("gw-time-display");
2362
+ this.updateInterval = window.setInterval(() => {
2363
+ const elapsed = Math.floor((Date.now() - this.startTime) / 1e3);
2364
+ const remaining = Math.max(0, this.initialSeconds - elapsed);
2365
+ const minutes2 = Math.floor(remaining / 60);
2366
+ const seconds2 = remaining % 60;
2367
+ if (this.timeDisplay) {
2368
+ this.timeDisplay.textContent = `${minutes2}:${seconds2.toString().padStart(2, "0")}`;
2369
+ }
2370
+ if (remaining === 0) {
2371
+ if (this.onEndCallback) {
2372
+ this.onEndCallback();
2373
+ }
2374
+ this.hide();
2375
+ }
2376
+ }, 1e3);
2377
+ }
2378
+ /**
2379
+ * Hide and remove modal
2380
+ */
2381
+ hide() {
2382
+ if (this.updateInterval !== null) {
2383
+ clearInterval(this.updateInterval);
2384
+ this.updateInterval = null;
2385
+ }
2386
+ if (this.modal && this.modal.parentNode) {
2387
+ this.modal.parentNode.removeChild(this.modal);
2388
+ this.modal = null;
2389
+ this.timeDisplay = null;
2390
+ }
2391
+ }
2392
+ /**
2393
+ * Check if modal is currently shown
2394
+ */
2395
+ isShown() {
2396
+ return this.modal !== null;
2397
+ }
2398
+ /**
2399
+ * Show "Session Ending" modal before redirect
2400
+ * Displays for a fixed duration (3 seconds) then calls callback
2401
+ */
2402
+ showEndingMessage(onComplete, durationMs = 3e3) {
2403
+ this.hide();
2404
+ const bgColor = this.legacyStyles?.backgroundColor || this.theme.colors.card;
2405
+ const textColor = this.legacyStyles?.textColor || this.theme.colors.cardForeground;
2406
+ const borderRadius = this.legacyStyles?.borderRadius || this.theme.spacing.borderRadius.lg;
2407
+ this.modal = document.createElement("div");
2408
+ this.modal.id = "gw-session-ending-modal";
2409
+ this.modal.style.cssText = `
2410
+ position: fixed;
2411
+ top: 0;
2412
+ left: 0;
2413
+ width: 100%;
2414
+ height: 100%;
2415
+ background-color: rgba(0, 0, 0, 0.5);
2416
+ display: flex;
2417
+ align-items: center;
2418
+ justify-content: center;
2419
+ z-index: 99999;
2420
+ font-family: ${this.legacyStyles?.fontFamily || this.theme.typography.fontFamily};
2421
+ animation: fadeIn 0.2s ease-in;
2422
+ `;
2423
+ const content = document.createElement("div");
2424
+ content.style.cssText = `
2425
+ background-color: ${bgColor};
2426
+ color: ${textColor};
2427
+ border-radius: ${borderRadius};
2428
+ padding: ${this.theme.spacing.padding.xl};
2429
+ max-width: 400px;
2430
+ width: 90%;
2431
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
2432
+ text-align: center;
2433
+ animation: slideIn 0.3s ease-out;
2434
+ border: 1px solid ${this.theme.colors.border};
2435
+ `;
2436
+ content.innerHTML = `
2437
+ <div style="font-size: 48px; margin-bottom: ${this.theme.spacing.padding.lg};">⏱️</div>
2438
+ <h2 style="margin: 0 0 ${this.theme.spacing.padding.md} 0; font-size: ${this.theme.typography.fontSize["2xl"]}; font-weight: ${this.theme.typography.fontWeight.semibold}; color: ${this.theme.colors.destructive};">
2439
+ Session Ending
2440
+ </h2>
2441
+ <p style="margin: 0 0 ${this.theme.spacing.padding.lg} 0; font-size: ${this.theme.typography.fontSize.base}; line-height: ${this.theme.typography.lineHeight.normal}; color: ${this.theme.colors.mutedForeground};">
2442
+ Your session has expired.<br/>
2443
+ Redirecting to marketplace...
2444
+ </p>
2445
+ <div style="
2446
+ width: 100%;
2447
+ height: 4px;
2448
+ background: ${this.theme.colors.muted};
2449
+ border-radius: ${this.theme.spacing.borderRadius.sm};
2450
+ overflow: hidden;
2451
+ margin-top: ${this.theme.spacing.padding.lg};
2452
+ ">
2453
+ <div id="gw-progress-bar" style="
2454
+ width: 100%;
2455
+ height: 100%;
2456
+ background: linear-gradient(90deg, ${this.theme.colors.destructive}, ${this.theme.colors.primary});
2457
+ animation: progressBar ${durationMs}ms linear forwards;
2458
+ "></div>
2459
+ </div>
2460
+ `;
2461
+ const style = document.createElement("style");
2462
+ style.textContent = `
2463
+ @keyframes fadeIn {
2464
+ from { opacity: 0; }
2465
+ to { opacity: 1; }
2466
+ }
2467
+ @keyframes slideIn {
2468
+ from { transform: translateY(-20px); opacity: 0; }
2469
+ to { transform: translateY(0); opacity: 1; }
2470
+ }
2471
+ @keyframes progressBar {
2472
+ from { width: 100%; }
2473
+ to { width: 0%; }
2474
+ }
2475
+ `;
2476
+ document.head.appendChild(style);
2477
+ this.modal.appendChild(content);
2478
+ document.body.appendChild(this.modal);
2479
+ setTimeout(() => {
2480
+ this.hide();
2481
+ onComplete();
2482
+ }, durationMs);
2483
+ }
2484
+ }
2485
+ function extractTokenFromURL(paramName = "jwt", url) {
2486
+ try {
2487
+ const targetUrl = url || (typeof window !== "undefined" ? window.location.href : "");
2488
+ if (!targetUrl) {
2489
+ return null;
2490
+ }
2491
+ const urlObj = new URL(targetUrl);
2492
+ const token = urlObj.searchParams.get(paramName);
2493
+ return token;
2494
+ } catch (error) {
2495
+ throw new SDKError(
2496
+ `Failed to extract token from URL: ${error instanceof Error ? error.message : "Unknown error"}`,
2497
+ "URL_PARSE_ERROR"
2498
+ );
2499
+ }
2500
+ }
2501
+ function isBrowser() {
2502
+ return typeof window !== "undefined" && typeof window.document !== "undefined";
2503
+ }
2504
+ class MarketplaceSDK {
2505
+ constructor(config) {
2506
+ this.timer = null;
2507
+ this.heartbeat = null;
2508
+ this.tabSync = null;
2509
+ this.modal = null;
2510
+ this.events = {};
2511
+ this.sessionData = null;
2512
+ this.jwtToken = null;
2513
+ this.endReason = "manual";
2514
+ this.config = {
2515
+ jwksUri: config.jwksUri || "https://api.generalwisdom.com/.well-known/jwks.json",
2516
+ apiEndpoint: config.apiEndpoint || "http://localhost:3000",
2517
+ debug: config.debug ?? false,
2518
+ autoStart: config.autoStart ?? true,
2519
+ warningThresholdSeconds: config.warningThresholdSeconds ?? 300,
2520
+ customStyles: config.customStyles ?? {},
2521
+ themeMode: config.themeMode ?? "light",
2522
+ applicationId: config.applicationId ?? "",
2523
+ marketplaceUrl: config.marketplaceUrl ?? "https://d3p2yqofgy75sz.cloudfront.net/",
2524
+ // Phase 2 options
2525
+ enableHeartbeat: config.enableHeartbeat ?? false,
2526
+ heartbeatIntervalSeconds: config.heartbeatIntervalSeconds ?? 30,
2527
+ enableTabSync: config.enableTabSync ?? false,
2528
+ pauseOnHidden: config.pauseOnHidden ?? false,
2529
+ useBackendValidation: config.useBackendValidation ?? false,
2530
+ // Lifecycle hooks
2531
+ hooks: config.hooks ?? {},
2532
+ hookTimeoutMs: config.hookTimeoutMs ?? 5e3
2533
+ };
2534
+ this.validator = new JWKSValidator(this.config.jwksUri, this.config.debug);
2535
+ this.logger = new Logger(this.config.debug, "[MarketplaceSDK]");
2536
+ this.logger.info("SDK initialized with config:", {
2537
+ jwksUri: this.config.jwksUri,
2538
+ apiEndpoint: this.config.apiEndpoint,
2539
+ enableHeartbeat: this.config.enableHeartbeat,
2540
+ enableTabSync: this.config.enableTabSync,
2541
+ pauseOnHidden: this.config.pauseOnHidden
2542
+ });
2543
+ }
2544
+ /**
2545
+ * Register event handlers
2546
+ */
2547
+ on(event, handler) {
2548
+ this.events[event] = handler;
2549
+ this.logger.log("Event handler registered:", event);
2550
+ }
2551
+ /**
2552
+ * Execute a lifecycle hook with timeout
2553
+ */
2554
+ async executeHook(hookName, hook, context, isStrict = true) {
2555
+ if (!hook) return;
2556
+ this.logger.log(`Calling ${hookName} hook`);
2557
+ const timeout = new Promise(
2558
+ (_, reject) => setTimeout(
2559
+ () => reject(new SDKError(`${hookName} hook timeout after ${this.config.hookTimeoutMs}ms`, "HOOK_TIMEOUT")),
2560
+ this.config.hookTimeoutMs
2561
+ )
2562
+ );
2563
+ try {
2564
+ await Promise.race([
2565
+ Promise.resolve(hook(context)),
2566
+ timeout
2567
+ ]);
2568
+ this.logger.log(`${hookName} hook completed successfully`);
2569
+ } catch (error) {
2570
+ this.logger.error(`${hookName} hook failed:`, error);
2571
+ if (isStrict) {
2572
+ throw new SDKError(
2573
+ `${hookName} hook failed: ${error instanceof Error ? error.message : "Unknown error"}`,
2574
+ "HOOK_ERROR"
2575
+ );
2576
+ } else {
2577
+ this.logger.warn(`${hookName} hook failed but continuing (lenient mode)`);
2578
+ }
2579
+ }
2580
+ }
2581
+ /**
2582
+ * Calculate actual session duration in minutes
2583
+ */
2584
+ calculateActualDuration() {
2585
+ if (!this.sessionData) return void 0;
2586
+ const now = Math.floor(Date.now() / 1e3);
2587
+ const durationSeconds = now - this.sessionData.startTime;
2588
+ return Math.ceil(durationSeconds / 60);
2589
+ }
2590
+ /**
2591
+ * Initialize SDK and validate session
2592
+ */
2593
+ async initialize() {
2594
+ this.logger.info("Initializing session...");
2595
+ try {
2596
+ const JWT_STORAGE_KEY = "gw_marketplace_jwt";
2597
+ this.jwtToken = extractTokenFromURL("jwt");
2598
+ if (!this.jwtToken && typeof sessionStorage !== "undefined") {
2599
+ this.jwtToken = sessionStorage.getItem(JWT_STORAGE_KEY);
2600
+ if (this.jwtToken) {
2601
+ this.logger.log("JWT token retrieved from storage");
2602
+ }
2603
+ }
2604
+ if (!this.jwtToken) {
2605
+ throw new SDKError(
2606
+ "No jwt token found in URL or storage",
2607
+ "MISSING_TOKEN"
2608
+ );
2609
+ }
2610
+ if (typeof sessionStorage !== "undefined") {
2611
+ sessionStorage.setItem(JWT_STORAGE_KEY, this.jwtToken);
2612
+ this.logger.log("JWT token stored in sessionStorage");
2613
+ }
2614
+ this.logger.log("JWT token extracted from URL");
2615
+ let verifiedClaims;
2616
+ if (this.config.useBackendValidation) {
2617
+ this.logger.log("Using backend validation");
2618
+ verifiedClaims = await this.validateWithBackend(this.jwtToken);
2619
+ } else {
2620
+ this.logger.log("Using JWKS validation");
2621
+ verifiedClaims = await this.validator.verify(
2622
+ this.jwtToken,
2623
+ "generalwisdom.com",
2624
+ this.config.applicationId || void 0
2625
+ );
2626
+ }
2627
+ this.logger.log("JWT verified successfully");
2628
+ this.sessionData = {
2629
+ sessionId: verifiedClaims.sessionId,
2630
+ applicationId: verifiedClaims.applicationId,
2631
+ userId: verifiedClaims.userId,
2632
+ orgId: verifiedClaims.orgId,
2633
+ startTime: verifiedClaims.startTime,
2634
+ durationMinutes: verifiedClaims.durationMinutes,
2635
+ iat: verifiedClaims.iat,
2636
+ exp: verifiedClaims.exp,
2637
+ iss: verifiedClaims.iss,
2638
+ sub: verifiedClaims.sub
2639
+ };
2640
+ const now = Math.floor(Date.now() / 1e3);
2641
+ const remainingSeconds = Math.max(0, this.sessionData.exp - now);
2642
+ if (remainingSeconds <= 0) {
2643
+ throw new SDKError(
2644
+ "Session has already expired",
2645
+ "SESSION_EXPIRED"
2646
+ );
2647
+ }
2648
+ this.logger.log("Remaining time:", remainingSeconds, "seconds");
2649
+ if (this.config.hooks?.onSessionStart) {
2650
+ const startContext = {
2651
+ sessionId: this.sessionData.sessionId,
2652
+ userId: this.sessionData.userId,
2653
+ email: verifiedClaims.email,
2654
+ // May not be in all JWTs
2655
+ orgId: this.sessionData.orgId,
2656
+ applicationId: this.sessionData.applicationId,
2657
+ durationMinutes: this.sessionData.durationMinutes,
2658
+ expiresAt: this.sessionData.exp,
2659
+ jwt: this.jwtToken
2660
+ };
2661
+ await this.executeHook("onSessionStart", this.config.hooks.onSessionStart, startContext, true);
2662
+ this.logger.log("Application auth synchronized with marketplace session");
2663
+ }
2664
+ this.timer = new TimerManager(
2665
+ remainingSeconds,
2666
+ this.config.warningThresholdSeconds,
2667
+ {
2668
+ onSessionWarning: (data) => {
2669
+ if (this.config.hooks?.onSessionWarning) {
2670
+ const warningContext = {
2671
+ sessionId: this.sessionData.sessionId,
2672
+ userId: this.sessionData.userId,
2673
+ remainingSeconds: data.remainingSeconds
2674
+ };
2675
+ this.executeHook("onSessionWarning", this.config.hooks.onSessionWarning, warningContext, false).catch((error) => this.logger.error("onSessionWarning hook failed:", error));
2676
+ }
2677
+ this.showWarningModal(data.remainingSeconds);
2678
+ this.events.onSessionWarning?.(data);
2679
+ },
2680
+ onSessionEnd: () => {
2681
+ this.endReason = "expired";
2682
+ this.endSession();
2683
+ }
2684
+ },
2685
+ this.config.debug
2686
+ );
2687
+ if (this.config.enableHeartbeat && this.jwtToken) {
2688
+ this.heartbeat = new HeartbeatManager(
2689
+ this.sessionData.sessionId,
2690
+ this.config.apiEndpoint,
2691
+ this.jwtToken,
2692
+ (remainingSeconds2) => {
2693
+ this.timer?.updateRemainingTime(remainingSeconds2);
2694
+ },
2695
+ (error) => {
2696
+ this.logger.error("Heartbeat error:", error);
2697
+ this.events.onError?.(error);
2698
+ },
2699
+ this.config.heartbeatIntervalSeconds,
2700
+ this.config.debug
2701
+ );
2702
+ }
2703
+ if (this.config.enableTabSync) {
2704
+ this.tabSync = new TabSyncManager(
2705
+ this.sessionData.sessionId,
2706
+ (message2) => this.handleTabSyncMessage(message2),
2707
+ this.config.debug
2708
+ );
2709
+ }
2710
+ if (this.config.pauseOnHidden) {
2711
+ this.initializeVisibilityHandling();
2712
+ }
2713
+ if (this.config.autoStart) {
2714
+ this.timer.start();
2715
+ this.logger.log("Timer started automatically");
2716
+ if (this.config.enableHeartbeat && this.heartbeat) {
2717
+ const isMaster = !this.tabSync || this.tabSync.isMasterTab();
2718
+ if (isMaster) {
2719
+ this.heartbeat.start();
2720
+ this.logger.log("Heartbeat started (master tab)");
2721
+ } else {
2722
+ this.logger.log("Heartbeat not started (slave tab)");
2723
+ }
2724
+ }
2725
+ }
2726
+ this.events.onSessionStart?.(this.sessionData);
2727
+ this.logger.info("Session initialized successfully");
2728
+ return this.sessionData;
2729
+ } catch (error) {
2730
+ this.logger.error("Initialization failed:", error);
2731
+ const sdkError = error instanceof SDKError ? error : new SDKError(
2732
+ error instanceof Error ? error.message : "Unknown error",
2733
+ "INITIALIZATION_ERROR"
2734
+ );
2735
+ this.events.onError?.(sdkError);
2736
+ throw sdkError;
2737
+ }
2738
+ }
2739
+ /**
2740
+ * Phase 2: Validate JWT with backend
2741
+ */
2742
+ async validateWithBackend(token) {
2743
+ const response = await fetch(
2744
+ `${this.config.apiEndpoint}/sessions/validate`,
2745
+ {
2746
+ method: "POST",
2747
+ headers: {
2748
+ "Authorization": `Bearer ${token}`,
2749
+ "Content-Type": "application/json"
2750
+ },
2751
+ body: JSON.stringify({ session_jwt: token })
2752
+ }
2753
+ );
2754
+ if (!response.ok) {
2755
+ throw new SDKError(
2756
+ "Backend validation failed",
2757
+ "BACKEND_VALIDATION_FAILED",
2758
+ response.status
2759
+ );
2760
+ }
2761
+ const data = await response.json();
2762
+ if (!data.valid) {
2763
+ throw new SDKError(
2764
+ data.error || "Session validation failed",
2765
+ "SESSION_INVALID"
2766
+ );
2767
+ }
2768
+ const decoded = JWTParser.decode(token);
2769
+ return decoded;
2770
+ }
2771
+ /**
2772
+ * Phase 2: Handle tab sync messages
2773
+ */
2774
+ handleTabSyncMessage(message2) {
2775
+ this.logger.log("Tab sync message:", message2.type);
2776
+ switch (message2.type) {
2777
+ case "pause":
2778
+ this.timer?.pause();
2779
+ break;
2780
+ case "resume":
2781
+ this.timer?.resume();
2782
+ break;
2783
+ case "end":
2784
+ this.endSession();
2785
+ break;
2786
+ case "timer_update":
2787
+ if (message2.remainingSeconds !== void 0) {
2788
+ this.timer?.updateRemainingTime(message2.remainingSeconds);
2789
+ }
2790
+ break;
2791
+ }
2792
+ }
2793
+ /**
2794
+ * Phase 2: Initialize Visibility API handling
2795
+ */
2796
+ initializeVisibilityHandling() {
2797
+ document.addEventListener("visibilitychange", () => {
2798
+ if (document.hidden) {
2799
+ this.logger.log("Tab hidden, pausing timer");
2800
+ this.pauseTimer();
2801
+ } else {
2802
+ this.logger.log("Tab visible, resuming timer");
2803
+ this.resumeTimer();
2804
+ }
2805
+ });
2806
+ this.logger.log("Visibility API handler initialized");
2807
+ }
2808
+ /**
2809
+ * Start session timer manually
2810
+ */
2811
+ startTimer() {
2812
+ if (!this.timer) {
2813
+ throw new SDKError(
2814
+ "SDK not initialized. Call initialize() first.",
2815
+ "NOT_INITIALIZED"
2816
+ );
2817
+ }
2818
+ this.timer.start();
2819
+ this.tabSync?.broadcast("resume");
2820
+ this.logger.info("Timer started manually");
2821
+ }
2822
+ /**
2823
+ * Pause session timer
2824
+ */
2825
+ pauseTimer() {
2826
+ this.timer?.pause();
2827
+ this.tabSync?.broadcast("pause");
2828
+ this.logger.info("Timer paused");
2829
+ }
2830
+ /**
2831
+ * Resume session timer
2832
+ */
2833
+ resumeTimer() {
2834
+ this.timer?.resume();
2835
+ this.tabSync?.broadcast("resume");
2836
+ this.logger.info("Timer resumed");
2837
+ }
2838
+ /**
2839
+ * Phase 2: Extend session
2840
+ */
2841
+ async extendSession(additionalMinutes) {
2842
+ if (!this.sessionData || !this.jwtToken) {
2843
+ throw new SDKError("No active session", "NO_SESSION");
2844
+ }
2845
+ this.logger.info("Extending session by", additionalMinutes, "minutes");
2846
+ try {
2847
+ const response = await fetch(
2848
+ `${this.config.apiEndpoint}/sessions/${this.sessionData.sessionId}/renew`,
2849
+ {
2850
+ method: "PUT",
2851
+ headers: {
2852
+ "Authorization": `Bearer ${this.jwtToken}`,
2853
+ "Content-Type": "application/json"
2854
+ },
2855
+ body: JSON.stringify({
2856
+ additional_minutes: additionalMinutes
2857
+ })
2858
+ }
2859
+ );
2860
+ if (!response.ok) {
2861
+ throw new SDKError(
2862
+ "Session extension failed",
2863
+ "EXTENSION_FAILED",
2864
+ response.status
2865
+ );
2866
+ }
2867
+ const data = await response.json();
2868
+ this.sessionData.exp = data.new_expires_at;
2869
+ const now = Math.floor(Date.now() / 1e3);
2870
+ const remainingSeconds = data.new_expires_at - now;
2871
+ this.timer?.updateRemainingTime(remainingSeconds);
2872
+ this.tabSync?.broadcast("timer_update", { remainingSeconds });
2873
+ if (this.config.hooks?.onSessionExtend) {
2874
+ const extendContext = {
2875
+ sessionId: this.sessionData.sessionId,
2876
+ userId: this.sessionData.userId,
2877
+ additionalMinutes,
2878
+ newExpiresAt: data.new_expires_at
2879
+ };
2880
+ await this.executeHook("onSessionExtend", this.config.hooks.onSessionExtend, extendContext, false);
2881
+ }
2882
+ this.logger.info("Session extended successfully");
2883
+ } catch (error) {
2884
+ this.logger.error("Failed to extend session:", error);
2885
+ const sdkError = error instanceof SDKError ? error : new SDKError(
2886
+ error instanceof Error ? error.message : "Extension failed",
2887
+ "EXTENSION_ERROR"
2888
+ );
2889
+ this.events.onError?.(sdkError);
2890
+ throw sdkError;
2891
+ }
2892
+ }
2893
+ /**
2894
+ * Phase 2: Complete session
2895
+ */
2896
+ async completeSession(actualUsageMinutes) {
2897
+ if (!this.sessionData || !this.jwtToken) {
2898
+ throw new SDKError("No active session", "NO_SESSION");
2899
+ }
2900
+ this.logger.info("Completing session...");
2901
+ try {
2902
+ const response = await fetch(
2903
+ `${this.config.apiEndpoint}/sessions/${this.sessionData.sessionId}/complete`,
2904
+ {
2905
+ method: "POST",
2906
+ headers: {
2907
+ "Authorization": `Bearer ${this.jwtToken}`,
2908
+ "Content-Type": "application/json"
2909
+ },
2910
+ body: JSON.stringify({
2911
+ actual_usage_minutes: actualUsageMinutes,
2912
+ metadata: {}
2913
+ })
2914
+ }
2915
+ );
2916
+ if (!response.ok) {
2917
+ throw new SDKError(
2918
+ "Session completion failed",
2919
+ "COMPLETION_FAILED",
2920
+ response.status
2921
+ );
2922
+ }
2923
+ const data = await response.json();
2924
+ this.logger.info("Session completed:", data);
2925
+ this.endSession();
2926
+ } catch (error) {
2927
+ this.logger.error("Failed to complete session:", error);
2928
+ const sdkError = error instanceof SDKError ? error : new SDKError(
2929
+ error instanceof Error ? error.message : "Completion failed",
2930
+ "COMPLETION_ERROR"
2931
+ );
2932
+ this.events.onError?.(sdkError);
2933
+ throw sdkError;
2934
+ }
2935
+ }
2936
+ /**
2937
+ * End session
2938
+ */
2939
+ async endSession() {
2940
+ this.logger.info("Ending session...");
2941
+ const endContext = {
2942
+ sessionId: this.sessionData?.sessionId || "",
2943
+ userId: this.sessionData?.userId || "",
2944
+ reason: this.endReason,
2945
+ actualDurationMinutes: this.calculateActualDuration()
2946
+ };
2947
+ if (this.config.hooks?.onSessionEnd) {
2948
+ try {
2949
+ await this.executeHook("onSessionEnd", this.config.hooks.onSessionEnd, endContext, false);
2950
+ this.logger.log("Application auth cleanup completed");
2951
+ } catch (error) {
2952
+ this.logger.error("onSessionEnd hook error (continuing anyway):", error);
2953
+ }
2954
+ }
2955
+ this.timer?.stop();
2956
+ this.heartbeat?.stop();
2957
+ this.tabSync?.broadcast("end");
2958
+ if (typeof sessionStorage !== "undefined") {
2959
+ sessionStorage.removeItem("gw_marketplace_jwt");
2960
+ this.logger.log("JWT token cleared from storage");
2961
+ }
2962
+ this.events.onSessionEnd?.();
2963
+ this.logger.info("Session ended");
2964
+ if (typeof window !== "undefined") {
2965
+ if (!this.modal) {
2966
+ this.modal = new WarningModal(
2967
+ this.config.themeMode || "light",
2968
+ this.config.customStyles
2969
+ );
2970
+ }
2971
+ this.modal.showEndingMessage(() => {
2972
+ window.location.href = this.config.marketplaceUrl;
2973
+ }, 3e3);
2974
+ }
2975
+ }
2976
+ /**
2977
+ * Show warning modal
2978
+ */
2979
+ showWarningModal(remainingSeconds) {
2980
+ if (!this.modal) {
2981
+ this.modal = new WarningModal(
2982
+ this.config.themeMode || "light",
2983
+ this.config.customStyles
2984
+ );
2985
+ }
2986
+ this.modal.show({
2987
+ remainingSeconds,
2988
+ onExtend: async () => {
2989
+ try {
2990
+ await this.extendSession(15);
2991
+ this.modal?.hide();
2992
+ this.logger.log("Session extended successfully from modal");
2993
+ } catch (error) {
2994
+ this.logger.error("Extension failed, redirecting to marketplace:", error);
2995
+ const marketplaceUrl = `${this.config.marketplaceUrl}extend-session?sessionId=${this.sessionData?.sessionId}`;
2996
+ if (typeof window !== "undefined") {
2997
+ if (!this.modal) {
2998
+ this.modal = new WarningModal(
2999
+ this.config.themeMode || "light",
3000
+ this.config.customStyles
3001
+ );
3002
+ }
3003
+ this.modal.showEndingMessage(() => {
3004
+ window.location.href = marketplaceUrl;
3005
+ }, 3e3);
3006
+ }
3007
+ }
3008
+ },
3009
+ onEnd: () => {
3010
+ this.endSession();
3011
+ }
3012
+ });
3013
+ }
3014
+ /**
3015
+ * Get current session data
3016
+ */
3017
+ getSessionData() {
3018
+ return this.sessionData;
3019
+ }
3020
+ /**
3021
+ * Get remaining time
3022
+ */
3023
+ getRemainingTime() {
3024
+ return this.timer?.getRemainingSeconds() ?? 0;
3025
+ }
3026
+ /**
3027
+ * Get formatted time (MM:SS)
3028
+ */
3029
+ getFormattedTime() {
3030
+ return this.timer?.getFormattedTime() ?? "0:00";
3031
+ }
3032
+ /**
3033
+ * Get formatted time with hours (HH:MM:SS)
3034
+ */
3035
+ getFormattedTimeWithHours() {
3036
+ return this.timer?.getFormattedTimeWithHours() ?? "0:00:00";
3037
+ }
3038
+ /**
3039
+ * Check if timer is running
3040
+ */
3041
+ isTimerRunning() {
3042
+ return this.timer?.isRunning() ?? false;
3043
+ }
3044
+ /**
3045
+ * Cleanup and destroy SDK instance
3046
+ */
3047
+ destroy() {
3048
+ this.logger.info("Destroying SDK instance...");
3049
+ this.timer?.stop();
3050
+ this.heartbeat?.stop();
3051
+ this.tabSync?.destroy();
3052
+ this.modal?.hide();
3053
+ if (typeof sessionStorage !== "undefined") {
3054
+ sessionStorage.removeItem("gw_marketplace_jwt");
3055
+ this.logger.log("JWT token cleared from storage");
3056
+ }
3057
+ this.sessionData = null;
3058
+ this.jwtToken = null;
3059
+ }
3060
+ }
3061
+ class SessionHeader {
3062
+ constructor(themeMode = "auto") {
3063
+ this.container = null;
3064
+ this.updateInterval = null;
3065
+ this.timeDisplay = null;
3066
+ this.mounted = false;
3067
+ const prefersDark = themeMode === "dark" || themeMode === "auto" && this.detectDarkMode();
3068
+ this.theme = getTheme(prefersDark);
3069
+ }
3070
+ detectDarkMode() {
3071
+ if (typeof window !== "undefined" && window.matchMedia) {
3072
+ return window.matchMedia("(prefers-color-scheme: dark)").matches;
3073
+ }
3074
+ return false;
3075
+ }
3076
+ /**
3077
+ * Mount the session header to a DOM element
3078
+ * @param targetElement - Element to mount to (or selector string)
3079
+ * @param options - Configuration options
3080
+ */
3081
+ mount(targetElement, options) {
3082
+ if (this.mounted) {
3083
+ this.unmount();
3084
+ }
3085
+ const target = typeof targetElement === "string" ? document.querySelector(targetElement) : targetElement;
3086
+ if (!target) {
3087
+ console.error("[SessionHeader] Mount target not found");
3088
+ return;
3089
+ }
3090
+ this.getTimeCallback = options.getTime;
3091
+ this.onExtendCallback = options.onExtend;
3092
+ this.onEndCallback = options.onEnd;
3093
+ this.container = document.createElement("div");
3094
+ this.container.id = "gw-session-header";
3095
+ const position = options.position || "right";
3096
+ const justifyContent = position === "left" ? "flex-start" : position === "center" ? "center" : "flex-end";
3097
+ this.container.style.cssText = `
3098
+ display: flex;
3099
+ align-items: center;
3100
+ justify-content: ${justifyContent};
3101
+ gap: ${this.theme.spacing.gap.md};
3102
+ padding: ${this.theme.spacing.padding.sm} ${this.theme.spacing.padding.md};
3103
+ background-color: ${this.theme.colors.card};
3104
+ border: 1px solid ${this.theme.colors.border};
3105
+ border-radius: ${this.theme.spacing.borderRadius.md};
3106
+ font-family: ${this.theme.typography.fontFamily};
3107
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
3108
+ `;
3109
+ const timerWrapper = document.createElement("div");
3110
+ timerWrapper.style.cssText = `
3111
+ display: flex;
3112
+ align-items: center;
3113
+ gap: ${this.theme.spacing.gap.sm};
3114
+ color: ${this.theme.colors.foreground};
3115
+ font-size: ${this.theme.typography.fontSize.sm};
3116
+ font-weight: ${this.theme.typography.fontWeight.medium};
3117
+ `;
3118
+ const clockIcon = document.createElement("span");
3119
+ clockIcon.textContent = "⏱️";
3120
+ clockIcon.style.fontSize = this.theme.typography.fontSize.base;
3121
+ this.timeDisplay = document.createElement("span");
3122
+ this.timeDisplay.id = "gw-session-time";
3123
+ this.timeDisplay.textContent = this.getTimeCallback?.() || "--:--";
3124
+ this.timeDisplay.style.cssText = `
3125
+ font-variant-numeric: tabular-nums;
3126
+ min-width: 50px;
3127
+ text-align: center;
3128
+ `;
3129
+ timerWrapper.appendChild(clockIcon);
3130
+ timerWrapper.appendChild(this.timeDisplay);
3131
+ this.container.appendChild(timerWrapper);
3132
+ const buttonContainer = document.createElement("div");
3133
+ buttonContainer.style.cssText = `
3134
+ display: flex;
3135
+ gap: ${this.theme.spacing.gap.sm};
3136
+ `;
3137
+ if (this.onExtendCallback) {
3138
+ const extendBtn = document.createElement("button");
3139
+ extendBtn.textContent = "Extend";
3140
+ extendBtn.style.cssText = `
3141
+ padding: ${this.theme.spacing.padding.sm};
3142
+ background-color: ${this.theme.colors.success};
3143
+ color: ${this.theme.colors.successForeground};
3144
+ border: none;
3145
+ border-radius: ${this.theme.spacing.borderRadius.sm};
3146
+ font-size: ${this.theme.typography.fontSize.sm};
3147
+ font-weight: ${this.theme.typography.fontWeight.medium};
3148
+ font-family: ${this.theme.typography.fontFamily};
3149
+ cursor: pointer;
3150
+ transition: opacity 0.2s;
3151
+ `;
3152
+ extendBtn.addEventListener("mouseenter", () => {
3153
+ extendBtn.style.opacity = "0.9";
3154
+ });
3155
+ extendBtn.addEventListener("mouseleave", () => {
3156
+ extendBtn.style.opacity = "1";
3157
+ });
3158
+ extendBtn.addEventListener("click", () => {
3159
+ this.onExtendCallback?.();
3160
+ });
3161
+ buttonContainer.appendChild(extendBtn);
3162
+ }
3163
+ if (this.onEndCallback) {
3164
+ const endBtn = document.createElement("button");
3165
+ endBtn.textContent = "End";
3166
+ endBtn.style.cssText = `
3167
+ padding: ${this.theme.spacing.padding.sm};
3168
+ background-color: ${this.theme.colors.secondary};
3169
+ color: ${this.theme.colors.secondaryForeground};
3170
+ border: 1px solid ${this.theme.colors.border};
3171
+ border-radius: ${this.theme.spacing.borderRadius.sm};
3172
+ font-size: ${this.theme.typography.fontSize.sm};
3173
+ font-weight: ${this.theme.typography.fontWeight.medium};
3174
+ font-family: ${this.theme.typography.fontFamily};
3175
+ cursor: pointer;
3176
+ transition: opacity 0.2s;
3177
+ `;
3178
+ endBtn.addEventListener("mouseenter", () => {
3179
+ endBtn.style.opacity = "0.9";
3180
+ });
3181
+ endBtn.addEventListener("mouseleave", () => {
3182
+ endBtn.style.opacity = "1";
3183
+ });
3184
+ endBtn.addEventListener("click", () => {
3185
+ this.onEndCallback?.();
3186
+ });
3187
+ buttonContainer.appendChild(endBtn);
3188
+ }
3189
+ if (buttonContainer.children.length > 0) {
3190
+ this.container.appendChild(buttonContainer);
3191
+ }
3192
+ target.appendChild(this.container);
3193
+ this.mounted = true;
3194
+ this.startUpdating();
3195
+ }
3196
+ /**
3197
+ * Start updating the time display
3198
+ */
3199
+ startUpdating() {
3200
+ if (this.updateInterval) {
3201
+ clearInterval(this.updateInterval);
3202
+ }
3203
+ this.updateInterval = window.setInterval(() => {
3204
+ if (this.timeDisplay && this.getTimeCallback) {
3205
+ const newTime = this.getTimeCallback();
3206
+ this.timeDisplay.textContent = newTime;
3207
+ const [minutes] = newTime.split(":").map(Number);
3208
+ if (!isNaN(minutes) && minutes < 5) {
3209
+ this.timeDisplay.style.color = this.theme.colors.destructive;
3210
+ this.timeDisplay.style.fontWeight = this.theme.typography.fontWeight.bold;
3211
+ } else {
3212
+ this.timeDisplay.style.color = this.theme.colors.foreground;
3213
+ this.timeDisplay.style.fontWeight = this.theme.typography.fontWeight.medium;
3214
+ }
3215
+ }
3216
+ }, 1e3);
3217
+ }
3218
+ /**
3219
+ * Unmount and cleanup the session header
3220
+ */
3221
+ unmount() {
3222
+ if (this.updateInterval) {
3223
+ clearInterval(this.updateInterval);
3224
+ this.updateInterval = null;
3225
+ }
3226
+ if (this.container && this.container.parentNode) {
3227
+ this.container.parentNode.removeChild(this.container);
3228
+ }
3229
+ this.container = null;
3230
+ this.timeDisplay = null;
3231
+ this.mounted = false;
3232
+ }
3233
+ /**
3234
+ * Check if component is mounted
3235
+ */
3236
+ isMounted() {
3237
+ return this.mounted;
3238
+ }
3239
+ /**
3240
+ * Update the theme
3241
+ */
3242
+ updateTheme(themeMode) {
3243
+ const prefersDark = themeMode === "dark" || themeMode === "auto" && this.detectDarkMode();
3244
+ this.theme = getTheme(prefersDark);
3245
+ if (this.mounted && this.container && this.container.parentElement) {
3246
+ const parent = this.container.parentElement;
3247
+ const options = {
3248
+ getTime: this.getTimeCallback,
3249
+ onExtend: this.onExtendCallback,
3250
+ onEnd: this.onEndCallback
3251
+ };
3252
+ this.unmount();
3253
+ this.mount(parent, options);
3254
+ }
3255
+ }
3256
+ }
3257
+ export {
3258
+ HeartbeatManager,
3259
+ JWKSValidator,
3260
+ JWTParser,
3261
+ Logger,
3262
+ MarketplaceSDK,
3263
+ SDKError,
3264
+ SessionHeader,
3265
+ TabSyncManager,
3266
+ TimerManager,
3267
+ WarningModal,
3268
+ darkTheme,
3269
+ MarketplaceSDK as default,
3270
+ extractTokenFromURL,
3271
+ generateCSSVariables,
3272
+ getTheme,
3273
+ isBrowser,
3274
+ lightTheme
3275
+ };
3276
+ //# sourceMappingURL=marketplace-sdk.es.js.map