@oxyhq/core 9.0.0 → 9.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/README.md +2 -4
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/boot/{coldBootV2.js → sessionColdBoot.js} +2 -2
- package/dist/cjs/index.js +23 -4
- package/dist/cjs/mixins/OxyServices.auth.js +61 -0
- package/dist/cjs/mixins/OxyServices.deviceBoot.js +29 -1
- package/dist/cjs/server/index.js +8 -1
- package/dist/cjs/session/SessionClient.js +57 -25
- package/dist/cjs/session/accountDialogController.js +20 -9
- package/dist/cjs/session/authStateStore.js +13 -7
- package/dist/cjs/session/hubSync.js +55 -0
- package/dist/cjs/session/sessionClientHost.js +5 -0
- package/dist/cjs/utils/coldBoot.js +1 -1
- package/dist/cjs/utils/oauthPkce.js +65 -1
- package/dist/cjs/utils/officialOrigins.js +128 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/boot/{coldBootV2.js → sessionColdBoot.js} +2 -2
- package/dist/esm/index.js +4 -2
- package/dist/esm/mixins/OxyServices.auth.js +61 -0
- package/dist/esm/mixins/OxyServices.deviceBoot.js +30 -2
- package/dist/esm/server/index.js +1 -0
- package/dist/esm/session/SessionClient.js +57 -25
- package/dist/esm/session/accountDialogController.js +20 -9
- package/dist/esm/session/authStateStore.js +13 -7
- package/dist/esm/session/hubSync.js +51 -0
- package/dist/esm/session/sessionClientHost.js +5 -0
- package/dist/esm/utils/coldBoot.js +1 -1
- package/dist/esm/utils/oauthPkce.js +60 -0
- package/dist/esm/utils/officialOrigins.js +119 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/boot/{coldBootV2.d.ts → sessionColdBoot.d.ts} +1 -1
- package/dist/types/index.d.ts +7 -4
- package/dist/types/mixins/OxyServices.auth.d.ts +14 -0
- package/dist/types/mixins/OxyServices.deviceBoot.d.ts +6 -2
- package/dist/types/models/interfaces.d.ts +15 -0
- package/dist/types/server/index.d.ts +1 -0
- package/dist/types/session/SessionClient.d.ts +8 -2
- package/dist/types/session/accountDialogController.d.ts +9 -1
- package/dist/types/session/authStateStore.d.ts +1 -1
- package/dist/types/session/hubSync.d.ts +20 -0
- package/dist/types/session/sessionClientHost.d.ts +2 -1
- package/dist/types/utils/coldBoot.d.ts +2 -2
- package/dist/types/utils/oauthPkce.d.ts +27 -0
- package/dist/types/utils/officialOrigins.d.ts +17 -0
- package/package.json +3 -3
- package/src/boot/__tests__/{coldBootV2.test.ts → sessionColdBoot.test.ts} +1 -1
- package/src/boot/{coldBootV2.ts → sessionColdBoot.ts} +2 -2
- package/src/index.ts +27 -3
- package/src/mixins/OxyServices.auth.ts +70 -1
- package/src/mixins/OxyServices.deviceBoot.ts +46 -1
- package/src/models/interfaces.ts +15 -0
- package/src/server/index.ts +8 -0
- package/src/session/SessionClient.ts +70 -28
- package/src/session/__tests__/SessionClient.additive.test.ts +1 -0
- package/src/session/__tests__/SessionClient.broadcastChannel.test.ts +1 -0
- package/src/session/__tests__/SessionClient.diagnostics.test.ts +1 -0
- package/src/session/__tests__/SessionClient.rest.test.ts +1 -0
- package/src/session/__tests__/SessionClient.socket.test.ts +1 -0
- package/src/session/__tests__/SessionClient.socketFactory.test.ts +1 -0
- package/src/session/__tests__/SessionClient.state.test.ts +1 -0
- package/src/session/__tests__/accountDialogController.test.ts +38 -6
- package/src/session/__tests__/sessionIntegration.test.ts +7 -0
- package/src/session/accountDialogController.ts +36 -10
- package/src/session/authStateStore.ts +18 -9
- package/src/session/hubSync.ts +79 -0
- package/src/session/sessionClientHost.ts +10 -2
- package/src/utils/__tests__/officialOrigins.test.ts +74 -0
- package/src/utils/coldBoot.ts +2 -2
- package/src/utils/oauthPkce.ts +71 -0
- package/src/utils/officialOrigins.ts +124 -0
package/src/utils/oauthPkce.ts
CHANGED
|
@@ -61,6 +61,11 @@ export interface BuildOAuthAuthorizeUrlParams {
|
|
|
61
61
|
state: string;
|
|
62
62
|
/** The PKCE `codeChallenge` from {@link generatePkcePair}. */
|
|
63
63
|
codeChallenge: string;
|
|
64
|
+
/**
|
|
65
|
+
* OAuth `prompt` parameter. Use `none` for silent cross-origin session restore
|
|
66
|
+
* (no UI when the IdP hub already has a session + grant).
|
|
67
|
+
*/
|
|
68
|
+
prompt?: 'none' | 'login' | 'consent';
|
|
64
69
|
}
|
|
65
70
|
|
|
66
71
|
/**
|
|
@@ -184,6 +189,72 @@ export function buildOAuthAuthorizeUrl(params: BuildOAuthAuthorizeUrlParams): st
|
|
|
184
189
|
url.searchParams.set('scope', scope);
|
|
185
190
|
url.searchParams.set('code_challenge', codeChallenge);
|
|
186
191
|
url.searchParams.set('code_challenge_method', 'S256');
|
|
192
|
+
if (params.prompt) {
|
|
193
|
+
url.searchParams.set('prompt', params.prompt);
|
|
194
|
+
}
|
|
187
195
|
|
|
188
196
|
return url.toString();
|
|
189
197
|
}
|
|
198
|
+
|
|
199
|
+
/** `sessionStorage` key for the OAuth CSRF `state` across an authorize redirect. */
|
|
200
|
+
export const OXY_OAUTH_STATE_STORAGE_KEY = 'oxy_oauth_state';
|
|
201
|
+
|
|
202
|
+
/** `sessionStorage` key for the PKCE `code_verifier` across an authorize redirect. */
|
|
203
|
+
export const OXY_OAUTH_CODE_VERIFIER_STORAGE_KEY = 'oxy_oauth_code_verifier';
|
|
204
|
+
|
|
205
|
+
/** `sessionStorage` key — at most one silent OAuth attempt per navigation. */
|
|
206
|
+
export const OXY_SILENT_OAUTH_ATTEMPTED_KEY = 'oxy.silent_oauth_attempted';
|
|
207
|
+
|
|
208
|
+
/** `sessionStorage` key — blocks further cross-origin auto-restore in this tab. */
|
|
209
|
+
export const OXY_CROSS_ORIGIN_RESTORE_ATTEMPTED_KEY = 'oxy.cross_origin_restore_attempted';
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Normalize a redirect URI to its origin. Official Oxy apps register apex
|
|
213
|
+
* origins (`https://inbox.oxy.so`) — never path-qualified URLs.
|
|
214
|
+
*/
|
|
215
|
+
export function normalizeOAuthRedirectUri(input: string): string {
|
|
216
|
+
try {
|
|
217
|
+
return new URL(input).origin;
|
|
218
|
+
} catch {
|
|
219
|
+
return input;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Persist the OAuth handshake for a full-page redirect return (web only). */
|
|
224
|
+
export function persistOAuthHandshake(state: string, codeVerifier: string): boolean {
|
|
225
|
+
const store = (globalThis as { sessionStorage?: Storage }).sessionStorage;
|
|
226
|
+
try {
|
|
227
|
+
if (!store) throw new Error('sessionStorage is unavailable');
|
|
228
|
+
store.setItem(OXY_OAUTH_STATE_STORAGE_KEY, state);
|
|
229
|
+
store.setItem(OXY_OAUTH_CODE_VERIFIER_STORAGE_KEY, codeVerifier);
|
|
230
|
+
return true;
|
|
231
|
+
} catch (error) {
|
|
232
|
+
logger.warn(
|
|
233
|
+
'Could not persist OAuth handshake to sessionStorage',
|
|
234
|
+
{ component: 'oauthPkce' },
|
|
235
|
+
error,
|
|
236
|
+
);
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Read the persisted OAuth handshake, or `null` when absent. */
|
|
242
|
+
export function readOAuthHandshake(): { state: string; codeVerifier: string } | null {
|
|
243
|
+
const store = (globalThis as { sessionStorage?: Storage }).sessionStorage;
|
|
244
|
+
if (!store) return null;
|
|
245
|
+
const state = store.getItem(OXY_OAUTH_STATE_STORAGE_KEY);
|
|
246
|
+
const codeVerifier = store.getItem(OXY_OAUTH_CODE_VERIFIER_STORAGE_KEY);
|
|
247
|
+
if (!state || !codeVerifier) return null;
|
|
248
|
+
return { state, codeVerifier };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Drop persisted OAuth handshake keys after a successful or aborted return. */
|
|
252
|
+
export function clearOAuthHandshake(): void {
|
|
253
|
+
const store = (globalThis as { sessionStorage?: Storage }).sessionStorage;
|
|
254
|
+
try {
|
|
255
|
+
store?.removeItem(OXY_OAUTH_STATE_STORAGE_KEY);
|
|
256
|
+
store?.removeItem(OXY_OAUTH_CODE_VERIFIER_STORAGE_KEY);
|
|
257
|
+
} catch {
|
|
258
|
+
// Best-effort cleanup only.
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official first-party web origin allowlist — shared by hub-ticket issuance,
|
|
3
|
+
* OAuth redirect validation, and cross-origin session restore.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { CENTRAL_IDP_APEX } from './authWebUrl';
|
|
7
|
+
import { registrableApex } from './registrableApex';
|
|
8
|
+
|
|
9
|
+
/** Official first-party registrable apexes (mirrors API trusted origins). */
|
|
10
|
+
const OFFICIAL_APEXES = new Set([
|
|
11
|
+
'oxy.so',
|
|
12
|
+
'fairco.in',
|
|
13
|
+
'mention.earth',
|
|
14
|
+
'homiio.com',
|
|
15
|
+
'alia.onl',
|
|
16
|
+
'syra.fm',
|
|
17
|
+
'allo.you',
|
|
18
|
+
'tnp.network',
|
|
19
|
+
'moovo.now',
|
|
20
|
+
'mercaria.co',
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
export function buildIdpHubOrigin(): string {
|
|
24
|
+
return `https://auth.${CENTRAL_IDP_APEX}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Whether the current web origin is the central IdP hub (`auth.oxy.so`). */
|
|
28
|
+
export function isIdpHubOrigin(): boolean {
|
|
29
|
+
if (typeof globalThis === 'undefined') {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
const location = (globalThis as { location?: Location }).location;
|
|
33
|
+
if (!location) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const { hostname } = new URL(location.href);
|
|
38
|
+
return hostname === `auth.${CENTRAL_IDP_APEX}`;
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isLoopbackOrigin(origin: string): boolean {
|
|
45
|
+
try {
|
|
46
|
+
const parsed = new URL(origin);
|
|
47
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const host = parsed.hostname.toLowerCase();
|
|
51
|
+
return host === 'localhost' || host === '127.0.0.1' || host === '[::1]';
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Whether an origin belongs to the official Oxy web ecosystem. */
|
|
58
|
+
export function isOfficialWebOrigin(origin: string): boolean {
|
|
59
|
+
if (isLoopbackOrigin(origin)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const parsed = new URL(origin);
|
|
64
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const host = parsed.hostname.toLowerCase();
|
|
68
|
+
if (host === CENTRAL_IDP_APEX || host.endsWith(`.${CENTRAL_IDP_APEX}`)) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
const apex = registrableApex(host);
|
|
72
|
+
return apex != null && OFFICIAL_APEXES.has(apex);
|
|
73
|
+
} catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Normalize and validate a return URL against official origins. Returns origin only. */
|
|
79
|
+
export function normalizeOfficialReturnOrigin(raw: string): string | null {
|
|
80
|
+
try {
|
|
81
|
+
const parsed = new URL(raw);
|
|
82
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (!isOfficialWebOrigin(parsed.origin)) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return parsed.origin;
|
|
89
|
+
} catch {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Validate a hub-sync return URL; returns the full normalized URL string. */
|
|
95
|
+
export function parseHubSyncReturnUrl(raw: string | null): string | null {
|
|
96
|
+
if (!raw) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const parsed = new URL(raw);
|
|
101
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
if (!isOfficialWebOrigin(parsed.origin)) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
return parsed.toString();
|
|
108
|
+
} catch {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Build auth.oxy.so/sync redirect URL with a one-time hub ticket. */
|
|
114
|
+
export function buildHubSyncUrl(ticket: string, returnUrl?: string): string {
|
|
115
|
+
const url = new URL('/sync', buildIdpHubOrigin());
|
|
116
|
+
url.searchParams.set('ticket', ticket);
|
|
117
|
+
if (returnUrl) {
|
|
118
|
+
url.searchParams.set('return', returnUrl);
|
|
119
|
+
}
|
|
120
|
+
return url.toString();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** @deprecated Use {@link isOfficialWebOrigin}. */
|
|
124
|
+
export const isAllowedDeviceJoinOrigin = isOfficialWebOrigin;
|