@icp-sdk/auth 8.0.3 → 10.0.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 (85) hide show
  1. package/dist/esm/client/app-delegation-source.d.ts +65 -0
  2. package/dist/esm/client/app-delegation-source.js +58 -0
  3. package/dist/esm/client/app-delegation-source.js.map +1 -0
  4. package/dist/esm/client/app-lock.d.ts +63 -0
  5. package/dist/esm/client/app-lock.js +92 -0
  6. package/dist/esm/client/app-lock.js.map +1 -0
  7. package/dist/esm/client/auth-client.d.ts +270 -53
  8. package/dist/esm/client/auth-client.js +1017 -414
  9. package/dist/esm/client/auth-client.js.map +1 -1
  10. package/dist/esm/client/base64.d.ts +8 -0
  11. package/dist/esm/client/base64.js +28 -0
  12. package/dist/esm/client/base64.js.map +1 -0
  13. package/dist/esm/client/cookie-state-storage.d.ts +59 -0
  14. package/dist/esm/client/cookie-state-storage.js +245 -0
  15. package/dist/esm/client/cookie-state-storage.js.map +1 -0
  16. package/dist/esm/client/credential-storage.d.ts +48 -0
  17. package/dist/esm/client/credential-storage.js +2 -0
  18. package/dist/esm/client/credential-storage.js.map +1 -0
  19. package/dist/esm/client/db.d.ts +6 -1
  20. package/dist/esm/client/db.js +56 -15
  21. package/dist/esm/client/db.js.map +1 -1
  22. package/dist/esm/client/foreground-refresh.d.ts +35 -0
  23. package/dist/esm/client/foreground-refresh.js +83 -0
  24. package/dist/esm/client/foreground-refresh.js.map +1 -0
  25. package/dist/esm/client/idb-credential-storage.d.ts +25 -0
  26. package/dist/esm/client/idb-credential-storage.js +69 -0
  27. package/dist/esm/client/idb-credential-storage.js.map +1 -0
  28. package/dist/esm/client/index.d.ts +14 -3
  29. package/dist/esm/client/index.js +13 -3
  30. package/dist/esm/client/index.js.map +1 -1
  31. package/dist/esm/client/local-credential-storage.d.ts +26 -0
  32. package/dist/esm/client/local-credential-storage.js +59 -0
  33. package/dist/esm/client/local-credential-storage.js.map +1 -0
  34. package/dist/esm/client/memory-credential-storage.d.ts +22 -0
  35. package/dist/esm/client/memory-credential-storage.js +35 -0
  36. package/dist/esm/client/memory-credential-storage.js.map +1 -0
  37. package/dist/esm/client/session-delegation.d.ts +40 -0
  38. package/dist/esm/client/session-delegation.js +100 -0
  39. package/dist/esm/client/session-delegation.js.map +1 -0
  40. package/dist/esm/client/session-identity.d.ts +97 -0
  41. package/dist/esm/client/session-identity.js +392 -0
  42. package/dist/esm/client/session-identity.js.map +1 -0
  43. package/dist/esm/client/session-minter.d.ts +94 -0
  44. package/dist/esm/client/session-minter.js +178 -0
  45. package/dist/esm/client/session-minter.js.map +1 -0
  46. package/dist/esm/client/shared-memory-credential-storage.d.ts +44 -0
  47. package/dist/esm/client/shared-memory-credential-storage.js +243 -0
  48. package/dist/esm/client/shared-memory-credential-storage.js.map +1 -0
  49. package/dist/esm/client/slots.d.ts +59 -0
  50. package/dist/esm/client/slots.js +67 -0
  51. package/dist/esm/client/slots.js.map +1 -0
  52. package/dist/esm/client/sso.d.ts +33 -0
  53. package/dist/esm/client/sso.js +120 -0
  54. package/dist/esm/client/sso.js.map +1 -0
  55. package/dist/esm/client/state-storage.d.ts +135 -0
  56. package/dist/esm/client/state-storage.js +178 -0
  57. package/dist/esm/client/state-storage.js.map +1 -0
  58. package/package.json +5 -4
  59. package/src/client/app-delegation-source.ts +86 -0
  60. package/src/client/app-lock.ts +120 -0
  61. package/src/client/auth-client.ts +1300 -487
  62. package/src/client/base64.ts +28 -0
  63. package/src/client/cookie-state-storage.ts +277 -0
  64. package/src/client/credential-storage.ts +56 -0
  65. package/src/client/db.ts +56 -12
  66. package/src/client/foreground-refresh.ts +92 -0
  67. package/src/client/idb-credential-storage.ts +82 -0
  68. package/src/client/index.ts +24 -8
  69. package/src/client/local-credential-storage.ts +68 -0
  70. package/src/client/memory-credential-storage.ts +41 -0
  71. package/src/client/session-delegation.ts +138 -0
  72. package/src/client/session-identity.ts +510 -0
  73. package/src/client/session-minter.ts +277 -0
  74. package/src/client/shared-memory-credential-storage.ts +303 -0
  75. package/src/client/slots.ts +78 -0
  76. package/src/client/sso.ts +122 -0
  77. package/src/client/state-storage.ts +284 -0
  78. package/dist/esm/client/idle-manager.d.ts +0 -83
  79. package/dist/esm/client/idle-manager.js +0 -100
  80. package/dist/esm/client/idle-manager.js.map +0 -1
  81. package/dist/esm/client/storage.d.ts +0 -51
  82. package/dist/esm/client/storage.js +0 -90
  83. package/dist/esm/client/storage.js.map +0 -1
  84. package/src/client/idle-manager.ts +0 -162
  85. package/src/client/storage.ts +0 -114
