@oxog/authkeeper 1.5.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +59 -2
  3. package/dist/{auth-source-CXqrhLDO.d.ts → auth-source-BwPxq1Za.d.ts} +1 -1
  4. package/dist/{auth-source-rjMWiP1Q.d.cts → auth-source-MSiTDST6.d.cts} +1 -1
  5. package/dist/{chunk-DLCD3SBV.js → chunk-K3XXGKGO.js} +2 -2
  6. package/dist/chunk-K3XXGKGO.js.map +1 -0
  7. package/dist/{chunk-XGKVBZSF.cjs → chunk-MONA6MGX.cjs} +2 -2
  8. package/dist/chunk-MONA6MGX.cjs.map +1 -0
  9. package/dist/{chunk-VVSZPARR.cjs → chunk-SI7KITBT.cjs} +323 -273
  10. package/dist/chunk-SI7KITBT.cjs.map +1 -0
  11. package/dist/{chunk-7B77NZGY.js → chunk-ZZMKSCXQ.js} +312 -263
  12. package/dist/chunk-ZZMKSCXQ.js.map +1 -0
  13. package/dist/{index-CQwGO-Wk.d.ts → index-BqxH4X40.d.ts} +52 -6
  14. package/dist/{index-C4pGS-TI.d.cts → index-ChJtNH74.d.cts} +52 -6
  15. package/dist/index.cjs +461 -52
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +32 -4
  18. package/dist/index.d.ts +32 -4
  19. package/dist/index.js +416 -7
  20. package/dist/index.js.map +1 -1
  21. package/dist/plugins/index.cjs +15 -15
  22. package/dist/plugins/index.d.cts +2 -2
  23. package/dist/plugins/index.d.ts +2 -2
  24. package/dist/plugins/index.js +2 -2
  25. package/dist/react/index.cjs +3 -3
  26. package/dist/react/index.d.cts +2 -2
  27. package/dist/react/index.d.ts +2 -2
  28. package/dist/react/index.js +1 -1
  29. package/dist/svelte/index.d.cts +2 -2
  30. package/dist/svelte/index.d.ts +2 -2
  31. package/dist/{types-DMPBl-Ym.d.ts → types-DWB4dbp8.d.cts} +26 -4
  32. package/dist/{types-DMPBl-Ym.d.cts → types-DWB4dbp8.d.ts} +26 -4
  33. package/dist/vue/index.cjs +2 -2
  34. package/dist/vue/index.d.cts +2 -2
  35. package/dist/vue/index.d.ts +2 -2
  36. package/dist/vue/index.js +1 -1
  37. package/package.json +1 -1
  38. package/dist/chunk-7B77NZGY.js.map +0 -1
  39. package/dist/chunk-DLCD3SBV.js.map +0 -1
  40. package/dist/chunk-VVSZPARR.cjs.map +0 -1
  41. package/dist/chunk-XGKVBZSF.cjs.map +0 -1
