@workos/authkit-session 0.3.4 → 0.4.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 +94 -27
- package/dist/core/AuthKitCore.d.ts +20 -0
- package/dist/core/AuthKitCore.d.ts.map +1 -1
- package/dist/core/AuthKitCore.js +36 -4
- package/dist/core/AuthKitCore.js.map +1 -1
- package/dist/core/encryption/ironWebcryptoEncryption.d.ts +2 -1
- package/dist/core/encryption/ironWebcryptoEncryption.d.ts.map +1 -1
- package/dist/core/encryption/ironWebcryptoEncryption.js +2 -2
- package/dist/core/encryption/ironWebcryptoEncryption.js.map +1 -1
- package/dist/core/errors.d.ts +9 -0
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +18 -0
- package/dist/core/errors.js.map +1 -1
- package/dist/core/pkce/cookieOptions.d.ts +36 -0
- package/dist/core/pkce/cookieOptions.d.ts.map +1 -0
- package/dist/core/pkce/cookieOptions.js +55 -0
- package/dist/core/pkce/cookieOptions.js.map +1 -0
- package/dist/core/pkce/generateAuthorizationUrl.d.ts +45 -0
- package/dist/core/pkce/generateAuthorizationUrl.d.ts.map +1 -0
- package/dist/core/pkce/generateAuthorizationUrl.js +82 -0
- package/dist/core/pkce/generateAuthorizationUrl.js.map +1 -0
- package/dist/core/pkce/state.d.ts +53 -0
- package/dist/core/pkce/state.d.ts.map +1 -0
- package/dist/core/pkce/state.js +91 -0
- package/dist/core/pkce/state.js.map +1 -0
- package/dist/core/session/CookieSessionStorage.d.ts +25 -15
- package/dist/core/session/CookieSessionStorage.d.ts.map +1 -1
- package/dist/core/session/CookieSessionStorage.js +20 -27
- package/dist/core/session/CookieSessionStorage.js.map +1 -1
- package/dist/core/session/TokenManager.js +2 -2
- package/dist/core/session/TokenManager.js.map +1 -1
- package/dist/core/session/serializeCookie.d.ts +13 -0
- package/dist/core/session/serializeCookie.d.ts.map +1 -0
- package/dist/core/session/serializeCookie.js +32 -0
- package/dist/core/session/serializeCookie.js.map +1 -0
- package/dist/core/session/types.d.ts +71 -2
- package/dist/core/session/types.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/operations/AuthOperations.d.ts +15 -13
- package/dist/operations/AuthOperations.d.ts.map +1 -1
- package/dist/operations/AuthOperations.js +22 -34
- package/dist/operations/AuthOperations.js.map +1 -1
- package/dist/service/AuthService.d.ts +58 -15
- package/dist/service/AuthService.d.ts.map +1 -1
- package/dist/service/AuthService.js +157 -71
- package/dist/service/AuthService.js.map +1 -1
- package/dist/service/factory.d.ts.map +1 -1
- package/dist/service/factory.js +4 -3
- package/dist/service/factory.js.map +1 -1
- package/dist/utils.d.ts +16 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +23 -0
- package/dist/utils.js.map +1 -1
- package/package.json +23 -20
- package/src/core/AuthKitCore.spec.ts +109 -0
- package/src/core/AuthKitCore.ts +55 -4
- package/src/core/encryption/ironWebcryptoEncryption.spec.ts +62 -0
- package/src/core/encryption/ironWebcryptoEncryption.ts +2 -2
- package/src/core/errors.spec.ts +33 -142
- package/src/core/errors.ts +21 -0
- package/src/core/pkce/cookieOptions.spec.ts +144 -0
- package/src/core/pkce/cookieOptions.ts +64 -0
- package/src/core/pkce/generateAuthorizationUrl.ts +123 -0
- package/src/core/pkce/pkce.spec.ts +166 -0
- package/src/core/pkce/state.spec.ts +216 -0
- package/src/core/pkce/state.ts +120 -0
- package/src/core/session/CookieSessionStorage.spec.ts +79 -3
- package/src/core/session/CookieSessionStorage.ts +56 -36
- package/src/core/session/TokenManager.ts +2 -2
- package/src/core/session/serializeCookie.ts +33 -0
- package/src/core/session/types.ts +79 -2
- package/src/index.ts +3 -0
- package/src/operations/AuthOperations.spec.ts +107 -62
- package/src/operations/AuthOperations.ts +34 -40
- package/src/service/AuthService.spec.ts +615 -98
- package/src/service/AuthService.ts +198 -73
- package/src/service/factory.spec.ts +4 -3
- package/src/service/factory.ts +6 -3
- package/src/utils.ts +22 -0
|
@@ -72,9 +72,57 @@ export interface Session {
|
|
|
72
72
|
impersonator?: Impersonator;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Map of HTTP response headers.
|
|
77
|
+
*
|
|
78
|
+
* `Set-Cookie` MUST be represented as `string[]` when multiple values exist
|
|
79
|
+
* — adapters must append each entry as its own header, never comma-join.
|
|
80
|
+
* A comma-joined `Set-Cookie` string is not a valid single HTTP header.
|
|
81
|
+
*/
|
|
75
82
|
export type HeadersBag = Record<string, string | string[]>;
|
|
76
83
|
|
|
77
84
|
export interface SessionStorage<TRequest, TResponse, TOptions = unknown> {
|
|
85
|
+
/**
|
|
86
|
+
* Read a named cookie from a request.
|
|
87
|
+
*
|
|
88
|
+
* MUST return the URL-decoded value (the inverse of `setCookie`'s
|
|
89
|
+
* `encodeURIComponent` on write). Returning raw on-wire bytes will break
|
|
90
|
+
* byte-comparison against the original value — notably PKCE state
|
|
91
|
+
* verification — for any seal format that contains characters
|
|
92
|
+
* `encodeURIComponent` escapes (`+`, `=`, etc.).
|
|
93
|
+
*
|
|
94
|
+
* @param request Framework-specific request object.
|
|
95
|
+
* @param name Cookie name.
|
|
96
|
+
* @returns The URL-decoded cookie value or null if absent.
|
|
97
|
+
*/
|
|
98
|
+
getCookie(request: TRequest, name: string): Promise<string | null>;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Write a named cookie to a response.
|
|
102
|
+
* @param response Framework-specific response object (or undefined to emit headers only).
|
|
103
|
+
* @param name Cookie name.
|
|
104
|
+
* @param value Cookie value (will be URL-encoded).
|
|
105
|
+
* @param options Per-call cookie options.
|
|
106
|
+
*/
|
|
107
|
+
setCookie(
|
|
108
|
+
response: TResponse | undefined,
|
|
109
|
+
name: string,
|
|
110
|
+
value: string,
|
|
111
|
+
options: CookieOptions,
|
|
112
|
+
): Promise<{ response?: TResponse; headers?: HeadersBag }>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Clear a named cookie by emitting a Set-Cookie with Max-Age=0.
|
|
116
|
+
* @param response Framework-specific response object (or undefined to emit headers only).
|
|
117
|
+
* @param name Cookie name.
|
|
118
|
+
* @param options Cookie options (must match those used at set time, especially `path`).
|
|
119
|
+
*/
|
|
120
|
+
clearCookie(
|
|
121
|
+
response: TResponse | undefined,
|
|
122
|
+
name: string,
|
|
123
|
+
options: CookieOptions,
|
|
124
|
+
): Promise<{ response?: TResponse; headers?: HeadersBag }>;
|
|
125
|
+
|
|
78
126
|
/*
|
|
79
127
|
* Extract session data from a request object
|
|
80
128
|
* @param request the framework-specific request object.
|
|
@@ -117,10 +165,26 @@ export interface SessionEncryption {
|
|
|
117
165
|
encryptedData: string,
|
|
118
166
|
options: {
|
|
119
167
|
password: string;
|
|
168
|
+
ttl?: number | undefined;
|
|
120
169
|
},
|
|
121
170
|
) => Promise<T>;
|
|
122
171
|
}
|
|
123
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Result shape returned by `createAuthorization` / `createSignIn` / `createSignUp`.
|
|
175
|
+
*
|
|
176
|
+
* The verifier cookie is written internally via `SessionStorage.setCookie` — callers
|
|
177
|
+
* only need to redirect the browser to `url` and apply any returned `headers`/`response`.
|
|
178
|
+
*/
|
|
179
|
+
export interface GetAuthorizationUrlResult {
|
|
180
|
+
url: string;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export type CreateAuthorizationResult<TResponse> = GetAuthorizationUrlResult & {
|
|
184
|
+
response?: TResponse;
|
|
185
|
+
headers?: HeadersBag;
|
|
186
|
+
};
|
|
187
|
+
|
|
124
188
|
export interface CookieOptions {
|
|
125
189
|
path?: string;
|
|
126
190
|
domain?: string;
|
|
@@ -142,12 +206,25 @@ export interface AuthUrlOptions {
|
|
|
142
206
|
organizationId?: string;
|
|
143
207
|
loginHint?: string;
|
|
144
208
|
prompt?: 'login' | 'none' | 'consent' | 'select_account';
|
|
145
|
-
/**
|
|
209
|
+
/**
|
|
210
|
+
* Custom state to pass through the OAuth flow. Returned in handleCallback.
|
|
211
|
+
*
|
|
212
|
+
* Bounded at 2048 UTF-8 bytes. The value is sealed alongside the PKCE
|
|
213
|
+
* verifier and stored in the `wos-auth-verifier` cookie; oversized values
|
|
214
|
+
* would push the cookie past the per-cookie browser limit (~4 KB) and
|
|
215
|
+
* would be silently dropped. Values over the supported limit throw
|
|
216
|
+
* `PKCEPayloadTooLargeError` at sign-in time.
|
|
217
|
+
*
|
|
218
|
+
* Note: total sealed payload size — not just `state` — drives the hard
|
|
219
|
+
* failure. Combining a large `returnPathname`, `redirectUri`, or
|
|
220
|
+
* `cookieDomain` with near-limit `state` can still overflow.
|
|
221
|
+
*/
|
|
146
222
|
state?: string;
|
|
147
223
|
}
|
|
148
224
|
|
|
149
225
|
/**
|
|
150
|
-
* Options for
|
|
226
|
+
* Options for `createAuthorization` / `createSignIn` / `createSignUp`,
|
|
227
|
+
* including the `screenHint` selector used by the sign-in/sign-up variants.
|
|
151
228
|
*/
|
|
152
229
|
export interface GetAuthorizationUrlOptions extends AuthUrlOptions {
|
|
153
230
|
screenHint?: 'sign-up' | 'sign-in';
|
package/src/index.ts
CHANGED
|
@@ -63,6 +63,9 @@ export { getWorkOS } from './core/client/workos.js';
|
|
|
63
63
|
// ============================================
|
|
64
64
|
export {
|
|
65
65
|
AuthKitError,
|
|
66
|
+
OAuthStateMismatchError,
|
|
67
|
+
PKCECookieMissingError,
|
|
68
|
+
PKCEPayloadTooLargeError,
|
|
66
69
|
SessionEncryptionError,
|
|
67
70
|
TokenValidationError,
|
|
68
71
|
TokenRefreshError,
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
import sessionEncryption from '../core/encryption/ironWebcryptoEncryption.js';
|
|
2
|
+
import { unsealState } from '../core/pkce/state.js';
|
|
1
3
|
import { AuthOperations } from './AuthOperations.js';
|
|
2
4
|
|
|
3
5
|
const mockConfig = {
|
|
4
6
|
clientId: 'test-client-id',
|
|
5
7
|
redirectUri: 'http://localhost:3000/callback',
|
|
6
8
|
cookieName: 'wos-session',
|
|
7
|
-
|
|
9
|
+
cookiePassword: 'this-is-a-test-password-that-is-32-characters-long!',
|
|
10
|
+
} as const;
|
|
8
11
|
|
|
9
12
|
const mockUser = {
|
|
10
13
|
id: 'user_123',
|
|
@@ -42,23 +45,45 @@ const mockCore = {
|
|
|
42
45
|
encryptSession: async () => 'encrypted-session-data',
|
|
43
46
|
};
|
|
44
47
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
48
|
+
function makeAuthUrlClient(capture?: { last?: Record<string, unknown> }) {
|
|
49
|
+
return {
|
|
50
|
+
pkce: {
|
|
51
|
+
generate: async () => ({
|
|
52
|
+
codeVerifier: 'generated-verifier-1234567890abcdef',
|
|
53
|
+
codeChallenge: 'generated-challenge',
|
|
54
|
+
codeChallengeMethod: 'S256',
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
userManagement: {
|
|
58
|
+
getLogoutUrl: ({ sessionId, returnTo }: any) =>
|
|
59
|
+
`https://api.workos.com/sso/logout?session_id=${sessionId}&return_to=${returnTo || ''}`,
|
|
60
|
+
getAuthorizationUrl: (opts: any) => {
|
|
61
|
+
if (capture) capture.last = opts;
|
|
62
|
+
const params = new URLSearchParams({
|
|
63
|
+
client_id: opts.clientId,
|
|
64
|
+
redirect_uri: opts.redirectUri,
|
|
65
|
+
state: opts.state ?? '',
|
|
66
|
+
screen_hint: opts.screenHint ?? '',
|
|
67
|
+
code_challenge: opts.codeChallenge ?? '',
|
|
68
|
+
code_challenge_method: opts.codeChallengeMethod ?? '',
|
|
69
|
+
});
|
|
70
|
+
return `https://api.workos.com/sso/authorize?${params.toString()}`;
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
53
75
|
|
|
54
76
|
describe('AuthOperations', () => {
|
|
55
77
|
let operations: AuthOperations;
|
|
78
|
+
let capture: { last?: Record<string, unknown> };
|
|
56
79
|
|
|
57
80
|
beforeEach(() => {
|
|
81
|
+
capture = {};
|
|
58
82
|
operations = new AuthOperations(
|
|
59
83
|
mockCore as any,
|
|
60
|
-
|
|
84
|
+
makeAuthUrlClient(capture) as any,
|
|
61
85
|
mockConfig as any,
|
|
86
|
+
sessionEncryption,
|
|
62
87
|
);
|
|
63
88
|
});
|
|
64
89
|
|
|
@@ -148,8 +173,9 @@ describe('AuthOperations', () => {
|
|
|
148
173
|
};
|
|
149
174
|
const testOps = new AuthOperations(
|
|
150
175
|
coreWithSpy as any,
|
|
151
|
-
|
|
176
|
+
makeAuthUrlClient() as any,
|
|
152
177
|
mockConfig as any,
|
|
178
|
+
sessionEncryption,
|
|
153
179
|
);
|
|
154
180
|
|
|
155
181
|
const session = {
|
|
@@ -193,8 +219,9 @@ describe('AuthOperations', () => {
|
|
|
193
219
|
};
|
|
194
220
|
const testOps = new AuthOperations(
|
|
195
221
|
coreWithSpy as any,
|
|
196
|
-
|
|
222
|
+
makeAuthUrlClient() as any,
|
|
197
223
|
mockConfig as any,
|
|
224
|
+
sessionEncryption,
|
|
198
225
|
);
|
|
199
226
|
|
|
200
227
|
const session = {
|
|
@@ -213,87 +240,105 @@ describe('AuthOperations', () => {
|
|
|
213
240
|
});
|
|
214
241
|
});
|
|
215
242
|
|
|
216
|
-
describe('
|
|
217
|
-
it('returns
|
|
218
|
-
const result = await operations.
|
|
243
|
+
describe('createAuthorization()', () => {
|
|
244
|
+
it('returns url + sealedState + cookieOptions for storage dispatch', async () => {
|
|
245
|
+
const result = await operations.createAuthorization();
|
|
219
246
|
|
|
220
|
-
expect(result).
|
|
221
|
-
expect(result).
|
|
247
|
+
expect(typeof result.url).toBe('string');
|
|
248
|
+
expect(typeof result.sealedState).toBe('string');
|
|
249
|
+
expect(result.sealedState.length).toBeGreaterThan(0);
|
|
250
|
+
expect(result.cookieOptions.maxAge).toBe(600);
|
|
251
|
+
expect(result.cookieOptions.path).toBe('/');
|
|
252
|
+
expect(result.url).toContain('client_id=test-client-id');
|
|
222
253
|
});
|
|
223
254
|
|
|
224
|
-
it('
|
|
225
|
-
const result = await operations.
|
|
226
|
-
|
|
227
|
-
|
|
255
|
+
it('passes the sealedState as the URL state param (identical string)', async () => {
|
|
256
|
+
const result = await operations.createAuthorization();
|
|
257
|
+
const urlState = decodeURIComponent(
|
|
258
|
+
new URL(result.url).searchParams.get('state') ?? '',
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
expect(urlState).toBe(result.sealedState);
|
|
262
|
+
});
|
|
228
263
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
expect(stateMatch).toBeTruthy();
|
|
264
|
+
it('includes codeChallenge + codeChallengeMethod in WorkOS URL', async () => {
|
|
265
|
+
await operations.createAuthorization();
|
|
232
266
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const standardBase64 = urlSafeState.replace(/-/g, '+').replace(/_/g, '/');
|
|
236
|
-
const decoded = JSON.parse(atob(standardBase64));
|
|
237
|
-
expect(decoded.returnPathname).toBe('/dashboard');
|
|
267
|
+
expect(capture.last?.codeChallenge).toBe('generated-challenge');
|
|
268
|
+
expect(capture.last?.codeChallengeMethod).toBe('S256');
|
|
238
269
|
});
|
|
239
270
|
|
|
240
|
-
it('
|
|
241
|
-
const result = await operations.
|
|
242
|
-
returnPathname: '/
|
|
243
|
-
state: 'my-custom-state',
|
|
271
|
+
it('seals returnPathname into the state blob', async () => {
|
|
272
|
+
const result = await operations.createAuthorization({
|
|
273
|
+
returnPathname: '/dashboard',
|
|
244
274
|
});
|
|
275
|
+
const unsealed = await unsealState(
|
|
276
|
+
sessionEncryption,
|
|
277
|
+
mockConfig.cookiePassword,
|
|
278
|
+
result.sealedState,
|
|
279
|
+
);
|
|
245
280
|
|
|
246
|
-
expect(
|
|
247
|
-
|
|
248
|
-
|
|
281
|
+
expect(unsealed.returnPathname).toBe('/dashboard');
|
|
282
|
+
expect(unsealed.codeVerifier).toBe('generated-verifier-1234567890abcdef');
|
|
283
|
+
});
|
|
249
284
|
|
|
250
|
-
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
285
|
+
it('seals customState into the state blob', async () => {
|
|
286
|
+
const result = await operations.createAuthorization({
|
|
287
|
+
state: 'my-custom-state',
|
|
288
|
+
});
|
|
289
|
+
const unsealed = await unsealState(
|
|
290
|
+
sessionEncryption,
|
|
291
|
+
mockConfig.cookiePassword,
|
|
292
|
+
result.sealedState,
|
|
293
|
+
);
|
|
255
294
|
|
|
256
|
-
|
|
257
|
-
const standardBase64 = internal!.replace(/-/g, '+').replace(/_/g, '/');
|
|
258
|
-
const decoded = JSON.parse(atob(standardBase64));
|
|
259
|
-
expect(decoded.returnPathname).toBe('/profile');
|
|
295
|
+
expect(unsealed.customState).toBe('my-custom-state');
|
|
260
296
|
});
|
|
261
297
|
|
|
262
|
-
it('
|
|
263
|
-
const result = await operations.
|
|
264
|
-
|
|
298
|
+
it('seals both returnPathname and customState together', async () => {
|
|
299
|
+
const result = await operations.createAuthorization({
|
|
300
|
+
returnPathname: '/profile',
|
|
301
|
+
state: 'custom',
|
|
265
302
|
});
|
|
303
|
+
const unsealed = await unsealState(
|
|
304
|
+
sessionEncryption,
|
|
305
|
+
mockConfig.cookiePassword,
|
|
306
|
+
result.sealedState,
|
|
307
|
+
);
|
|
266
308
|
|
|
267
|
-
expect(
|
|
268
|
-
|
|
269
|
-
expect(stateMatch).toBeTruthy();
|
|
270
|
-
|
|
271
|
-
// State should be passed through directly without internal wrapper
|
|
272
|
-
expect(stateMatch![1]).toBe('only-user-state');
|
|
309
|
+
expect(unsealed.returnPathname).toBe('/profile');
|
|
310
|
+
expect(unsealed.customState).toBe('custom');
|
|
273
311
|
});
|
|
274
312
|
|
|
275
313
|
it('includes screenHint when provided', async () => {
|
|
276
|
-
const result = await operations.
|
|
314
|
+
const result = await operations.createAuthorization({
|
|
277
315
|
screenHint: 'sign-up',
|
|
278
316
|
});
|
|
279
317
|
|
|
280
|
-
expect(result).toContain('screen_hint=sign-up');
|
|
318
|
+
expect(result.url).toContain('screen_hint=sign-up');
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it('generates a unique nonce per call (concurrent last-flow-wins)', async () => {
|
|
322
|
+
const a = await operations.createAuthorization();
|
|
323
|
+
const b = await operations.createAuthorization();
|
|
324
|
+
|
|
325
|
+
expect(a.sealedState).not.toBe(b.sealedState);
|
|
281
326
|
});
|
|
282
327
|
});
|
|
283
328
|
|
|
284
|
-
describe('
|
|
329
|
+
describe('createSignIn()', () => {
|
|
285
330
|
it('returns authorization URL with sign-in hint', async () => {
|
|
286
|
-
const result = await operations.
|
|
331
|
+
const result = await operations.createSignIn();
|
|
287
332
|
|
|
288
|
-
expect(result).toContain('screen_hint=sign-in');
|
|
333
|
+
expect(result.url).toContain('screen_hint=sign-in');
|
|
289
334
|
});
|
|
290
335
|
});
|
|
291
336
|
|
|
292
|
-
describe('
|
|
337
|
+
describe('createSignUp()', () => {
|
|
293
338
|
it('returns authorization URL with sign-up hint', async () => {
|
|
294
|
-
const result = await operations.
|
|
339
|
+
const result = await operations.createSignUp();
|
|
295
340
|
|
|
296
|
-
expect(result).toContain('screen_hint=sign-up');
|
|
341
|
+
expect(result.url).toContain('screen_hint=sign-up');
|
|
297
342
|
});
|
|
298
343
|
});
|
|
299
344
|
});
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import type { WorkOS } from '@workos-inc/node';
|
|
2
2
|
import type { AuthKitCore } from '../core/AuthKitCore.js';
|
|
3
3
|
import type { AuthKitConfig } from '../core/config/types.js';
|
|
4
|
+
import {
|
|
5
|
+
type GeneratedAuthorizationUrl,
|
|
6
|
+
generateAuthorizationUrl,
|
|
7
|
+
} from '../core/pkce/generateAuthorizationUrl.js';
|
|
4
8
|
import type {
|
|
5
9
|
AuthResult,
|
|
6
10
|
GetAuthorizationUrlOptions,
|
|
7
11
|
Session,
|
|
12
|
+
SessionEncryption,
|
|
8
13
|
} from '../core/session/types.js';
|
|
9
14
|
|
|
10
15
|
/**
|
|
@@ -23,11 +28,18 @@ export class AuthOperations {
|
|
|
23
28
|
private core: AuthKitCore;
|
|
24
29
|
private client: WorkOS;
|
|
25
30
|
private config: AuthKitConfig;
|
|
31
|
+
private encryption: SessionEncryption;
|
|
26
32
|
|
|
27
|
-
constructor(
|
|
33
|
+
constructor(
|
|
34
|
+
core: AuthKitCore,
|
|
35
|
+
client: WorkOS,
|
|
36
|
+
config: AuthKitConfig,
|
|
37
|
+
encryption: SessionEncryption,
|
|
38
|
+
) {
|
|
28
39
|
this.core = core;
|
|
29
40
|
this.client = client;
|
|
30
41
|
this.config = config;
|
|
42
|
+
this.encryption = encryption;
|
|
31
43
|
}
|
|
32
44
|
|
|
33
45
|
/**
|
|
@@ -115,63 +127,45 @@ export class AuthOperations {
|
|
|
115
127
|
}
|
|
116
128
|
|
|
117
129
|
/**
|
|
118
|
-
*
|
|
130
|
+
* Create a PKCE-bound WorkOS authorization URL.
|
|
119
131
|
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
132
|
+
* Returns the URL, the sealed state blob (to be used as both the cookie
|
|
133
|
+
* value and — already present in the URL — the OAuth `state` param), and
|
|
134
|
+
* the cookie options the verifier cookie should be written with.
|
|
135
|
+
* AuthService dispatches the write via `SessionStorage.setCookie`.
|
|
123
136
|
*
|
|
124
|
-
* @param options -
|
|
125
|
-
* @returns The authorization URL
|
|
137
|
+
* @param options - returnPathname, screenHint, custom state, redirectUri, etc.
|
|
126
138
|
*/
|
|
127
|
-
async
|
|
139
|
+
async createAuthorization(
|
|
128
140
|
options: GetAuthorizationUrlOptions = {},
|
|
129
|
-
): Promise<
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
: null;
|
|
136
|
-
|
|
137
|
-
// If both internal and custom state, combine as internal.custom
|
|
138
|
-
// Otherwise use whichever is provided
|
|
139
|
-
const state =
|
|
140
|
-
internalState && options.state
|
|
141
|
-
? `${internalState}.${options.state}`
|
|
142
|
-
: internalState || options.state || undefined;
|
|
143
|
-
|
|
144
|
-
return this.client.userManagement.getAuthorizationUrl({
|
|
145
|
-
provider: 'authkit',
|
|
146
|
-
redirectUri: options.redirectUri ?? this.config.redirectUri,
|
|
147
|
-
screenHint: options.screenHint,
|
|
148
|
-
organizationId: options.organizationId,
|
|
149
|
-
loginHint: options.loginHint,
|
|
150
|
-
prompt: options.prompt,
|
|
151
|
-
clientId: this.config.clientId,
|
|
152
|
-
state,
|
|
141
|
+
): Promise<GeneratedAuthorizationUrl> {
|
|
142
|
+
return generateAuthorizationUrl({
|
|
143
|
+
client: this.client,
|
|
144
|
+
config: this.config,
|
|
145
|
+
encryption: this.encryption,
|
|
146
|
+
options,
|
|
153
147
|
});
|
|
154
148
|
}
|
|
155
149
|
|
|
156
150
|
/**
|
|
157
|
-
* Convenience method:
|
|
151
|
+
* Convenience method: Create sign-in authorization URL.
|
|
158
152
|
*/
|
|
159
|
-
async
|
|
153
|
+
async createSignIn(
|
|
160
154
|
options: Omit<GetAuthorizationUrlOptions, 'screenHint'> = {},
|
|
161
|
-
): Promise<
|
|
162
|
-
return this.
|
|
155
|
+
): Promise<GeneratedAuthorizationUrl> {
|
|
156
|
+
return this.createAuthorization({
|
|
163
157
|
...options,
|
|
164
158
|
screenHint: 'sign-in',
|
|
165
159
|
});
|
|
166
160
|
}
|
|
167
161
|
|
|
168
162
|
/**
|
|
169
|
-
* Convenience method:
|
|
163
|
+
* Convenience method: Create sign-up authorization URL.
|
|
170
164
|
*/
|
|
171
|
-
async
|
|
165
|
+
async createSignUp(
|
|
172
166
|
options: Omit<GetAuthorizationUrlOptions, 'screenHint'> = {},
|
|
173
|
-
): Promise<
|
|
174
|
-
return this.
|
|
167
|
+
): Promise<GeneratedAuthorizationUrl> {
|
|
168
|
+
return this.createAuthorization({
|
|
175
169
|
...options,
|
|
176
170
|
screenHint: 'sign-up',
|
|
177
171
|
});
|