@shellui/core 0.7.0-alpha.1 → 0.7.0-alpha.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shellui/core",
3
- "version": "0.7.0-alpha.1",
3
+ "version": "0.7.0-alpha.2",
4
4
  "description": "Shellui Core - Core React application runtime",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -73,7 +73,7 @@
73
73
  "workbox-routing": "^7.4.0",
74
74
  "workbox-strategies": "^7.4.0",
75
75
  "workbox-window": "^7.4.0",
76
- "@shellui/sdk": "0.7.0-alpha.1"
76
+ "@shellui/sdk": "0.7.0-alpha.2"
77
77
  },
78
78
  "peerDependencies": {
79
79
  "react": "^18.0.0 || ^19.0.0",
@@ -5,7 +5,7 @@ import urls from '../../../constants/urls';
5
5
  import { SHELLUI_AUTH_CODE_PARAM } from '../constants/oauth';
6
6
  import {
7
7
  buildAuthUrlWithNext,
8
- buildOAuthSessionRedirectTo,
8
+ resolveOAuthSessionRedirectTo,
9
9
  captureCliCallbackFromSearch,
10
10
  hashHasOAuthTokens,
11
11
  inferAccessPendingErrorCode,
@@ -15,7 +15,7 @@ import {
15
15
  redirectCliCallbackError,
16
16
  redirectToCliCallback,
17
17
  } from '../utils';
18
- import { WEB_PKCE_FLOW_PARAM, isMagicLinkFlowId, takeWebPkceVerifier } from '../utils/webPkce';
18
+ import { WEB_PKCE_FLOW_PARAM, isMagicLinkFlowId, takeWebPkceFlow } from '../utils/webPkce';
19
19
  import {
20
20
  isSilentAuthCallbackFrame,
21
21
  publishSilentAuthCallbackResult,
@@ -231,13 +231,15 @@ export const OAuthCallbackView = () => {
231
231
 
232
232
  let cancelled = false;
233
233
  const runSessionExchange = async () => {
234
- const redirectTo = buildOAuthSessionRedirectTo(
234
+ const pkceFlowId = params.get(WEB_PKCE_FLOW_PARAM);
235
+ const pkceFlow = takeWebPkceFlow(pkceFlowId);
236
+ const codeVerifier = pkceFlow?.verifier ?? undefined;
237
+ const redirectTo = resolveOAuthSessionRedirectTo(
238
+ pkceFlow?.redirectTo,
235
239
  window.location.origin,
236
240
  location.pathname,
237
241
  location.search,
238
242
  );
239
- const pkceFlowId = params.get(WEB_PKCE_FLOW_PARAM);
240
- const codeVerifier = takeWebPkceVerifier(pkceFlowId) ?? undefined;
241
243
  if (!codeVerifier && isMagicLinkFlowId(pkceFlowId)) {
242
244
  // The magic link verifier lives only in the browser that asked for the link.
243
245
  // Do not send the code: it stays unused.
@@ -1,6 +1,9 @@
1
1
  import { describe, expect, test } from 'vitest';
2
2
  import { SHELLUI_AUTH_CODE_PARAM } from '../constants/oauth';
3
- import { buildOAuthSessionRedirectTo } from './buildOAuthSessionRedirectTo';
3
+ import {
4
+ buildOAuthSessionRedirectTo,
5
+ resolveOAuthSessionRedirectTo,
6
+ } from './buildOAuthSessionRedirectTo';
4
7
 
5
8
  describe('buildOAuthSessionRedirectTo', () => {
6
9
  test('strips shellui_auth_code and keeps other query params', () => {
@@ -20,3 +23,41 @@ describe('buildOAuthSessionRedirectTo', () => {
20
23
  ).toBe('http://127.0.0.1:8765/callback');
21
24
  });
22
25
  });
26
+
27
+ describe('resolveOAuthSessionRedirectTo', () => {
28
+ const start =
29
+ 'https://playground.shellui.com/login/callback?provider=github&shellui_pkce_flow=o0123';
30
+
31
+ test('uses the stored start value when the host added a trailing slash', () => {
32
+ expect(
33
+ resolveOAuthSessionRedirectTo(
34
+ start,
35
+ 'https://playground.shellui.com',
36
+ '/login/callback/',
37
+ '?provider=github&shellui_pkce_flow=o0123&shellui_auth_code=abc',
38
+ ),
39
+ ).toBe(start);
40
+ });
41
+
42
+ test('falls back to the current URL when nothing was stored', () => {
43
+ expect(
44
+ resolveOAuthSessionRedirectTo(
45
+ null,
46
+ 'https://playground.shellui.com',
47
+ '/login/callback/',
48
+ '?provider=github&shellui_auth_code=abc',
49
+ ),
50
+ ).toBe('https://playground.shellui.com/login/callback/?provider=github');
51
+ });
52
+
53
+ test('ignores a stored value for another origin', () => {
54
+ expect(
55
+ resolveOAuthSessionRedirectTo(
56
+ 'https://evil.test/login/callback',
57
+ 'https://playground.shellui.com',
58
+ '/login/callback',
59
+ '?shellui_auth_code=abc',
60
+ ),
61
+ ).toBe('https://playground.shellui.com/login/callback');
62
+ });
63
+ });
@@ -15,3 +15,26 @@ export function buildOAuthSessionRedirectTo(
15
15
  url.search = params.toString();
16
16
  return url.toString();
17
17
  }
18
+
19
+ /**
20
+ * Pick the `redirect_to` for the session exchange. Identity compares it exactly
21
+ * with the value sent at sign-in start, so prefer that stored value. A static
22
+ * host can add a trailing slash on the way back (GitHub Pages turns
23
+ * `/login/callback` into `/login/callback/`). Fall back to the current URL when
24
+ * nothing was stored or the stored value is for another origin.
25
+ */
26
+ export function resolveOAuthSessionRedirectTo(
27
+ storedRedirectTo: string | null | undefined,
28
+ origin: string,
29
+ pathname: string,
30
+ search: string,
31
+ ): string {
32
+ if (storedRedirectTo) {
33
+ try {
34
+ if (new URL(storedRedirectTo).origin === origin) return storedRedirectTo;
35
+ } catch {
36
+ // invalid stored value: rebuild from the current URL
37
+ }
38
+ }
39
+ return buildOAuthSessionRedirectTo(origin, pathname, search);
40
+ }
@@ -5,7 +5,10 @@ export {
5
5
  } from './clientLoginContext';
6
6
  export { buildAuthUrlWithNext } from './buildAuthUrlWithNext';
7
7
  export { getLoginCancelTarget, isPublicNavigationPage } from './loginCancelTarget';
8
- export { buildOAuthSessionRedirectTo } from './buildOAuthSessionRedirectTo';
8
+ export {
9
+ buildOAuthSessionRedirectTo,
10
+ resolveOAuthSessionRedirectTo,
11
+ } from './buildOAuthSessionRedirectTo';
9
12
  export { buildSessionFromParams } from './buildSessionFromParams';
10
13
  export {
11
14
  buildSessionFromTokenPayload,
@@ -60,6 +63,7 @@ export {
60
63
  WEB_PKCE_FLOW_PARAM,
61
64
  prepareWebPkce,
62
65
  takeWebPkceVerifier,
66
+ takeWebPkceFlow,
63
67
  clearWebPkce,
64
68
  isWebPkceErrorCode,
65
69
  isMagicLinkFlowId,
@@ -5,6 +5,7 @@ import {
5
5
  isMagicLinkFlowId,
6
6
  isWebPkceErrorCode,
7
7
  prepareWebPkce,
8
+ takeWebPkceFlow,
8
9
  takeWebPkceVerifier,
9
10
  } from './webPkce';
10
11
 
@@ -93,4 +94,26 @@ describe('webPkce', () => {
93
94
  expect(isWebPkceErrorCode('pkce_required')).toBe(true);
94
95
  expect(isWebPkceErrorCode('other')).toBe(false);
95
96
  });
97
+
98
+ it('returns the exact start redirect_to with the verifier, single use', async () => {
99
+ for (const kind of ['oauth', 'magic_link'] as const) {
100
+ const prepared = await prepareWebPkce(
101
+ 'https://app.test/login/callback?provider=github',
102
+ kind,
103
+ );
104
+ const taken = takeWebPkceFlow(prepared.flowId);
105
+ expect(taken?.redirectTo).toBe(prepared.redirectTo);
106
+ expect(taken?.verifier).toMatch(/^[A-Za-z0-9_-]{43,128}$/);
107
+ expect(takeWebPkceFlow(prepared.flowId)).toBeNull();
108
+ }
109
+ });
110
+
111
+ it('returns a null redirect_to for an entry stored without one', () => {
112
+ const id = 'o' + '0'.repeat(32);
113
+ sessionStorage.setItem(
114
+ 'shellui.auth.pkce.' + id,
115
+ JSON.stringify({ verifier: 'v'.repeat(43), createdAt: Date.now() }),
116
+ );
117
+ expect(takeWebPkceFlow(id)).toEqual({ verifier: 'v'.repeat(43), redirectTo: null });
118
+ });
96
119
  });
@@ -31,6 +31,14 @@ export type WebPkceFlowKind = 'oauth' | 'magic_link';
31
31
  interface StoredVerifier {
32
32
  verifier: string;
33
33
  createdAt: number;
34
+ /** Exact `redirect_to` sent at sign-in start. The exchange must send it back unchanged. */
35
+ redirectTo?: string;
36
+ }
37
+
38
+ export interface TakenWebPkceFlow {
39
+ verifier: string;
40
+ /** Exact `redirect_to` from sign-in start, or null when an older entry has none. */
41
+ redirectTo: string | null;
34
42
  }
35
43
 
36
44
  export interface PreparedWebPkce {
@@ -66,7 +74,7 @@ function write(s: Storage | null, key: string, value: StoredVerifier): boolean {
66
74
  }
67
75
  }
68
76
 
69
- function readAndRemove(s: Storage | null, key: string, maxAgeMs: number): string | null {
77
+ function readAndRemove(s: Storage | null, key: string, maxAgeMs: number): TakenWebPkceFlow | null {
70
78
  if (!s) return null;
71
79
  try {
72
80
  const raw = s.getItem(key);
@@ -75,7 +83,8 @@ function readAndRemove(s: Storage | null, key: string, maxAgeMs: number): string
75
83
  const parsed = JSON.parse(raw) as StoredVerifier;
76
84
  if (typeof parsed?.verifier !== 'string' || typeof parsed.createdAt !== 'number') return null;
77
85
  if (Date.now() - parsed.createdAt > maxAgeMs) return null;
78
- return parsed.verifier;
86
+ const redirectTo = typeof parsed.redirectTo === 'string' ? parsed.redirectTo : null;
87
+ return { verifier: parsed.verifier, redirectTo };
79
88
  } catch {
80
89
  return null;
81
90
  }
@@ -108,20 +117,28 @@ export async function prepareWebPkce(
108
117
  ): Promise<PreparedWebPkce> {
109
118
  const { verifier, challenge } = await createPkcePair();
110
119
  const flowId = randomFlowId(kind);
111
- const entry = { verifier, createdAt: Date.now() };
120
+ const url = new URL(redirectTo);
121
+ url.searchParams.set(WEB_PKCE_FLOW_PARAM, flowId);
122
+ const finalRedirectTo = url.toString();
123
+ // Static hosts such as GitHub Pages can redirect `/login/callback` to
124
+ // `/login/callback/`, so the callback URL is not always the one sent at start.
125
+ const entry: StoredVerifier = { verifier, createdAt: Date.now(), redirectTo: finalRedirectTo };
112
126
  if (kind === 'magic_link') {
113
127
  pruneMagicLinkVerifiers();
114
128
  write(store('local'), MAGIC_LINK_PREFIX + flowId, entry);
115
129
  } else {
116
130
  write(store('session'), SESSION_PREFIX + flowId, entry);
117
131
  }
118
- const url = new URL(redirectTo);
119
- url.searchParams.set(WEB_PKCE_FLOW_PARAM, flowId);
120
- return { flowId, challenge, redirectTo: url.toString() };
132
+ return { flowId, challenge, redirectTo: finalRedirectTo };
121
133
  }
122
134
 
123
135
  /** Read the verifier for a callback and remove every stored copy. */
124
136
  export function takeWebPkceVerifier(flowId: string | null | undefined): string | null {
137
+ return takeWebPkceFlow(flowId)?.verifier ?? null;
138
+ }
139
+
140
+ /** Read the verifier and the exact start `redirect_to` for a callback, single use. */
141
+ export function takeWebPkceFlow(flowId: string | null | undefined): TakenWebPkceFlow | null {
125
142
  const id = flowId?.trim();
126
143
  if (!id || !FLOW_ID_PATTERN.test(id)) return null;
127
144
  if (isMagicLinkFlowId(id)) {