@@ -1,4 +1,251 @@
1
- import { AuthError } from './chunk-DLCD3SBV.js';
1
+ import { AuthError } from './chunk-K3XXGKGO.js';
2
+
3
+ // src/plugins/core/storage-memory.ts
4
+ var MemoryStorageAdapter = class {
5
+ constructor(options = {}) {
6
+ this.store = /* @__PURE__ */ new Map();
7
+ this.prefix = options.prefix || "authkeeper:";
8
+ }
9
+ /**
10
+ * Get value by key
11
+ */
12
+ get(key) {
13
+ return this.store.get(this.prefix + key) || null;
14
+ }
15
+ /**
16
+ * Set value by key
17
+ */
18
+ set(key, value) {
19
+ this.store.set(this.prefix + key, value);
20
+ }
21
+ /**
22
+ * Remove value by key
23
+ */
24
+ remove(key) {
25
+ this.store.delete(this.prefix + key);
26
+ }
27
+ /**
28
+ * Clear all values
29
+ */
30
+ clear() {
31
+ const keysToDelete = [];
32
+ for (const key of this.store.keys()) {
33
+ if (key.startsWith(this.prefix)) {
34
+ keysToDelete.push(key);
35
+ }
36
+ }
37
+ keysToDelete.forEach((key) => this.store.delete(key));
38
+ }
39
+ };
40
+ function createMemoryStorage(options) {
41
+ return new MemoryStorageAdapter(options);
42
+ }
43
+
44
+ // src/utils/cookie.ts
45
+ function parseCookies(cookieString) {
46
+ const cookies = {};
47
+ if (!cookieString) {
48
+ return cookies;
49
+ }
50
+ cookieString.split(";").forEach((cookie) => {
51
+ const [key, ...valueParts] = cookie.split("=");
52
+ const trimmedKey = key?.trim();
53
+ const value = valueParts.join("=").trim();
54
+ if (trimmedKey) {
55
+ cookies[safeDecode(trimmedKey)] = safeDecode(value);
56
+ }
57
+ });
58
+ return cookies;
59
+ }
60
+ function safeDecode(value) {
61
+ try {
62
+ return decodeURIComponent(value);
63
+ } catch {
64
+ return value;
65
+ }
66
+ }
67
+ var INVALID_ATTRIBUTE_VALUE = /[;\r\n\0]/;
68
+ function assertAttributeValue(name, value) {
69
+ if (INVALID_ATTRIBUTE_VALUE.test(value)) {
70
+ throw new TypeError(`Invalid cookie ${name}: must not contain ';', CR, LF or NUL`);
71
+ }
72
+ }
73
+ function getCookie(name) {
74
+ if (typeof document === "undefined") {
75
+ return null;
76
+ }
77
+ const cookies = parseCookies(document.cookie);
78
+ return cookies[name] || null;
79
+ }
80
+ function serializeCookie(name, value, attributes = {}) {
81
+ let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
82
+ if (attributes.path) {
83
+ assertAttributeValue("path", attributes.path);
84
+ cookie += `; Path=${attributes.path}`;
85
+ }
86
+ if (attributes.domain) {
87
+ assertAttributeValue("domain", attributes.domain);
88
+ cookie += `; Domain=${attributes.domain}`;
89
+ }
90
+ if (attributes.maxAge !== void 0) {
91
+ cookie += `; Max-Age=${attributes.maxAge}`;
92
+ }
93
+ if (attributes.expires) {
94
+ cookie += `; Expires=${attributes.expires.toUTCString()}`;
95
+ }
96
+ if (attributes.secure || attributes.sameSite === "none") {
97
+ cookie += "; Secure";
98
+ }
99
+ if (attributes.sameSite) {
100
+ const sameSite = attributes.sameSite.charAt(0).toUpperCase() + attributes.sameSite.slice(1);
101
+ cookie += `; SameSite=${sameSite}`;
102
+ }
103
+ if (attributes.httpOnly) {
104
+ cookie += "; HttpOnly";
105
+ }
106
+ return cookie;
107
+ }
108
+ function setCookie(name, value, attributes = {}) {
109
+ if (typeof document === "undefined") {
110
+ return;
111
+ }
112
+ document.cookie = serializeCookie(name, value, attributes);
113
+ }
114
+ function deleteCookie(name, attributes = {}) {
115
+ if (typeof document === "undefined") {
116
+ return;
117
+ }
118
+ document.cookie = serializeCookie(name, "", {
119
+ ...attributes,
120
+ expires: /* @__PURE__ */ new Date(0)
121
+ });
122
+ }
123
+
124
+ // src/utils/storage.ts
125
+ function isQuotaExceeded(error) {
126
+ if (typeof error !== "object" || error === null) return false;
127
+ const { name, code } = error;
128
+ return name === "QuotaExceededError" || code === 22;
129
+ }
130
+ function isStorageAvailable(storage) {
131
+ if (!storage) {
132
+ return false;
133
+ }
134
+ try {
135
+ const testKey = "__storage_test__";
136
+ storage.setItem(testKey, "test");
137
+ storage.removeItem(testKey);
138
+ return true;
139
+ } catch (error) {
140
+ return isQuotaExceeded(error) && storage.length > 0;
141
+ }
142
+ }
143
+ function safeGetItem(storage, key) {
144
+ try {
145
+ return storage.get(key);
146
+ } catch {
147
+ return null;
148
+ }
149
+ }
150
+ function safeSetItem(storage, key, value) {
151
+ try {
152
+ storage.set(key, value);
153
+ return true;
154
+ } catch {
155
+ return false;
156
+ }
157
+ }
158
+ function safeRemoveItem(storage, key) {
159
+ try {
160
+ storage.remove(key);
161
+ return true;
162
+ } catch {
163
+ return false;
164
+ }
165
+ }
166
+
167
+ // src/plugins/core/storage-web.ts
168
+ var WebStorageAdapter = class {
169
+ constructor(storage, options = {}) {
170
+ this.storage = storage;
171
+ this.prefix = options.prefix ?? "authkeeper:";
172
+ }
173
+ get(key) {
174
+ return this.storage.getItem(this.prefix + key);
175
+ }
176
+ set(key, value) {
177
+ this.storage.setItem(this.prefix + key, value);
178
+ }
179
+ remove(key) {
180
+ this.storage.removeItem(this.prefix + key);
181
+ }
182
+ clear() {
183
+ const keys = [];
184
+ for (let i = 0; i < this.storage.length; i++) {
185
+ const key = this.storage.key(i);
186
+ if (key !== null && key.startsWith(this.prefix)) {
187
+ keys.push(key);
188
+ }
189
+ }
190
+ keys.forEach((key) => this.storage.removeItem(key));
191
+ }
192
+ };
193
+ var CookieStorageAdapter = class {
194
+ constructor(options = {}) {
195
+ this.keys = /* @__PURE__ */ new Set();
196
+ this.prefix = options.prefix ?? "authkeeper_";
197
+ this.attributes = { path: "/", secure: true, sameSite: "strict", ...options.attributes };
198
+ }
199
+ get(key) {
200
+ return getCookie(this.prefix + key);
201
+ }
202
+ set(key, value) {
203
+ setCookie(this.prefix + key, value, this.attributes);
204
+ this.keys.add(key);
205
+ if (typeof document !== "undefined" && this.get(key) !== value) {
206
+ throw new AuthError(
207
+ "STORAGE_ERROR",
208
+ `Cookie "${this.prefix + key}" was not stored (too large or blocked)`
209
+ );
210
+ }
211
+ }
212
+ remove(key) {
213
+ const { path, domain, secure, sameSite } = this.attributes;
214
+ deleteCookie(this.prefix + key, { path, domain, secure, sameSite });
215
+ this.keys.delete(key);
216
+ }
217
+ clear() {
218
+ Array.from(this.keys).forEach((key) => this.remove(key));
219
+ }
220
+ };
221
+ function getWebStorage(type) {
222
+ try {
223
+ return typeof window !== "undefined" ? window[type] : void 0;
224
+ } catch {
225
+ return void 0;
226
+ }
227
+ }
228
+ function resolveStorage(storage) {
229
+ if (storage === void 0 || storage === "memory") {
230
+ return void 0;
231
+ }
232
+ if (typeof storage === "object" && storage !== null) {
233
+ return storage;
234
+ }
235
+ switch (storage) {
236
+ case "localStorage":
237
+ case "sessionStorage": {
238
+ const webStorage = getWebStorage(storage);
239
+ return webStorage && isStorageAvailable(webStorage) ? new WebStorageAdapter(webStorage) : createMemoryStorage();
240
+ }
241
+ case "cookie":
242
+ return typeof document !== "undefined" ? new CookieStorageAdapter() : createMemoryStorage();
243
+ default:
244
+ throw new AuthError("CONFIG_ERROR", `Unknown storage type: ${String(storage)}`, {
245
+ context: { storage }
246
+ });
247
+ }
248
+ }
2
249
 
