@icp-sdk/auth 11.0.0 → 11.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/client/auth-client.d.ts +73 -17
- package/dist/esm/client/auth-client.js +155 -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 +15 -12
- package/dist/esm/client/index.js +12 -10
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/notification-consent.d.ts +102 -0
- package/dist/esm/client/notification-consent.js +143 -0
- package/dist/esm/client/notification-consent.js.map +1 -0
- 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 +9 -9
- package/src/client/auth-client.ts +174 -13
- package/src/client/{idb-credential-storage.ts → credentials/idb-credential-storage.ts} +1 -1
- package/src/client/index.ts +28 -20
- package/src/client/notification-consent.ts +214 -0
- 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
|
@@ -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,"file":"memory-state-storage.js","sourceRoot":"","sources":["../../../../src/client/state/memory-state-storage.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,OAAO,kBAAkB;IAC7B,MAAM,GAAG,IAAI,GAAG,EAAsC,CAAC;IACvD,YAAY,GAAG,IAAI,GAAG,EAA2B,CAAC;IAE3C,GAAG,CAAC,GAAW;QACpB,+EAA+E;QAC/E,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnC,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IAC/D,CAAC;IAEM,GAAG,CAAC,GAAW,EAAE,KAAiC;QACvD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC5B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,CAAC;IAEM,MAAM,CAAC,GAAW;QACvB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACxB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,CAAC;IAED,uEAAuE;IAChE,OAAO,CAAC,GAAW;QACxB,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC;IAED;;;;;;;;;OASG;IACI,SAAS,CAAC,GAAW,EAAE,QAAoB;QAChD,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC1B,MAAM,KAAK,GAAG,GAAS,EAAE;YACvB,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAC3B,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;gBACjB,IAAI,GAAG,GAAG,CAAC;gBACX,QAAQ,EAAE,CAAC;YACb,CAAC;QACH,CAAC,CAAC;QACF,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.
|
|
3
|
+
"version": "11.2.0",
|
|
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
57
|
"@icp-sdk/signer": "^6.1.0",
|
|
58
|
-
"idb": "^
|
|
58
|
+
"idb": "^8.0.4"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "tsc -p tsconfig.build.json && pnpm lint:package",
|
|
@@ -13,19 +13,35 @@ 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 { AccountMismatchError, chainAuthorisesKey } from './app-delegation-source.js';
|
|
17
|
-
import { type HeldLock, stealLock, stealMintLock } from './app-lock.js';
|
|
18
16
|
import { fromBase64, toBase64 } from './base64.js';
|
|
19
|
-
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';
|
|
20
19
|
import { watchActivity, watchForeground } from './foreground-refresh.js';
|
|
21
|
-
import {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
import {
|
|
21
|
+
NotificationConsent,
|
|
22
|
+
type NotificationConsentStatus,
|
|
23
|
+
// Aliased: the method below is this library's name for the operation, and this
|
|
24
|
+
// is the request it sends.
|
|
25
|
+
requestNotificationConsent as sendConsentRequest,
|
|
26
|
+
} from './notification-consent.js';
|
|
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';
|
|
25
36
|
import { type Slots, slotsFor } from './slots.js';
|
|
26
|
-
import {
|
|
27
|
-
|
|
28
|
-
|
|
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';
|
|
29
45
|
|
|
30
46
|
/**
|
|
31
47
|
* The lock one signer interaction at a time holds.
|
|
@@ -79,7 +95,9 @@ export interface AuthClientBaseOptions {
|
|
|
79
95
|
|
|
80
96
|
/**
|
|
81
97
|
* Where the state of the sign-in is kept: which account is signed in here,
|
|
82
|
-
* 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.
|
|
83
101
|
*/
|
|
84
102
|
stateStorage?: StateStorage;
|
|
85
103
|
|
|
@@ -343,6 +361,15 @@ export class SessionNotHeldError extends Error {
|
|
|
343
361
|
* ? await authClient.getIdentity()
|
|
344
362
|
* : await authClient.signIn();
|
|
345
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
|
+
|
|
346
373
|
/**
|
|
347
374
|
* Thrown when a later sign-in or sign-out took over from this one.
|
|
348
375
|
*
|
|
@@ -401,7 +428,7 @@ export class AuthClient {
|
|
|
401
428
|
this.#options = options;
|
|
402
429
|
this.#credentialStorage = options.credentialStorage ?? new IdbCredentialStorage();
|
|
403
430
|
this.#slots = slotsFor(options.namespace);
|
|
404
|
-
this.#stateStorage = options.stateStorage ??
|
|
431
|
+
this.#stateStorage = options.stateStorage ?? defaultStateStorage();
|
|
405
432
|
|
|
406
433
|
// A string or URL is what this option used to be, and a caller still passing
|
|
407
434
|
// one would otherwise be silently ignored — both halves falling back to
|
|
@@ -559,6 +586,31 @@ export class AuthClient {
|
|
|
559
586
|
this.#init();
|
|
560
587
|
}
|
|
561
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
|
+
|
|
562
614
|
/**
|
|
563
615
|
* Returns the current identity, restoring a previous session if available.
|
|
564
616
|
*/
|
|
@@ -869,6 +921,7 @@ export class AuthClient {
|
|
|
869
921
|
}
|
|
870
922
|
|
|
871
923
|
async signIn(options?: AuthClientSignInOptions): Promise<Identity> {
|
|
924
|
+
if (this.#isReadOnly) throw readOnlyError();
|
|
872
925
|
// Counted here and locked inside, once the window is open: a ceremony is
|
|
873
926
|
// both things — the signer channel an origin has one of, and this
|
|
874
927
|
// namespace's sign-in, which `signOut` moves too — but neither lock may be
|
|
@@ -1190,6 +1243,97 @@ export class AuthClient {
|
|
|
1190
1243
|
}
|
|
1191
1244
|
}
|
|
1192
1245
|
|
|
1246
|
+
/**
|
|
1247
|
+
* Asks the user whether this application may notify them.
|
|
1248
|
+
*
|
|
1249
|
+
* Opens the identity provider the way a sign-in does, because consent is the
|
|
1250
|
+
* user's answer and nothing else can stand in for it. Where they agree, the
|
|
1251
|
+
* browser they answered on is registered for delivery.
|
|
1252
|
+
*
|
|
1253
|
+
* A refusal is an answer rather than a failure: it returns `granted: false`,
|
|
1254
|
+
* and an application renders that. Calling it again where consent is already
|
|
1255
|
+
* held returns what is recorded without asking a second time.
|
|
1256
|
+
*
|
|
1257
|
+
* @returns Whether this application may notify the user.
|
|
1258
|
+
* @throws {InteractionRequiredError} Where the client was built with
|
|
1259
|
+
* `prompt: 'none'`, which no consent ceremony can be run under.
|
|
1260
|
+
* @throws {NotificationsUnsupportedError} Where this identity provider does
|
|
1261
|
+
* not send notifications at all.
|
|
1262
|
+
* @example
|
|
1263
|
+
* const { granted } = await authClient.requestNotificationConsent();
|
|
1264
|
+
*/
|
|
1265
|
+
async requestNotificationConsent(): Promise<NotificationConsentStatus> {
|
|
1266
|
+
// The channel lock and not the sign-in lock, for the reason `requestAttributes`
|
|
1267
|
+
// takes it: this opens the signer channel, which an origin has one of, and
|
|
1268
|
+
// writes nothing anyone else reads.
|
|
1269
|
+
this.#beginInteraction();
|
|
1270
|
+
try {
|
|
1271
|
+
const response = sendConsentRequest(this.#signer, this.#options.derivationOrigin?.toString());
|
|
1272
|
+
this.#takeChannelLock();
|
|
1273
|
+
return await response;
|
|
1274
|
+
} finally {
|
|
1275
|
+
this.#endInteraction();
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
/**
|
|
1280
|
+
* Withdraws this application's consent to notify the user.
|
|
1281
|
+
*
|
|
1282
|
+
* A canister call made as the session, so it needs no ceremony and opens
|
|
1283
|
+
* nothing: the user already decided, and this records that they changed their
|
|
1284
|
+
* mind. Idempotent — consent that is already withdrawn is not an error.
|
|
1285
|
+
*
|
|
1286
|
+
* @throws {SessionGoneError} Where this origin holds no live session.
|
|
1287
|
+
*/
|
|
1288
|
+
async revokeNotificationConsent(): Promise<void> {
|
|
1289
|
+
await (await this.#consent()).revoke();
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
/**
|
|
1293
|
+
* Whether this application may notify the signed-in user.
|
|
1294
|
+
*
|
|
1295
|
+
* What a notifications control is painted from. A query, so the answer is
|
|
1296
|
+
* uncertified, and it reports the consent alone: whether a notification would
|
|
1297
|
+
* reach the user also depends on the device they hold, which this does not
|
|
1298
|
+
* speak for.
|
|
1299
|
+
*
|
|
1300
|
+
* @returns Whether this application may notify the user.
|
|
1301
|
+
* @throws {SessionGoneError} Where this origin holds no live session.
|
|
1302
|
+
* @example
|
|
1303
|
+
* const { granted } = await authClient.getNotificationConsentStatus();
|
|
1304
|
+
*/
|
|
1305
|
+
async getNotificationConsentStatus(): Promise<NotificationConsentStatus> {
|
|
1306
|
+
return (await this.#consent()).status();
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* Builds the consent calls from the session this origin holds.
|
|
1311
|
+
*
|
|
1312
|
+
* Read at the call rather than kept, so a session replaced since the last call
|
|
1313
|
+
* is not the one spoken for, and a sign-out between them is reported as what it
|
|
1314
|
+
* is instead of answered from a stale actor.
|
|
1315
|
+
*/
|
|
1316
|
+
async #consent(): Promise<NotificationConsent> {
|
|
1317
|
+
// Wait for the constructor's session restore, which is what puts the
|
|
1318
|
+
// credential these calls are made with where this can read it.
|
|
1319
|
+
await this.#init();
|
|
1320
|
+
|
|
1321
|
+
// The chain is checked and not just its presence: `#init()` resolves once, so a
|
|
1322
|
+
// session that ran out while the page stayed open is still in the slot, and a
|
|
1323
|
+
// call made with it would fail as a rejected signature rather than as what it is.
|
|
1324
|
+
const session = await this.#credentialStorage.get(this.#slots.session);
|
|
1325
|
+
if (session === null || session.chain === undefined || !isDelegationValid(session.chain)) {
|
|
1326
|
+
throw new SessionGoneError();
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
return NotificationConsent.create({
|
|
1330
|
+
sessionKey: session.identity,
|
|
1331
|
+
sessionChain: session.chain,
|
|
1332
|
+
canisterId: this.#canisterId,
|
|
1333
|
+
agentOptions: this.#options.agentOptions,
|
|
1334
|
+
});
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1193
1337
|
/**
|
|
1194
1338
|
* Runs and journals a piece of your own async work so its result stays stable
|
|
1195
1339
|
* across the `'redirect'` flow.
|
|
@@ -1243,6 +1387,7 @@ export class AuthClient {
|
|
|
1243
1387
|
* @param options.returnTo - URL to navigate to after sign-out.
|
|
1244
1388
|
*/
|
|
1245
1389
|
async signOut(options: { returnTo?: string } = {}): Promise<void> {
|
|
1390
|
+
if (this.#isReadOnly) throw readOnlyError();
|
|
1246
1391
|
// Wait for the constructor's session restore: hydration racing the
|
|
1247
1392
|
// deletion below could re-populate the identity from already-read state.
|
|
1248
1393
|
await this.#init();
|
|
@@ -1467,6 +1612,7 @@ export class AuthClient {
|
|
|
1467
1612
|
* @param principal - The account whose session ended.
|
|
1468
1613
|
*/
|
|
1469
1614
|
async #expireSession(principal: Principal): Promise<void> {
|
|
1615
|
+
if (this.#isReadOnly) return;
|
|
1470
1616
|
this.#stateStorage.set(this.#slots.state, {
|
|
1471
1617
|
principal,
|
|
1472
1618
|
expiration: BigInt(Date.now()) * 1_000_000n,
|
|
@@ -1534,6 +1680,7 @@ export class AuthClient {
|
|
|
1534
1680
|
async #restore(): Promise<void> {
|
|
1535
1681
|
this.#restoring = true;
|
|
1536
1682
|
try {
|
|
1683
|
+
await this.#stateStorage.ready?.();
|
|
1537
1684
|
await this.#hydrate();
|
|
1538
1685
|
} finally {
|
|
1539
1686
|
this.#restoring = false;
|
|
@@ -1565,6 +1712,9 @@ export class AuthClient {
|
|
|
1565
1712
|
// signed in removes nothing.
|
|
1566
1713
|
if (this.#stateStorage.get(this.#slots.state) === null) {
|
|
1567
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());
|
|
1568
1718
|
return;
|
|
1569
1719
|
}
|
|
1570
1720
|
|
|
@@ -1651,7 +1801,7 @@ export class AuthClient {
|
|
|
1651
1801
|
// the session's, so a chain this old leaves a record that reads as expired —
|
|
1652
1802
|
// which is what a page loading after a session ran out should say.
|
|
1653
1803
|
if (credential.chain === undefined || !isDelegationValid(credential.chain)) {
|
|
1654
|
-
await this.#clearCredentials();
|
|
1804
|
+
if (!this.#isReadOnly) await this.#clearCredentials();
|
|
1655
1805
|
return null;
|
|
1656
1806
|
}
|
|
1657
1807
|
return credential;
|
|
@@ -1664,6 +1814,7 @@ export class AuthClient {
|
|
|
1664
1814
|
* origin, because a sibling reading a shared record must stop seeing one.
|
|
1665
1815
|
*/
|
|
1666
1816
|
async #endSession(): Promise<void> {
|
|
1817
|
+
if (this.#isReadOnly) return;
|
|
1667
1818
|
this.#stateStorage.remove(this.#slots.state);
|
|
1668
1819
|
await this.#clearCredentials();
|
|
1669
1820
|
}
|
|
@@ -1678,10 +1829,20 @@ export class AuthClient {
|
|
|
1678
1829
|
* obtained is gone.
|
|
1679
1830
|
*/
|
|
1680
1831
|
async #dropSession(): Promise<void> {
|
|
1832
|
+
if (this.#isReadOnly) return;
|
|
1681
1833
|
this.#stateStorage.discard(this.#slots.state);
|
|
1682
1834
|
await this.#clearCredentials();
|
|
1683
1835
|
}
|
|
1684
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
|
+
|
|
1685
1846
|
// Every slot a completed sign-in writes. Not the ceremony's pending slot: a
|
|
1686
1847
|
// flow that completed has already emptied it, and one that has not may still
|
|
1687
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 {
|
package/src/client/index.ts
CHANGED
|
@@ -2,29 +2,37 @@
|
|
|
2
2
|
* @module api/client
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
export { type AppDelegationSource, SessionGoneError } from './app-delegation-source.js';
|
|
6
5
|
export * from './auth-client.js';
|
|
6
|
+
export type { Credential, CredentialStorage } from './credentials/credential-storage.js';
|
|
7
|
+
export { IdbCredentialStorage } from './credentials/idb-credential-storage.js';
|
|
8
|
+
export { LocalCredentialStorage } from './credentials/local-credential-storage.js';
|
|
9
|
+
export { MemoryCredentialStorage } from './credentials/memory-credential-storage.js';
|
|
7
10
|
export {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from './
|
|
11
|
-
export type { Credential, CredentialStorage } from './credential-storage.js';
|
|
11
|
+
DEFAULT_SHARED_MEMORY_NAME,
|
|
12
|
+
SharedMemoryCredentialStorage,
|
|
13
|
+
} from './credentials/shared-memory-credential-storage.js';
|
|
12
14
|
export { DB_VERSION, type DBCreateOptions, IdbKeyVal } from './db.js';
|
|
13
15
|
export { watchActivity, watchForeground } from './foreground-refresh.js';
|
|
14
|
-
export { IdbCredentialStorage } from './idb-credential-storage.js';
|
|
15
|
-
export { LocalCredentialStorage } from './local-credential-storage.js';
|
|
16
|
-
export { MemoryCredentialStorage } from './memory-credential-storage.js';
|
|
17
|
-
export { InteractionRequiredError, requestSessionDelegation } from './session-delegation.js';
|
|
18
|
-
export { SessionIdentity, type SessionIdentityOptions } from './session-identity.js';
|
|
19
|
-
export { SessionMinter, type SessionMinterOptions } from './session-minter.js';
|
|
20
16
|
export {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
NotificationConsent,
|
|
18
|
+
type NotificationConsentOptions,
|
|
19
|
+
type NotificationConsentService,
|
|
20
|
+
type NotificationConsentStatus,
|
|
21
|
+
NotificationsUnsupportedError,
|
|
22
|
+
requestNotificationConsent,
|
|
23
|
+
} from './notification-consent.js';
|
|
24
|
+
export { type AppDelegationSource, SessionGoneError } from './session/app-delegation-source.js';
|
|
25
|
+
export {
|
|
26
|
+
InteractionRequiredError,
|
|
27
|
+
requestSessionDelegation,
|
|
28
|
+
} from './session/session-delegation.js';
|
|
29
|
+
export { SessionIdentity, type SessionIdentityOptions } from './session/session-identity.js';
|
|
30
|
+
export { SessionMinter, type SessionMinterOptions } from './session/session-minter.js';
|
|
31
|
+
export type { SsoStatus } from './sso.js';
|
|
25
32
|
export {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
} from './state-storage.js';
|
|
33
|
+
CookieStateStorage,
|
|
34
|
+
type CookieStateStorageOptions,
|
|
35
|
+
} from './state/cookie-state-storage.js';
|
|
36
|
+
export { LocalStateStorage } from './state/local-state-storage.js';
|
|
37
|
+
export { MemoryStateStorage } from './state/memory-state-storage.js';
|
|
38
|
+
export type { SessionState, StateStorage } from './state/state-storage.js';
|