@baukit/auth-web 0.18.2 → 0.19.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.
Files changed (59) hide show
  1. package/README.md +78 -33
  2. package/dist/browser-session-client.d.ts +85 -0
  3. package/dist/browser-session-client.d.ts.map +1 -0
  4. package/dist/browser-session-client.js +339 -0
  5. package/dist/browser-session-client.js.map +1 -0
  6. package/dist/browser-session-support.d.ts +44 -0
  7. package/dist/browser-session-support.d.ts.map +1 -0
  8. package/dist/browser-session-support.js +102 -0
  9. package/dist/browser-session-support.js.map +1 -0
  10. package/dist/clerk.d.ts +1 -1
  11. package/dist/clerk.d.ts.map +1 -1
  12. package/dist/clerk.js.map +1 -1
  13. package/dist/index.d.ts +5 -95
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +3 -466
  16. package/dist/index.js.map +1 -1
  17. package/dist/oidc-client.d.ts +67 -0
  18. package/dist/oidc-client.d.ts.map +1 -0
  19. package/dist/oidc-client.js +135 -0
  20. package/dist/oidc-client.js.map +1 -0
  21. package/dist/oidc-errors.d.ts +15 -0
  22. package/dist/oidc-errors.d.ts.map +1 -0
  23. package/dist/oidc-errors.js +30 -0
  24. package/dist/oidc-errors.js.map +1 -0
  25. package/dist/oidc-helpers.d.ts +1 -1
  26. package/dist/oidc-helpers.d.ts.map +1 -1
  27. package/dist/oidc-validation.d.ts +15 -0
  28. package/dist/oidc-validation.d.ts.map +1 -0
  29. package/dist/oidc-validation.js +76 -0
  30. package/dist/oidc-validation.js.map +1 -0
  31. package/dist/session-events.d.ts +10 -0
  32. package/dist/session-events.d.ts.map +1 -0
  33. package/dist/session-events.js +2 -0
  34. package/dist/session-events.js.map +1 -0
  35. package/dist/silent-token-broker.d.ts.map +1 -1
  36. package/dist/silent-token-broker.js +39 -28
  37. package/dist/silent-token-broker.js.map +1 -1
  38. package/dist/workos.d.ts +1 -1
  39. package/dist/workos.d.ts.map +1 -1
  40. package/dist/workos.js +1 -1
  41. package/dist/workos.js.map +1 -1
  42. package/package.json +5 -3
  43. package/src/browser-session-client.test.ts +426 -0
  44. package/src/browser-session-client.ts +414 -0
  45. package/src/browser-session-refresh.test.ts +58 -0
  46. package/src/browser-session-storage.test.ts +113 -0
  47. package/src/browser-session-support.ts +153 -0
  48. package/src/clerk.ts +5 -1
  49. package/src/index.test.ts +98 -594
  50. package/src/index.ts +28 -663
  51. package/src/oidc-client.ts +229 -0
  52. package/src/oidc-errors.ts +46 -0
  53. package/src/oidc-helpers.ts +1 -1
  54. package/src/oidc-validation.ts +93 -0
  55. package/src/session-events.ts +12 -0
  56. package/src/silent-token-broker.ts +49 -33
  57. package/src/test-support/browser-session.ts +203 -0
  58. package/src/test-support/session-backend.ts +104 -0
  59. package/src/workos.ts +6 -6