3
250
  // src/utils/base64.ts
4
251
  function base64UrlEncode(buffer) {
@@ -149,6 +396,16 @@ function getClaims(token, claims) {
149
396
  }
150
397
  }
151
398
 
399
+ // src/plugins/core/stored-tokens.ts
400
+ function isStoredTokens(value) {
401
+ if (value === null || typeof value !== "object") return false;
402
+ const v = value;
403
+ return typeof v["accessToken"] === "string" && v["accessToken"] !== "" && (v["refreshToken"] === null || typeof v["refreshToken"] === "string") && typeof v["tokenType"] === "string" && (v["expiresAt"] === null || isFiniteNumber(v["expiresAt"])) && isFiniteNumber(v["setAt"]) && isFiniteNumber(v["refreshCount"]);
404
+ }
405
+ function isFiniteNumber(value) {
406
+ return typeof value === "number" && Number.isFinite(value);
407
+ }
408
+
152
409
  // src/plugins/core/token-store.ts
153
410
  var TokenStorePlugin = class {
154
411
  constructor(options = {}) {
@@ -187,7 +444,12 @@ var TokenStorePlugin = class {
187
444
  if (!this.tokens?.setAt) return null;
188
445
  return new Date(this.tokens.setAt);
189
446
  },
190
- getRefreshCount: () => this.tokens?.refreshCount || 0
447
+ getRefreshCount: () => this.tokens?.refreshCount || 0,
448
+ replace: (tokens) => {
449
+ this.tokens = tokens === null ? null : { ...tokens };
450
+ },
451
+ readPersisted: () => this.readPersisted(),
452
+ getStorage: () => this.options.storage ? { adapter: this.options.storage, key: this.storageKey } : null
191
453
  };
192
454
  }
