@oxyhq/core 12.10.5 → 12.11.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 +142 -37
- package/dist/cjs/i18n/locales/ar-SA.json +13 -1
- package/dist/cjs/i18n/locales/ca-ES.json +13 -1
- package/dist/cjs/i18n/locales/de-DE.json +13 -1
- package/dist/cjs/i18n/locales/en-US.json +13 -4
- package/dist/cjs/i18n/locales/es-ES.json +13 -4
- package/dist/cjs/i18n/locales/fr-FR.json +13 -1
- package/dist/cjs/i18n/locales/it-IT.json +13 -1
- package/dist/cjs/i18n/locales/ja-JP.json +13 -1
- package/dist/cjs/i18n/locales/ko-KR.json +13 -1
- package/dist/cjs/i18n/locales/locales/ar-SA.json +13 -1
- package/dist/cjs/i18n/locales/locales/ca-ES.json +13 -1
- package/dist/cjs/i18n/locales/locales/de-DE.json +13 -1
- package/dist/cjs/i18n/locales/locales/en-US.json +13 -4
- package/dist/cjs/i18n/locales/locales/es-ES.json +13 -4
- package/dist/cjs/i18n/locales/locales/fr-FR.json +13 -1
- package/dist/cjs/i18n/locales/locales/it-IT.json +13 -1
- package/dist/cjs/i18n/locales/locales/ja-JP.json +13 -1
- package/dist/cjs/i18n/locales/locales/ko-KR.json +13 -1
- package/dist/cjs/i18n/locales/locales/pt-PT.json +13 -1
- package/dist/cjs/i18n/locales/locales/zh-CN.json +13 -1
- package/dist/cjs/i18n/locales/pt-PT.json +13 -1
- package/dist/cjs/i18n/locales/zh-CN.json +13 -1
- package/dist/cjs/index.js +31 -5
- package/dist/cjs/mixins/OxyServices.auth.js +257 -17
- package/dist/cjs/mixins/OxyServices.deviceBoot.js +54 -3
- package/dist/cjs/mixins/OxyServices.notifications.js +63 -0
- package/dist/cjs/mixins/index.js +5 -0
- package/dist/cjs/session/SessionClient.js +40 -4
- package/dist/cjs/session/accountDialogController.js +288 -41
- package/dist/cjs/session/hubSync.js +1 -1
- package/dist/cjs/session/identityPin.js +256 -0
- package/dist/cjs/session/identitySession.js +135 -0
- package/dist/cjs/session/projectSessionState.js +54 -15
- package/dist/cjs/session/refresh.js +73 -8
- package/dist/cjs/utils/commonsDelivery.js +59 -0
- package/dist/cjs/utils/oauthPkce.js +33 -3
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/boot/sessionColdBoot.js +142 -37
- package/dist/esm/i18n/locales/ar-SA.json +13 -1
- package/dist/esm/i18n/locales/ca-ES.json +13 -1
- package/dist/esm/i18n/locales/de-DE.json +13 -1
- package/dist/esm/i18n/locales/en-US.json +13 -4
- package/dist/esm/i18n/locales/es-ES.json +13 -4
- package/dist/esm/i18n/locales/fr-FR.json +13 -1
- package/dist/esm/i18n/locales/it-IT.json +13 -1
- package/dist/esm/i18n/locales/ja-JP.json +13 -1
- package/dist/esm/i18n/locales/ko-KR.json +13 -1
- package/dist/esm/i18n/locales/locales/ar-SA.json +13 -1
- package/dist/esm/i18n/locales/locales/ca-ES.json +13 -1
- package/dist/esm/i18n/locales/locales/de-DE.json +13 -1
- package/dist/esm/i18n/locales/locales/en-US.json +13 -4
- package/dist/esm/i18n/locales/locales/es-ES.json +13 -4
- package/dist/esm/i18n/locales/locales/fr-FR.json +13 -1
- package/dist/esm/i18n/locales/locales/it-IT.json +13 -1
- package/dist/esm/i18n/locales/locales/ja-JP.json +13 -1
- package/dist/esm/i18n/locales/locales/ko-KR.json +13 -1
- package/dist/esm/i18n/locales/locales/pt-PT.json +13 -1
- package/dist/esm/i18n/locales/locales/zh-CN.json +13 -1
- package/dist/esm/i18n/locales/pt-PT.json +13 -1
- package/dist/esm/i18n/locales/zh-CN.json +13 -1
- package/dist/esm/index.js +14 -1
- package/dist/esm/mixins/OxyServices.auth.js +257 -17
- package/dist/esm/mixins/OxyServices.deviceBoot.js +52 -3
- package/dist/esm/mixins/OxyServices.notifications.js +60 -0
- package/dist/esm/mixins/index.js +5 -0
- package/dist/esm/session/SessionClient.js +40 -4
- package/dist/esm/session/accountDialogController.js +288 -41
- package/dist/esm/session/hubSync.js +1 -1
- package/dist/esm/session/identityPin.js +249 -0
- package/dist/esm/session/identitySession.js +131 -0
- package/dist/esm/session/projectSessionState.js +54 -15
- package/dist/esm/session/refresh.js +73 -8
- package/dist/esm/utils/commonsDelivery.js +54 -0
- package/dist/esm/utils/oauthPkce.js +31 -2
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/boot/sessionColdBoot.d.ts +38 -1
- package/dist/types/index.d.ts +12 -4
- package/dist/types/mixins/OxyServices.auth.d.ts +262 -11
- package/dist/types/mixins/OxyServices.deviceBoot.d.ts +31 -1
- package/dist/types/mixins/OxyServices.notifications.d.ts +143 -0
- package/dist/types/mixins/index.d.ts +2 -1
- package/dist/types/session/SessionClient.d.ts +28 -0
- package/dist/types/session/accountDialogController.d.ts +187 -9
- package/dist/types/session/identityPin.d.ts +87 -0
- package/dist/types/session/identitySession.d.ts +105 -0
- package/dist/types/session/projectSessionState.d.ts +21 -19
- package/dist/types/session/refresh.d.ts +32 -1
- package/dist/types/utils/commonsDelivery.d.ts +105 -0
- package/dist/types/utils/oauthPkce.d.ts +21 -1
- package/package.json +2 -2
- package/src/boot/__tests__/sessionColdBoot.identity.test.ts +393 -0
- package/src/boot/sessionColdBoot.ts +183 -37
- package/src/i18n/locales/ar-SA.json +13 -1
- package/src/i18n/locales/ca-ES.json +13 -1
- package/src/i18n/locales/de-DE.json +13 -1
- package/src/i18n/locales/en-US.json +13 -4
- package/src/i18n/locales/es-ES.json +13 -4
- package/src/i18n/locales/fr-FR.json +13 -1
- package/src/i18n/locales/it-IT.json +13 -1
- package/src/i18n/locales/ja-JP.json +13 -1
- package/src/i18n/locales/ko-KR.json +13 -1
- package/src/i18n/locales/pt-PT.json +13 -1
- package/src/i18n/locales/zh-CN.json +13 -1
- package/src/index.ts +49 -0
- package/src/mixins/OxyServices.auth.ts +464 -24
- package/src/mixins/OxyServices.deviceBoot.ts +54 -2
- package/src/mixins/OxyServices.notifications.ts +142 -0
- package/src/mixins/__tests__/commonsSignIn.test.ts +754 -2
- package/src/mixins/__tests__/deviceTokenMintPinned.test.ts +113 -0
- package/src/mixins/__tests__/preSessionSkipAuth.test.ts +18 -0
- package/src/mixins/index.ts +6 -0
- package/src/session/SessionClient.ts +57 -4
- package/src/session/__tests__/SessionClient.identityPin.test.ts +175 -0
- package/src/session/__tests__/accountDialogController.test.ts +812 -1
- package/src/session/__tests__/hubSync.test.ts +51 -0
- package/src/session/__tests__/identityPin.test.ts +131 -0
- package/src/session/__tests__/identitySession.test.ts +194 -0
- package/src/session/__tests__/projectSessionState.test.ts +88 -0
- package/src/session/__tests__/refresh.identityPin.test.ts +306 -0
- package/src/session/accountDialogController.ts +435 -44
- package/src/session/hubSync.ts +1 -1
- package/src/session/identityPin.ts +310 -0
- package/src/session/identitySession.ts +206 -0
- package/src/session/projectSessionState.ts +61 -13
- package/src/session/refresh.ts +103 -9
- package/src/utils/__tests__/oauthPkce.test.ts +51 -0
- package/src/utils/commonsDelivery.ts +132 -0
- package/src/utils/oauthPkce.ts +56 -3
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identity pin — the durable binding between THIS device's PRIMARY identity key
|
|
3
|
+
* and the account that key authenticates as.
|
|
4
|
+
*
|
|
5
|
+
* An identity-bound client (Commons, the identity vault) must always act as the
|
|
6
|
+
* owner of the local signing key — permanently. The `DeviceSession` it shares
|
|
7
|
+
* with every other Oxy app on the same device tracks a MUTABLE `activeAccountId`
|
|
8
|
+
* that any sibling app can switch; following it silently changes both the user
|
|
9
|
+
* such a client renders and the bearer it sends. The pin is the client-side
|
|
10
|
+
* record that breaks that coupling: `{ publicKey, accountId }`, written when an
|
|
11
|
+
* identity-mode session is established (the only moment both facts are known
|
|
12
|
+
* first-hand), read on every boot, and reconciled against the live
|
|
13
|
+
* `KeyManager.getPublicKey()` before it is trusted (see
|
|
14
|
+
* `resolveIdentityPin` in `identitySession.ts`).
|
|
15
|
+
*
|
|
16
|
+
* It holds NO secret — a public key and an account id, both already known to the
|
|
17
|
+
* server — so it lives in the same per-origin storage as {@link PersistedAuthState}
|
|
18
|
+
* (`authStateStore.ts`) rather than the keychain, and mirrors that module's
|
|
19
|
+
* store shape: `load / save / clear`, an in-memory mirror, a read-back-verified
|
|
20
|
+
* durable write, and graceful degradation when storage is unavailable.
|
|
21
|
+
*
|
|
22
|
+
* ESM-safe (no `require()`); no react/react-native/expo imports.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { logger } from '../logger';
|
|
26
|
+
import type { NativeKeyValueStorage } from './authStateStore';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The persisted identity binding.
|
|
30
|
+
*
|
|
31
|
+
* `publicKey` is the device's PRIMARY identity public key (lower-case hex, as
|
|
32
|
+
* `KeyManager` stores it) — never the cross-app shared-slot key, which may hold
|
|
33
|
+
* a different identity. `accountId` is the account the server resolved for that
|
|
34
|
+
* key when the session was established.
|
|
35
|
+
*/
|
|
36
|
+
export interface IdentityPin {
|
|
37
|
+
publicKey: string;
|
|
38
|
+
accountId: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The storage seam for {@link IdentityPin}. Async throughout so one interface
|
|
43
|
+
* fits both synchronous web `localStorage` and asynchronous native
|
|
44
|
+
* SecureStore/AsyncStorage.
|
|
45
|
+
*/
|
|
46
|
+
export interface IdentityPinStore {
|
|
47
|
+
load(): Promise<IdentityPin | null>;
|
|
48
|
+
/**
|
|
49
|
+
* Persist the pin and report whether it durably landed.
|
|
50
|
+
*
|
|
51
|
+
* `true` means the value is retained consistent with this store's durability
|
|
52
|
+
* guarantee (a durable backing whose read-back matched, or a degraded/in-memory
|
|
53
|
+
* store that held it in memory). `false` means a DURABLE backing was expected
|
|
54
|
+
* but the write did not land — the in-memory mirror keeps this process pinned,
|
|
55
|
+
* but the next cold boot will have to re-establish the identity session (which
|
|
56
|
+
* rewrites the pin) instead of taking the fast pinned-mint lane.
|
|
57
|
+
*/
|
|
58
|
+
save(pin: IdentityPin): Promise<boolean>;
|
|
59
|
+
clear(): Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Versioned storage key. Deliberately distinct from the `oxy.auth.*` keys so a
|
|
64
|
+
* session sign-out (which clears the auth blob) and an identity change (which
|
|
65
|
+
* clears the pin) can never take each other down.
|
|
66
|
+
*/
|
|
67
|
+
export const IDENTITY_PIN_STORAGE_KEY = 'oxy.identity.pin.v1';
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Structural check for a storable identity public key: `KeyManager` only ever
|
|
71
|
+
* persists secp256k1 hex — compressed (66 chars) or uncompressed (130). A stored
|
|
72
|
+
* value of any other shape is corrupt, so it is treated as "no pin" (fail
|
|
73
|
+
* closed) rather than compared. Full curve validation belongs to `KeyManager`;
|
|
74
|
+
* the pin's real authority is the reconcile against the live local key.
|
|
75
|
+
*/
|
|
76
|
+
function isStorablePublicKey(value: unknown): value is string {
|
|
77
|
+
if (typeof value !== 'string') {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
if (value.length !== 66 && value.length !== 130) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
return /^[0-9a-fA-F]+$/.test(value);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Parse + shape-validate a stored blob. Returns `null` for anything that is not
|
|
88
|
+
* a well-formed {@link IdentityPin} (absent, malformed JSON, wrong types, junk
|
|
89
|
+
* public key) so a corrupt entry degrades to "not pinned" rather than throwing —
|
|
90
|
+
* and, critically, never yields a pin that could bind a client to the wrong
|
|
91
|
+
* account.
|
|
92
|
+
*/
|
|
93
|
+
function deserialize(raw: string | null): IdentityPin | null {
|
|
94
|
+
if (!raw) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
let parsed: unknown;
|
|
98
|
+
try {
|
|
99
|
+
parsed = JSON.parse(raw);
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
const candidate = parsed as Record<string, unknown>;
|
|
107
|
+
if (!isStorablePublicKey(candidate.publicKey)) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
if (typeof candidate.accountId !== 'string' || candidate.accountId.length === 0) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
publicKey: candidate.publicKey.toLowerCase(),
|
|
115
|
+
accountId: candidate.accountId,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Serialize a pin, or `null` when the caller handed over a value that would not
|
|
121
|
+
* survive {@link deserialize} — writing it would produce an entry that silently
|
|
122
|
+
* reads back as "not pinned".
|
|
123
|
+
*/
|
|
124
|
+
function serialize(pin: IdentityPin): string | null {
|
|
125
|
+
if (!isStorablePublicKey(pin.publicKey) || pin.accountId.length === 0) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
return JSON.stringify({ publicKey: pin.publicKey.toLowerCase(), accountId: pin.accountId });
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Whether `pin` still describes the identity currently on this device.
|
|
133
|
+
*
|
|
134
|
+
* `publicKey` comparison is case-insensitive (hex). A `null` local key is a
|
|
135
|
+
* definitive "no identity here" and therefore never a match — the caller clears
|
|
136
|
+
* the pin rather than binding to an account whose key is gone.
|
|
137
|
+
*/
|
|
138
|
+
export function identityPinMatches(pin: IdentityPin | null, localPublicKey: string | null): boolean {
|
|
139
|
+
if (!pin || !localPublicKey) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
return pin.publicKey.toLowerCase() === localPublicKey.toLowerCase();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A process-lifetime, in-memory {@link IdentityPinStore}. Used directly for
|
|
147
|
+
* tests/SSR and as the degraded fallback of the web store.
|
|
148
|
+
*/
|
|
149
|
+
export function createMemoryIdentityPinStore(): IdentityPinStore {
|
|
150
|
+
let current: IdentityPin | null = null;
|
|
151
|
+
return {
|
|
152
|
+
load: async () => current,
|
|
153
|
+
save: async (pin) => {
|
|
154
|
+
if (!serialize(pin)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
// Memory IS this store's durability backing — the write always lands.
|
|
158
|
+
current = { publicKey: pin.publicKey.toLowerCase(), accountId: pin.accountId };
|
|
159
|
+
return true;
|
|
160
|
+
},
|
|
161
|
+
clear: async () => {
|
|
162
|
+
current = null;
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Read the ambient `localStorage`, tolerating the case where merely ACCESSING
|
|
169
|
+
* `window.localStorage` throws (sandboxed iframe `SecurityError`). Returns
|
|
170
|
+
* `null` when storage is unavailable.
|
|
171
|
+
*/
|
|
172
|
+
function safeGetLocalStorage(): Storage | null {
|
|
173
|
+
try {
|
|
174
|
+
if (typeof globalThis === 'undefined') {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
const store = (globalThis as { localStorage?: Storage }).localStorage;
|
|
178
|
+
return store ?? null;
|
|
179
|
+
} catch {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A `localStorage`-backed {@link IdentityPinStore}. Degrades to an in-memory
|
|
186
|
+
* store when `localStorage` is unreachable, and keeps an in-memory mirror so a
|
|
187
|
+
* failed persist still pins THIS page's lifetime.
|
|
188
|
+
*/
|
|
189
|
+
export function createWebIdentityPinStore(): IdentityPinStore {
|
|
190
|
+
const storage = safeGetLocalStorage();
|
|
191
|
+
if (!storage) {
|
|
192
|
+
return createMemoryIdentityPinStore();
|
|
193
|
+
}
|
|
194
|
+
// `undefined` = never written this session → fall back to storage; any set
|
|
195
|
+
// value (including `null` after clear) is authoritative.
|
|
196
|
+
let sessionMirror: IdentityPin | null | undefined;
|
|
197
|
+
return {
|
|
198
|
+
load: async () => {
|
|
199
|
+
if (sessionMirror !== undefined) {
|
|
200
|
+
return sessionMirror;
|
|
201
|
+
}
|
|
202
|
+
try {
|
|
203
|
+
return deserialize(storage.getItem(IDENTITY_PIN_STORAGE_KEY));
|
|
204
|
+
} catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
save: async (pin) => {
|
|
209
|
+
const json = serialize(pin);
|
|
210
|
+
if (!json) {
|
|
211
|
+
logger.error(
|
|
212
|
+
'[identityPin] refusing to persist a malformed identity pin',
|
|
213
|
+
undefined,
|
|
214
|
+
{ component: 'identityPin' },
|
|
215
|
+
);
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
sessionMirror = { publicKey: pin.publicKey.toLowerCase(), accountId: pin.accountId };
|
|
219
|
+
try {
|
|
220
|
+
storage.setItem(IDENTITY_PIN_STORAGE_KEY, json);
|
|
221
|
+
if (storage.getItem(IDENTITY_PIN_STORAGE_KEY) === json) {
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
logger.error(
|
|
225
|
+
'[identityPin] read-back mismatch after save — the identity pin did not persist; this process stays pinned via the in-memory mirror but the next boot must re-establish the identity session',
|
|
226
|
+
undefined,
|
|
227
|
+
{ component: 'identityPin' },
|
|
228
|
+
);
|
|
229
|
+
return false;
|
|
230
|
+
} catch (error) {
|
|
231
|
+
logger.error(
|
|
232
|
+
'[identityPin] persist threw — the identity pin did not persist; this process stays pinned via the in-memory mirror but the next boot must re-establish the identity session',
|
|
233
|
+
error,
|
|
234
|
+
{ component: 'identityPin' },
|
|
235
|
+
);
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
},
|
|
239
|
+
clear: async () => {
|
|
240
|
+
sessionMirror = null;
|
|
241
|
+
try {
|
|
242
|
+
storage.removeItem(IDENTITY_PIN_STORAGE_KEY);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
logger.debug('[identityPin] clear failed', { component: 'identityPin' }, error);
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* A native {@link IdentityPinStore} over an injected async key/value store —
|
|
252
|
+
* the same seam `createNativeAuthStateStore` uses, so `@oxyhq/core` never
|
|
253
|
+
* imports `expo-secure-store`.
|
|
254
|
+
*/
|
|
255
|
+
export function createNativeIdentityPinStore(storage: NativeKeyValueStorage): IdentityPinStore {
|
|
256
|
+
let sessionMirror: IdentityPin | null | undefined;
|
|
257
|
+
return {
|
|
258
|
+
load: async () => {
|
|
259
|
+
if (sessionMirror !== undefined) {
|
|
260
|
+
return sessionMirror;
|
|
261
|
+
}
|
|
262
|
+
try {
|
|
263
|
+
return deserialize(await storage.getItem(IDENTITY_PIN_STORAGE_KEY));
|
|
264
|
+
} catch {
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
save: async (pin) => {
|
|
269
|
+
const json = serialize(pin);
|
|
270
|
+
if (!json) {
|
|
271
|
+
logger.error(
|
|
272
|
+
'[identityPin] refusing to persist a malformed identity pin',
|
|
273
|
+
undefined,
|
|
274
|
+
{ component: 'identityPin' },
|
|
275
|
+
);
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
sessionMirror = { publicKey: pin.publicKey.toLowerCase(), accountId: pin.accountId };
|
|
279
|
+
try {
|
|
280
|
+
await storage.setItem(IDENTITY_PIN_STORAGE_KEY, json);
|
|
281
|
+
// A native write can resolve WITHOUT throwing yet not land, so a
|
|
282
|
+
// read-back is the only reliable proof.
|
|
283
|
+
if ((await storage.getItem(IDENTITY_PIN_STORAGE_KEY)) === json) {
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
logger.error(
|
|
287
|
+
'[identityPin] read-back mismatch after save — the identity pin did not persist; this app run stays pinned via the in-memory mirror but the next cold start must re-establish the identity session',
|
|
288
|
+
undefined,
|
|
289
|
+
{ component: 'identityPin' },
|
|
290
|
+
);
|
|
291
|
+
return false;
|
|
292
|
+
} catch (error) {
|
|
293
|
+
logger.error(
|
|
294
|
+
'[identityPin] persist threw — the identity pin did not persist; this app run stays pinned via the in-memory mirror but the next cold start must re-establish the identity session',
|
|
295
|
+
error,
|
|
296
|
+
{ component: 'identityPin' },
|
|
297
|
+
);
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
clear: async () => {
|
|
302
|
+
sessionMirror = null;
|
|
303
|
+
try {
|
|
304
|
+
await storage.removeItem(IDENTITY_PIN_STORAGE_KEY);
|
|
305
|
+
} catch (error) {
|
|
306
|
+
logger.debug('[identityPin] clear failed', { component: 'identityPin' }, error);
|
|
307
|
+
}
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identity-bound session — pin reconciliation + establishment.
|
|
3
|
+
*
|
|
4
|
+
* This is the SDK half of "an identity vault's authenticated user is whoever
|
|
5
|
+
* owns the local primary key". Two operations, both consumed by the cold boot
|
|
6
|
+
* (`boot/sessionColdBoot.ts`) and the re-mint lane (`session/refresh.ts`):
|
|
7
|
+
*
|
|
8
|
+
* - {@link resolveIdentityPin} — read the persisted `{publicKey, accountId}`
|
|
9
|
+
* pin and reconcile it against the identity currently on this device. A
|
|
10
|
+
* definitive mismatch (the key was replaced, or is gone) CLEARS the pin; an
|
|
11
|
+
* INDETERMINATE read (keychain locked / storage threw) leaves it untouched.
|
|
12
|
+
* - {@link establishIdentitySession} — mint a session from the PRIMARY local
|
|
13
|
+
* key (`getPublicKey` → `requestChallenge` → `signChallenge` →
|
|
14
|
+
* `verifyChallenge`), persist the resulting device credential, and write the
|
|
15
|
+
* pin. This is the identity-mode replacement for the shared-keychain lane,
|
|
16
|
+
* which reads the CROSS-APP shared slot and may therefore hold a different
|
|
17
|
+
* identity than this device's primary.
|
|
18
|
+
*
|
|
19
|
+
* Both take their key/signature functions from an injectable {@link IdentityBinding}
|
|
20
|
+
* (defaulting to `KeyManager` / `SignatureService`) so the lanes above stay
|
|
21
|
+
* testable without a keychain.
|
|
22
|
+
*
|
|
23
|
+
* ESM-safe (no `require()`); no react/react-native/expo imports.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import type { OxyServices } from '../OxyServices';
|
|
27
|
+
import type { AuthStateStore } from './authStateStore';
|
|
28
|
+
import type { SessionLoginResponse } from '../models/session';
|
|
29
|
+
import { KeyManager } from '../crypto/keyManager';
|
|
30
|
+
import { SignatureService, type AuthChallenge } from '../crypto/signatureService';
|
|
31
|
+
import { identityPinMatches, type IdentityPin, type IdentityPinStore } from './identityPin';
|
|
32
|
+
import { logger } from '../logger';
|
|
33
|
+
|
|
34
|
+
/** Per-call transport overrides forwarded to the challenge/verify round-trips. */
|
|
35
|
+
export interface IdentityRequestOptions {
|
|
36
|
+
retry?: boolean;
|
|
37
|
+
timeout?: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Everything an identity-bound client needs to resolve and re-establish its
|
|
42
|
+
* session. `@oxyhq/services` builds one of these (platform-appropriate pin
|
|
43
|
+
* store) and passes it to the cold boot and the refresh handler.
|
|
44
|
+
*/
|
|
45
|
+
export interface IdentityBinding {
|
|
46
|
+
/** Where the `{publicKey, accountId}` pin is persisted. */
|
|
47
|
+
pinStore: IdentityPinStore;
|
|
48
|
+
/**
|
|
49
|
+
* Reads this device's PRIMARY identity public key. Defaults to
|
|
50
|
+
* `KeyManager.getPublicKey()`. NEVER the shared-slot key.
|
|
51
|
+
*/
|
|
52
|
+
readPublicKey?: () => Promise<string | null>;
|
|
53
|
+
/**
|
|
54
|
+
* Signs a server challenge with the PRIMARY local private key. Defaults to
|
|
55
|
+
* `SignatureService.signChallenge`.
|
|
56
|
+
*/
|
|
57
|
+
signChallenge?: (challenge: string) => Promise<AuthChallenge>;
|
|
58
|
+
/** Optional device labels forwarded to `verifyChallenge`. */
|
|
59
|
+
deviceName?: string;
|
|
60
|
+
deviceFingerprint?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The result of a successful {@link establishIdentitySession}. */
|
|
64
|
+
export interface EstablishedIdentitySession {
|
|
65
|
+
session: SessionLoginResponse;
|
|
66
|
+
/** The pin as written: the local key plus the account the server resolved for it. */
|
|
67
|
+
pin: IdentityPin;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function readPublicKeyOf(binding: IdentityBinding): () => Promise<string | null> {
|
|
71
|
+
return binding.readPublicKey ?? (() => KeyManager.getPublicKey());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function signChallengeOf(binding: IdentityBinding): (challenge: string) => Promise<AuthChallenge> {
|
|
75
|
+
return binding.signChallenge ?? ((challenge) => SignatureService.signChallenge(challenge));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Read the persisted pin and reconcile it against the identity on this device.
|
|
80
|
+
*
|
|
81
|
+
* Returns the pin ONLY when the local primary public key still matches it — that
|
|
82
|
+
* is the single condition under which a client may bind its token and its
|
|
83
|
+
* rendered user to that account.
|
|
84
|
+
*
|
|
85
|
+
* Outcomes:
|
|
86
|
+
* - no pin stored → `null` (nothing to clear).
|
|
87
|
+
* - local key MATCHES → the pin (trusted).
|
|
88
|
+
* - local key read succeeded and differs / is absent → the identity was
|
|
89
|
+
* replaced or lost: CLEAR the pin and return `null`. Keeping it would pin the
|
|
90
|
+
* client to an account it can no longer prove control of.
|
|
91
|
+
* - the local key read THREW (keychain locked, storage unavailable) → return
|
|
92
|
+
* `null` WITHOUT clearing. A read that produced no verdict is never evidence
|
|
93
|
+
* of an identity change (the same rule `KeyManager` applies to its own
|
|
94
|
+
* transient errors); the caller falls through to the identity sign-in lane,
|
|
95
|
+
* which fails closed on a locked keychain rather than adopting a foreign
|
|
96
|
+
* account.
|
|
97
|
+
*/
|
|
98
|
+
export async function resolveIdentityPin(binding: IdentityBinding): Promise<IdentityPin | null> {
|
|
99
|
+
const pin = await binding.pinStore.load();
|
|
100
|
+
if (!pin) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let localPublicKey: string | null;
|
|
105
|
+
try {
|
|
106
|
+
localPublicKey = await readPublicKeyOf(binding)();
|
|
107
|
+
} catch (error) {
|
|
108
|
+
logger.debug(
|
|
109
|
+
'Identity key read did not produce a verdict — keeping the pin untouched',
|
|
110
|
+
{ component: 'identitySession', method: 'resolveIdentityPin' },
|
|
111
|
+
error,
|
|
112
|
+
);
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (identityPinMatches(pin, localPublicKey)) {
|
|
117
|
+
return pin;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
logger.warn(
|
|
121
|
+
'Identity pin no longer matches the device identity key — clearing it (the identity was replaced or removed)',
|
|
122
|
+
{ component: 'identitySession', method: 'resolveIdentityPin' },
|
|
123
|
+
);
|
|
124
|
+
await binding.pinStore.clear();
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Establish a session for the device's PRIMARY identity key and pin it.
|
|
130
|
+
*
|
|
131
|
+
* `requestChallenge` → `signChallenge` → `verifyChallenge`; `verifyChallenge`
|
|
132
|
+
* plants the access token itself. The server resolves the account from the
|
|
133
|
+
* VERIFIED SIGNER, so the returned session is identity-authoritative — it
|
|
134
|
+
* ignores the device's `activeAccountId` entirely.
|
|
135
|
+
*
|
|
136
|
+
* Side effects on success: the durable device credential is persisted (so the
|
|
137
|
+
* next boot can take the fast pinned-mint lane) and the pin is written.
|
|
138
|
+
*
|
|
139
|
+
* Returns `null` — never throws — for the two "nothing to do" cases: no local
|
|
140
|
+
* identity (including every web caller, where `KeyManager` has no key), and a
|
|
141
|
+
* verify that produced no access token. Network/crypto failures propagate so the
|
|
142
|
+
* caller can classify them.
|
|
143
|
+
*/
|
|
144
|
+
export async function establishIdentitySession(args: {
|
|
145
|
+
oxy: OxyServices;
|
|
146
|
+
store: AuthStateStore;
|
|
147
|
+
binding: IdentityBinding;
|
|
148
|
+
requestOptions?: IdentityRequestOptions;
|
|
149
|
+
}): Promise<EstablishedIdentitySession | null> {
|
|
150
|
+
const { oxy, store, binding, requestOptions } = args;
|
|
151
|
+
|
|
152
|
+
const publicKey = await readPublicKeyOf(binding)();
|
|
153
|
+
if (!publicKey) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const { challenge } = await oxy.requestChallenge(publicKey, requestOptions);
|
|
158
|
+
const signed = await signChallengeOf(binding)(challenge);
|
|
159
|
+
if (signed.publicKey.toLowerCase() !== publicKey.toLowerCase()) {
|
|
160
|
+
// The signer disagrees with the key we resolved a challenge for — the
|
|
161
|
+
// identity changed mid-flight, or a custom signer was misconfigured. Refuse:
|
|
162
|
+
// verifying under a different key would bind this client to another account.
|
|
163
|
+
throw new Error('Identity sign-in aborted: the signing key does not match the device identity key');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// `signed.challenge` carries the SIGNATURE (mirrors `signChallengeWithSharedKey`).
|
|
167
|
+
const session = await oxy.verifyChallenge(
|
|
168
|
+
signed.publicKey,
|
|
169
|
+
challenge,
|
|
170
|
+
signed.challenge,
|
|
171
|
+
signed.timestamp,
|
|
172
|
+
binding.deviceName,
|
|
173
|
+
binding.deviceFingerprint,
|
|
174
|
+
requestOptions,
|
|
175
|
+
);
|
|
176
|
+
if (!session?.accessToken) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Persist the rotating device credential so later boots re-mint through the
|
|
181
|
+
// fast `POST /session/device/token` lane instead of re-signing a challenge.
|
|
182
|
+
if (session.deviceId && session.deviceSecret) {
|
|
183
|
+
await store.save({
|
|
184
|
+
sessionId: session.sessionId,
|
|
185
|
+
userId: session.user.id,
|
|
186
|
+
deviceId: session.deviceId,
|
|
187
|
+
deviceSecret: session.deviceSecret,
|
|
188
|
+
accessToken: session.accessToken,
|
|
189
|
+
expiresAt: session.expiresAt,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const pin: IdentityPin = { publicKey: publicKey.toLowerCase(), accountId: session.user.id };
|
|
194
|
+
// A failed pin persist is NOT fatal: this process is already pinned in memory,
|
|
195
|
+
// and a later boot without a pin skips the (unpinned) mint lane and re-runs
|
|
196
|
+
// this exact lane, which rewrites it. Never fail an established session on it.
|
|
197
|
+
const pinned = await binding.pinStore.save(pin);
|
|
198
|
+
if (!pinned) {
|
|
199
|
+
logger.warn(
|
|
200
|
+
'Identity session established but the pin did not durably persist — the next cold start will re-establish it',
|
|
201
|
+
{ component: 'identitySession', method: 'establishIdentitySession' },
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { session, pin };
|
|
206
|
+
}
|
|
@@ -12,8 +12,30 @@ import type { User } from '../models/interfaces';
|
|
|
12
12
|
* `oxyServices.getUsersByIds(accountIdsOf(state))` and builds `usersById`
|
|
13
13
|
* from the result before calling `deviceStateToClientSessions` /
|
|
14
14
|
* `activeUserOf`.
|
|
15
|
+
*
|
|
16
|
+
* Each projection takes an OPTIONAL `pinnedAccountId`. Omit it (or pass `null`)
|
|
17
|
+
* and the projection resolves the device's `activeAccountId` exactly as before.
|
|
18
|
+
* Pass it — an IDENTITY-BOUND client, whose user is fixed by the local identity
|
|
19
|
+
* key — and the projection resolves THAT account instead, so an account switch
|
|
20
|
+
* made by another app on the same device changes `state` but never the user this
|
|
21
|
+
* client renders.
|
|
15
22
|
*/
|
|
16
23
|
|
|
24
|
+
/**
|
|
25
|
+
* The account a projection should resolve: the pin when one is supplied and
|
|
26
|
+
* non-empty, else the device's active account. An empty-string pin is treated as
|
|
27
|
+
* "not pinned" rather than as an account that can never match.
|
|
28
|
+
*/
|
|
29
|
+
function boundAccountIdOf(
|
|
30
|
+
state: DeviceSessionState,
|
|
31
|
+
pinnedAccountId?: string | null,
|
|
32
|
+
): string | null {
|
|
33
|
+
if (typeof pinnedAccountId === 'string' && pinnedAccountId.length > 0) {
|
|
34
|
+
return pinnedAccountId;
|
|
35
|
+
}
|
|
36
|
+
return state.activeAccountId;
|
|
37
|
+
}
|
|
38
|
+
|
|
17
39
|
/**
|
|
18
40
|
* Maps every `SessionAccount` in `state.accounts` to a `ClientSession`.
|
|
19
41
|
*
|
|
@@ -25,12 +47,18 @@ import type { User } from '../models/interfaces';
|
|
|
25
47
|
* though `ClientSession` only stores `userId` — a session is still
|
|
26
48
|
* projected for an account whose id is absent from `usersById` (no
|
|
27
49
|
* placeholder user is fabricated).
|
|
50
|
+
*
|
|
51
|
+
* `isCurrent` marks the PINNED account when one is supplied, so it can never
|
|
52
|
+
* disagree with {@link activeSessionIdOf} / {@link activeUserOf} for the same
|
|
53
|
+
* pin.
|
|
28
54
|
*/
|
|
29
55
|
export function deviceStateToClientSessions(
|
|
30
56
|
state: DeviceSessionState,
|
|
31
57
|
usersById: Map<string, User>,
|
|
58
|
+
pinnedAccountId?: string | null,
|
|
32
59
|
): ClientSession[] {
|
|
33
60
|
const provisionalTimestamp = new Date(state.updatedAt).toISOString();
|
|
61
|
+
const boundAccountId = boundAccountIdOf(state, pinnedAccountId);
|
|
34
62
|
return state.accounts.map((account) => ({
|
|
35
63
|
sessionId: account.sessionId,
|
|
36
64
|
deviceId: state.deviceId,
|
|
@@ -38,37 +66,57 @@ export function deviceStateToClientSessions(
|
|
|
38
66
|
expiresAt: provisionalTimestamp,
|
|
39
67
|
lastActive: provisionalTimestamp,
|
|
40
68
|
userId: account.accountId,
|
|
41
|
-
isCurrent: account.accountId ===
|
|
69
|
+
isCurrent: account.accountId === boundAccountId,
|
|
42
70
|
authuser: account.authuser,
|
|
43
71
|
}));
|
|
44
72
|
}
|
|
45
73
|
|
|
46
74
|
/**
|
|
47
|
-
* The
|
|
48
|
-
*
|
|
75
|
+
* The bound account's `sessionId`, or `null` when there is no state, no bound
|
|
76
|
+
* account, or that account has no session on this device.
|
|
77
|
+
*
|
|
78
|
+
* A pinned account that is ABSENT from `state.accounts` yields `null` — the
|
|
79
|
+
* honest signal that this device no longer carries a session for the pinned
|
|
80
|
+
* identity, which the caller answers by re-establishing the identity session
|
|
81
|
+
* (never by adopting the device's active account).
|
|
49
82
|
*/
|
|
50
|
-
export function activeSessionIdOf(
|
|
51
|
-
|
|
83
|
+
export function activeSessionIdOf(
|
|
84
|
+
state: DeviceSessionState | null,
|
|
85
|
+
pinnedAccountId?: string | null,
|
|
86
|
+
): string | null {
|
|
87
|
+
if (state === null) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
const boundAccountId = boundAccountIdOf(state, pinnedAccountId);
|
|
91
|
+
if (boundAccountId === null) {
|
|
52
92
|
return null;
|
|
53
93
|
}
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
return activeAccount?.sessionId ?? null;
|
|
94
|
+
const boundAccount = state.accounts.find((account) => account.accountId === boundAccountId);
|
|
95
|
+
return boundAccount?.sessionId ?? null;
|
|
57
96
|
}
|
|
58
97
|
|
|
59
98
|
/**
|
|
60
|
-
* The
|
|
61
|
-
*
|
|
62
|
-
*
|
|
99
|
+
* The bound account's `User`, resolved from `usersById`. `null` when there is no
|
|
100
|
+
* state, no bound account, or the bound account id is absent from `usersById`.
|
|
101
|
+
*
|
|
102
|
+
* A pinned user is resolved from `usersById` alone — deliberately NOT gated on
|
|
103
|
+
* device membership — so a transient device-state gap cannot flicker the
|
|
104
|
+
* identity vault's rendered user. Whether the pinned session still exists on the
|
|
105
|
+
* device is answered by {@link activeSessionIdOf}.
|
|
63
106
|
*/
|
|
64
107
|
export function activeUserOf(
|
|
65
108
|
state: DeviceSessionState | null,
|
|
66
109
|
usersById: Map<string, User>,
|
|
110
|
+
pinnedAccountId?: string | null,
|
|
67
111
|
): User | null {
|
|
68
|
-
if (state === null
|
|
112
|
+
if (state === null) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
const boundAccountId = boundAccountIdOf(state, pinnedAccountId);
|
|
116
|
+
if (boundAccountId === null) {
|
|
69
117
|
return null;
|
|
70
118
|
}
|
|
71
|
-
return usersById.get(
|
|
119
|
+
return usersById.get(boundAccountId) ?? null;
|
|
72
120
|
}
|
|
73
121
|
|
|
74
122
|
/**
|