@@ -1,90 +0,0 @@
1
- import { IdbKeyVal } from './db.js';
2
- export const KEY_STORAGE_KEY = 'identity';
3
- export const KEY_STORAGE_DELEGATION = 'delegation';
4
- export const KEY_VECTOR = 'iv';
5
- // Increment if any fields are modified
6
- export const DB_VERSION = 1;
7
- /**
8
- * Legacy implementation of AuthClientStorage, for use where IndexedDb is not available
9
- */
10
- export class LocalStorage {
11
- prefix;
12
- _localStorage;
13
- constructor(prefix = 'ic-', _localStorage) {
14
- this.prefix = prefix;
15
- this._localStorage = _localStorage;
16
- }
17
- get(key) {
18
- return Promise.resolve(this._getLocalStorage().getItem(this.prefix + key));
19
- }
20
- set(key, value) {
21
- this._getLocalStorage().setItem(this.prefix + key, value);
22
- return Promise.resolve();
23
- }
24
- remove(key) {
25
- this._getLocalStorage().removeItem(this.prefix + key);
26
- return Promise.resolve();
27
- }
28
- _getLocalStorage() {
29
- if (this._localStorage) {
30
- return this._localStorage;
31
- }
32
- const ls = globalThis.localStorage;
33
- if (!ls) {
34
- throw new Error('Could not find local storage.');
35
- }
36
- return ls;
37
- }
38
- }
39
- /**
40
- * IdbStorage is an interface for simple storage of string key-value pairs built on {@link IdbKeyVal}
41
- *
42
- * It replaces {@link LocalStorage}
43
- * @see implements {@link AuthClientStorage}
44
- */
45
- export class IdbStorage {
46
- #options;
47
- /**
48
- * @param options - DBCreateOptions
49
- * @param options.dbName - name for the indexeddb database
50
- * @param options.storeName - name for the indexeddb Data Store
51
- * @param options.version - version of the database. Increment to safely upgrade
52
- * @example
53
- * ```ts
54
- * const storage = new IdbStorage({ dbName: 'my-db', storeName: 'my-store', version: 2 });
55
- * ```
56
- */
57
- constructor(options) {
58
- this.#options = options ?? {};
59
- }
60
- // Initializes a KeyVal on first request
61
- initializedDb;
62
- get _db() {
63
- return new Promise((resolve, reject) => {
64
- if (this.initializedDb) {
65
- resolve(this.initializedDb);
66
- return;
67
- }
68
- IdbKeyVal.create(this.#options)
69
- .then((db) => {
70
- this.initializedDb = db;
71
- resolve(db);
72
- })
73
- .catch(reject);
74
- });
75
- }
76
- async get(key) {
77
- const db = await this._db;
78
- return await db.get(key);
79
- // return (await db.get<string>(key)) ?? null;
80
- }
81
- async set(key, value) {
82
- const db = await this._db;
83
- await db.set(key, value);
84
- }
85
- async remove(key) {
86
- const db = await this._db;
87
- await db.remove(key);
88
- }
89
- }
90
- //# sourceMappingURL=storage.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../../src/client/storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,SAAS,EAAE,MAAM,SAAS,CAAC;AAE1D,MAAM,CAAC,MAAM,eAAe,GAAG,UAAU,CAAC;AAC1C,MAAM,CAAC,MAAM,sBAAsB,GAAG,YAAY,CAAC;AACnD,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,CAAC;AAC/B,uCAAuC;AACvC,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC;AAe5B;;GAEG;AACH,MAAM,OAAO,YAAY;IAEL;IACC;IAFnB,YACkB,SAAS,KAAK,EACb,aAAuB;QADxB,WAAM,GAAN,MAAM,CAAQ;QACb,kBAAa,GAAb,aAAa,CAAU;IACvC,CAAC;IAEG,GAAG,CAAC,GAAW;QACpB,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,gBAAgB,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,CAAC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,CAAC,EAAE,CAAC,CAAC;YACd,CAAC,CAAC;iBACD,KAAK,CAAC,MAAM,CAAC,CAAC;QACnB,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,GAAG,CAAa,GAAW;QACtC,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC;QAC1B,OAAO,MAAM,EAAE,CAAC,GAAG,CAAI,GAAG,CAAC,CAAC;QAC5B,8CAA8C;IAChD,CAAC;IAEM,KAAK,CAAC,GAAG,CAAa,GAAW,EAAE,KAAQ;QAChD,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC;QAC1B,MAAM,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAC3B,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,GAAW;QAC7B,MAAM,EAAE,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC;QAC1B,MAAM,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC;CACF"}
@@ -1,162 +0,0 @@
1
- type IdleCB = () => unknown;
2
-
3
- export type IdleManagerOptions = {
4
- /**
5
- * Callback after the user has gone idle
6
- */
7
- onIdle?: IdleCB;
8
- /**
9
- * timeout in ms
10
- * @default 30 minutes [600_000]
11
- */
12
- idleTimeout?: number;
13
- /**
14
- * capture scroll events
15
- * @default false
16
- */
17
- captureScroll?: boolean;
18
- /**
19
- * scroll debounce time in ms
20
- * @default 100
21
- */
22
- scrollDebounce?: number;
23
- };
24
-
25
- const events = ['mousedown', 'mousemove', 'keydown', 'touchstart', 'wheel'];
26
-
27
- /**
28
- * Detects if the user has been idle for a duration of `idleTimeout` ms, and calls `onIdle` and registered callbacks.
29
- * By default, the IdleManager will log a user out after 10 minutes of inactivity.
30
- * To override these defaults, you can pass an `onIdle` callback, or configure a custom `idleTimeout` in milliseconds.
31
- *
32
- * IdleManager is a singleton: multiple calls to `create()` return the same instance,
33
- * registering any new `onIdle` callback. Call `exit()` to tear down the singleton.
34
- */
35
- export class IdleManager {
36
- static #instance: IdleManager | undefined;
37
-
38
- #callbacks: IdleCB[] = [];
39
- #idleTimeout: number;
40
- #timeoutID?: number = undefined;
41
- #resetTimer: () => void;
42
-
43
- /**
44
- * Creates or returns the singleton {@link IdleManager}.
45
- * If the instance already exists, any provided `onIdle` callback is registered
46
- * on the existing instance.
47
- * @param {IdleManagerOptions} options Optional configuration
48
- * @see {@link IdleManagerOptions}
49
- * @param options.onIdle Callback once user has been idle. Use to prompt for fresh sign-in, and use `Actor.agentOf(your_actor).invalidateIdentity()` to protect the user
50
- * @param options.idleTimeout timeout in ms
51
- * @param options.captureScroll capture scroll events
52
- * @param options.scrollDebounce scroll debounce time in ms
53
- */
54
- public static create(
55
- options: {
56
- /**
57
- * Callback after the user has gone idle
58
- * @see {@link IdleCB}
59
- */
60
- onIdle?: () => unknown;
61
- /**
62
- * timeout in ms
63
- * @default 10 minutes [600_000]
64
- */
65
- idleTimeout?: number;
66
- /**
67
- * capture scroll events
68
- * @default false
69
- */
70
- captureScroll?: boolean;
71
- /**
72
- * scroll debounce time in ms
73
- * @default 100
74
- */
75
- scrollDebounce?: number;
76
- } = {},
77
- ): IdleManager {
78
- if (IdleManager.#instance) {
79
- if (options.onIdle) {
80
- IdleManager.#instance.registerCallback(options.onIdle);
81
- }
82
- return IdleManager.#instance;
83
- }
84
- const instance = new IdleManager(options);
85
- IdleManager.#instance = instance;
86
- return instance;
87
- }
88
-
89
- /**
90
- * @param options {@link IdleManagerOptions}
91
- */
92
- private constructor(options: IdleManagerOptions = {}) {
93
- const { onIdle, idleTimeout = 10 * 60 * 1000 } = options || {};
94
-
95
- this.#callbacks = onIdle ? [onIdle] : [];
96
- this.#idleTimeout = idleTimeout;
97
-
98
- // Store the bound function once so the same reference is used
99
- // for both addEventListener and removeEventListener.
100
- this.#resetTimer = this._resetTimer.bind(this);
101
-
102
- window.addEventListener('load', this.#resetTimer, true);
103
-
104
- events.forEach((name) => {
105
- document.addEventListener(name, this.#resetTimer, true);
106
- });
107
-
108
- const debounce = (func: (...args: unknown[]) => void, wait: number) => {
109
- let timeout: number | undefined;
110
- return (...args: unknown[]) => {
111
- const context = this;
112
- const later = () => {
113
- timeout = undefined;
114
- func.apply(context, args);
115
- };
116
- clearTimeout(timeout);
117
- timeout = window.setTimeout(later, wait);
118
- };
119
- };
120
-
121
- if (options?.captureScroll) {
122
- // debounce scroll events
123
- const scroll = debounce(this.#resetTimer, options?.scrollDebounce ?? 100);
124
- window.addEventListener('scroll', scroll, true);
125
- }
126
-
127
- this.#resetTimer();
128
- }
129
-
130
- /**
131
- * @param {IdleCB} callback function to be called when user goes idle
132
- */
133
- public registerCallback(callback: IdleCB): void {
134
- this.#callbacks.push(callback);
135
- }
136
-
137
- /**
138
- * Tears down listeners, fires all callbacks, and clears the singleton.
139
- */
140
- public exit(): void {
141
- clearTimeout(this.#timeoutID);
142
- window.removeEventListener('load', this.#resetTimer, true);
143
-
144
- events.forEach((name) => {
145
- document.removeEventListener(name, this.#resetTimer, true);
146
- });
147
- this.#callbacks.forEach((cb) => {
148
- cb();
149
- });
150
-
151
- IdleManager.#instance = undefined;
152
- }
153
-
154
- /**
155
- * Resets the timeouts during cleanup
156
- */
157
- private _resetTimer(): void {
158
- const exit = this.exit.bind(this);
159
- window.clearTimeout(this.#timeoutID);
160
- this.#timeoutID = window.setTimeout(exit, this.#idleTimeout);
161
- }
162
- }
@@ -1,114 +0,0 @@
1
- import { type DBCreateOptions, IdbKeyVal } from './db.js';
2
-
3
- export const KEY_STORAGE_KEY = 'identity';
4
- export const KEY_STORAGE_DELEGATION = 'delegation';
5
- export const KEY_VECTOR = 'iv';
6
- // Increment if any fields are modified
7
- export const DB_VERSION = 1;
8
-
9
- export type StoredKey = string | CryptoKeyPair;
10
-
11
- /**
12
- * Interface for persisting user authentication data
13
- */
14
- export interface AuthClientStorage {
15
- get(key: string): Promise<StoredKey | null>;
16
-
17
- set(key: string, value: StoredKey): Promise<void>;
18
-
19
- remove(key: string): Promise<void>;
20
- }
21
-
22
- /**
23
- * Legacy implementation of AuthClientStorage, for use where IndexedDb is not available
24
- */
25
- export class LocalStorage implements AuthClientStorage {
26
- constructor(
27
- public readonly prefix = 'ic-',
28
- private readonly _localStorage?: Storage,
29
- ) {}
30
-
31
- public get(key: string): Promise<string | null> {
32
- return Promise.resolve(this._getLocalStorage().getItem(this.prefix + key));
33
- }
34
-
35
- public set(key: string, value: string): Promise<void> {
36
- this._getLocalStorage().setItem(this.prefix + key, value);
37
- return Promise.resolve();
38
- }
39
-
40
- public remove(key: string): Promise<void> {
41
- this._getLocalStorage().removeItem(this.prefix + key);
42
- return Promise.resolve();
43
- }
44
-
45
- private _getLocalStorage() {
46
- if (this._localStorage) {
47
- return this._localStorage;
48
- }
49
-
50
- const ls = globalThis.localStorage;
51
- if (!ls) {
52
- throw new Error('Could not find local storage.');
53
- }
54
-
55
- return ls;
56
- }
57
- }
58
-
59
- /**
60
- * IdbStorage is an interface for simple storage of string key-value pairs built on {@link IdbKeyVal}
61
- *
62
- * It replaces {@link LocalStorage}
63
- * @see implements {@link AuthClientStorage}
64
- */
65
- export class IdbStorage implements AuthClientStorage {
66
- #options: DBCreateOptions;
67
-
68
- /**
69
- * @param options - DBCreateOptions
70
- * @param options.dbName - name for the indexeddb database
71
- * @param options.storeName - name for the indexeddb Data Store
72
- * @param options.version - version of the database. Increment to safely upgrade
73
- * @example
74
- * ```ts
75
- * const storage = new IdbStorage({ dbName: 'my-db', storeName: 'my-store', version: 2 });
76
- * ```
77
- */
78
- constructor(options?: DBCreateOptions) {
79
- this.#options = options ?? {};
80
- }
81
-
82
- // Initializes a KeyVal on first request
83
- private initializedDb: IdbKeyVal | undefined;
84
- get _db(): Promise<IdbKeyVal> {
85
- return new Promise((resolve, reject) => {
86
- if (this.initializedDb) {
87
- resolve(this.initializedDb);
88
- return;
89
- }
90
- IdbKeyVal.create(this.#options)
91
- .then((db) => {
92
- this.initializedDb = db;
93
- resolve(db);
94
- })
95
- .catch(reject);
96
- });
97
- }
98
-
99
- public async get<T = string>(key: string): Promise<T | null> {
100
- const db = await this._db;
101
- return await db.get<T>(key);
102
- // return (await db.get<string>(key)) ?? null;
103
- }
104
-
105
- public async set<T = string>(key: string, value: T): Promise<void> {
106
- const db = await this._db;
107
- await db.set(key, value);
108
- }
109
-
110
- public async remove(key: string): Promise<void> {
111
- const db = await this._db;
112
- await db.remove(key);
113
- }
114
- }