193
455
  /**
@@ -201,9 +463,9 @@ var TokenStorePlugin = class {
201
463
  throw new AuthError("TOKEN_INVALID", "accessToken is not a valid JWT");
202
464
  }
203
465
  let expiresAt;
204
- if (isFiniteNumber(tokens.expiresAt)) {
466
+ if (isFiniteNumber2(tokens.expiresAt)) {
205
467
  expiresAt = tokens.expiresAt * 1e3;
206
- } else if (isFiniteNumber(tokens.expiresIn)) {
468
+ } else if (isFiniteNumber2(tokens.expiresIn)) {
207
469
  expiresAt = Date.now() + tokens.expiresIn * 1e3;
208
470
  } else {
209
471
  expiresAt = getTokenExpiry(tokens.accessToken)?.getTime() ?? null;
@@ -269,6 +531,15 @@ var TokenStorePlugin = class {
269
531
  this.reportStorageError("Failed to persist tokens", error);
270
532
  }
271
533
  }
534
+ /** The tokens in the storage adapter, if readable and well-formed */
535
+ readPersisted() {
536
+ try {
537
+ const parsed = JSON.parse(this.options.storage?.get(this.storageKey) ?? "null");
538
+ return isStoredTokens(parsed) ? parsed : null;
539
+ } catch {
540
+ return null;
541
+ }
542
+ }
272
543
  /**
273
544
  * Load tokens from the storage adapter. Corrupt or malformed entries are
274
545
  * removed rather than trusted.
@@ -310,14 +581,9 @@ var TokenStorePlugin = class {
310
581
  if (this.kernel && typeof this.kernel.emit === "function") {
311
582
  this.kernel.emit({ type: "error", error, context: "storage", timestamp: Date.now() });
312
583
  }
313
- }
314
- };
315
- function isStoredTokens(value) {
316
- if (value === null || typeof value !== "object") return false;
317
- const v = value;
318
- return typeof v["accessToken"] === "string" && v["accessToken"] !== "" && (v["refreshToken"] === null || typeof v["refreshToken"] === "string") && typeof v["tokenType"] === "string" && (v["expiresAt"] === null || isFiniteNumber(v["expiresAt"])) && isFiniteNumber(v["setAt"]) && isFiniteNumber(v["refreshCount"]);
319
- }
320
- function isFiniteNumber(value) {
584
+ }
585
+ };
586
+ function isFiniteNumber2(value) {
321
587
  return typeof value === "number" && Number.isFinite(value);
322
588
  }
323
589
  function createTokenStorePlugin(options) {
@@ -462,6 +728,7 @@ var RefreshEnginePlugin = class {
462
728
  this.backoffs = /* @__PURE__ */ new Set();
