@icp-sdk/auth 11.1.0 → 11.2.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.
- package/dist/esm/client/auth-client.d.ts +28 -17
- package/dist/esm/client/auth-client.js +65 -11
- package/dist/esm/client/auth-client.js.map +1 -1
- package/dist/esm/client/credentials/credential-storage.js.map +1 -0
- package/dist/esm/client/{idb-credential-storage.d.ts → credentials/idb-credential-storage.d.ts} +1 -1
- package/dist/esm/client/{idb-credential-storage.js → credentials/idb-credential-storage.js} +1 -1
- package/dist/esm/client/credentials/idb-credential-storage.js.map +1 -0
- package/dist/esm/client/credentials/local-credential-storage.js.map +1 -0
- package/dist/esm/client/credentials/memory-credential-storage.js.map +1 -0
- package/dist/esm/client/credentials/shared-memory-credential-storage.js.map +1 -0
- package/dist/esm/client/index.d.ts +14 -12
- package/dist/esm/client/index.js +11 -10
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/notification-consent.js +3 -3
- package/dist/esm/client/notification-consent.js.map +1 -1
- package/dist/esm/client/session/app-delegation-source.js.map +1 -0
- package/dist/esm/client/session/app-lock.js.map +1 -0
- package/dist/esm/client/{session-delegation.js → session/session-delegation.js} +1 -1
- package/dist/esm/client/session/session-delegation.js.map +1 -0
- package/dist/esm/client/{session-identity.d.ts → session/session-identity.d.ts} +1 -1
- package/dist/esm/client/session/session-identity.js.map +1 -0
- package/dist/esm/client/session/session-minter.js.map +1 -0
- package/dist/esm/client/sso.d.ts +91 -0
- package/dist/esm/client/sso.js +206 -0
- package/dist/esm/client/sso.js.map +1 -1
- package/dist/esm/client/{cookie-state-storage.d.ts → state/cookie-state-storage.d.ts} +11 -1
- package/dist/esm/client/{cookie-state-storage.js → state/cookie-state-storage.js} +25 -1
- package/dist/esm/client/state/cookie-state-storage.js.map +1 -0
- package/dist/esm/client/state/local-state-storage.d.ts +36 -0
- package/dist/esm/client/{state-storage.js → state/local-state-storage.js} +92 -66
- package/dist/esm/client/state/local-state-storage.js.map +1 -0
- package/dist/esm/client/state/memory-state-storage.d.ts +27 -0
- package/dist/esm/client/state/memory-state-storage.js +67 -0
- package/dist/esm/client/state/memory-state-storage.js.map +1 -0
- package/dist/esm/client/{state-storage.d.ts → state/state-storage.d.ts} +12 -46
- package/dist/esm/client/state/state-storage.js +2 -0
- package/dist/esm/client/state/state-storage.js.map +1 -0
- package/package.json +10 -10
- package/src/client/auth-client.ts +76 -17
- package/src/client/{idb-credential-storage.ts → credentials/idb-credential-storage.ts} +1 -1
- package/src/client/index.ts +20 -20
- package/src/client/notification-consent.ts +3 -3
- package/src/client/{session-delegation.ts → session/session-delegation.ts} +1 -1
- package/src/client/{session-identity.ts → session/session-identity.ts} +1 -1
- package/src/client/sso.ts +258 -0
- package/src/client/{cookie-state-storage.ts → state/cookie-state-storage.ts} +28 -1
- package/src/client/state/local-state-storage.ts +216 -0
- package/src/client/state/memory-state-storage.ts +75 -0
- package/src/client/state/state-storage.ts +112 -0
- package/dist/esm/client/app-delegation-source.js.map +0 -1
- package/dist/esm/client/app-lock.js.map +0 -1
- package/dist/esm/client/cookie-state-storage.js.map +0 -1
- package/dist/esm/client/credential-storage.js.map +0 -1
- package/dist/esm/client/idb-credential-storage.js.map +0 -1
- package/dist/esm/client/local-credential-storage.js.map +0 -1
- package/dist/esm/client/memory-credential-storage.js.map +0 -1
- package/dist/esm/client/session-delegation.js.map +0 -1
- package/dist/esm/client/session-identity.js.map +0 -1
- package/dist/esm/client/session-minter.js.map +0 -1
- package/dist/esm/client/shared-memory-credential-storage.js.map +0 -1
- package/dist/esm/client/sso-status.d.ts +0 -91
- package/dist/esm/client/sso-status.js +0 -207
- package/dist/esm/client/sso-status.js.map +0 -1
- package/dist/esm/client/state-storage.js.map +0 -1
- package/src/client/sso-status.ts +0 -257
- package/src/client/state-storage.ts +0 -284
- /package/dist/esm/client/{credential-storage.d.ts → credentials/credential-storage.d.ts} +0 -0
- /package/dist/esm/client/{credential-storage.js → credentials/credential-storage.js} +0 -0
- /package/dist/esm/client/{local-credential-storage.d.ts → credentials/local-credential-storage.d.ts} +0 -0
- /package/dist/esm/client/{local-credential-storage.js → credentials/local-credential-storage.js} +0 -0
- /package/dist/esm/client/{memory-credential-storage.d.ts → credentials/memory-credential-storage.d.ts} +0 -0
- /package/dist/esm/client/{memory-credential-storage.js → credentials/memory-credential-storage.js} +0 -0
- /package/dist/esm/client/{shared-memory-credential-storage.d.ts → credentials/shared-memory-credential-storage.d.ts} +0 -0
- /package/dist/esm/client/{shared-memory-credential-storage.js → credentials/shared-memory-credential-storage.js} +0 -0
- /package/dist/esm/client/{app-delegation-source.d.ts → session/app-delegation-source.d.ts} +0 -0
- /package/dist/esm/client/{app-delegation-source.js → session/app-delegation-source.js} +0 -0
- /package/dist/esm/client/{app-lock.d.ts → session/app-lock.d.ts} +0 -0
- /package/dist/esm/client/{app-lock.js → session/app-lock.js} +0 -0
- /package/dist/esm/client/{session-delegation.d.ts → session/session-delegation.d.ts} +0 -0
- /package/dist/esm/client/{session-identity.js → session/session-identity.js} +0 -0
- /package/dist/esm/client/{session-minter.d.ts → session/session-minter.d.ts} +0 -0
- /package/dist/esm/client/{session-minter.js → session/session-minter.js} +0 -0
- /package/src/client/{credential-storage.ts → credentials/credential-storage.ts} +0 -0
- /package/src/client/{local-credential-storage.ts → credentials/local-credential-storage.ts} +0 -0
- /package/src/client/{memory-credential-storage.ts → credentials/memory-credential-storage.ts} +0 -0
- /package/src/client/{shared-memory-credential-storage.ts → credentials/shared-memory-credential-storage.ts} +0 -0
- /package/src/client/{app-delegation-source.ts → session/app-delegation-source.ts} +0 -0
- /package/src/client/{app-lock.ts → session/app-lock.ts} +0 -0
- /package/src/client/{session-minter.ts → session/session-minter.ts} +0 -0
|
@@ -1,74 +1,28 @@
|
|
|
1
1
|
import { Principal } from '@icp-sdk/core/principal';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
get(key) {
|
|
13
|
-
// Nothing reaches past this instance, so a record here is always one it holds.
|
|
14
|
-
const state = this.#state.get(key);
|
|
15
|
-
return state === undefined ? null : { ...state, held: true };
|
|
16
|
-
}
|
|
17
|
-
set(key, state) {
|
|
18
|
-
this.#state.set(key, state);
|
|
19
|
-
this.#fire(key);
|
|
2
|
+
import { IdbKeyVal } from '../db.js';
|
|
3
|
+
/** Where tabs mirror their `localStorage` records for workers, which cannot read it. */
|
|
4
|
+
const MIRROR_DB = { dbName: 'auth-client-state-db', storeName: 'ic-keyval' };
|
|
5
|
+
/** The channel a tab announces a mirrored record on, carrying only its key. */
|
|
6
|
+
const MIRROR_CHANNEL = 'ic-session-state';
|
|
7
|
+
/** What a store throws when asked to change the sign-in where it can only read it. */
|
|
8
|
+
export const readOnlyError = () => new Error('The sign-in state is read-only in a worker: sign in and sign out from a page of this origin.');
|
|
9
|
+
const hasLocalStorage = () => {
|
|
10
|
+
try {
|
|
11
|
+
return globalThis.localStorage !== undefined && globalThis.localStorage !== null;
|
|
20
12
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
/** The same as {@link remove}: this store publishes nothing at all. */
|
|
26
|
-
discard(key) {
|
|
27
|
-
this.remove(key);
|
|
13
|
+
catch {
|
|
14
|
+
// Reading `localStorage` throws where the browser blocks storage for the page.
|
|
15
|
+
return false;
|
|
28
16
|
}
|
|
29
|
-
|
|
30
|
-
* Fires when the state changes, which here means when something writes to
|
|
31
|
-
* this instance.
|
|
32
|
-
*
|
|
33
|
-
* Nothing outside the process can reach a `Map`, so a medium is not what makes
|
|
34
|
-
* this worth having: two clients can be handed the same instance, and this is
|
|
35
|
-
* how the one that did not write finds out. Each listener is told only when the
|
|
36
|
-
* record actually changed since it was last told, so a client's own writes do
|
|
37
|
-
* not come back to it as news.
|
|
38
|
-
*/
|
|
39
|
-
subscribe(key, listener) {
|
|
40
|
-
let last = this.#raw(key);
|
|
41
|
-
const check = () => {
|
|
42
|
-
const now = this.#raw(key);
|
|
43
|
-
if (now !== last) {
|
|
44
|
-
last = now;
|
|
45
|
-
listener();
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
const listeners = this.#subscribers.get(key) ?? new Set();
|
|
49
|
-
listeners.add(check);
|
|
50
|
-
this.#subscribers.set(key, listeners);
|
|
51
|
-
return () => {
|
|
52
|
-
listeners.delete(check);
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
#fire(key) {
|
|
56
|
-
for (const check of [...(this.#subscribers.get(key) ?? [])])
|
|
57
|
-
check();
|
|
58
|
-
}
|
|
59
|
-
// The same shape `LocalStateStorage` keeps, so "changed" means the same thing
|
|
60
|
-
// in both: a record is one string, and comparing strings needs no field walk.
|
|
61
|
-
#raw(key) {
|
|
62
|
-
const state = this.#state.get(key);
|
|
63
|
-
return state === undefined
|
|
64
|
-
? null
|
|
65
|
-
: `${state.principal.toText()}|${state.expiration.toString()}`;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
17
|
+
};
|
|
68
18
|
/**
|
|
69
19
|
* State in `localStorage`, so every tab of an origin agrees on it and it
|
|
70
20
|
* survives a reload.
|
|
71
21
|
*
|
|
22
|
+
* Workers cannot read `localStorage`, so a tab mirrors each record it writes or
|
|
23
|
+
* subscribes to into IndexedDB and announces it, and a worker loads the mirror
|
|
24
|
+
* into memory. A worker changes nothing: signing in and out happens in a page.
|
|
25
|
+
*
|
|
72
26
|
* The two fields are written as `<principal>|<expiration>` rather than JSON,
|
|
73
27
|
* because an expiration is a `bigint` and JSON has no way to carry one.
|
|
74
28
|
* @see implements {@link StateStorage}
|
|
@@ -78,8 +32,22 @@ export class LocalStateStorage {
|
|
|
78
32
|
// deliberately not the one that wrote. Per key, because one store answers for
|
|
79
33
|
// whatever key it is handed.
|
|
80
34
|
#subscribers = new Map();
|
|
35
|
+
/** True in a worker, which reads what its tabs mirror and changes nothing. */
|
|
36
|
+
isReadOnly = !hasLocalStorage();
|
|
37
|
+
// In a worker: the mirrored records, and the loads that fill them, one after
|
|
38
|
+
// another so a later load always lands last.
|
|
39
|
+
#mirrored = new Map();
|
|
40
|
+
#loads = Promise.resolve();
|
|
41
|
+
/** Resolves once a worker has loaded the keys subscribed to so far, and every load queued meanwhile. */
|
|
42
|
+
async ready() {
|
|
43
|
+
let loads;
|
|
44
|
+
do {
|
|
45
|
+
loads = this.#loads;
|
|
46
|
+
await loads;
|
|
47
|
+
} while (loads !== this.#loads);
|
|
48
|
+
}
|
|
81
49
|
get(key) {
|
|
82
|
-
const raw = this.#
|
|
50
|
+
const raw = this.#raw(key);
|
|
83
51
|
if (raw === null)
|
|
84
52
|
return null;
|
|
85
53
|
const [principalText, expiration] = raw.split('|');
|
|
@@ -99,12 +67,18 @@ export class LocalStateStorage {
|
|
|
99
67
|
}
|
|
100
68
|
}
|
|
101
69
|
set(key, state) {
|
|
70
|
+
if (this.isReadOnly)
|
|
71
|
+
throw readOnlyError();
|
|
102
72
|
this.#localStorage().setItem(key, `${state.principal.toText()}|${state.expiration.toString()}`);
|
|
103
73
|
this.#fire(key);
|
|
74
|
+
void this.#mirror(key);
|
|
104
75
|
}
|
|
105
76
|
remove(key) {
|
|
77
|
+
if (this.isReadOnly)
|
|
78
|
+
throw readOnlyError();
|
|
106
79
|
this.#localStorage().removeItem(key);
|
|
107
80
|
this.#fire(key);
|
|
81
|
+
void this.#mirror(key);
|
|
108
82
|
}
|
|
109
83
|
/** The same as {@link remove}: this store publishes nothing beyond the origin. */
|
|
110
84
|
discard(key) {
|
|
@@ -130,6 +104,27 @@ export class LocalStateStorage {
|
|
|
130
104
|
const listeners = this.#subscribers.get(key) ?? new Set();
|
|
131
105
|
listeners.add(check);
|
|
132
106
|
this.#subscribers.set(key, listeners);
|
|
107
|
+
// A tab mirrors what it holds now, which also repairs a mirror an earlier
|
|
108
|
+
// version or a closing page left behind; a worker loads it. Afterwards each
|
|
109
|
+
// announcement is a write in some tab: a worker loads it, and a tab re-checks,
|
|
110
|
+
// because a write by another store in this tab raises no `storage` event.
|
|
111
|
+
if (this.isReadOnly) {
|
|
112
|
+
this.#load(key);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
void this.#mirror(key);
|
|
116
|
+
}
|
|
117
|
+
const channel = typeof BroadcastChannel === 'undefined' ? undefined : new BroadcastChannel(MIRROR_CHANNEL);
|
|
118
|
+
channel?.addEventListener('message', (event) => {
|
|
119
|
+
if (event.data !== key)
|
|
120
|
+
return;
|
|
121
|
+
if (this.isReadOnly) {
|
|
122
|
+
this.#load(key);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
check();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
133
128
|
const onStorage = (event) => {
|
|
134
129
|
// `key` is null when a tab cleared the whole store, which changes this too.
|
|
135
130
|
if (event.key === key || event.key === null)
|
|
@@ -152,6 +147,7 @@ export class LocalStateStorage {
|
|
|
152
147
|
}
|
|
153
148
|
return () => {
|
|
154
149
|
listeners.delete(check);
|
|
150
|
+
channel?.close();
|
|
155
151
|
globalThis.removeEventListener('storage', onStorage);
|
|
156
152
|
if (hooked) {
|
|
157
153
|
document.removeEventListener('visibilitychange', onVisible);
|
|
@@ -165,8 +161,38 @@ export class LocalStateStorage {
|
|
|
165
161
|
check();
|
|
166
162
|
}
|
|
167
163
|
#raw(key) {
|
|
164
|
+
if (this.isReadOnly)
|
|
165
|
+
return this.#mirrored.get(key) ?? null;
|
|
168
166
|
return this.#localStorage().getItem(key);
|
|
169
167
|
}
|
|
168
|
+
/** Writes what `localStorage` holds under `key` to the mirror, and announces it. */
|
|
169
|
+
async #mirror(key) {
|
|
170
|
+
try {
|
|
171
|
+
const db = await IdbKeyVal.create(MIRROR_DB);
|
|
172
|
+
// Read after the await and right before the write: IndexedDB applies writes
|
|
173
|
+
// in the order they are made, so the last to land holds what is current.
|
|
174
|
+
await db.set(key, { raw: this.#raw(key) });
|
|
175
|
+
const channel = new BroadcastChannel(MIRROR_CHANNEL);
|
|
176
|
+
channel.postMessage(key);
|
|
177
|
+
channel.close();
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
// No IndexedDB or no channel here: workers keep what they have until the
|
|
181
|
+
// next write reaches them.
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
#load(key) {
|
|
185
|
+
this.#loads = this.#loads.then(async () => {
|
|
186
|
+
try {
|
|
187
|
+
const db = await IdbKeyVal.create(MIRROR_DB);
|
|
188
|
+
this.#mirrored.set(key, (await db.get(key))?.raw ?? null);
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
// No IndexedDB here: the worker reads as signed out.
|
|
192
|
+
}
|
|
193
|
+
this.#fire(key);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
170
196
|
#localStorage() {
|
|
171
197
|
const ls = globalThis.localStorage;
|
|
172
198
|
if (!ls) {
|
|
@@ -175,4 +201,4 @@ export class LocalStateStorage {
|
|
|
175
201
|
return ls;
|
|
176
202
|
}
|
|
177
203
|
}
|
|
178
|
-
//# sourceMappingURL=state-storage.js.map
|
|
204
|
+
//# sourceMappingURL=local-state-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-state-storage.js","sourceRoot":"","sources":["../../../../src/client/state/local-state-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAGrC,wFAAwF;AACxF,MAAM,SAAS,GAAG,EAAE,MAAM,EAAE,sBAAsB,EAAE,SAAS,EAAE,WAAW,EAAE,CAAC;AAE7E,+EAA+E;AAC/E,MAAM,cAAc,GAAG,kBAAkB,CAAC;AAE1C,sFAAsF;AACtF,MAAM,CAAC,MAAM,aAAa,GAAG,GAAU,EAAE,CACvC,IAAI,KAAK,CACP,8FAA8F,CAC/F,CAAC;AAEJ,MAAM,eAAe,GAAG,GAAY,EAA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
+
* State held for the lifetime of the instance and shared with nothing.
|
|
4
|
+
*
|
|
5
|
+
* For tests, and for a client that should share its sign-in with nothing: a
|
|
6
|
+
* reload starts from nothing.
|
|
7
|
+
* @see implements {@link StateStorage}
|
|
8
|
+
*/
|
|
9
|
+
export declare class MemoryStateStorage implements StateStorage {
|
|
10
|
+
#private;
|
|
11
|
+
get(key: string): SessionState | null;
|
|
12
|
+
set(key: string, state: Omit<SessionState, 'held'>): void;
|
|
13
|
+
remove(key: string): void;
|
|
14
|
+
/** The same as {@link remove}: this store publishes nothing at all. */
|
|
15
|
+
discard(key: string): void;
|
|
16
|
+
/**
|
|
17
|
+
* Fires when the state changes, which here means when something writes to
|
|
18
|
+
* this instance.
|
|
19
|
+
*
|
|
20
|
+
* Nothing outside the process can reach a `Map`, so a medium is not what makes
|
|
21
|
+
* this worth having: two clients can be handed the same instance, and this is
|
|
22
|
+
* how the one that did not write finds out. Each listener is told only when the
|
|
23
|
+
* record actually changed since it was last told, so a client's own writes do
|
|
24
|
+
* not come back to it as news.
|
|
25
|
+
*/
|
|
26
|
+
subscribe(key: string, listener: () => void): () => void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* State held for the lifetime of the instance and shared with nothing.
|
|
3
|
+
*
|
|
4
|
+
* For tests, and for a client that should share its sign-in with nothing: a
|
|
5
|
+
* reload starts from nothing.
|
|
6
|
+
* @see implements {@link StateStorage}
|
|
7
|
+
*/
|
|
8
|
+
export class MemoryStateStorage {
|
|
9
|
+
#state = new Map();
|
|
10
|
+
#subscribers = new Map();
|
|
11
|
+
get(key) {
|
|
12
|
+
// Nothing reaches past this instance, so a record here is always one it holds.
|
|
13
|
+
const state = this.#state.get(key);
|
|
14
|
+
return state === undefined ? null : { ...state, held: true };
|
|
15
|
+
}
|
|
16
|
+
set(key, state) {
|
|
17
|
+
this.#state.set(key, state);
|
|
18
|
+
this.#fire(key);
|
|
19
|
+
}
|
|
20
|
+
remove(key) {
|
|
21
|
+
this.#state.delete(key);
|
|
22
|
+
this.#fire(key);
|
|
23
|
+
}
|
|
24
|
+
/** The same as {@link remove}: this store publishes nothing at all. */
|
|
25
|
+
discard(key) {
|
|
26
|
+
this.remove(key);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Fires when the state changes, which here means when something writes to
|
|
30
|
+
* this instance.
|
|
31
|
+
*
|
|
32
|
+
* Nothing outside the process can reach a `Map`, so a medium is not what makes
|
|
33
|
+
* this worth having: two clients can be handed the same instance, and this is
|
|
34
|
+
* how the one that did not write finds out. Each listener is told only when the
|
|
35
|
+
* record actually changed since it was last told, so a client's own writes do
|
|
36
|
+
* not come back to it as news.
|
|
37
|
+
*/
|
|
38
|
+
subscribe(key, listener) {
|
|
39
|
+
let last = this.#raw(key);
|
|
40
|
+
const check = () => {
|
|
41
|
+
const now = this.#raw(key);
|
|
42
|
+
if (now !== last) {
|
|
43
|
+
last = now;
|
|
44
|
+
listener();
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const listeners = this.#subscribers.get(key) ?? new Set();
|
|
48
|
+
listeners.add(check);
|
|
49
|
+
this.#subscribers.set(key, listeners);
|
|
50
|
+
return () => {
|
|
51
|
+
listeners.delete(check);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
#fire(key) {
|
|
55
|
+
for (const check of [...(this.#subscribers.get(key) ?? [])])
|
|
56
|
+
check();
|
|
57
|
+
}
|
|
58
|
+
// The same shape `LocalStateStorage` keeps, so "changed" means the same thing
|
|
59
|
+
// in both: a record is one string, and comparing strings needs no field walk.
|
|
60
|
+
#raw(key) {
|
|
61
|
+
const state = this.#state.get(key);
|
|
62
|
+
return state === undefined
|
|
63
|
+
? null
|
|
64
|
+
: `${state.principal.toText()}|${state.expiration.toString()}`;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=memory-state-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC;QAC1D,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACrB,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAEtC,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC1B,CAAC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,GAAW;QACf,KAAK,MAAM,KAAK,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;YAAE,KAAK,EAAE,CAAC;IACvE,CAAC;IAED,8EAA8E;IAC9E,8EAA8E;IAC9E,IAAI,CAAC,GAAW;QACd,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnC,OAAO,KAAK,KAAK,SAAS;YACxB,CAAC,CAAC,IAAI;YACN,CAAC,CAAC,GAAG,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,KAAK,CAAC,UAAU,CAAC,QAAQ,EAAE,EAAE,CAAC;IACnE,CAAC;CACF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Principal } from '@icp-sdk/core/principal';
|
|
1
|
+
import type { Principal } from '@icp-sdk/core/principal';
|
|
2
2
|
/**
|
|
3
3
|
* What a client knows about its sign-in without holding a credential: which
|
|
4
4
|
* account is signed in here, and when that ends.
|
|
@@ -81,55 +81,21 @@ export interface StateStorage {
|
|
|
81
81
|
* changed under it goes on answering for a sign-in that is no longer there.
|
|
82
82
|
*/
|
|
83
83
|
subscribe(key: string, listener: () => void): () => void;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* State held for the lifetime of the instance and shared with nothing.
|
|
87
|
-
*
|
|
88
|
-
* For tests and for environments with no `localStorage`, where a client is
|
|
89
|
-
* alone and a reload starts from nothing.
|
|
90
|
-
* @see implements {@link StateStorage}
|
|
91
|
-
*/
|
|
92
|
-
export declare class MemoryStateStorage implements StateStorage {
|
|
93
|
-
#private;
|
|
94
|
-
get(key: string): SessionState | null;
|
|
95
|
-
set(key: string, state: Omit<SessionState, 'held'>): void;
|
|
96
|
-
remove(key: string): void;
|
|
97
|
-
/** The same as {@link remove}: this store publishes nothing at all. */
|
|
98
|
-
discard(key: string): void;
|
|
99
84
|
/**
|
|
100
|
-
*
|
|
101
|
-
*
|
|
85
|
+
* Whether this store only reads the state here, as a store does in a worker.
|
|
86
|
+
* The client then changes nothing: it cannot sign in or out, and a session it
|
|
87
|
+
* finds unusable is left for a page of this origin to end.
|
|
102
88
|
*
|
|
103
|
-
*
|
|
104
|
-
* this worth having: two clients can be handed the same instance, and this is
|
|
105
|
-
* how the one that did not write finds out. Each listener is told only when the
|
|
106
|
-
* record actually changed since it was last told, so a client's own writes do
|
|
107
|
-
* not come back to it as news.
|
|
89
|
+
* Absent means false.
|
|
108
90
|
*/
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* State in `localStorage`, so every tab of an origin agrees on it and it
|
|
113
|
-
* survives a reload.
|
|
114
|
-
*
|
|
115
|
-
* The two fields are written as `<principal>|<expiration>` rather than JSON,
|
|
116
|
-
* because an expiration is a `bigint` and JSON has no way to carry one.
|
|
117
|
-
* @see implements {@link StateStorage}
|
|
118
|
-
*/
|
|
119
|
-
export declare class LocalStateStorage implements StateStorage {
|
|
120
|
-
#private;
|
|
121
|
-
get(key: string): SessionState | null;
|
|
122
|
-
set(key: string, state: Omit<SessionState, 'held'>): void;
|
|
123
|
-
remove(key: string): void;
|
|
124
|
-
/** The same as {@link remove}: this store publishes nothing beyond the origin. */
|
|
125
|
-
discard(key: string): void;
|
|
91
|
+
readonly isReadOnly?: boolean;
|
|
126
92
|
/**
|
|
127
|
-
*
|
|
93
|
+
* Resolves once {@link get} answers with the stored state, for a store that
|
|
94
|
+
* loads it asynchronously. The client waits for it before restoring a session:
|
|
95
|
+
* a store still loading reads as signed out, and restoring from that would end
|
|
96
|
+
* the session.
|
|
128
97
|
*
|
|
129
|
-
*
|
|
130
|
-
* that event carries a change between tabs and a same-tab write is announced
|
|
131
|
-
* directly. Either way the record is readable before it is announced, so a
|
|
132
|
-
* listener asking who is signed in sees what it was told about.
|
|
98
|
+
* Absent means the store is ready from the start.
|
|
133
99
|
*/
|
|
134
|
-
|
|
100
|
+
ready?(): Promise<void>;
|
|
135
101
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-storage.js","sourceRoot":"","sources":["../../../../src/client/state/state-storage.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icp-sdk/auth",
|
|
3
|
-
"version": "11.1
|
|
3
|
+
"version": "11.2.1",
|
|
4
4
|
"author": "DFINITY Stiftung <sdk@dfinity.org>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"description": "Authentication library for Internet Computer web apps",
|
|
@@ -42,20 +42,20 @@
|
|
|
42
42
|
"internet identity"
|
|
43
43
|
],
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@biomejs/biome": "^2.
|
|
46
|
-
"@icp-sdk/core": "^6.1.
|
|
47
|
-
"fake-indexeddb": "^6.2.
|
|
48
|
-
"jsdom": "^
|
|
49
|
-
"publint": "^0.3.
|
|
50
|
-
"typescript": "^
|
|
51
|
-
"vitest": "
|
|
45
|
+
"@biomejs/biome": "^2.6.4",
|
|
46
|
+
"@icp-sdk/core": "^6.1.1",
|
|
47
|
+
"fake-indexeddb": "^6.2.7",
|
|
48
|
+
"jsdom": "^30.1.3",
|
|
49
|
+
"publint": "^0.3.25",
|
|
50
|
+
"typescript": "^6.0.3",
|
|
51
|
+
"vitest": "5.0.5"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@icp-sdk/core": "^6"
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@icp-sdk/signer": "^6.1.
|
|
58
|
-
"idb": "^
|
|
57
|
+
"@icp-sdk/signer": "^6.1.1",
|
|
58
|
+
"idb": "^8.0.4"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "tsc -p tsconfig.build.json && pnpm lint:package",
|
|
@@ -13,16 +13,10 @@ import {
|
|
|
13
13
|
import { Principal } from '@icp-sdk/core/principal';
|
|
14
14
|
import { Signer } from '@icp-sdk/signer';
|
|
15
15
|
import { PostMessageTransport, UrlTransport } from '@icp-sdk/signer/web';
|
|
16
|
-
import {
|
|
17
|
-
AccountMismatchError,
|
|
18
|
-
chainAuthorisesKey,
|
|
19
|
-
SessionGoneError,
|
|
20
|
-
} from './app-delegation-source.js';
|
|
21
|
-
import { type HeldLock, stealLock, stealMintLock } from './app-lock.js';
|
|
22
16
|
import { fromBase64, toBase64 } from './base64.js';
|
|
23
|
-
import type { Credential, CredentialStorage } from './credential-storage.js';
|
|
17
|
+
import type { Credential, CredentialStorage } from './credentials/credential-storage.js';
|
|
18
|
+
import { IdbCredentialStorage } from './credentials/idb-credential-storage.js';
|
|
24
19
|
import { watchActivity, watchForeground } from './foreground-refresh.js';
|
|
25
|
-
import { IdbCredentialStorage } from './idb-credential-storage.js';
|
|
26
20
|
import {
|
|
27
21
|
NotificationConsent,
|
|
28
22
|
type NotificationConsentStatus,
|
|
@@ -30,13 +24,24 @@ import {
|
|
|
30
24
|
// is the request it sends.
|
|
31
25
|
requestNotificationConsent as sendConsentRequest,
|
|
32
26
|
} from './notification-consent.js';
|
|
33
|
-
import {
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
import {
|
|
28
|
+
AccountMismatchError,
|
|
29
|
+
chainAuthorisesKey,
|
|
30
|
+
SessionGoneError,
|
|
31
|
+
} from './session/app-delegation-source.js';
|
|
32
|
+
import { type HeldLock, stealLock, stealMintLock } from './session/app-lock.js';
|
|
33
|
+
import { requestSessionDelegation } from './session/session-delegation.js';
|
|
34
|
+
import { SessionIdentity } from './session/session-identity.js';
|
|
35
|
+
import { SessionMinter } from './session/session-minter.js';
|
|
36
36
|
import { type Slots, slotsFor } from './slots.js';
|
|
37
|
-
import {
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
import {
|
|
38
|
+
createSsoDomainService,
|
|
39
|
+
normalizeSsoDomain,
|
|
40
|
+
type SsoStatus,
|
|
41
|
+
SsoStatusChecker,
|
|
42
|
+
} from './sso.js';
|
|
43
|
+
import { LocalStateStorage, readOnlyError } from './state/local-state-storage.js';
|
|
44
|
+
import type { SessionState, StateStorage } from './state/state-storage.js';
|
|
40
45
|
|
|
41
46
|
/**
|
|
42
47
|
* The lock one signer interaction at a time holds.
|
|
@@ -90,7 +95,9 @@ export interface AuthClientBaseOptions {
|
|
|
90
95
|
|
|
91
96
|
/**
|
|
92
97
|
* Where the state of the sign-in is kept: which account is signed in here,
|
|
93
|
-
* and until when. Defaults to
|
|
98
|
+
* and until when. Defaults to one {@link LocalStateStorage} shared by every
|
|
99
|
+
* client in this page or worker that sets none, and followed by the other
|
|
100
|
+
* tabs and workers of this origin.
|
|
94
101
|
*/
|
|
95
102
|
stateStorage?: StateStorage;
|
|
96
103
|
|
|
@@ -354,6 +361,15 @@ export class SessionNotHeldError extends Error {
|
|
|
354
361
|
* ? await authClient.getIdentity()
|
|
355
362
|
* : await authClient.signIn();
|
|
356
363
|
*/
|
|
364
|
+
// Every client in this page or worker that brings no store of its own shares
|
|
365
|
+
// this one. They share the sign-in anyway, since it lives in one record, and one
|
|
366
|
+
// store tells each of them about the others' writes as they happen.
|
|
367
|
+
let sharedStateStorage: LocalStateStorage | undefined;
|
|
368
|
+
const defaultStateStorage = (): LocalStateStorage => {
|
|
369
|
+
sharedStateStorage ??= new LocalStateStorage();
|
|
370
|
+
return sharedStateStorage;
|
|
371
|
+
};
|
|
372
|
+
|
|
357
373
|
/**
|
|
358
374
|
* Thrown when a later sign-in or sign-out took over from this one.
|
|
359
375
|
*
|
|
@@ -412,7 +428,7 @@ export class AuthClient {
|
|
|
412
428
|
this.#options = options;
|
|
413
429
|
this.#credentialStorage = options.credentialStorage ?? new IdbCredentialStorage();
|
|
414
430
|
this.#slots = slotsFor(options.namespace);
|
|
415
|
-
this.#stateStorage = options.stateStorage ??
|
|
431
|
+
this.#stateStorage = options.stateStorage ?? defaultStateStorage();
|
|
416
432
|
|
|
417
433
|
// A string or URL is what this option used to be, and a caller still passing
|
|
418
434
|
// one would otherwise be silently ignored — both halves falling back to
|
|
@@ -570,6 +586,31 @@ export class AuthClient {
|
|
|
570
586
|
this.#init();
|
|
571
587
|
}
|
|
572
588
|
|
|
589
|
+
/**
|
|
590
|
+
* Resolves once this client has loaded the stored state and restored the
|
|
591
|
+
* session, so that {@link isAuthenticated}, {@link getStatus} and
|
|
592
|
+
* {@link getPrincipal} give the stored answer.
|
|
593
|
+
*
|
|
594
|
+
* Only needed where there is no `localStorage`, which is a web worker or a
|
|
595
|
+
* service worker. There the state is loaded from IndexedDB, and until then the
|
|
596
|
+
* client reads as signed out:
|
|
597
|
+
*
|
|
598
|
+
* ```ts
|
|
599
|
+
* const client = new AuthClient();
|
|
600
|
+
* await client.ready();
|
|
601
|
+
* client.isAuthenticated();
|
|
602
|
+
* ```
|
|
603
|
+
*
|
|
604
|
+
* In a browser tab, don't wait for it. The status is correct from the moment
|
|
605
|
+
* the client is constructed, and waiting would hold up the page on IndexedDB
|
|
606
|
+
* and a session restore for nothing.
|
|
607
|
+
*
|
|
608
|
+
* Rejects where restoring the session fails, as {@link getIdentity} does.
|
|
609
|
+
*/
|
|
610
|
+
ready(): Promise<void> {
|
|
611
|
+
return this.#init();
|
|
612
|
+
}
|
|
613
|
+
|
|
573
614
|
/**
|
|
574
615
|
* Returns the current identity, restoring a previous session if available.
|
|
575
616
|
*/
|
|
@@ -880,6 +921,7 @@ export class AuthClient {
|
|
|
880
921
|
}
|
|
881
922
|
|
|
882
923
|
async signIn(options?: AuthClientSignInOptions): Promise<Identity> {
|
|
924
|
+
if (this.#isReadOnly) throw readOnlyError();
|
|
883
925
|
// Counted here and locked inside, once the window is open: a ceremony is
|
|
884
926
|
// both things — the signer channel an origin has one of, and this
|
|
885
927
|
// namespace's sign-in, which `signOut` moves too — but neither lock may be
|
|
@@ -1345,6 +1387,7 @@ export class AuthClient {
|
|
|
1345
1387
|
* @param options.returnTo - URL to navigate to after sign-out.
|
|
1346
1388
|
*/
|
|
1347
1389
|
async signOut(options: { returnTo?: string } = {}): Promise<void> {
|
|
1390
|
+
if (this.#isReadOnly) throw readOnlyError();
|
|
1348
1391
|
// Wait for the constructor's session restore: hydration racing the
|
|
1349
1392
|
// deletion below could re-populate the identity from already-read state.
|
|
1350
1393
|
await this.#init();
|
|
@@ -1569,6 +1612,7 @@ export class AuthClient {
|
|
|
1569
1612
|
* @param principal - The account whose session ended.
|
|
1570
1613
|
*/
|
|
1571
1614
|
async #expireSession(principal: Principal): Promise<void> {
|
|
1615
|
+
if (this.#isReadOnly) return;
|
|
1572
1616
|
this.#stateStorage.set(this.#slots.state, {
|
|
1573
1617
|
principal,
|
|
1574
1618
|
expiration: BigInt(Date.now()) * 1_000_000n,
|
|
@@ -1636,6 +1680,7 @@ export class AuthClient {
|
|
|
1636
1680
|
async #restore(): Promise<void> {
|
|
1637
1681
|
this.#restoring = true;
|
|
1638
1682
|
try {
|
|
1683
|
+
await this.#stateStorage.ready?.();
|
|
1639
1684
|
await this.#hydrate();
|
|
1640
1685
|
} finally {
|
|
1641
1686
|
this.#restoring = false;
|
|
@@ -1667,6 +1712,9 @@ export class AuthClient {
|
|
|
1667
1712
|
// signed in removes nothing.
|
|
1668
1713
|
if (this.#stateStorage.get(this.#slots.state) === null) {
|
|
1669
1714
|
await this.#endSession();
|
|
1715
|
+
// Installed here too: on a restore after the record changed, an identity
|
|
1716
|
+
// from the sign-in that just ended is still in place.
|
|
1717
|
+
this.#installIdentity(new AnonymousIdentity());
|
|
1670
1718
|
return;
|
|
1671
1719
|
}
|
|
1672
1720
|
|
|
@@ -1753,7 +1801,7 @@ export class AuthClient {
|
|
|
1753
1801
|
// the session's, so a chain this old leaves a record that reads as expired —
|
|
1754
1802
|
// which is what a page loading after a session ran out should say.
|
|
1755
1803
|
if (credential.chain === undefined || !isDelegationValid(credential.chain)) {
|
|
1756
|
-
await this.#clearCredentials();
|
|
1804
|
+
if (!this.#isReadOnly) await this.#clearCredentials();
|
|
1757
1805
|
return null;
|
|
1758
1806
|
}
|
|
1759
1807
|
return credential;
|
|
@@ -1766,6 +1814,7 @@ export class AuthClient {
|
|
|
1766
1814
|
* origin, because a sibling reading a shared record must stop seeing one.
|
|
1767
1815
|
*/
|
|
1768
1816
|
async #endSession(): Promise<void> {
|
|
1817
|
+
if (this.#isReadOnly) return;
|
|
1769
1818
|
this.#stateStorage.remove(this.#slots.state);
|
|
1770
1819
|
await this.#clearCredentials();
|
|
1771
1820
|
}
|
|
@@ -1780,10 +1829,20 @@ export class AuthClient {
|
|
|
1780
1829
|
* obtained is gone.
|
|
1781
1830
|
*/
|
|
1782
1831
|
async #dropSession(): Promise<void> {
|
|
1832
|
+
if (this.#isReadOnly) return;
|
|
1783
1833
|
this.#stateStorage.discard(this.#slots.state);
|
|
1784
1834
|
await this.#clearCredentials();
|
|
1785
1835
|
}
|
|
1786
1836
|
|
|
1837
|
+
/**
|
|
1838
|
+
* Whether this client only reads the sign-in, as it does in a worker. It then
|
|
1839
|
+
* changes nothing shared: a session it cannot use is left for a page of this
|
|
1840
|
+
* origin to end, which runs the same checks.
|
|
1841
|
+
*/
|
|
1842
|
+
get #isReadOnly(): boolean {
|
|
1843
|
+
return this.#stateStorage.isReadOnly === true;
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1787
1846
|
// Every slot a completed sign-in writes. Not the ceremony's pending slot: a
|
|
1788
1847
|
// flow that completed has already emptied it, and one that has not may still
|
|
1789
1848
|
// return for it — a sign-out here cannot tell an abandoned key from a live one.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DelegationChain, ECDSAKeyIdentity } from '@icp-sdk/core/identity';
|
|
2
|
+
import { type DBCreateOptions, IdbKeyVal } from '../db.js';
|
|
2
3
|
import type { Credential, CredentialStorage } from './credential-storage.js';
|
|
3
|
-
import { type DBCreateOptions, IdbKeyVal } from './db.js';
|
|
4
4
|
|
|
5
5
|
/** What one slot holds in the database: a key handle, and the chain as JSON. */
|
|
6
6
|
interface StoredCredential {
|