@@ -0,0 +1,153 @@
1
+ import { isRecord } from '@baukit/type-guards';
2
+
3
+ import { browserOidcEnvironment, type OidcClientEnvironment } from './oidc-client.js';
4
+ import type { SessionExpiredReason } from './session-events.js';
5
+
6
+ /** The part of the Web Locks API the session client needs. */
7
+ export interface SessionLocks {
8
+ request<T>(name: string, callback: () => Promise<T>): Promise<T>;
9
+ }
10
+
11
+ /** The part of `BroadcastChannel` the session client needs. */
12
+ export interface SessionChannel {
13
+ postMessage(message: unknown): void;
14
+ subscribe(listener: (message: unknown) => void): void;
15
+ close(): void;
16
+ }
17
+
18
+ export interface BrowserSessionEnvironment extends OidcClientEnvironment {
19
+ /** Without locks, tabs may refresh concurrently and the backend grace window absorbs it. */
20
+ readonly locks?: SessionLocks;
21
+ readonly openChannel?: (name: string) => SessionChannel;
22
+ }
23
+
24
+ /** What the backend returns from `exchange` and `refresh`. */
25
+ export interface IssuedSession {
26
+ readonly accessToken: string;
27
+ /** Epoch milliseconds. */
28
+ readonly expiresAt: number;
29
+ readonly subject: string;
30
+ }
31
+
32
+ /** Sign-in state kept in localStorage beside a session counter. It holds no token. */
33
+ export interface AccountHint {
34
+ readonly subject: string;
35
+ /** Epoch milliseconds. */
36
+ readonly signedInAt: number;
37
+ }
38
+
39
+ export type SignedOutCause = SessionExpiredReason | 'cleared';
40
+
41
+ export type SessionMessage =
42
+ | ({ readonly type: 'session' } & IssuedSession)
43
+ | { readonly type: 'signed-out'; readonly cause: SignedOutCause };
44
+
45
+ const SIGNED_OUT_CAUSES: readonly string[] = [
46
+ 'refresh_rejected',
47
+ 'signed_out',
48
+ 'profile_erased',
49
+ 'cleared',
50
+ ];
51
+
52
+ function isSignedOutCause(value: string): value is SignedOutCause {
53
+ return SIGNED_OUT_CAUSES.includes(value);
54
+ }
55
+
56
+ export function isIssuedSession(value: unknown): value is IssuedSession {
57
+ return (
58
+ isRecord(value) &&
59
+ typeof value['accessToken'] === 'string' &&
60
+ value['accessToken'].length > 0 &&
61
+ typeof value['expiresAt'] === 'number' &&
62
+ Number.isFinite(value['expiresAt']) &&
63
+ typeof value['subject'] === 'string' &&
64
+ value['subject'].length > 0
65
+ );
66
+ }
67
+
68
+ export function readSessionMessage(value: unknown): SessionMessage | undefined {
69
+ if (!isRecord(value)) {
70
+ return undefined;
71
+ }
72
+ if (value['type'] === 'session' && isIssuedSession(value)) {
73
+ return {
74
+ type: 'session',
75
+ accessToken: value.accessToken,
76
+ expiresAt: value.expiresAt,
77
+ subject: value.subject,
78
+ };
79
+ }
80
+ const cause = value['cause'];
81
+ if (value['type'] === 'signed-out' && typeof cause === 'string') {
82
+ return isSignedOutCause(cause) ? { type: 'signed-out', cause } : undefined;
83
+ }
84
+ return undefined;
85
+ }
86
+
87
+ export function readAccountHint(raw: string | null): AccountHint | undefined {
88
+ if (raw === null) {
89
+ return undefined;
90
+ }
91
+ try {
92
+ const value: unknown = JSON.parse(raw);
93
+ if (
94
+ isRecord(value) &&
95
+ typeof value['subject'] === 'string' &&
96
+ value['subject'].length > 0 &&
97
+ typeof value['signedInAt'] === 'number' &&
98
+ Number.isFinite(value['signedInAt'])
99
+ ) {
100
+ return { subject: value['subject'], signedInAt: value['signedInAt'] };
101
+ }
102
+ } catch {
103
+ return undefined;
104
+ }
105
+ return undefined;
106
+ }
107
+
108
+ /** Accepts only http(s) navigation targets from the logout response. */
109
+ export function readEndSessionUrl(value: unknown): string | undefined {
110
+ if (!isRecord(value) || typeof value['endSessionUrl'] !== 'string') {
111
+ return undefined;
112
+ }
113
+ try {
114
+ const url = new URL(value['endSessionUrl']);
115
+ return url.protocol === 'https:' || url.protocol === 'http:' ? url.toString() : undefined;
116
+ } catch {
117
+ return undefined;
118
+ }
119
+ }
120
+
121
+ export function browserSessionEnvironment(): BrowserSessionEnvironment {
122
+ const base = browserOidcEnvironment();
123
+ const lockManager = typeof navigator === 'undefined' ? undefined : navigator.locks;
124
+ return {
125
+ ...base,
126
+ ...(lockManager === undefined
127
+ ? {}
128
+ : {
129
+ locks: {
130
+ request: <T>(name: string, callback: () => Promise<T>) =>
131
+ lockManager.request(name, callback),
132
+ },
133
+ }),
134
+ ...(typeof BroadcastChannel === 'function' ? { openChannel: openBroadcastChannel } : {}),
135
+ };
136
+ }
137
+
138
+ function openBroadcastChannel(name: string): SessionChannel {
139
+ const channel = new BroadcastChannel(name);
140
+ return {
141
+ postMessage: (message) => {
142
+ channel.postMessage(message);
143
+ },
144
+ subscribe: (listener) => {
145
+ channel.addEventListener('message', (event: MessageEvent<unknown>) => {
146
+ listener(event.data);
147
+ });
148
+ },
149
+ close: () => {
150
+ channel.close();
151
+ },
152
+ };
153
+ }
package/src/clerk.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import { Clerk } from '@clerk/clerk-js';
2
- import type { ClearSessionOptions, SessionExpiredEvent, SessionExpiredReason } from './index.js';
2
+ import type {
3
+ ClearSessionOptions,
4
+ SessionExpiredEvent,
5
+ SessionExpiredReason,
6
+ } from './session-events.js';
3
7
 
4
8
  export interface ClerkWebConfig {
5
9
  readonly publishableKey: string;