463
729
  this.uninstalled = false;
464
730
  this.scheduledRefreshTimeout = null;
731
+ this.coordinator = null;
465
732
  this.options = options;
466
733
  this.refreshFn = options.refreshFn;
467
734
  this.threshold = options.threshold ?? 60;
@@ -483,6 +750,9 @@ var RefreshEnginePlugin = class {
483
750
  },
484
751
  setThreshold: (seconds) => {
485
752
  this.threshold = seconds;
753
+ },
754
+ setCoordinator: (coordinator) => {
755
+ this.coordinator = coordinator;
486
756
  }
487
757
  };
488
758
  }
@@ -498,7 +768,8 @@ var RefreshEnginePlugin = class {
498
768
  }
499
769
  this.endBackoffs();
500
770
  this.isRefreshingFlag = true;
501
- const promise = this.executeRefresh();
771
+ const coordinator = this.coordinator;
772
+ const promise = coordinator && refreshToken ? this.coordinatedRefresh(coordinator, refreshToken) : this.executeRefresh();
502
773
  this.refreshPromise = promise;
503
774
  this.refreshPromiseToken = refreshToken;
504
775
  try {
@@ -512,6 +783,24 @@ var RefreshEnginePlugin = class {
512
783
  }
513
784
  }
514
785
  }
786
+ /**
787
+ * Refresh while holding the cross-tab lock. Another tab may have refreshed
788
+ * this session meanwhile: its result is used instead of refreshing again
789
+ * (with rotating refresh tokens a second refresh would be rejected).
790
+ */
791
+ coordinatedRefresh(coordinator, refreshToken) {
792
+ return coordinator.exclusive(async () => {
793
+ if (this.kernel.getRefreshToken() === refreshToken) {
794
+ const adopted = await coordinator.beforeRefresh(refreshToken);
795
+ if (adopted) return adopted;
796
+ }
797
+ return this.executeRefresh(0, refreshToken);
798
+ });
799
+ }
800
+ /** The session another tab replaced `refreshToken` with, if any */
801
+ replacementFor(refreshToken) {
802
+ return this.coordinator?.replacement(refreshToken) ?? null;
803
+ }
515
804
  /** Wait before a retry; endBackoffs() cuts the wait short */
