@oxyhq/core 20.1.0 → 21.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.
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/boot/sessionColdBoot.js +107 -8
- package/dist/cjs/i18n/locales/en-US.json +19 -2
- package/dist/cjs/i18n/locales/es-ES.json +19 -2
- package/dist/cjs/i18n/locales/locales/en-US.json +19 -2
- package/dist/cjs/i18n/locales/locales/es-ES.json +19 -2
- package/dist/cjs/index.js +50 -16
- package/dist/cjs/mixins/OxyServices.auth.js +27 -3
- package/dist/cjs/session/SessionClient.js +361 -1
- package/dist/cjs/session/accountDialogController.js +121 -147
- package/dist/cjs/session/accountSwitchTargets.js +75 -0
- package/dist/cjs/session/deviceDirectory.js +143 -0
- package/dist/cjs/session/deviceSwitcherRows.js +76 -0
- package/dist/cjs/session/projectSessionState.js +8 -1
- package/dist/cjs/session/sharedDeviceCredential.js +247 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/boot/sessionColdBoot.js +107 -8
- package/dist/esm/i18n/locales/en-US.json +19 -2
- package/dist/esm/i18n/locales/es-ES.json +19 -2
- package/dist/esm/i18n/locales/locales/en-US.json +19 -2
- package/dist/esm/i18n/locales/locales/es-ES.json +19 -2
- package/dist/esm/index.js +32 -10
- package/dist/esm/mixins/OxyServices.auth.js +27 -3
- package/dist/esm/session/SessionClient.js +362 -2
- package/dist/esm/session/accountDialogController.js +121 -147
- package/dist/esm/session/accountSwitchTargets.js +71 -0
- package/dist/esm/session/deviceDirectory.js +135 -0
- package/dist/esm/session/deviceSwitcherRows.js +72 -0
- package/dist/esm/session/projectSessionState.js +8 -2
- package/dist/esm/session/sharedDeviceCredential.js +239 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/boot/sessionColdBoot.d.ts +24 -4
- package/dist/types/index.d.ts +8 -3
- package/dist/types/mixins/OxyServices.auth.d.ts +75 -3
- package/dist/types/models/session.d.ts +11 -0
- package/dist/types/session/SessionClient.d.ts +202 -1
- package/dist/types/session/accountDialogController.d.ts +76 -64
- package/dist/types/session/accountSwitchTargets.d.ts +64 -0
- package/dist/types/session/deviceDirectory.d.ts +182 -0
- package/dist/types/session/deviceSwitcherRows.d.ts +92 -0
- package/dist/types/session/projectSessionState.d.ts +29 -0
- package/dist/types/session/sharedDeviceCredential.d.ts +202 -0
- package/package.json +3 -3
- package/src/boot/__tests__/sessionColdBoot.sharedDevice.test.ts +325 -0
- package/src/boot/sessionColdBoot.ts +133 -9
- package/src/i18n/locales/en-US.json +19 -2
- package/src/i18n/locales/es-ES.json +19 -2
- package/src/index.ts +75 -18
- package/src/mixins/OxyServices.auth.ts +67 -5
- package/src/mixins/__tests__/preSessionSkipAuth.test.ts +54 -1
- package/src/models/session.ts +11 -0
- package/src/session/SessionClient.ts +386 -1
- package/src/session/__tests__/SessionClient.directory.test.ts +688 -0
- package/src/session/__tests__/accountDialogController.test.ts +411 -278
- package/src/session/__tests__/accountSwitchTargets.test.ts +132 -0
- package/src/session/__tests__/deviceDirectory.test.ts +422 -0
- package/src/session/__tests__/deviceSwitcherRows.test.ts +223 -0
- package/src/session/__tests__/projectSessionState.test.ts +17 -0
- package/src/session/__tests__/sharedDeviceCredential.test.ts +300 -0
- package/src/session/accountDialogController.ts +141 -179
- package/src/session/accountSwitchTargets.ts +87 -0
- package/src/session/deviceDirectory.ts +269 -0
- package/src/session/deviceSwitcherRows.ts +145 -0
- package/src/session/projectSessionState.ts +9 -3
- package/src/session/sharedDeviceCredential.ts +349 -0
- package/dist/cjs/session/accountProjection.js +0 -213
- package/dist/esm/session/accountProjection.js +0 -207
- package/dist/types/session/accountProjection.d.ts +0 -198
- package/src/session/__tests__/accountProjection.test.ts +0 -447
- package/src/session/accountProjection.ts +0 -354
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deviceSwitcherRows` — the switcher's render model.
|
|
3
|
+
*
|
|
4
|
+
* Pure, so it is tested without a render. The cases that matter are the ones
|
|
5
|
+
* where a plausible shortcut and the correct answer disagree: naming a row by
|
|
6
|
+
* recomposing a name the server deliberately did not compose, marking a row
|
|
7
|
+
* active by comparing account ids on a device where two people reach one
|
|
8
|
+
* account, and deciding switchability from anything other than `available`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { DeviceDirectory } from '@oxyhq/contracts';
|
|
12
|
+
import { projectDevicePrincipals } from '../deviceDirectory';
|
|
13
|
+
import { buildSwitcherRows, showsPrincipalHeaders } from '../deviceSwitcherRows';
|
|
14
|
+
|
|
15
|
+
const avatarUrl = (avatar: string | null | undefined): string | undefined =>
|
|
16
|
+
avatar ? `https://cdn/${avatar}` : undefined;
|
|
17
|
+
|
|
18
|
+
/** Nate and Alice, both able to act as `org`. Nate is enumerated first. */
|
|
19
|
+
function sharedDirectory(activeContextId: string | null): DeviceDirectory {
|
|
20
|
+
return {
|
|
21
|
+
deviceId: 'device-1',
|
|
22
|
+
revision: 4,
|
|
23
|
+
activeContextId,
|
|
24
|
+
updatedAt: 1_720_000_000_000,
|
|
25
|
+
principals: [
|
|
26
|
+
{
|
|
27
|
+
id: 'p-nate',
|
|
28
|
+
userId: 'nate',
|
|
29
|
+
authuser: 0,
|
|
30
|
+
user: {
|
|
31
|
+
id: 'nate',
|
|
32
|
+
username: 'nate',
|
|
33
|
+
name: { displayName: 'Nate I.' },
|
|
34
|
+
avatar: 'av-nate',
|
|
35
|
+
color: 'purple',
|
|
36
|
+
},
|
|
37
|
+
contexts: [
|
|
38
|
+
{
|
|
39
|
+
id: 'ctx-nate',
|
|
40
|
+
accountId: 'nate',
|
|
41
|
+
kind: 'personal',
|
|
42
|
+
relationship: 'self',
|
|
43
|
+
account: {
|
|
44
|
+
id: 'nate',
|
|
45
|
+
username: 'nate',
|
|
46
|
+
name: { displayName: 'Nate I.' },
|
|
47
|
+
avatar: 'av-nate',
|
|
48
|
+
color: 'purple',
|
|
49
|
+
},
|
|
50
|
+
onDevice: true,
|
|
51
|
+
available: true,
|
|
52
|
+
active: activeContextId === 'ctx-nate',
|
|
53
|
+
lastUsedAt: null,
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: 'ctx-nate-org',
|
|
57
|
+
accountId: 'org',
|
|
58
|
+
kind: 'organization',
|
|
59
|
+
relationship: 'owner',
|
|
60
|
+
account: { id: 'org', username: 'oxy', avatar: null, color: 'amber' },
|
|
61
|
+
onDevice: true,
|
|
62
|
+
available: true,
|
|
63
|
+
active: activeContextId === 'ctx-nate-org',
|
|
64
|
+
lastUsedAt: null,
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
id: 'p-alice',
|
|
70
|
+
userId: 'alice',
|
|
71
|
+
authuser: 1,
|
|
72
|
+
user: { id: 'alice', username: 'alice' },
|
|
73
|
+
contexts: [
|
|
74
|
+
{
|
|
75
|
+
id: 'ctx-alice-org',
|
|
76
|
+
accountId: 'org',
|
|
77
|
+
kind: 'organization',
|
|
78
|
+
relationship: 'member',
|
|
79
|
+
account: { id: 'org', username: 'oxy', avatar: null, color: 'amber' },
|
|
80
|
+
// A live delegated session under a principal whose own session died:
|
|
81
|
+
// on the device, and still not activatable.
|
|
82
|
+
onDevice: true,
|
|
83
|
+
available: false,
|
|
84
|
+
active: activeContextId === 'ctx-alice-org',
|
|
85
|
+
lastUsedAt: null,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const rowsFor = (directory: DeviceDirectory, activeContextId: string | null) =>
|
|
94
|
+
buildSwitcherRows(projectDevicePrincipals(directory), activeContextId, avatarUrl, 'en-US');
|
|
95
|
+
|
|
96
|
+
describe('buildSwitcherRows', () => {
|
|
97
|
+
it('renders the same account under both people, as two rows with two ids', () => {
|
|
98
|
+
const rows = rowsFor(sharedDirectory('ctx-nate'), 'ctx-nate');
|
|
99
|
+
|
|
100
|
+
const orgRows = rows.flatMap((row) => row.contexts.filter((c) => c.accountId === 'org'));
|
|
101
|
+
expect(orgRows.map((c) => c.contextId)).toEqual(['ctx-nate-org', 'ctx-alice-org']);
|
|
102
|
+
expect(rows.map((row) => row.principalId)).toEqual(['p-nate', 'p-alice']);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('marks active by CONTEXT id, so one of two routes to an account lights up', () => {
|
|
106
|
+
const rows = rowsFor(sharedDirectory('ctx-nate-org'), 'ctx-nate-org');
|
|
107
|
+
|
|
108
|
+
const orgRows = rows.flatMap((row) => row.contexts.filter((c) => c.accountId === 'org'));
|
|
109
|
+
// Comparing on `accountId` would light BOTH of these, i.e. claim the device
|
|
110
|
+
// is signed in as one account through two different people at once.
|
|
111
|
+
expect(orgRows.map((c) => c.isActive)).toEqual([true, false]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('takes switchability from `available` alone', () => {
|
|
115
|
+
const rows = rowsFor(sharedDirectory('ctx-nate'), 'ctx-nate');
|
|
116
|
+
|
|
117
|
+
const alice = rows[1].contexts[0];
|
|
118
|
+
expect(alice.canActivate).toBe(false);
|
|
119
|
+
// `available || onDevice` would offer this row: the delegated session is
|
|
120
|
+
// alive, so `onDevice` is true, and the server still answers 403 because the
|
|
121
|
+
// principal's own session is gone.
|
|
122
|
+
const nateOrg = rows[0].contexts[1];
|
|
123
|
+
expect(nateOrg.canActivate).toBe(true);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('names a row by displayName, then handle — never a recomposed name', () => {
|
|
127
|
+
const rows = rowsFor(sharedDirectory('ctx-nate'), 'ctx-nate');
|
|
128
|
+
|
|
129
|
+
expect(rows[0].displayName).toBe('Nate I.');
|
|
130
|
+
// `org` has no `displayName`: the handle, not a synthesized one.
|
|
131
|
+
expect(rows[0].contexts[1].displayName).toBe('oxy');
|
|
132
|
+
expect(rows[0].contexts[1].handle).toBe('oxy');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('resolves avatar file ids through the caller-supplied resolver', () => {
|
|
136
|
+
const rows = rowsFor(sharedDirectory('ctx-nate'), 'ctx-nate');
|
|
137
|
+
|
|
138
|
+
expect(rows[0].avatarUrl).toBe('https://cdn/av-nate');
|
|
139
|
+
// A null avatar resolves to nothing, never to a URL with an empty id in it.
|
|
140
|
+
expect(rows[0].contexts[1].avatarUrl).toBeUndefined();
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Issue #961. The row model dropped the accent when the switcher moved onto
|
|
145
|
+
* the directory, so every non-active row fell back to the ambient theme accent
|
|
146
|
+
* and a device holding two people drew them identically.
|
|
147
|
+
*/
|
|
148
|
+
it('carries each account’s own accent, per row', () => {
|
|
149
|
+
const rows = rowsFor(sharedDirectory('ctx-nate'), 'ctx-nate');
|
|
150
|
+
|
|
151
|
+
expect(rows[0].color).toBe('purple');
|
|
152
|
+
expect(rows[0].contexts[0].color).toBe('purple');
|
|
153
|
+
// The SUBJECT's colour, not the person's: this row is Nate acting as the
|
|
154
|
+
// organization, and it is the organization being drawn.
|
|
155
|
+
expect(rows[0].contexts[1].color).toBe('amber');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('answers null for an account with no colour, so the renderer uses the theme accent', () => {
|
|
159
|
+
const rows = rowsFor(sharedDirectory('ctx-nate'), 'ctx-nate');
|
|
160
|
+
|
|
161
|
+
// Alice's profile carries no `color` at all — absent must reach the renderer
|
|
162
|
+
// as null, never as `undefined` it has to re-guess the meaning of.
|
|
163
|
+
expect(rows[1].color).toBeNull();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('flags delegation by comparing the pair, not by reading the relationship word', () => {
|
|
167
|
+
const rows = rowsFor(sharedDirectory('ctx-nate'), 'ctx-nate');
|
|
168
|
+
|
|
169
|
+
expect(rows[0].contexts[0].isDelegated).toBe(false);
|
|
170
|
+
expect(rows[0].contexts[1].isDelegated).toBe(true);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('marks the person the active context belongs to, and only them', () => {
|
|
174
|
+
expect(rowsFor(sharedDirectory('ctx-alice-org'), 'ctx-alice-org').map((r) => r.isActive)).toEqual([
|
|
175
|
+
false,
|
|
176
|
+
true,
|
|
177
|
+
]);
|
|
178
|
+
expect(rowsFor(sharedDirectory('ctx-nate-org'), 'ctx-nate-org').map((r) => r.isActive)).toEqual([
|
|
179
|
+
true,
|
|
180
|
+
false,
|
|
181
|
+
]);
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe('showsPrincipalHeaders', () => {
|
|
186
|
+
const person = (id: string, contextIds: string[]) => ({
|
|
187
|
+
principalId: id,
|
|
188
|
+
displayName: id,
|
|
189
|
+
handle: id,
|
|
190
|
+
avatarUrl: undefined,
|
|
191
|
+
color: null,
|
|
192
|
+
isActive: false,
|
|
193
|
+
contexts: contextIds.map((contextId) => ({
|
|
194
|
+
contextId,
|
|
195
|
+
accountId: contextId,
|
|
196
|
+
displayName: contextId,
|
|
197
|
+
handle: contextId,
|
|
198
|
+
avatarUrl: undefined,
|
|
199
|
+
color: null,
|
|
200
|
+
isActive: false,
|
|
201
|
+
isDelegated: false,
|
|
202
|
+
canActivate: true,
|
|
203
|
+
})),
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('stays off while every person holds exactly one account', () => {
|
|
207
|
+
// Both of these are the flat list again: every row already IS a person, so a
|
|
208
|
+
// header would print each name twice.
|
|
209
|
+
expect(showsPrincipalHeaders([person('a', ['ctx-a'])])).toBe(false);
|
|
210
|
+
expect(showsPrincipalHeaders([person('a', ['ctx-a']), person('b', ['ctx-b'])])).toBe(false);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('turns on as soon as one person holds two', () => {
|
|
214
|
+
expect(showsPrincipalHeaders([person('a', ['ctx-a', 'ctx-a-org'])])).toBe(true);
|
|
215
|
+
expect(
|
|
216
|
+
showsPrincipalHeaders([person('a', ['ctx-a', 'ctx-a-org']), person('b', ['ctx-b'])]),
|
|
217
|
+
).toBe(true);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('is off for an empty device', () => {
|
|
221
|
+
expect(showsPrincipalHeaders([])).toBe(false);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
@@ -86,3 +86,20 @@ describe('projections — pinned', () => {
|
|
|
86
86
|
expect(accountIdsOf(STATE)).toEqual([PINNED, OTHER]);
|
|
87
87
|
});
|
|
88
88
|
});
|
|
89
|
+
|
|
90
|
+
describe('the operator behind a delegated session', () => {
|
|
91
|
+
it('carries operatedByUserId through instead of dropping it', () => {
|
|
92
|
+
const operated: DeviceSessionState = {
|
|
93
|
+
...STATE,
|
|
94
|
+
accounts: [
|
|
95
|
+
{ accountId: PINNED, sessionId: 'sess-vault', authuser: 0 },
|
|
96
|
+
{ accountId: OTHER, sessionId: 'sess-other', authuser: 1, operatedByUserId: PINNED },
|
|
97
|
+
],
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const sessions = deviceStateToClientSessions(operated, USERS);
|
|
101
|
+
|
|
102
|
+
// The delegated row names the human behind it; the direct one has nobody.
|
|
103
|
+
expect(sessions.map((session) => session.operatedByUserId)).toEqual([undefined, PINNED]);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared native DeviceSession credential — the decision rules that separate
|
|
3
|
+
* "this device already has a session" from "this device could not be read".
|
|
4
|
+
*
|
|
5
|
+
* Every case here is written against the one failure this module exists to make
|
|
6
|
+
* unreachable: a secure store that is LOCKED or BROKEN looking like a secure
|
|
7
|
+
* store that is EMPTY. The empty answer authorises a write into the slot; the
|
|
8
|
+
* failed answer must authorise nothing. Several assertions below are the only
|
|
9
|
+
* thing standing between that misreading and a live shared session being
|
|
10
|
+
* overwritten, so they are written to fail loudly rather than shrink quietly.
|
|
11
|
+
*/
|
|
12
|
+
import { createMemoryAuthStateStore, type AuthStateStore, type PersistedAuthState } from '../authStateStore';
|
|
13
|
+
import {
|
|
14
|
+
createSharedMirroringAuthStateStore,
|
|
15
|
+
decideSharedDeviceJoin,
|
|
16
|
+
decideSharedDevicePublish,
|
|
17
|
+
normalizeSharedDeviceSessionRead,
|
|
18
|
+
publishProvenDeviceCredential,
|
|
19
|
+
readLocalDeviceCredential,
|
|
20
|
+
type SharedDeviceCredential,
|
|
21
|
+
type SharedDeviceCredentialRead,
|
|
22
|
+
type SharedDeviceCredentialStore,
|
|
23
|
+
} from '../sharedDeviceCredential';
|
|
24
|
+
|
|
25
|
+
const CRED: SharedDeviceCredential = { deviceId: 'dev-shared', deviceSecret: 'ds-shared' };
|
|
26
|
+
const OTHER: SharedDeviceCredential = { deviceId: 'dev-other', deviceSecret: 'ds-other' };
|
|
27
|
+
|
|
28
|
+
const PRESENT: SharedDeviceCredentialRead = { state: 'present', credential: CRED };
|
|
29
|
+
const ABSENT: SharedDeviceCredentialRead = { state: 'absent' };
|
|
30
|
+
const UNAVAILABLE: SharedDeviceCredentialRead = { state: 'unavailable', cause: new Error('keychain locked') };
|
|
31
|
+
const UNSUPPORTED: SharedDeviceCredentialRead = { state: 'unsupported' };
|
|
32
|
+
|
|
33
|
+
/** Every read state, so a new one cannot be added without deciding what it means. */
|
|
34
|
+
const ALL_READS: SharedDeviceCredentialRead[] = [PRESENT, ABSENT, UNAVAILABLE, UNSUPPORTED];
|
|
35
|
+
|
|
36
|
+
function localWith(credential: SharedDeviceCredential | null): PersistedAuthState {
|
|
37
|
+
return {
|
|
38
|
+
sessionId: 'sess-local',
|
|
39
|
+
userId: 'user-local',
|
|
40
|
+
...(credential ? { deviceId: credential.deviceId, deviceSecret: credential.deviceSecret } : {}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** An in-memory {@link SharedDeviceCredentialStore} with scripted read behaviour. */
|
|
45
|
+
function makeSharedStore(initial: SharedDeviceCredentialRead) {
|
|
46
|
+
let current = initial;
|
|
47
|
+
const publish = jest.fn(async (credential: SharedDeviceCredential) => {
|
|
48
|
+
current = { state: 'present', credential };
|
|
49
|
+
return true;
|
|
50
|
+
});
|
|
51
|
+
const clear = jest.fn(async () => {
|
|
52
|
+
current = { state: 'absent' };
|
|
53
|
+
});
|
|
54
|
+
const store: SharedDeviceCredentialStore = {
|
|
55
|
+
read: async () => current,
|
|
56
|
+
publish,
|
|
57
|
+
clear,
|
|
58
|
+
};
|
|
59
|
+
return { store, publish, clear, peek: () => current };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe('readLocalDeviceCredential', () => {
|
|
63
|
+
test('requires BOTH halves of the mint credential', () => {
|
|
64
|
+
expect(readLocalDeviceCredential(null)).toBeNull();
|
|
65
|
+
expect(readLocalDeviceCredential({ sessionId: 's', userId: 'u' })).toBeNull();
|
|
66
|
+
expect(readLocalDeviceCredential({ sessionId: 's', userId: 'u', deviceId: 'd' })).toBeNull();
|
|
67
|
+
expect(readLocalDeviceCredential({ sessionId: 's', userId: 'u', deviceSecret: 'x' })).toBeNull();
|
|
68
|
+
expect(readLocalDeviceCredential(localWith(CRED))).toEqual(CRED);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('normalizeSharedDeviceSessionRead', () => {
|
|
73
|
+
test('narrows a well-formed present payload', () => {
|
|
74
|
+
expect(
|
|
75
|
+
normalizeSharedDeviceSessionRead({ status: 'present', deviceId: 'dev-x', deviceSecret: 'ds-x' }),
|
|
76
|
+
).toEqual({ state: 'present', credential: { deviceId: 'dev-x', deviceSecret: 'ds-x' } });
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('narrows an explicit absent payload', () => {
|
|
80
|
+
expect(normalizeSharedDeviceSessionRead({ status: 'absent' })).toEqual({ state: 'absent' });
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test.each([
|
|
84
|
+
['null', null],
|
|
85
|
+
['undefined', undefined],
|
|
86
|
+
['a string', 'absent'],
|
|
87
|
+
['a number', 0],
|
|
88
|
+
['an empty object', {}],
|
|
89
|
+
['an unknown status', { status: 'maybe' }],
|
|
90
|
+
['present with no deviceId', { status: 'present', deviceSecret: 'ds-x' }],
|
|
91
|
+
['present with no deviceSecret', { status: 'present', deviceId: 'dev-x' }],
|
|
92
|
+
['present with an empty deviceSecret', { status: 'present', deviceId: 'dev-x', deviceSecret: '' }],
|
|
93
|
+
['present with a non-string deviceId', { status: 'present', deviceId: 7, deviceSecret: 'ds-x' }],
|
|
94
|
+
['an explicit unavailable', { status: 'unavailable', reason: 'keystore' }],
|
|
95
|
+
])('reports %s as unavailable, never absent', (_label, raw) => {
|
|
96
|
+
const read = normalizeSharedDeviceSessionRead(raw);
|
|
97
|
+
// `unavailable` and `absent` are the pair that must never be confused: the
|
|
98
|
+
// second one authorises a write into the slot.
|
|
99
|
+
expect(read.state).toBe('unavailable');
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('decideSharedDeviceJoin', () => {
|
|
104
|
+
test('adopts when the slot holds a credential and this app has none', () => {
|
|
105
|
+
expect(decideSharedDeviceJoin(localWith(null), PRESENT)).toEqual({ action: 'adopt', credential: CRED });
|
|
106
|
+
expect(decideSharedDeviceJoin(null, PRESENT)).toEqual({ action: 'adopt', credential: CRED });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('NEVER adopts when the slot could not be read', () => {
|
|
110
|
+
// The keystore-unavailable case. Adopting nothing is right; the danger is a
|
|
111
|
+
// caller downstream reading this as "fresh device".
|
|
112
|
+
expect(decideSharedDeviceJoin(localWith(null), UNAVAILABLE)).toEqual({
|
|
113
|
+
action: 'skip',
|
|
114
|
+
reason: 'shared-unreadable',
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('reports an unsupported slot distinctly from an unreadable one', () => {
|
|
119
|
+
// Same behaviour, different diagnosis: no shared transport in this build is
|
|
120
|
+
// normal, an unreadable one is not.
|
|
121
|
+
expect(decideSharedDeviceJoin(localWith(null), UNSUPPORTED)).toEqual({
|
|
122
|
+
action: 'skip',
|
|
123
|
+
reason: 'shared-unsupported',
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test('skips an empty slot', () => {
|
|
128
|
+
expect(decideSharedDeviceJoin(localWith(null), ABSENT)).toEqual({ action: 'skip', reason: 'shared-empty' });
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('never moves an app that already holds its own credential — in ANY slot state', () => {
|
|
132
|
+
// "No user is signed out merely because one app updates first" holds by
|
|
133
|
+
// construction: whatever the slot says, an app with a working credential of
|
|
134
|
+
// its own is left exactly where it is. Including when the slot holds a
|
|
135
|
+
// DIFFERENT device session.
|
|
136
|
+
for (const read of ALL_READS) {
|
|
137
|
+
expect(decideSharedDeviceJoin(localWith(OTHER), read)).toEqual({
|
|
138
|
+
action: 'skip',
|
|
139
|
+
reason: 'local-credential-present',
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
expect(ALL_READS).toHaveLength(4);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('decideSharedDevicePublish', () => {
|
|
147
|
+
test('seeds an empty slot with a proven credential', () => {
|
|
148
|
+
expect(decideSharedDevicePublish(CRED, ABSENT)).toEqual({ action: 'publish' });
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('NEVER writes over a slot it could not read', () => {
|
|
152
|
+
// The destructive direction. An unreadable slot may hold another principal's
|
|
153
|
+
// live session; writing on the strength of a failed read is how it is lost.
|
|
154
|
+
expect(decideSharedDevicePublish(CRED, UNAVAILABLE)).toEqual({
|
|
155
|
+
action: 'skip',
|
|
156
|
+
reason: 'shared-unreadable',
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('does not write when there is no slot at all', () => {
|
|
161
|
+
expect(decideSharedDevicePublish(CRED, UNSUPPORTED)).toEqual({
|
|
162
|
+
action: 'skip',
|
|
163
|
+
reason: 'shared-unsupported',
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('refreshes the secret for the SAME device session', () => {
|
|
168
|
+
const rotated = { deviceId: CRED.deviceId, deviceSecret: 'ds-rotated' };
|
|
169
|
+
expect(decideSharedDevicePublish(rotated, PRESENT)).toEqual({ action: 'publish' });
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test('is a no-op when the slot already holds exactly this credential', () => {
|
|
173
|
+
expect(decideSharedDevicePublish(CRED, PRESENT)).toEqual({ action: 'skip', reason: 'already-current' });
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('leaves a slot owned by a DIFFERENT device session alone', () => {
|
|
177
|
+
// Overwriting would silently migrate every other app on this device onto our
|
|
178
|
+
// session at their next cold boot. That is a user-visible change of identity,
|
|
179
|
+
// not something a background persist decides.
|
|
180
|
+
expect(decideSharedDevicePublish(OTHER, PRESENT)).toEqual({
|
|
181
|
+
action: 'skip',
|
|
182
|
+
reason: 'owned-by-another-device',
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe('publishProvenDeviceCredential', () => {
|
|
188
|
+
test('writes into an empty slot', async () => {
|
|
189
|
+
const shared = makeSharedStore(ABSENT);
|
|
190
|
+
await expect(publishProvenDeviceCredential({ shared: shared.store, credential: CRED })).resolves.toEqual({
|
|
191
|
+
status: 'published',
|
|
192
|
+
});
|
|
193
|
+
expect(shared.publish).toHaveBeenCalledWith(CRED);
|
|
194
|
+
expect(shared.peek()).toEqual(PRESENT);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('does not call publish at all when the slot is unreadable', async () => {
|
|
198
|
+
const shared = makeSharedStore(UNAVAILABLE);
|
|
199
|
+
await expect(publishProvenDeviceCredential({ shared: shared.store, credential: CRED })).resolves.toEqual({
|
|
200
|
+
status: 'skipped',
|
|
201
|
+
reason: 'shared-unreadable',
|
|
202
|
+
});
|
|
203
|
+
expect(shared.publish).not.toHaveBeenCalled();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('reports an unverified write as a failure rather than a success', async () => {
|
|
207
|
+
const shared: SharedDeviceCredentialStore = {
|
|
208
|
+
read: async () => ABSENT,
|
|
209
|
+
// The contract is read-back-verified; a store that could not confirm the
|
|
210
|
+
// bytes landed must say so, or a fresh install joins with a phantom.
|
|
211
|
+
publish: async () => false,
|
|
212
|
+
clear: async () => undefined,
|
|
213
|
+
};
|
|
214
|
+
await expect(publishProvenDeviceCredential({ shared, credential: CRED })).resolves.toEqual({
|
|
215
|
+
status: 'publish-failed',
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
describe('createSharedMirroringAuthStateStore', () => {
|
|
221
|
+
function harness(initial: SharedDeviceCredentialRead = ABSENT) {
|
|
222
|
+
const local = createMemoryAuthStateStore();
|
|
223
|
+
const shared = makeSharedStore(initial);
|
|
224
|
+
return { local, shared, store: createSharedMirroringAuthStateStore({ local, shared: shared.store }) };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
test('mirrors a saved credential into the shared slot', async () => {
|
|
228
|
+
const h = harness();
|
|
229
|
+
await expect(h.store.save(localWith(CRED))).resolves.toBe(true);
|
|
230
|
+
expect(h.shared.publish).toHaveBeenCalledWith(CRED);
|
|
231
|
+
expect(await h.local.load()).toEqual(localWith(CRED));
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test('does not re-write the slot when the credential has not changed', async () => {
|
|
235
|
+
const h = harness();
|
|
236
|
+
await h.store.save(localWith(CRED));
|
|
237
|
+
await h.store.save({ ...localWith(CRED), accessToken: 'warm', expiresAt: 'later' });
|
|
238
|
+
await h.store.save({ ...localWith(CRED), accessToken: 'warmer', expiresAt: 'later-still' });
|
|
239
|
+
expect(h.shared.publish).toHaveBeenCalledTimes(1);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test('mirrors again once the credential really changes', async () => {
|
|
243
|
+
const h = harness();
|
|
244
|
+
await h.store.save(localWith(CRED));
|
|
245
|
+
await h.store.save(localWith({ deviceId: CRED.deviceId, deviceSecret: 'ds-rotated' }));
|
|
246
|
+
expect(h.shared.publish).toHaveBeenCalledTimes(2);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('saves nothing to the slot when the state carries no credential', async () => {
|
|
250
|
+
const h = harness();
|
|
251
|
+
await h.store.save({ sessionId: 's', userId: 'u' });
|
|
252
|
+
expect(h.shared.publish).not.toHaveBeenCalled();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test('reports the LOCAL durability verdict, not the mirror outcome', async () => {
|
|
256
|
+
// The caller's contract is about the durable local write — a lane persisting
|
|
257
|
+
// a rotated secret treats `false` as fatal. A best-effort mirror must never
|
|
258
|
+
// be able to flip that answer in either direction.
|
|
259
|
+
const failingLocal: AuthStateStore = {
|
|
260
|
+
load: async () => null,
|
|
261
|
+
save: async () => false,
|
|
262
|
+
clear: async () => undefined,
|
|
263
|
+
};
|
|
264
|
+
const shared = makeSharedStore(ABSENT);
|
|
265
|
+
const store = createSharedMirroringAuthStateStore({ local: failingLocal, shared: shared.store });
|
|
266
|
+
await expect(store.save(localWith(CRED))).resolves.toBe(false);
|
|
267
|
+
expect(shared.publish).toHaveBeenCalledWith(CRED);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test('a throwing shared store cannot fail a successful local write', async () => {
|
|
271
|
+
const shared: SharedDeviceCredentialStore = {
|
|
272
|
+
read: async () => {
|
|
273
|
+
throw new Error('binder died');
|
|
274
|
+
},
|
|
275
|
+
publish: async () => true,
|
|
276
|
+
clear: async () => undefined,
|
|
277
|
+
};
|
|
278
|
+
const store = createSharedMirroringAuthStateStore({ local: createMemoryAuthStateStore(), shared });
|
|
279
|
+
await expect(store.save(localWith(CRED))).resolves.toBe(true);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test('signing out of THIS app does not clear the device-wide slot', async () => {
|
|
283
|
+
// Other apps may still be signed in on that credential, and once the server
|
|
284
|
+
// session is gone the credential mints `no_active_session` for everyone
|
|
285
|
+
// anyway. This app's sign-out is not authority over the join point.
|
|
286
|
+
const h = harness();
|
|
287
|
+
await h.store.save(localWith(CRED));
|
|
288
|
+
await h.store.clear();
|
|
289
|
+
expect(h.shared.clear).not.toHaveBeenCalled();
|
|
290
|
+
expect(h.shared.peek()).toEqual(PRESENT);
|
|
291
|
+
expect(await h.local.load()).toBeNull();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test('load is a pass-through and never adopts', async () => {
|
|
295
|
+
// Adoption is an explicit cold-boot step. A storage primitive must not be
|
|
296
|
+
// able to change who the app is signed in as.
|
|
297
|
+
const h = harness(PRESENT);
|
|
298
|
+
expect(await h.store.load()).toBeNull();
|
|
299
|
+
});
|
|
300
|
+
});
|