@workos/authkit-session 0.7.1 → 0.9.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 +40 -14
- package/dist/core/AuthKitCore.d.ts +7 -0
- package/dist/core/AuthKitCore.d.ts.map +1 -1
- package/dist/core/AuthKitCore.js +26 -2
- package/dist/core/AuthKitCore.js.map +1 -1
- package/dist/core/client/workos.d.ts +16 -2
- package/dist/core/client/workos.d.ts.map +1 -1
- package/dist/core/client/workos.js +24 -16
- package/dist/core/client/workos.js.map +1 -1
- package/dist/core/config/ConfigurationProvider.d.ts +1 -0
- package/dist/core/config/ConfigurationProvider.d.ts.map +1 -1
- package/dist/core/config/ConfigurationProvider.js +20 -3
- package/dist/core/config/ConfigurationProvider.js.map +1 -1
- package/dist/core/config/types.d.ts +44 -7
- package/dist/core/config/types.d.ts.map +1 -1
- package/dist/core/encryption/ironWebcryptoEncryption.d.ts.map +1 -1
- package/dist/core/encryption/ironWebcryptoEncryption.js +3 -0
- package/dist/core/encryption/ironWebcryptoEncryption.js.map +1 -1
- package/dist/service/AuthService.d.ts +5 -0
- package/dist/service/AuthService.d.ts.map +1 -1
- package/dist/service/AuthService.js +5 -0
- package/dist/service/AuthService.js.map +1 -1
- package/dist/utils.d.ts +3 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +10 -5
- package/dist/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/core/AuthKitCore.issuer.spec.ts +97 -0
- package/src/core/AuthKitCore.spec.ts +48 -3
- package/src/core/AuthKitCore.ts +34 -2
- package/src/core/client/workos.spec.ts +99 -0
- package/src/core/client/workos.ts +24 -20
- package/src/core/config/ConfigurationProvider.spec.ts +236 -2
- package/src/core/config/ConfigurationProvider.ts +26 -2
- package/src/core/config/types.ts +47 -8
- package/src/core/config.spec.ts +1 -1
- package/src/core/encryption/ironWebcryptoEncryption.spec.ts +12 -0
- package/src/core/encryption/ironWebcryptoEncryption.ts +3 -0
- package/src/operations/AuthOperations.spec.ts +3 -3
- package/src/service/AuthService.spec.ts +30 -0
- package/src/service/AuthService.ts +5 -0
- package/src/service/publicClient.spec.ts +278 -0
- package/src/utils.spec.ts +25 -0
- package/src/utils.ts +10 -5
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { vi } from 'vitest';
|
|
2
2
|
import { ConfigurationProvider } from './ConfigurationProvider.js';
|
|
3
|
+
import type {
|
|
4
|
+
AuthKitConfidentialConfig,
|
|
5
|
+
AuthKitConfig,
|
|
6
|
+
AuthKitPublicConfig,
|
|
7
|
+
} from './types.js';
|
|
3
8
|
|
|
4
9
|
describe('ConfigurationProvider', () => {
|
|
5
10
|
let provider: ConfigurationProvider;
|
|
@@ -78,6 +83,31 @@ describe('ConfigurationProvider', () => {
|
|
|
78
83
|
|
|
79
84
|
expect(provider.getValue('apiPort')).toBeUndefined();
|
|
80
85
|
});
|
|
86
|
+
|
|
87
|
+
it('parses a comma-separated issuer into a list', () => {
|
|
88
|
+
const source = vi
|
|
89
|
+
.fn()
|
|
90
|
+
.mockReturnValue('https://a.example.com, https://b.example.com,');
|
|
91
|
+
provider.configure(source);
|
|
92
|
+
|
|
93
|
+
expect(provider.getValue('issuer')).toEqual([
|
|
94
|
+
'https://a.example.com',
|
|
95
|
+
'https://b.example.com',
|
|
96
|
+
]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('keeps a single issuer as a string', () => {
|
|
100
|
+
const source = vi.fn().mockReturnValue('https://a.example.com');
|
|
101
|
+
provider.configure(source);
|
|
102
|
+
|
|
103
|
+
expect(provider.getValue('issuer')).toBe('https://a.example.com');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('does not split a programmatic issuer string', () => {
|
|
107
|
+
provider.configure({ issuer: 'https://a.example.com/path,a' });
|
|
108
|
+
|
|
109
|
+
expect(provider.getValue('issuer')).toBe('https://a.example.com/path,a');
|
|
110
|
+
});
|
|
81
111
|
});
|
|
82
112
|
|
|
83
113
|
describe('getEnvironmentVariableName()', () => {
|
|
@@ -91,6 +121,12 @@ describe('ConfigurationProvider', () => {
|
|
|
91
121
|
expect(provider['getEnvironmentVariableName']('cookieMaxAge')).toBe(
|
|
92
122
|
'WORKOS_COOKIE_MAX_AGE',
|
|
93
123
|
);
|
|
124
|
+
expect(provider['getEnvironmentVariableName']('cookieDomain')).toBe(
|
|
125
|
+
'WORKOS_COOKIE_DOMAIN',
|
|
126
|
+
);
|
|
127
|
+
expect(provider['getEnvironmentVariableName']('cookieSameSite')).toBe(
|
|
128
|
+
'WORKOS_COOKIE_SAME_SITE',
|
|
129
|
+
);
|
|
94
130
|
});
|
|
95
131
|
});
|
|
96
132
|
|
|
@@ -118,6 +154,50 @@ describe('ConfigurationProvider', () => {
|
|
|
118
154
|
const config = provider.getConfig();
|
|
119
155
|
expect(config.cookieName).toBe('test-cookie');
|
|
120
156
|
});
|
|
157
|
+
|
|
158
|
+
it('reads optional env vars that have no defaults without configure()', () => {
|
|
159
|
+
const validPassword = 'a'.repeat(32);
|
|
160
|
+
const source = vi.fn((key: string) => {
|
|
161
|
+
if (key === 'WORKOS_CLIENT_ID') return 'env-client';
|
|
162
|
+
if (key === 'WORKOS_API_KEY') return 'env-api-key';
|
|
163
|
+
if (key === 'WORKOS_REDIRECT_URI')
|
|
164
|
+
return 'http://localhost:3000/callback';
|
|
165
|
+
if (key === 'WORKOS_COOKIE_PASSWORD') return validPassword;
|
|
166
|
+
if (key === 'WORKOS_COOKIE_DOMAIN') return '.example.com';
|
|
167
|
+
if (key === 'WORKOS_COOKIE_SAME_SITE') return 'strict';
|
|
168
|
+
if (key === 'WORKOS_API_PORT') return '8443';
|
|
169
|
+
return undefined;
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
provider.configure(source);
|
|
173
|
+
|
|
174
|
+
const config = provider.getConfig();
|
|
175
|
+
expect(config.cookieDomain).toBe('.example.com');
|
|
176
|
+
expect(config.cookieSameSite).toBe('strict');
|
|
177
|
+
expect(config.apiPort).toBe(8443);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('includes optional keys that are only set in the value source', () => {
|
|
181
|
+
const validPassword = 'a'.repeat(32);
|
|
182
|
+
provider.configure(
|
|
183
|
+
{
|
|
184
|
+
clientId: 'test-client',
|
|
185
|
+
apiKey: 'test-api-key',
|
|
186
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
187
|
+
cookiePassword: validPassword,
|
|
188
|
+
},
|
|
189
|
+
key =>
|
|
190
|
+
key === 'WORKOS_ISSUER'
|
|
191
|
+
? 'https://a.example.com,https://b.example.com'
|
|
192
|
+
: undefined,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const config = provider.getConfig();
|
|
196
|
+
expect(config.issuer).toEqual([
|
|
197
|
+
'https://a.example.com',
|
|
198
|
+
'https://b.example.com',
|
|
199
|
+
]);
|
|
200
|
+
});
|
|
121
201
|
});
|
|
122
202
|
|
|
123
203
|
describe('validate()', () => {
|
|
@@ -135,7 +215,7 @@ describe('ConfigurationProvider', () => {
|
|
|
135
215
|
|
|
136
216
|
it('throws with all missing required fields at once', () => {
|
|
137
217
|
expect(() => provider.validate()).toThrow(
|
|
138
|
-
/AuthKit configuration error\. Missing or invalid environment variables:\n\n • WORKOS_CLIENT_ID is required\n •
|
|
218
|
+
/AuthKit configuration error\. Missing or invalid environment variables:\n\n • WORKOS_CLIENT_ID is required\n • WORKOS_REDIRECT_URI is required\n • WORKOS_COOKIE_PASSWORD is required/,
|
|
139
219
|
);
|
|
140
220
|
});
|
|
141
221
|
|
|
@@ -178,7 +258,7 @@ describe('ConfigurationProvider', () => {
|
|
|
178
258
|
});
|
|
179
259
|
|
|
180
260
|
const error = () => provider.validate();
|
|
181
|
-
expect(error).toThrow(/WORKOS_API_KEY
|
|
261
|
+
expect(error).not.toThrow(/WORKOS_API_KEY/);
|
|
182
262
|
expect(error).toThrow(/WORKOS_REDIRECT_URI is required/);
|
|
183
263
|
expect(error).toThrow(
|
|
184
264
|
/WORKOS_COOKIE_PASSWORD must be at least 32 characters/,
|
|
@@ -199,4 +279,158 @@ describe('ConfigurationProvider', () => {
|
|
|
199
279
|
expect(() => provider.validate()).not.toThrow();
|
|
200
280
|
});
|
|
201
281
|
});
|
|
282
|
+
|
|
283
|
+
describe('public client (no API key)', () => {
|
|
284
|
+
const publicEnv: Record<string, string> = {
|
|
285
|
+
WORKOS_CLIENT_ID: 'client_public',
|
|
286
|
+
WORKOS_REDIRECT_URI: 'http://localhost:3000/callback',
|
|
287
|
+
WORKOS_COOKIE_PASSWORD: 'a'.repeat(32),
|
|
288
|
+
};
|
|
289
|
+
let savedApiKey: string | undefined;
|
|
290
|
+
|
|
291
|
+
beforeEach(() => {
|
|
292
|
+
// Make sure a key in the test runner's environment can't leak in.
|
|
293
|
+
savedApiKey = process.env.WORKOS_API_KEY;
|
|
294
|
+
delete process.env.WORKOS_API_KEY;
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
afterEach(() => {
|
|
298
|
+
if (savedApiKey !== undefined) process.env.WORKOS_API_KEY = savedApiKey;
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it('resolves apiKey to undefined instead of throwing', () => {
|
|
302
|
+
expect(provider.getValue('apiKey')).toBeUndefined();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('validates without an apiKey', () => {
|
|
306
|
+
provider.setValueSource(publicEnv);
|
|
307
|
+
|
|
308
|
+
expect(() => provider.validate()).not.toThrow();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('builds a full config from an env-only value source', () => {
|
|
312
|
+
provider.setValueSource(publicEnv);
|
|
313
|
+
|
|
314
|
+
const config = provider.getConfig();
|
|
315
|
+
expect(config).not.toHaveProperty('apiKey');
|
|
316
|
+
expect(config).toMatchObject({
|
|
317
|
+
clientId: 'client_public',
|
|
318
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
319
|
+
cookiePassword: publicEnv.WORKOS_COOKIE_PASSWORD,
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('builds a full config from process.env with no WORKOS_API_KEY', () => {
|
|
324
|
+
const fresh = new ConfigurationProvider(); // default process.env source
|
|
325
|
+
const saved = { ...process.env };
|
|
326
|
+
Object.assign(process.env, publicEnv);
|
|
327
|
+
try {
|
|
328
|
+
expect(() => fresh.validate()).not.toThrow();
|
|
329
|
+
const config = fresh.getConfig();
|
|
330
|
+
expect(config.apiKey).toBeUndefined();
|
|
331
|
+
expect(config.clientId).toBe('client_public');
|
|
332
|
+
} finally {
|
|
333
|
+
for (const key of Object.keys(publicEnv)) {
|
|
334
|
+
if (saved[key] === undefined) delete process.env[key];
|
|
335
|
+
else process.env[key] = saved[key];
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it('accepts a programmatic config without apiKey', () => {
|
|
341
|
+
provider.configure({
|
|
342
|
+
clientId: 'client_public',
|
|
343
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
344
|
+
cookiePassword: 'a'.repeat(32),
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
expect(() => provider.validate()).not.toThrow();
|
|
348
|
+
expect(provider.getConfig().apiKey).toBeUndefined();
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it('still reads apiKey from an env-only source (confidential mode)', () => {
|
|
352
|
+
provider.setValueSource({ ...publicEnv, WORKOS_API_KEY: 'sk_test_env' });
|
|
353
|
+
|
|
354
|
+
expect(provider.getConfig().apiKey).toBe('sk_test_env');
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it.each([
|
|
358
|
+
['clientId', 'WORKOS_CLIENT_ID'],
|
|
359
|
+
['redirectUri', 'WORKOS_REDIRECT_URI'],
|
|
360
|
+
['cookiePassword', 'WORKOS_COOKIE_PASSWORD'],
|
|
361
|
+
] as const)('still requires %s', (key, envKey) => {
|
|
362
|
+
const rest = { ...publicEnv };
|
|
363
|
+
delete rest[envKey];
|
|
364
|
+
provider.setValueSource(rest);
|
|
365
|
+
|
|
366
|
+
expect(() => provider.getValue(key)).toThrow(
|
|
367
|
+
`Missing required configuration value for ${key} (${envKey}).`,
|
|
368
|
+
);
|
|
369
|
+
expect(() => provider.getConfig()).toThrow(envKey);
|
|
370
|
+
expect(() => provider.validate()).toThrow(`${envKey} is required`);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it('still validates the cookie password length', () => {
|
|
374
|
+
provider.setValueSource({
|
|
375
|
+
...publicEnv,
|
|
376
|
+
WORKOS_COOKIE_PASSWORD: 'short',
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
expect(() => provider.validate()).toThrow(
|
|
380
|
+
/WORKOS_COOKIE_PASSWORD must be at least 32 characters/,
|
|
381
|
+
);
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
describe('AuthKitConfig types', () => {
|
|
386
|
+
const base = {
|
|
387
|
+
clientId: 'client_123',
|
|
388
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
389
|
+
cookiePassword: 'a'.repeat(32),
|
|
390
|
+
apiHttps: true,
|
|
391
|
+
cookieMaxAge: 60,
|
|
392
|
+
cookieName: 'wos-session',
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
it('narrows on the presence of apiKey', () => {
|
|
396
|
+
const narrow = (config: AuthKitConfig) => {
|
|
397
|
+
if (config.apiKey !== undefined) {
|
|
398
|
+
expectTypeOf(config).toEqualTypeOf<AuthKitConfidentialConfig>();
|
|
399
|
+
expectTypeOf(config.apiKey).toEqualTypeOf<string>();
|
|
400
|
+
} else {
|
|
401
|
+
expectTypeOf(config).toEqualTypeOf<AuthKitPublicConfig>();
|
|
402
|
+
}
|
|
403
|
+
if ('apiKey' in config) {
|
|
404
|
+
expectTypeOf(config.apiKey).toEqualTypeOf<string | undefined>();
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
narrow({ ...base, apiKey: 'sk_test' });
|
|
408
|
+
narrow(base);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it('types a runtime-resolved apiKey as optional', () => {
|
|
412
|
+
expectTypeOf<AuthKitConfig['apiKey']>().toEqualTypeOf<
|
|
413
|
+
string | undefined
|
|
414
|
+
>();
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
it('accepts both client shapes and rejects invalid ones', () => {
|
|
418
|
+
const confidential: AuthKitConfig = { ...base, apiKey: 'sk_test' };
|
|
419
|
+
const publicConfig: AuthKitConfig = base;
|
|
420
|
+
// @ts-expect-error a confidential config must carry a key
|
|
421
|
+
const missingKey: AuthKitConfidentialConfig = base;
|
|
422
|
+
// @ts-expect-error a public config cannot carry a key
|
|
423
|
+
const keyedPublic: AuthKitPublicConfig = { ...base, apiKey: 'sk_test' };
|
|
424
|
+
const { clientId: _clientId, ...noClientId } = base;
|
|
425
|
+
// @ts-expect-error clientId is required in both modes
|
|
426
|
+
const missingClientId: AuthKitPublicConfig = noClientId;
|
|
427
|
+
expect([
|
|
428
|
+
confidential,
|
|
429
|
+
publicConfig,
|
|
430
|
+
missingKey,
|
|
431
|
+
keyedPublic,
|
|
432
|
+
missingClientId,
|
|
433
|
+
]).toHaveLength(5);
|
|
434
|
+
});
|
|
435
|
+
});
|
|
202
436
|
});
|
|
@@ -33,13 +33,22 @@ export class ConfigurationProvider {
|
|
|
33
33
|
|
|
34
34
|
private valueSource: ValueSource = defaultSource;
|
|
35
35
|
|
|
36
|
+
// `apiKey` is intentionally optional: without it AuthKit runs as a PKCE
|
|
37
|
+
// public client (sign-in only). See `AuthKitPublicConfig`.
|
|
36
38
|
private readonly requiredKeys: (keyof AuthKitConfig)[] = [
|
|
37
39
|
'clientId',
|
|
38
|
-
'apiKey',
|
|
39
40
|
'redirectUri',
|
|
40
41
|
'cookiePassword',
|
|
41
42
|
];
|
|
42
43
|
|
|
44
|
+
private readonly optionalKeys: (keyof AuthKitConfig)[] = [
|
|
45
|
+
'apiKey',
|
|
46
|
+
'apiPort',
|
|
47
|
+
'issuer',
|
|
48
|
+
'cookieSameSite',
|
|
49
|
+
'cookieDomain',
|
|
50
|
+
];
|
|
51
|
+
|
|
43
52
|
/**
|
|
44
53
|
* Convert a camelCase string to an uppercase, underscore-separated environment variable name.
|
|
45
54
|
* @param str The string to convert
|
|
@@ -79,7 +88,11 @@ export class ConfigurationProvider {
|
|
|
79
88
|
const rawValue = envValue ?? this.config[key];
|
|
80
89
|
|
|
81
90
|
if (rawValue != null) {
|
|
82
|
-
return this.convertValueType(
|
|
91
|
+
return this.convertValueType(
|
|
92
|
+
key,
|
|
93
|
+
rawValue,
|
|
94
|
+
envValue != null,
|
|
95
|
+
) as AuthKitConfig[K];
|
|
83
96
|
}
|
|
84
97
|
|
|
85
98
|
if (this.requiredKeys.includes(key)) {
|
|
@@ -108,6 +121,7 @@ export class ConfigurationProvider {
|
|
|
108
121
|
private convertValueType<K extends keyof AuthKitConfig>(
|
|
109
122
|
key: K,
|
|
110
123
|
value: unknown,
|
|
124
|
+
fromEnvironment = false,
|
|
111
125
|
): AuthKitConfig[K] | undefined {
|
|
112
126
|
if (typeof value !== 'string') {
|
|
113
127
|
return value as AuthKitConfig[K];
|
|
@@ -124,6 +138,15 @@ export class ConfigurationProvider {
|
|
|
124
138
|
return (isNaN(num) ? undefined : num) as AuthKitConfig[K];
|
|
125
139
|
}
|
|
126
140
|
|
|
141
|
+
// Handle comma-separated issuer lists (environment values only)
|
|
142
|
+
if (key === 'issuer' && fromEnvironment) {
|
|
143
|
+
const issuers = value
|
|
144
|
+
.split(',')
|
|
145
|
+
.map(issuer => issuer.trim())
|
|
146
|
+
.filter(Boolean);
|
|
147
|
+
return (issuers.length <= 1 ? issuers[0] : issuers) as AuthKitConfig[K];
|
|
148
|
+
}
|
|
149
|
+
|
|
127
150
|
return value as AuthKitConfig[K];
|
|
128
151
|
}
|
|
129
152
|
|
|
@@ -184,6 +207,7 @@ export class ConfigurationProvider {
|
|
|
184
207
|
const allKeys = new Set<keyof AuthKitConfig>([
|
|
185
208
|
...(Object.keys(this.config) as (keyof AuthKitConfig)[]),
|
|
186
209
|
...this.requiredKeys,
|
|
210
|
+
...this.optionalKeys,
|
|
187
211
|
]);
|
|
188
212
|
|
|
189
213
|
// Merge each key, with environment variables taking precedence
|
package/src/core/config/types.ts
CHANGED
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Configuration shared by both client modes.
|
|
3
3
|
*/
|
|
4
|
-
|
|
4
|
+
interface AuthKitBaseConfig {
|
|
5
5
|
/**
|
|
6
6
|
* The WorkOS Client ID
|
|
7
7
|
* Equivalent to the WORKOS_CLIENT_ID environment variable
|
|
8
8
|
*/
|
|
9
9
|
clientId: string;
|
|
10
10
|
|
|
11
|
-
/**
|
|
12
|
-
* The WorkOS API Key
|
|
13
|
-
* Equivalent to the WORKOS_API_KEY environment variable
|
|
14
|
-
*/
|
|
15
|
-
apiKey: string;
|
|
16
|
-
|
|
17
11
|
/**
|
|
18
12
|
* The redirect URI for the authentication callback
|
|
19
13
|
* Equivalent to the WORKOS_REDIRECT_URI environment variable
|
|
@@ -45,6 +39,13 @@ export interface AuthKitConfig {
|
|
|
45
39
|
*/
|
|
46
40
|
apiPort?: number;
|
|
47
41
|
|
|
42
|
+
/**
|
|
43
|
+
* The expected `iss` claim of WorkOS access tokens, or a list of accepted issuers
|
|
44
|
+
* Equivalent to the WORKOS_ISSUER environment variable (comma-separated for a list)
|
|
45
|
+
* When not set, the issuer claim is not validated
|
|
46
|
+
*/
|
|
47
|
+
issuer?: string | string[];
|
|
48
|
+
|
|
48
49
|
/**
|
|
49
50
|
* The maximum age of the session cookie in seconds
|
|
50
51
|
* Equivalent to the WORKOS_COOKIE_MAX_AGE environment variable
|
|
@@ -53,6 +54,7 @@ export interface AuthKitConfig {
|
|
|
53
54
|
|
|
54
55
|
/**
|
|
55
56
|
* The sameSite attribute for the session cookie
|
|
57
|
+
* Equivalent to the WORKOS_COOKIE_SAME_SITE environment variable
|
|
56
58
|
*/
|
|
57
59
|
cookieSameSite?: 'strict' | 'lax' | 'none';
|
|
58
60
|
|
|
@@ -65,9 +67,46 @@ export interface AuthKitConfig {
|
|
|
65
67
|
|
|
66
68
|
/**
|
|
67
69
|
* The domain for the session cookie
|
|
70
|
+
* Equivalent to the WORKOS_COOKIE_DOMAIN environment variable
|
|
68
71
|
*/
|
|
69
72
|
cookieDomain?: string;
|
|
70
73
|
}
|
|
71
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Confidential client: the server holds a WorkOS API key.
|
|
77
|
+
*
|
|
78
|
+
* The key is sent as the client secret on code exchange and refresh
|
|
79
|
+
* (alongside PKCE) and is required for WorkOS management APIs called through
|
|
80
|
+
* `getWorkOS()`, such as `organizations.*` or `userManagement.getUser`.
|
|
81
|
+
*/
|
|
82
|
+
export interface AuthKitConfidentialConfig extends AuthKitBaseConfig {
|
|
83
|
+
/**
|
|
84
|
+
* The WorkOS API Key
|
|
85
|
+
* Equivalent to the WORKOS_API_KEY environment variable
|
|
86
|
+
*/
|
|
87
|
+
apiKey: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Public client (keyless): the server holds no WorkOS API key.
|
|
92
|
+
*
|
|
93
|
+
* Sign-in, callback, session refresh, organization switching and sign-out all
|
|
94
|
+
* work; PKCE protects the code exchange and the refresh token is bound to the
|
|
95
|
+
* client ID. WorkOS management APIs are unavailable.
|
|
96
|
+
*/
|
|
97
|
+
export interface AuthKitPublicConfig extends AuthKitBaseConfig {
|
|
98
|
+
apiKey?: never;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* AuthKit Configuration Options
|
|
103
|
+
*
|
|
104
|
+
* Discriminated on the presence of `apiKey`: {@link AuthKitConfidentialConfig}
|
|
105
|
+
* when set, {@link AuthKitPublicConfig} when not. Configuration is resolved at
|
|
106
|
+
* runtime from environment variables and `configure()`, so
|
|
107
|
+
* `getConfig('apiKey')` is typed `string | undefined`.
|
|
108
|
+
*/
|
|
109
|
+
export type AuthKitConfig = AuthKitConfidentialConfig | AuthKitPublicConfig;
|
|
110
|
+
|
|
72
111
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
112
|
export type ValueSource = Record<string, any> | ((key: string) => any);
|
package/src/core/config.spec.ts
CHANGED
|
@@ -131,7 +131,7 @@ describe('config', () => {
|
|
|
131
131
|
|
|
132
132
|
it('shows all missing fields at once', () => {
|
|
133
133
|
expect(() => validateConfig()).toThrow(/WORKOS_CLIENT_ID is required/);
|
|
134
|
-
expect(() => validateConfig()).toThrow(/WORKOS_API_KEY
|
|
134
|
+
expect(() => validateConfig()).not.toThrow(/WORKOS_API_KEY/);
|
|
135
135
|
expect(() => validateConfig()).toThrow(/WORKOS_REDIRECT_URI is required/);
|
|
136
136
|
expect(() => validateConfig()).toThrow(
|
|
137
137
|
/WORKOS_COOKIE_PASSWORD is required/,
|
|
@@ -21,6 +21,18 @@ describe('ironWebcryptoEncryption', () => {
|
|
|
21
21
|
expect(unsealed).toEqual(testData);
|
|
22
22
|
});
|
|
23
23
|
|
|
24
|
+
it('seals Dates as ISO strings, like iron-session', async () => {
|
|
25
|
+
const sealed = await encryption.sealData(
|
|
26
|
+
{ createdAt: new Date('2024-01-01T00:00:00Z') },
|
|
27
|
+
{ password: testPassword },
|
|
28
|
+
);
|
|
29
|
+
const unsealed = await encryption.unsealData(sealed, {
|
|
30
|
+
password: testPassword,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
expect(unsealed).toEqual({ createdAt: '2024-01-01T00:00:00.000Z' });
|
|
34
|
+
});
|
|
35
|
+
|
|
24
36
|
it('produces version 2 tokens', async () => {
|
|
25
37
|
const sealed = await encryption.sealData(testData, {
|
|
26
38
|
password: testPassword,
|
|
@@ -49,6 +49,9 @@ export class SessionEncryption implements SessionEncryptionInterface {
|
|
|
49
49
|
ttl: ttl * 1000, // Convert seconds to milliseconds
|
|
50
50
|
timestampSkewSec: 60,
|
|
51
51
|
localtimeOffsetMsec: 0,
|
|
52
|
+
// Same as iron-session: Dates (e.g. @workos-inc/node 12's User timestamps)
|
|
53
|
+
// become ISO strings. iron-webcrypto's default encoder throws on them.
|
|
54
|
+
encode: JSON.stringify,
|
|
52
55
|
});
|
|
53
56
|
|
|
54
57
|
// Add the version delimiter exactly like iron-session does
|
|
@@ -18,9 +18,9 @@ const mockUser = {
|
|
|
18
18
|
name: 'Test User',
|
|
19
19
|
emailVerified: true,
|
|
20
20
|
profilePictureUrl: null,
|
|
21
|
-
createdAt: '2023-01-01T00:00:00Z',
|
|
22
|
-
updatedAt: '2023-01-01T00:00:00Z',
|
|
23
|
-
lastSignInAt: '2023-01-01T00:00:00Z',
|
|
21
|
+
createdAt: new Date('2023-01-01T00:00:00Z'),
|
|
22
|
+
updatedAt: new Date('2023-01-01T00:00:00Z'),
|
|
23
|
+
lastSignInAt: new Date('2023-01-01T00:00:00Z'),
|
|
24
24
|
locale: 'en-US',
|
|
25
25
|
externalId: null,
|
|
26
26
|
metadata: {},
|
|
@@ -464,6 +464,36 @@ describe('AuthService', () => {
|
|
|
464
464
|
});
|
|
465
465
|
|
|
466
466
|
describe('handleCallback()', () => {
|
|
467
|
+
it.each([
|
|
468
|
+
'x:\\evil.com',
|
|
469
|
+
'x:/\\evil.com',
|
|
470
|
+
'x:\\\\evil.com',
|
|
471
|
+
'x:\\/evil.com',
|
|
472
|
+
])(
|
|
473
|
+
'keeps the callback return path same-origin for %s',
|
|
474
|
+
async returnPathname => {
|
|
475
|
+
const realStorage = makeStorage();
|
|
476
|
+
const realService = new AuthService(
|
|
477
|
+
mockConfig as any,
|
|
478
|
+
realStorage as any,
|
|
479
|
+
makeClient() as any,
|
|
480
|
+
sessionEncryption,
|
|
481
|
+
);
|
|
482
|
+
const { cookieName } = await realService.createAuthorization('res', {
|
|
483
|
+
returnPathname,
|
|
484
|
+
});
|
|
485
|
+
const result = await realService.handleCallback('req', 'res', {
|
|
486
|
+
code: 'auth-code',
|
|
487
|
+
state: realStorage.cookies.get(cookieName)!,
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
expect(
|
|
491
|
+
new URL(result.returnPathname, mockConfig.redirectUri).origin,
|
|
492
|
+
).toBe(new URL(mockConfig.redirectUri).origin);
|
|
493
|
+
expect(result.returnPathname).toBe('/');
|
|
494
|
+
},
|
|
495
|
+
);
|
|
496
|
+
|
|
467
497
|
it('round-trips through createSignIn → handleCallback', async () => {
|
|
468
498
|
const capture: { authCall?: Record<string, unknown> } = {};
|
|
469
499
|
const realStorage = makeStorage();
|
|
@@ -325,6 +325,11 @@ export class AuthService<TRequest, TResponse> {
|
|
|
325
325
|
/**
|
|
326
326
|
* Get the WorkOS client instance.
|
|
327
327
|
* Useful for direct API calls not covered by AuthKit.
|
|
328
|
+
*
|
|
329
|
+
* WorkOS management APIs (e.g. `organizations.*`,
|
|
330
|
+
* `userManagement.getUser`) require an API key (`WORKOS_API_KEY` or
|
|
331
|
+
* `configure({ apiKey })`). In public-client (keyless) mode they throw an
|
|
332
|
+
* `ApiKeyRequiredException` before any request.
|
|
328
333
|
*/
|
|
329
334
|
getWorkOS(): WorkOS {
|
|
330
335
|
return this.client;
|