516
805
  waitBackoff(ms) {
517
806
  return new Promise((resolve) => {
@@ -538,6 +827,8 @@ var RefreshEnginePlugin = class {
538
827
  async executeRefresh(retryCount = 0, initialRefreshToken) {
539
828
  const refreshToken = this.uninstalled ? null : this.kernel.getRefreshToken();
540
829
  if (initialRefreshToken !== void 0 && refreshToken !== initialRefreshToken) {
830
+ const replacement = this.replacementFor(initialRefreshToken);
831
+ if (replacement) return replacement;
541
832
  throw supersededError();
542
833
  }
543
834
  if (this.options.onRefreshStart) {
@@ -557,6 +848,8 @@ var RefreshEnginePlugin = class {
557
848
  const previousExpiresAt = this.kernel.getExpiresAt();
558
849
  const newTokens = await this.refreshFn(refreshToken);
559
850
  if (this.kernel.getRefreshToken() !== refreshToken) {
851
+ const replacement = this.replacementFor(refreshToken);
852
+ if (replacement) return replacement;
560
853
  throw new SupersededRefreshError();
561
854
  }
562
855
  this.applyingRefresh = true;
@@ -584,6 +877,8 @@ var RefreshEnginePlugin = class {
584
877
  if (error instanceof SupersededRefreshError) {
585
878
  throw supersededError();
586
879
  }
880
+ const replacement = this.replacementFor(refreshToken);
881
+ if (replacement) return replacement;
587
882
  if (retryCount < this.maxRetries) {
588
883
  const delay = this.retryDelay * Math.pow(2, retryCount);
589
884
  await this.waitBackoff(Math.min(delay, MAX_TIMEOUT_DELAY));
@@ -675,6 +970,7 @@ var RefreshEnginePlugin = class {
675
970
  */
676
971
  uninstall() {
677
972
  this.cancelScheduledRefresh();
973
+ this.coordinator = null;
678
974
  this.uninstalled = true;
679
975
  this.endBackoffs();
680
976
  }
@@ -693,253 +989,6 @@ function createRefreshEnginePlugin(options) {
693
989
  return new RefreshEnginePlugin(options);
694
990
  }
695
991
 
696
- // src/plugins/core/storage-memory.ts
697
- var MemoryStorageAdapter = class {
698
- constructor(options = {}) {
699
- this.store = /* @__PURE__ */ new Map();
700
- this.prefix = options.prefix || "authkeeper:";
701
- }
702
- /**
703
- * Get value by key
704
- */
705
- get(key) {
706
- return this.store.get(this.prefix + key) || null;
707
- }
708
- /**
709
- * Set value by key
710
- */
711
- set(key, value) {
712
- this.store.set(this.prefix + key, value);
713
- }
714
- /**
715
- * Remove value by key
716
- */
717
- remove(key) {
718
- this.store.delete(this.prefix + key);
719
- }
720
- /**
721
- * Clear all values
722
- */
723
- clear() {
724
- const keysToDelete = [];
725
- for (const key of this.store.keys()) {
726
- if (key.startsWith(this.prefix)) {
727
- keysToDelete.push(key);
728
- }
729
- }
730
- keysToDelete.forEach((key) => this.store.delete(key));
731
- }
732
- };
733
- function createMemoryStorage(options) {
734
- return new MemoryStorageAdapter(options);
735
- }
736
-
737
- // src/utils/cookie.ts
738
- function parseCookies(cookieString) {
739
- const cookies = {};
740
- if (!cookieString) {
741
- return cookies;
742
- }
743
- cookieString.split(";").forEach((cookie) => {
744
- const [key, ...valueParts] = cookie.split("=");
745
- const trimmedKey = key?.trim();
746
- const value = valueParts.join("=").trim();
747
- if (trimmedKey) {
748
- cookies[safeDecode(trimmedKey)] = safeDecode(value);
749
- }
750
- });
751
- return cookies;
752
- }
753
- function safeDecode(value) {
754
- try {
755
- return decodeURIComponent(value);
756
- } catch {
757
- return value;
758
- }
759
- }
760
- var INVALID_ATTRIBUTE_VALUE = /[;\r\n\0]/;
761
- function assertAttributeValue(name, value) {
762
- if (INVALID_ATTRIBUTE_VALUE.test(value)) {
763
- throw new TypeError(`Invalid cookie ${name}: must not contain ';', CR, LF or NUL`);
764
- }
765
- }
766
- function getCookie(name) {
767
- if (typeof document === "undefined") {
768
- return null;
769
- }
770
- const cookies = parseCookies(document.cookie);
771
- return cookies[name] || null;
772
- }
773
- function serializeCookie(name, value, attributes = {}) {
774
- let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
775
- if (attributes.path) {
776
- assertAttributeValue("path", attributes.path);
777
- cookie += `; Path=${attributes.path}`;
778
- }
779
- if (attributes.domain) {
780
- assertAttributeValue("domain", attributes.domain);
781
- cookie += `; Domain=${attributes.domain}`;
782
- }
783
- if (attributes.maxAge !== void 0) {
784
- cookie += `; Max-Age=${attributes.maxAge}`;
785
- }
786
- if (attributes.expires) {
787
- cookie += `; Expires=${attributes.expires.toUTCString()}`;
788
- }
789
- if (attributes.secure || attributes.sameSite === "none") {
790
- cookie += "; Secure";
791
- }
792
- if (attributes.sameSite) {
793
- const sameSite = attributes.sameSite.charAt(0).toUpperCase() + attributes.sameSite.slice(1);
794
- cookie += `; SameSite=${sameSite}`;
795
- }
796
- if (attributes.httpOnly) {
797
- cookie += "; HttpOnly";
798
- }
799
- return cookie;
800
- }
801
- function setCookie(name, value, attributes = {}) {
802
- if (typeof document === "undefined") {
803
- return;
804
- }
805
- document.cookie = serializeCookie(name, value, attributes);
806
- }
807
- function deleteCookie(name, attributes = {}) {
808
- if (typeof document === "undefined") {
809
- return;
810
- }
811
- document.cookie = serializeCookie(name, "", {
812
- ...attributes,
813
- expires: /* @__PURE__ */ new Date(0)
814
- });
815
- }
816
-
817
- // src/utils/storage.ts
818
- function isQuotaExceeded(error) {
819
- if (typeof error !== "object" || error === null) return false;
820
- const { name, code } = error;
821
- return name === "QuotaExceededError" || code === 22;
822
- }
823
- function isStorageAvailable(storage) {
824
- if (!storage) {
825
- return false;
826
- }
827
- try {
828
- const testKey = "__storage_test__";
829
- storage.setItem(testKey, "test");
830
- storage.removeItem(testKey);
831
- return true;
832
- } catch (error) {
833
- return isQuotaExceeded(error) && storage.length > 0;
834
- }
835
- }
836
- function safeGetItem(storage, key) {
837
- try {
838
- return storage.get(key);
839
- } catch {
840
- return null;
841
- }
842
- }
843
- function safeSetItem(storage, key, value) {
844
- try {
845
- storage.set(key, value);
846
- return true;
847
- } catch {
848
- return false;
849
- }
850
- }
851
- function safeRemoveItem(storage, key) {
852
- try {
853
- storage.remove(key);
854
- return true;
855
- } catch {
856
- return false;
857
- }
858
- }
859
-
860
- // src/plugins/core/storage-web.ts
861
- var WebStorageAdapter = class {
862
- constructor(storage, options = {}) {
863
- this.storage = storage;
864
- this.prefix = options.prefix ?? "authkeeper:";
865
- }
866
- get(key) {
867
- return this.storage.getItem(this.prefix + key);
868
- }
869
- set(key, value) {
870
- this.storage.setItem(this.prefix + key, value);
871
- }
872
- remove(key) {
873
- this.storage.removeItem(this.prefix + key);
874
- }
875
- clear() {
876
- const keys = [];
877
- for (let i = 0; i < this.storage.length; i++) {
878
- const key = this.storage.key(i);
879
- if (key !== null && key.startsWith(this.prefix)) {
880
- keys.push(key);
881
- }
882
- }
883
- keys.forEach((key) => this.storage.removeItem(key));
884
- }
885
- };
886
- var CookieStorageAdapter = class {
887
- constructor(options = {}) {
888
- this.keys = /* @__PURE__ */ new Set();
889
- this.prefix = options.prefix ?? "authkeeper_";
890
- this.attributes = { path: "/", secure: true, sameSite: "strict", ...options.attributes };
891
- }
892
- get(key) {
893
- return getCookie(this.prefix + key);
894
- }
895
- set(key, value) {
896
- setCookie(this.prefix + key, value, this.attributes);
897
- this.keys.add(key);
898
- if (typeof document !== "undefined" && this.get(key) !== value) {
899
- throw new AuthError(
900
- "STORAGE_ERROR",
901
- `Cookie "${this.prefix + key}" was not stored (too large or blocked)`
902
- );
903
- }
904
- }
905
- remove(key) {
906
- const { path, domain, secure, sameSite } = this.attributes;
907
- deleteCookie(this.prefix + key, { path, domain, secure, sameSite });
908
- this.keys.delete(key);
909
- }
910
- clear() {
911
- Array.from(this.keys).forEach((key) => this.remove(key));
912
- }
913
- };
914
- function getWebStorage(type) {
915
- try {
916
- return typeof window !== "undefined" ? window[type] : void 0;
917
- } catch {
918
- return void 0;
919
- }
920
- }
921
- function resolveStorage(storage) {
922
- if (storage === void 0 || storage === "memory") {
923
- return void 0;
924
- }
925
- if (typeof storage === "object" && storage !== null) {
926
- return storage;
927
- }
928
- switch (storage) {
929
- case "localStorage":
930
- case "sessionStorage": {
931
- const webStorage = getWebStorage(storage);
932
- return webStorage && isStorageAvailable(webStorage) ? new WebStorageAdapter(webStorage) : createMemoryStorage();
933
- }
934
- case "cookie":
935
- return typeof document !== "undefined" ? new CookieStorageAdapter() : createMemoryStorage();
936
- default:
937
- throw new AuthError("CONFIG_ERROR", `Unknown storage type: ${String(storage)}`, {
938
- context: { storage }
939
- });
940
- }
941
- }
942
-
943
992
  // src/plugins/core/fetch-interceptor.ts
944
993
  var FetchInterceptorPlugin = class {
945
994
  constructor() {
@@ -1055,6 +1104,6 @@ function createFetchInterceptorPlugin() {
1055
1104
  return new FetchInterceptorPlugin();
1056
1105
  }
1057
1106
 
1058
- export { CookieStorageAdapter, FetchInterceptorPlugin, MemoryStorageAdapter, RefreshEnginePlugin, TokenDecoderPlugin, TokenStorePlugin, WebStorageAdapter, base64UrlDecode, base64UrlDecodeToBytes, base64UrlEncode, createFetchInterceptorPlugin, createMemoryStorage, createRefreshEnginePlugin, createTokenDecoderPlugin, createTokenStorePlugin, decodeHeader, decodeJwt, decodePayload, deleteCookie, getClaim, getClaims, getCookie, getTimeUntilExpiry, getTokenExpiry, isStorageAvailable, isTokenExpired, isValidJwtFormat, parseCookies, resolveStorage, safeGetItem, safeRemoveItem, safeSetItem, serializeCookie, setCookie, stringToBase64Url };
1059
- //# sourceMappingURL=chunk-7B77NZGY.js.map
1060
- //# sourceMappingURL=chunk-7B77NZGY.js.map
1107
+ export { CookieStorageAdapter, FetchInterceptorPlugin, MemoryStorageAdapter, RefreshEnginePlugin, TokenDecoderPlugin, TokenStorePlugin, WebStorageAdapter, base64UrlDecode, base64UrlDecodeToBytes, base64UrlEncode, createFetchInterceptorPlugin, createMemoryStorage, createRefreshEnginePlugin, createTokenDecoderPlugin, createTokenStorePlugin, decodeHeader, decodeJwt, decodePayload, deleteCookie, getClaim, getClaims, getCookie, getTimeUntilExpiry, getTokenExpiry, isStorageAvailable, isStoredTokens, isTokenExpired, isValidJwtFormat, parseCookies, resolveStorage, safeGetItem, safeRemoveItem, safeSetItem, serializeCookie, setCookie, stringToBase64Url };
1108
+ //# sourceMappingURL=chunk-ZZMKSCXQ.js.map
1109
+ //# sourceMappingURL=chunk-ZZMKSCXQ.js.map