@baukit/auth-web 0.18.3 → 0.19.1
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 +78 -33
- package/dist/browser-session-client.d.ts +85 -0
- package/dist/browser-session-client.d.ts.map +1 -0
- package/dist/browser-session-client.js +339 -0
- package/dist/browser-session-client.js.map +1 -0
- package/dist/browser-session-support.d.ts +44 -0
- package/dist/browser-session-support.d.ts.map +1 -0
- package/dist/browser-session-support.js +102 -0
- package/dist/browser-session-support.js.map +1 -0
- package/dist/clerk.d.ts +1 -1
- package/dist/clerk.d.ts.map +1 -1
- package/dist/clerk.js.map +1 -1
- package/dist/index.d.ts +5 -95
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -466
- package/dist/index.js.map +1 -1
- package/dist/oidc-client.d.ts +67 -0
- package/dist/oidc-client.d.ts.map +1 -0
- package/dist/oidc-client.js +135 -0
- package/dist/oidc-client.js.map +1 -0
- package/dist/oidc-errors.d.ts +15 -0
- package/dist/oidc-errors.d.ts.map +1 -0
- package/dist/oidc-errors.js +30 -0
- package/dist/oidc-errors.js.map +1 -0
- package/dist/oidc-helpers.d.ts +1 -1
- package/dist/oidc-helpers.d.ts.map +1 -1
- package/dist/oidc-validation.d.ts +15 -0
- package/dist/oidc-validation.d.ts.map +1 -0
- package/dist/oidc-validation.js +76 -0
- package/dist/oidc-validation.js.map +1 -0
- package/dist/session-events.d.ts +10 -0
- package/dist/session-events.d.ts.map +1 -0
- package/dist/session-events.js +2 -0
- package/dist/session-events.js.map +1 -0
- package/dist/silent-token-broker.d.ts.map +1 -1
- package/dist/silent-token-broker.js +39 -28
- package/dist/silent-token-broker.js.map +1 -1
- package/dist/workos.d.ts +1 -1
- package/dist/workos.d.ts.map +1 -1
- package/dist/workos.js +1 -1
- package/dist/workos.js.map +1 -1
- package/package.json +5 -3
- package/src/browser-session-client.test.ts +426 -0
- package/src/browser-session-client.ts +414 -0
- package/src/browser-session-refresh.test.ts +58 -0
- package/src/browser-session-storage.test.ts +113 -0
- package/src/browser-session-support.ts +153 -0
- package/src/clerk.ts +5 -1
- package/src/index.test.ts +98 -594
- package/src/index.ts +28 -663
- package/src/oidc-client.ts +229 -0
- package/src/oidc-errors.ts +46 -0
- package/src/oidc-helpers.ts +1 -1
- package/src/oidc-validation.ts +93 -0
- package/src/session-events.ts +12 -0
- package/src/silent-token-broker.ts +49 -33
- package/src/test-support/browser-session.ts +203 -0
- package/src/test-support/session-backend.ts +104 -0
- package/src/workos.ts +6 -6
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
BrowserSessionEnvironment,
|
|
5
|
+
SessionChannel,
|
|
6
|
+
SessionLocks,
|
|
7
|
+
} from '../browser-session-support.js';
|
|
8
|
+
|
|
9
|
+
export class MemoryStorage implements Storage {
|
|
10
|
+
private readonly values = new Map<string, string>();
|
|
11
|
+
|
|
12
|
+
public get length(): number {
|
|
13
|
+
return this.values.size;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public clear(): void {
|
|
17
|
+
this.values.clear();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public getItem(key: string): string | null {
|
|
21
|
+
return this.values.get(key) ?? null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public key(index: number): string | null {
|
|
25
|
+
return [...this.values.keys()][index] ?? null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public removeItem(key: string): void {
|
|
29
|
+
this.values.delete(key);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public setItem(key: string, value: string): void {
|
|
33
|
+
this.values.set(key, value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Grants each named lock to one holder at a time, in request order, like Web Locks. */
|
|
38
|
+
export function fakeLocks(): SessionLocks & { readonly requests: string[] } {
|
|
39
|
+
const tails = new Map<string, Promise<unknown>>();
|
|
40
|
+
const requests: string[] = [];
|
|
41
|
+
return {
|
|
42
|
+
requests,
|
|
43
|
+
request: <T>(name: string, callback: () => Promise<T>): Promise<T> => {
|
|
44
|
+
requests.push(name);
|
|
45
|
+
const run = (tails.get(name) ?? Promise.resolve()).then(callback);
|
|
46
|
+
tails.set(
|
|
47
|
+
name,
|
|
48
|
+
run.then(
|
|
49
|
+
() => undefined,
|
|
50
|
+
() => undefined,
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
return run;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Delivers each message asynchronously to every other channel with the same name.
|
|
60
|
+
* `task` delivery runs after pending lock handoffs, like a slow BroadcastChannel.
|
|
61
|
+
*/
|
|
62
|
+
export class ChannelHub {
|
|
63
|
+
public readonly posted: unknown[] = [];
|
|
64
|
+
|
|
65
|
+
public constructor(private readonly delivery: 'microtask' | 'task' = 'microtask') {}
|
|
66
|
+
|
|
67
|
+
private readonly channels = new Set<{ name: string; listeners: ((m: unknown) => void)[] }>();
|
|
68
|
+
|
|
69
|
+
public open = (name: string): SessionChannel => {
|
|
70
|
+
const entry = { name, listeners: [] as ((m: unknown) => void)[] };
|
|
71
|
+
this.channels.add(entry);
|
|
72
|
+
return {
|
|
73
|
+
postMessage: (message) => {
|
|
74
|
+
this.posted.push(message);
|
|
75
|
+
const copy: unknown = structuredClone(message);
|
|
76
|
+
for (const other of this.channels) {
|
|
77
|
+
if (other !== entry && other.name === name) {
|
|
78
|
+
const deliver = () => {
|
|
79
|
+
for (const listener of other.listeners) listener(copy);
|
|
80
|
+
};
|
|
81
|
+
if (this.delivery === 'task') {
|
|
82
|
+
setTimeout(deliver, 0);
|
|
83
|
+
} else {
|
|
84
|
+
queueMicrotask(deliver);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
subscribe: (listener) => {
|
|
90
|
+
entry.listeners.push(listener);
|
|
91
|
+
},
|
|
92
|
+
close: () => {
|
|
93
|
+
this.channels.delete(entry);
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface Tab {
|
|
100
|
+
readonly environment: BrowserSessionEnvironment;
|
|
101
|
+
readonly sessionStorage: MemoryStorage;
|
|
102
|
+
readonly navigations: string[];
|
|
103
|
+
setCurrentUrl(url: string): void;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface Browser {
|
|
107
|
+
readonly localStorage: MemoryStorage;
|
|
108
|
+
readonly locks: ReturnType<typeof fakeLocks>;
|
|
109
|
+
readonly hub: ChannelHub;
|
|
110
|
+
readonly fetch: ReturnType<typeof vi.fn<typeof globalThis.fetch>>;
|
|
111
|
+
now(): number;
|
|
112
|
+
setNow(value: number): void;
|
|
113
|
+
tab(options?: { readonly coordinated?: boolean }): Tab;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function fakeBrowser(fetch: typeof globalThis.fetch): Browser {
|
|
117
|
+
const localStorage = new MemoryStorage();
|
|
118
|
+
const locks = fakeLocks();
|
|
119
|
+
const hub = new ChannelHub();
|
|
120
|
+
const fetchMock = vi.fn<typeof globalThis.fetch>(fetch);
|
|
121
|
+
let now = 1_000_000;
|
|
122
|
+
return {
|
|
123
|
+
localStorage,
|
|
124
|
+
locks,
|
|
125
|
+
hub,
|
|
126
|
+
fetch: fetchMock,
|
|
127
|
+
now: () => now,
|
|
128
|
+
setNow: (value) => {
|
|
129
|
+
now = value;
|
|
130
|
+
},
|
|
131
|
+
tab: ({ coordinated = true } = {}) => {
|
|
132
|
+
const sessionStorage = new MemoryStorage();
|
|
133
|
+
const navigations: string[] = [];
|
|
134
|
+
let currentUrl = 'https://app.example.test/';
|
|
135
|
+
return {
|
|
136
|
+
sessionStorage,
|
|
137
|
+
navigations,
|
|
138
|
+
setCurrentUrl: (url) => {
|
|
139
|
+
currentUrl = url;
|
|
140
|
+
},
|
|
141
|
+
environment: {
|
|
142
|
+
fetch: fetchMock,
|
|
143
|
+
crypto: globalThis.crypto,
|
|
144
|
+
localStorage,
|
|
145
|
+
sessionStorage,
|
|
146
|
+
currentUrl: () => currentUrl,
|
|
147
|
+
navigate: (url) => navigations.push(url),
|
|
148
|
+
replaceUrl: () => undefined,
|
|
149
|
+
now: () => now,
|
|
150
|
+
...(coordinated ? { locks, openChannel: hub.open } : {}),
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function jsonResponse(body: unknown, status = 200): Response {
|
|
158
|
+
return new Response(JSON.stringify(body), {
|
|
159
|
+
status,
|
|
160
|
+
headers: { 'content-type': 'application/json' },
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function problem(status: number, code: string): Response {
|
|
165
|
+
return jsonResponse({ code, message: 'problem' }, status);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function discoveryResponse(): Response {
|
|
169
|
+
return jsonResponse({
|
|
170
|
+
issuer: 'https://identity.example.test/realm',
|
|
171
|
+
authorization_endpoint: 'https://identity.example.test/realm/authorize',
|
|
172
|
+
token_endpoint: 'https://identity.example.test/realm/token',
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function requestUrl(input: RequestInfo | URL): string {
|
|
177
|
+
return input instanceof Request ? input.url : input.toString();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function required<T>(value: T | null | undefined): T {
|
|
181
|
+
if (value === undefined || value === null) {
|
|
182
|
+
throw new Error('Expected test value to be present.');
|
|
183
|
+
}
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function readLoginTransaction(
|
|
188
|
+
storage: Storage,
|
|
189
|
+
key: string,
|
|
190
|
+
): { state: string; verifier: string } {
|
|
191
|
+
const value: unknown = JSON.parse(required(storage.getItem(key)));
|
|
192
|
+
if (
|
|
193
|
+
typeof value !== 'object' ||
|
|
194
|
+
value === null ||
|
|
195
|
+
!('state' in value) ||
|
|
196
|
+
!('verifier' in value) ||
|
|
197
|
+
typeof value.state !== 'string' ||
|
|
198
|
+
typeof value.verifier !== 'string'
|
|
199
|
+
) {
|
|
200
|
+
throw new Error('Expected a login transaction.');
|
|
201
|
+
}
|
|
202
|
+
return { state: value.state, verifier: value.verifier };
|
|
203
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { expect } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { BrowserSessionClient, type BrowserSessionClientConfig } from '../index.js';
|
|
4
|
+
import {
|
|
5
|
+
discoveryResponse,
|
|
6
|
+
fakeBrowser,
|
|
7
|
+
jsonResponse,
|
|
8
|
+
requestUrl,
|
|
9
|
+
required,
|
|
10
|
+
type Browser,
|
|
11
|
+
type Tab,
|
|
12
|
+
} from './browser-session.js';
|
|
13
|
+
|
|
14
|
+
export const API = 'https://api.example.test';
|
|
15
|
+
export const ACCESS_TOKEN = 'eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ1c2VyLTEifQ.signature-one';
|
|
16
|
+
export const ROTATED_TOKEN = 'eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ1c2VyLTEifQ.signature-two';
|
|
17
|
+
export const LIFETIME_MS = 300_000;
|
|
18
|
+
|
|
19
|
+
export const CONFIG: BrowserSessionClientConfig = {
|
|
20
|
+
issuer: 'https://identity.example.test/realm',
|
|
21
|
+
clientId: 'product-web',
|
|
22
|
+
redirectUri: 'https://app.example.test/',
|
|
23
|
+
storageKeyPrefix: 'product',
|
|
24
|
+
apiBaseUrl: `${API}/`,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export interface Backend {
|
|
28
|
+
readonly fetch: typeof globalThis.fetch;
|
|
29
|
+
readonly calls: { readonly url: string; readonly init: RequestInit | undefined }[];
|
|
30
|
+
refresh: () => Response | Promise<Response>;
|
|
31
|
+
logout: () => Response;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function backend(browserNow: () => number, lifetimeMs = LIFETIME_MS): Backend {
|
|
35
|
+
const state: Backend = {
|
|
36
|
+
calls: [],
|
|
37
|
+
refresh: () =>
|
|
38
|
+
jsonResponse({
|
|
39
|
+
accessToken: ROTATED_TOKEN,
|
|
40
|
+
expiresAt: browserNow() + lifetimeMs,
|
|
41
|
+
subject: 'user-1',
|
|
42
|
+
}),
|
|
43
|
+
logout: () => new Response(null, { status: 204 }),
|
|
44
|
+
fetch: (input, init) => {
|
|
45
|
+
const url = requestUrl(input);
|
|
46
|
+
state.calls.push({ url, init });
|
|
47
|
+
if (url.endsWith('/.well-known/openid-configuration')) {
|
|
48
|
+
return Promise.resolve(discoveryResponse());
|
|
49
|
+
}
|
|
50
|
+
if (url === `${API}/auth/session/exchange`) {
|
|
51
|
+
return Promise.resolve(
|
|
52
|
+
jsonResponse({
|
|
53
|
+
accessToken: ACCESS_TOKEN,
|
|
54
|
+
expiresAt: browserNow() + lifetimeMs,
|
|
55
|
+
subject: 'user-1',
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
if (url === `${API}/auth/session/refresh`) {
|
|
60
|
+
return Promise.resolve(state.refresh());
|
|
61
|
+
}
|
|
62
|
+
if (url === `${API}/auth/session/logout`) {
|
|
63
|
+
return Promise.resolve(state.logout());
|
|
64
|
+
}
|
|
65
|
+
return Promise.resolve(new Response('{}', { status: 200 }));
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
return state;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function setup(lifetimeMs = LIFETIME_MS): {
|
|
72
|
+
browser: Browser;
|
|
73
|
+
server: Backend;
|
|
74
|
+
now: () => number;
|
|
75
|
+
} {
|
|
76
|
+
const clock = { browser: undefined as Browser | undefined };
|
|
77
|
+
const now = () => required(clock.browser).now();
|
|
78
|
+
const server = backend(now, lifetimeMs);
|
|
79
|
+
const browser = fakeBrowser((input, init) => server.fetch(input, init));
|
|
80
|
+
clock.browser = browser;
|
|
81
|
+
return { browser, server, now };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function signIn(
|
|
85
|
+
tab: Tab,
|
|
86
|
+
client = new BrowserSessionClient(CONFIG, tab.environment),
|
|
87
|
+
): Promise<BrowserSessionClient> {
|
|
88
|
+
await client.login();
|
|
89
|
+
const state = required(new URL(required(tab.navigations.at(-1))).searchParams.get('state'));
|
|
90
|
+
tab.setCurrentUrl(`https://app.example.test/?code=one-time-code&state=${state}`);
|
|
91
|
+
await expect(client.handleCallback()).resolves.toBe(true);
|
|
92
|
+
return client;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function storageEntries(storage: Storage): [string, string | null][] {
|
|
96
|
+
return Array.from({ length: storage.length }, (_, index) => {
|
|
97
|
+
const key = storage.key(index) ?? '';
|
|
98
|
+
return [key, storage.getItem(key)];
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function sessionCalls(server: Backend, endpoint: string): Backend['calls'] {
|
|
103
|
+
return server.calls.filter(({ url }) => url === `${API}/auth/session/${endpoint}`);
|
|
104
|
+
}
|
package/src/workos.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { createClient, LoginRequiredError, RefreshError } from '@workos-inc/authkit-js';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from './
|
|
2
|
+
import { OidcError } from './oidc-errors.js';
|
|
3
|
+
import type {
|
|
4
|
+
ClearSessionOptions,
|
|
5
|
+
SessionExpiredEvent,
|
|
6
|
+
SessionExpiredReason,
|
|
7
|
+
} from './session-events.js';
|
|
8
8
|
|
|
9
9
|
export interface WorkOsWebConfig {
|
|
10
10
|
readonly clientId: string;
|