@workos/authkit-session 0.0.1-alpha.3 → 0.1.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/README.md +80 -5
- package/dist/core/client/types.d.ts +75 -0
- package/dist/core/client/types.d.ts.map +1 -0
- package/dist/core/client/types.js +2 -0
- package/dist/core/client/types.js.map +1 -0
- package/dist/core/client/workos.d.ts +11 -0
- package/dist/core/client/workos.d.ts.map +1 -0
- package/dist/core/client/workos.js +33 -0
- package/dist/core/client/workos.js.map +1 -0
- package/dist/core/config/ConfigurationProvider.d.ts +43 -0
- package/dist/core/config/ConfigurationProvider.d.ts.map +1 -0
- package/dist/core/config/ConfigurationProvider.js +171 -0
- package/dist/core/config/ConfigurationProvider.js.map +1 -0
- package/dist/core/config/types.d.ts +62 -0
- package/dist/core/config/types.d.ts.map +1 -0
- package/dist/core/config/types.js +2 -0
- package/dist/core/config/types.js.map +1 -0
- package/dist/core/config.d.ts +69 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +49 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/createAuthKitFactory.d.ts +64 -0
- package/dist/core/createAuthKitFactory.d.ts.map +1 -0
- package/dist/core/createAuthKitFactory.js +95 -0
- package/dist/core/createAuthKitFactory.js.map +1 -0
- package/dist/core/encryption/ironSessionEncryption.d.ts +16 -0
- package/dist/core/encryption/ironSessionEncryption.d.ts.map +1 -0
- package/dist/core/encryption/ironSessionEncryption.js +6 -0
- package/dist/core/encryption/ironSessionEncryption.js.map +1 -0
- package/dist/core/encryption/ironWebcryptoEncryption.d.ts +19 -0
- package/dist/core/encryption/ironWebcryptoEncryption.d.ts.map +1 -0
- package/dist/core/encryption/ironWebcryptoEncryption.js +84 -0
- package/dist/core/encryption/ironWebcryptoEncryption.js.map +1 -0
- package/dist/core/encryption/pureWebcryptoEncryption.d.ts +29 -0
- package/dist/core/encryption/pureWebcryptoEncryption.d.ts.map +1 -0
- package/dist/core/encryption/pureWebcryptoEncryption.js +182 -0
- package/dist/core/encryption/pureWebcryptoEncryption.js.map +1 -0
- package/dist/core/errors.d.ts +14 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +28 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/session/CookieSessionStorage.d.ts +33 -0
- package/dist/core/session/CookieSessionStorage.d.ts.map +1 -0
- package/dist/core/session/CookieSessionStorage.js +52 -0
- package/dist/core/session/CookieSessionStorage.js.map +1 -0
- package/dist/core/session/SessionManager.d.ts +51 -0
- package/dist/core/session/SessionManager.d.ts.map +1 -0
- package/dist/core/session/SessionManager.js +238 -0
- package/dist/core/session/SessionManager.js.map +1 -0
- package/dist/core/session/TokenManager.d.ts +14 -0
- package/dist/core/session/TokenManager.d.ts.map +1 -0
- package/dist/core/session/TokenManager.js +43 -0
- package/dist/core/session/TokenManager.js.map +1 -0
- package/dist/core/session/types.d.ts +101 -0
- package/dist/core/session/types.d.ts.map +1 -0
- package/dist/core/session/types.js +2 -0
- package/dist/core/session/types.js.map +1 -0
- package/dist/index.d.ts +10 -406
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -923
- package/dist/index.js.map +1 -1
- package/dist/utils.d.ts +9 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +19 -0
- package/dist/utils.js.map +1 -0
- package/package.json +19 -11
- package/src/core/client/types.ts +2 -7
- package/src/core/client/workos.ts +5 -5
- package/src/core/config/ConfigurationProvider.spec.ts +201 -0
- package/src/core/config/ConfigurationProvider.ts +75 -7
- package/src/core/config.spec.ts +160 -0
- package/src/core/config.ts +29 -3
- package/src/core/createAuthKitFactory.spec.ts +161 -0
- package/src/core/createAuthKitFactory.ts +28 -23
- package/src/core/encryption/ironWebcryptoEncryption.spec.ts +129 -0
- package/src/core/encryption/ironWebcryptoEncryption.ts +28 -23
- package/src/core/encryption/pureWebcryptoEncryption.spec.ts +177 -0
- package/src/core/encryption/pureWebcryptoEncryption.ts +339 -0
- package/src/core/errors.spec.ts +130 -0
- package/src/core/session/CookieSessionStorage.spec.ts +136 -0
- package/src/core/session/CookieSessionStorage.ts +40 -7
- package/src/core/session/SessionManager.spec.ts +357 -0
- package/src/core/session/SessionManager.ts +71 -72
- package/src/core/session/TokenManager.spec.ts +132 -0
- package/src/core/session/TokenManager.ts +8 -6
- package/src/core/session/types.ts +39 -3
- package/src/index.ts +20 -8
- package/src/utils.spec.ts +12 -0
- package/dist/index.cjs +0 -924
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -406
- package/src/HttpClient.ts +0 -160
- package/src/adapters/chrome/ChromeExtensionStorage.ts +0 -34
- package/src/core/UserManagement.ts +0 -8
- package/src/core/client/WorkOSLite.ts +0 -284
- package/src/core/iron.ts +0 -103
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
configure,
|
|
4
|
+
getConfig,
|
|
5
|
+
getConfigurationProvider,
|
|
6
|
+
getFullConfig,
|
|
7
|
+
validateConfig,
|
|
8
|
+
} from './config.js';
|
|
9
|
+
import { ConfigurationProvider } from './config/ConfigurationProvider.js';
|
|
10
|
+
|
|
11
|
+
describe('config', () => {
|
|
12
|
+
let originalProvider: ConfigurationProvider;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
// Store reference to reset later
|
|
16
|
+
originalProvider = getConfigurationProvider();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
// Reset the provider's internal state
|
|
21
|
+
originalProvider['config'] = {
|
|
22
|
+
cookieName: 'wos-session',
|
|
23
|
+
apiHttps: true,
|
|
24
|
+
cookieMaxAge: 60 * 60 * 24 * 400,
|
|
25
|
+
apiHostname: 'api.workos.com',
|
|
26
|
+
};
|
|
27
|
+
originalProvider['valueSource'] = () => undefined;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('configure()', () => {
|
|
31
|
+
it('accepts config object', () => {
|
|
32
|
+
const config = { clientId: 'test-client-id' };
|
|
33
|
+
|
|
34
|
+
expect(() => configure(config)).not.toThrow();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('accepts value source function', () => {
|
|
38
|
+
const source = vi.fn().mockReturnValue('test-value');
|
|
39
|
+
|
|
40
|
+
expect(() => configure(source)).not.toThrow();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('accepts config and source together', () => {
|
|
44
|
+
const config = { clientId: 'test-client' };
|
|
45
|
+
const source = vi.fn();
|
|
46
|
+
|
|
47
|
+
expect(() => configure(config, source)).not.toThrow();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('accepts short cookie password (validation happens in validateConfig)', () => {
|
|
51
|
+
const config = { cookiePassword: 'short' };
|
|
52
|
+
|
|
53
|
+
expect(() => configure(config)).not.toThrow();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('getConfig()', () => {
|
|
58
|
+
it('returns configured values', () => {
|
|
59
|
+
const clientId = 'test-client-configured';
|
|
60
|
+
const validPassword = 'a'.repeat(32);
|
|
61
|
+
configure({ clientId, cookiePassword: validPassword });
|
|
62
|
+
|
|
63
|
+
expect(getConfig('clientId')).toBe(clientId);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('throws for missing required config', () => {
|
|
67
|
+
expect(() => getConfig('clientId')).toThrow(
|
|
68
|
+
'Missing required configuration value for clientId',
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('returns defaults for optional config', () => {
|
|
73
|
+
expect(getConfig('cookieName')).toBe('wos-session');
|
|
74
|
+
expect(getConfig('apiHttps')).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('prefers environment over config', () => {
|
|
78
|
+
const envValue = 'env-client-id';
|
|
79
|
+
const validPassword = 'a'.repeat(32);
|
|
80
|
+
configure(
|
|
81
|
+
{ clientId: 'config-client-id', cookiePassword: validPassword },
|
|
82
|
+
() => envValue,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
expect(getConfig('clientId')).toBe(envValue);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('getConfigurationProvider()', () => {
|
|
90
|
+
it('returns same instance', () => {
|
|
91
|
+
const provider1 = getConfigurationProvider();
|
|
92
|
+
const provider2 = getConfigurationProvider();
|
|
93
|
+
|
|
94
|
+
expect(provider1).toBe(provider2);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('getFullConfig()', () => {
|
|
99
|
+
it('returns config object', () => {
|
|
100
|
+
const validPassword = 'a'.repeat(32);
|
|
101
|
+
configure({
|
|
102
|
+
clientId: 'test-client',
|
|
103
|
+
apiKey: 'test-api-key',
|
|
104
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
105
|
+
cookiePassword: validPassword,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const config = getFullConfig();
|
|
109
|
+
expect(config).toMatchObject({ clientId: 'test-client' });
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('validateConfig()', () => {
|
|
114
|
+
it('passes with all required config', () => {
|
|
115
|
+
const validPassword = 'a'.repeat(32);
|
|
116
|
+
configure({
|
|
117
|
+
clientId: 'test-client',
|
|
118
|
+
apiKey: 'test-api-key',
|
|
119
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
120
|
+
cookiePassword: validPassword,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(() => validateConfig()).not.toThrow();
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('throws with batch of missing fields', () => {
|
|
127
|
+
expect(() => validateConfig()).toThrow(
|
|
128
|
+
/AuthKit configuration error\. Missing or invalid environment variables/,
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('shows all missing fields at once', () => {
|
|
133
|
+
expect(() => validateConfig()).toThrow(/WORKOS_CLIENT_ID is required/);
|
|
134
|
+
expect(() => validateConfig()).toThrow(/WORKOS_API_KEY is required/);
|
|
135
|
+
expect(() => validateConfig()).toThrow(/WORKOS_REDIRECT_URI is required/);
|
|
136
|
+
expect(() => validateConfig()).toThrow(
|
|
137
|
+
/WORKOS_COOKIE_PASSWORD is required/,
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('throws for short cookie password', () => {
|
|
142
|
+
configure({
|
|
143
|
+
clientId: 'test-client',
|
|
144
|
+
apiKey: 'test-api-key',
|
|
145
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
146
|
+
cookiePassword: 'short',
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(() => validateConfig()).toThrow(
|
|
150
|
+
/WORKOS_COOKIE_PASSWORD must be at least 32 characters \(currently 5\)/,
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('includes dashboard link in error', () => {
|
|
155
|
+
expect(() => validateConfig()).toThrow(
|
|
156
|
+
/Get your values from the WorkOS Dashboard: https:\/\/dashboard\.workos\.com/,
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
package/src/core/config.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { once } from '../utils';
|
|
2
|
-
import { ConfigurationProvider } from './config/ConfigurationProvider';
|
|
3
|
-
import type { AuthKitConfig, ValueSource } from './config/types';
|
|
1
|
+
import { once } from '../utils.js';
|
|
2
|
+
import { ConfigurationProvider } from './config/ConfigurationProvider.js';
|
|
3
|
+
import type { AuthKitConfig, ValueSource } from './config/types.js';
|
|
4
4
|
|
|
5
5
|
const getConfigurationInstance = once(() => new ConfigurationProvider());
|
|
6
6
|
|
|
@@ -67,3 +67,29 @@ export function getConfigurationProvider(): ConfigurationProvider {
|
|
|
67
67
|
export function getFullConfig(): AuthKitConfig {
|
|
68
68
|
return getConfigurationInstance().getConfig();
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Validates that all required configuration values are present and meet requirements.
|
|
73
|
+
* Collects all validation errors before throwing to provide comprehensive feedback.
|
|
74
|
+
*
|
|
75
|
+
* This is useful to call early in your application lifecycle to fail fast with
|
|
76
|
+
* helpful error messages showing all missing/invalid configuration at once.
|
|
77
|
+
*
|
|
78
|
+
* @throws {Error} If any required configuration is missing or invalid
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { validateConfig } from '@workos/authkit-session';
|
|
83
|
+
*
|
|
84
|
+
* // Validate configuration on startup
|
|
85
|
+
* try {
|
|
86
|
+
* validateConfig();
|
|
87
|
+
* } catch (error) {
|
|
88
|
+
* console.error(error.message); // Shows all missing config at once
|
|
89
|
+
* process.exit(1);
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export function validateConfig(): void {
|
|
94
|
+
return getConfigurationInstance().validate();
|
|
95
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { createAuthKitFactory } from './createAuthKitFactory.js';
|
|
2
|
+
import { configure } from './config.js';
|
|
3
|
+
|
|
4
|
+
// Mock session storage factory
|
|
5
|
+
const mockSessionStorageFactory = () => ({
|
|
6
|
+
getSession: async () => null,
|
|
7
|
+
saveSession: async (response: any) => response,
|
|
8
|
+
clearSession: async (response: any) => response,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('createAuthKitFactory', () => {
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
// Configure required values for tests
|
|
14
|
+
configure({
|
|
15
|
+
clientId: 'test-client-id',
|
|
16
|
+
apiKey: 'test-api-key',
|
|
17
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
18
|
+
cookiePassword: 'test-password-that-is-32-chars-long!!',
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('factory function', () => {
|
|
23
|
+
it('creates factory with required session storage', () => {
|
|
24
|
+
const factory = createAuthKitFactory({
|
|
25
|
+
sessionStorageFactory: mockSessionStorageFactory,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
expect(typeof factory).toBe('object');
|
|
29
|
+
expect(typeof factory.withAuth).toBe('function');
|
|
30
|
+
expect(typeof factory.getSignInUrl).toBe('function');
|
|
31
|
+
expect(typeof factory.getSignUpUrl).toBe('function');
|
|
32
|
+
expect(typeof factory.handleCallback).toBe('function');
|
|
33
|
+
expect(typeof factory.signOut).toBe('function');
|
|
34
|
+
expect(typeof factory.refreshSession).toBe('function');
|
|
35
|
+
expect(typeof factory.saveSession).toBe('function');
|
|
36
|
+
expect(typeof factory.terminateSession).toBe('function');
|
|
37
|
+
expect(typeof factory.switchToOrganization).toBe('function');
|
|
38
|
+
expect(typeof factory.getTokenClaims).toBe('function');
|
|
39
|
+
expect(typeof factory.getAuthorizationUrl).toBe('function');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns same instance on multiple calls (singleton)', () => {
|
|
43
|
+
const factory1 = createAuthKitFactory({
|
|
44
|
+
sessionStorageFactory: mockSessionStorageFactory,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const factory2 = createAuthKitFactory({
|
|
48
|
+
sessionStorageFactory: mockSessionStorageFactory,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
expect(factory1).toBe(factory2);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('accepts optional client factory', () => {
|
|
55
|
+
const factory = createAuthKitFactory({
|
|
56
|
+
sessionStorageFactory: mockSessionStorageFactory,
|
|
57
|
+
clientFactory: () => ({}) as any,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
expect(typeof factory.withAuth).toBe('function');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('accepts optional session encryption factory', () => {
|
|
64
|
+
const mockEncryptionFactory = () => ({
|
|
65
|
+
sealData: async () => 'encrypted',
|
|
66
|
+
unsealData: async <T>() => ({}) as T,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const factory = createAuthKitFactory({
|
|
70
|
+
sessionStorageFactory: mockSessionStorageFactory,
|
|
71
|
+
sessionEncryptionFactory: mockEncryptionFactory,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
expect(typeof factory.withAuth).toBe('function');
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('API surface', () => {
|
|
79
|
+
let authKit: ReturnType<typeof createAuthKitFactory>;
|
|
80
|
+
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
authKit = createAuthKitFactory({
|
|
83
|
+
sessionStorageFactory: mockSessionStorageFactory,
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('authentication methods', () => {
|
|
88
|
+
it('provides withAuth method', async () => {
|
|
89
|
+
const result = await authKit.withAuth('test-request');
|
|
90
|
+
expect(result).toHaveProperty('user');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('provides getTokenClaims method', async () => {
|
|
94
|
+
const claims = await authKit.getTokenClaims('request');
|
|
95
|
+
expect(typeof claims).toBe('object');
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe('URL generation methods', () => {
|
|
100
|
+
it('provides getSignInUrl method', async () => {
|
|
101
|
+
const url = await authKit.getSignInUrl({});
|
|
102
|
+
expect(typeof url).toBe('string');
|
|
103
|
+
expect(url).toContain('client_id=test-client-id');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('provides getSignUpUrl method', async () => {
|
|
107
|
+
const url = await authKit.getSignUpUrl({});
|
|
108
|
+
expect(typeof url).toBe('string');
|
|
109
|
+
expect(url).toContain('client_id=test-client-id');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('provides getAuthorizationUrl method', async () => {
|
|
113
|
+
const url = await authKit.getAuthorizationUrl({});
|
|
114
|
+
expect(typeof url).toBe('string');
|
|
115
|
+
expect(url).toContain('client_id=test-client-id');
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe('session management methods', () => {
|
|
120
|
+
it('provides saveSession method', async () => {
|
|
121
|
+
const result = await authKit.saveSession('response', 'session-data');
|
|
122
|
+
expect(result).toBe('response');
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('provides signOut method', async () => {
|
|
126
|
+
const result = await authKit.signOut('request', 'response');
|
|
127
|
+
expect(result).toBe('response');
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('method parameter handling', () => {
|
|
132
|
+
it('getSignInUrl accepts options', async () => {
|
|
133
|
+
const url = await authKit.getSignInUrl({
|
|
134
|
+
organizationId: 'org_123',
|
|
135
|
+
loginHint: 'user@example.com',
|
|
136
|
+
redirectUri: 'https://custom.com/callback',
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(typeof url).toBe('string');
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('getSignUpUrl accepts options', async () => {
|
|
143
|
+
const url = await authKit.getSignUpUrl({
|
|
144
|
+
organizationId: 'org_123',
|
|
145
|
+
loginHint: 'user@example.com',
|
|
146
|
+
redirectUri: 'https://custom.com/callback',
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(typeof url).toBe('string');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('signOut accepts options', async () => {
|
|
153
|
+
const result = await authKit.signOut('request', 'response', {
|
|
154
|
+
returnTo: 'https://example.com',
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
expect(result).toBe('response');
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
});
|
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import type { WorkOS } from '@workos-inc/node';
|
|
2
|
+
import { once } from '../utils.js';
|
|
3
|
+
import { getWorkOS } from './client/workos.js';
|
|
4
|
+
import {
|
|
5
|
+
getConfig,
|
|
6
|
+
getConfigurationProvider,
|
|
7
|
+
getFullConfig,
|
|
8
|
+
} from './config.js';
|
|
9
|
+
import type { AuthKitConfig } from './config/types.js';
|
|
10
|
+
import sessionEncryption from './encryption/ironWebcryptoEncryption.js';
|
|
11
|
+
import { SessionManager } from './session/SessionManager.js';
|
|
12
|
+
import TokenManager from './session/TokenManager.js';
|
|
9
13
|
import type {
|
|
14
|
+
AuthUrlOptions,
|
|
10
15
|
BaseTokenClaims,
|
|
11
16
|
CustomClaims,
|
|
12
17
|
SessionEncryption,
|
|
13
18
|
SessionStorage,
|
|
14
|
-
} from './session/types';
|
|
19
|
+
} from './session/types.js';
|
|
15
20
|
|
|
16
21
|
export const createAuthKitFactory = once(function createAuthKit<
|
|
17
22
|
TRequest,
|
|
@@ -21,7 +26,7 @@ export const createAuthKitFactory = once(function createAuthKit<
|
|
|
21
26
|
config: AuthKitConfig,
|
|
22
27
|
) => SessionStorage<TRequest, TResponse>;
|
|
23
28
|
sessionEncryptionFactory?: (confg: AuthKitConfig) => SessionEncryption;
|
|
24
|
-
clientFactory?: (config: AuthKitConfig) =>
|
|
29
|
+
clientFactory?: (config: AuthKitConfig) => WorkOS;
|
|
25
30
|
}) {
|
|
26
31
|
const {
|
|
27
32
|
sessionStorageFactory,
|
|
@@ -64,26 +69,25 @@ export const createAuthKitFactory = once(function createAuthKit<
|
|
|
64
69
|
...args: Parameters<SessionStorage<TRequest, TResponse>['saveSession']>
|
|
65
70
|
) => sessionStorageFactory(getFullConfig()).saveSession(...args),
|
|
66
71
|
|
|
67
|
-
getSignInUrl: (options
|
|
68
|
-
organizationId?: string;
|
|
69
|
-
loginHint?: string;
|
|
70
|
-
redirectUri?: string;
|
|
71
|
-
}) =>
|
|
72
|
+
getSignInUrl: (options?: AuthUrlOptions) =>
|
|
72
73
|
getSessionManager().getAuthorizationUrl({
|
|
73
74
|
...options,
|
|
74
75
|
screenHint: 'sign-in',
|
|
75
76
|
}),
|
|
76
77
|
|
|
77
|
-
getSignUpUrl: (options
|
|
78
|
-
organizationId?: string;
|
|
79
|
-
loginHint?: string;
|
|
80
|
-
redirectUri?: string;
|
|
81
|
-
}) =>
|
|
78
|
+
getSignUpUrl: (options?: AuthUrlOptions) =>
|
|
82
79
|
getSessionManager().getAuthorizationUrl({
|
|
83
80
|
...options,
|
|
84
81
|
screenHint: 'sign-up',
|
|
85
82
|
}),
|
|
86
83
|
|
|
84
|
+
// Expose getWorkOS for direct client access
|
|
85
|
+
getWorkOS: () => clientFactory(getFullConfig()),
|
|
86
|
+
|
|
87
|
+
// Expose SessionManager's getSession directly
|
|
88
|
+
getSession: (request: TRequest) => getSessionManager().getSession(request),
|
|
89
|
+
|
|
90
|
+
// terminateSession uses the standard SessionManager signature
|
|
87
91
|
terminateSession: (
|
|
88
92
|
...args: Parameters<(typeof SessionManager.prototype)['terminateSession']>
|
|
89
93
|
) => getSessionManager().terminateSession(...args),
|
|
@@ -130,17 +134,18 @@ export const createAuthKitFactory = once(function createAuthKit<
|
|
|
130
134
|
) => getSessionManager().switchToOrganization(...args),
|
|
131
135
|
|
|
132
136
|
handleCallback: async (
|
|
133
|
-
|
|
137
|
+
_request: TRequest,
|
|
134
138
|
response: TResponse,
|
|
135
139
|
options: { code: string; state?: string },
|
|
136
140
|
) => {
|
|
137
|
-
const client = clientFactory(getFullConfig());
|
|
138
141
|
const config = getFullConfig();
|
|
142
|
+
const clientId = config.clientId;
|
|
143
|
+
const client = clientFactory(config);
|
|
139
144
|
|
|
140
145
|
// Authenticate with the OAuth code
|
|
141
146
|
const authResponse = await client.userManagement.authenticateWithCode({
|
|
142
147
|
code: options.code,
|
|
143
|
-
clientId
|
|
148
|
+
clientId,
|
|
144
149
|
});
|
|
145
150
|
|
|
146
151
|
// Create session using SessionManager
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sealData as ironSessionSeal,
|
|
3
|
+
unsealData as ironSessionUnseal,
|
|
4
|
+
} from 'iron-session';
|
|
5
|
+
import { SessionEncryption } from './ironWebcryptoEncryption.js';
|
|
6
|
+
import ironSessionEncryption from './ironSessionEncryption.js';
|
|
7
|
+
|
|
8
|
+
const testPassword = 'this-is-a-test-password-that-is-32-characters-long!';
|
|
9
|
+
const testData = {
|
|
10
|
+
userId: '123',
|
|
11
|
+
email: 'test@example.com',
|
|
12
|
+
timestamp: Date.now(),
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
describe('ironWebcryptoEncryption', () => {
|
|
16
|
+
const encryption = new SessionEncryption();
|
|
17
|
+
|
|
18
|
+
describe('cross-compatibility with iron-session', () => {
|
|
19
|
+
it('can unseal data sealed by iron-session', async () => {
|
|
20
|
+
const sealed = await ironSessionSeal(testData, {
|
|
21
|
+
password: testPassword,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const unsealed = await encryption.unsealData(sealed, {
|
|
25
|
+
password: testPassword,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
expect(unsealed).toEqual(testData);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('produces data that iron-session can unseal', async () => {
|
|
32
|
+
const sealed = await encryption.sealData(testData, {
|
|
33
|
+
password: testPassword,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const unsealed = await ironSessionUnseal(sealed, {
|
|
37
|
+
password: testPassword,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(unsealed).toEqual(testData);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('handles version 2 tokens correctly', async () => {
|
|
44
|
+
const sealed = await encryption.sealData(testData, {
|
|
45
|
+
password: testPassword,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(sealed).toMatch(/~2$/);
|
|
49
|
+
|
|
50
|
+
const unsealed = await encryption.unsealData(sealed, {
|
|
51
|
+
password: testPassword,
|
|
52
|
+
});
|
|
53
|
+
expect(unsealed).toEqual(testData);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('handles legacy tokens without version', async () => {
|
|
57
|
+
const legacySealed = await ironSessionSeal(testData, {
|
|
58
|
+
password: testPassword,
|
|
59
|
+
});
|
|
60
|
+
const sealWithoutVersion = legacySealed.split('~')[0]!;
|
|
61
|
+
|
|
62
|
+
const unsealed = await encryption.unsealData(sealWithoutVersion, {
|
|
63
|
+
password: testPassword,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(unsealed).toEqual(testData);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe('basic functionality', () => {
|
|
71
|
+
it('seals and unseals data correctly', async () => {
|
|
72
|
+
const sealed = await encryption.sealData(testData, {
|
|
73
|
+
password: testPassword,
|
|
74
|
+
});
|
|
75
|
+
const unsealed = await encryption.unsealData(sealed, {
|
|
76
|
+
password: testPassword,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
expect(unsealed).toEqual(testData);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('handles TTL parameter', async () => {
|
|
83
|
+
const sealed = await encryption.sealData(testData, {
|
|
84
|
+
password: testPassword,
|
|
85
|
+
ttl: 3600,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(sealed).toBeDefined();
|
|
89
|
+
expect(typeof sealed).toBe('string');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('fails with wrong password', async () => {
|
|
93
|
+
const sealed = await encryption.sealData(testData, {
|
|
94
|
+
password: testPassword,
|
|
95
|
+
});
|
|
96
|
+
const wrongPassword = 'wrong-password-that-is-32-chars!!';
|
|
97
|
+
|
|
98
|
+
await expect(
|
|
99
|
+
encryption.unsealData(sealed, { password: wrongPassword }),
|
|
100
|
+
).rejects.toThrow();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('compatibility with ironSessionEncryption export', () => {
|
|
105
|
+
it('can unseal data sealed by ironSessionEncryption', async () => {
|
|
106
|
+
const sealed = await ironSessionEncryption.sealData(testData, {
|
|
107
|
+
password: testPassword,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const unsealed = await encryption.unsealData(sealed, {
|
|
111
|
+
password: testPassword,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
expect(unsealed).toEqual(testData);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('produces data that ironSessionEncryption can unseal', async () => {
|
|
118
|
+
const sealed = await encryption.sealData(testData, {
|
|
119
|
+
password: testPassword,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const unsealed = await ironSessionEncryption.unsealData(sealed, {
|
|
123
|
+
password: testPassword,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
expect(unsealed).toEqual(testData);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import { seal as sealData, unseal as unsealData } from 'iron-webcrypto';
|
|
2
|
-
import type { SessionEncryption as SessionEncryptionInterface } from '../session/types';
|
|
2
|
+
import type { SessionEncryption as SessionEncryptionInterface } from '../session/types.js';
|
|
3
|
+
|
|
4
|
+
// Extract the precise Crypto type that iron-webcrypto expects
|
|
5
|
+
// This avoids importing iron-webcrypto's private _Crypto type
|
|
6
|
+
type IronCrypto = Parameters<typeof sealData>[0];
|
|
7
|
+
|
|
8
|
+
// Cast globalThis.crypto to iron-webcrypto's expected type
|
|
9
|
+
// Runtime-safe: DOM Crypto is fully compatible with iron-webcrypto's _Crypto
|
|
10
|
+
// Type-level workaround: iron-webcrypto's _Crypto expects Uint8Array<ArrayBuffer>
|
|
11
|
+
// while DOM Crypto provides Uint8Array<ArrayBufferLike>
|
|
12
|
+
const ironCrypto: IronCrypto = globalThis.crypto as unknown as IronCrypto;
|
|
3
13
|
|
|
4
14
|
/**
|
|
5
15
|
* A compatible implementation that works with iron-session
|
|
@@ -33,7 +43,7 @@ export class SessionEncryption implements SessionEncryptionInterface {
|
|
|
33
43
|
};
|
|
34
44
|
|
|
35
45
|
// Seal the data using iron-webcrypto with properly formatted password
|
|
36
|
-
const seal = await sealData(
|
|
46
|
+
const seal = await sealData(ironCrypto, data, passwordObj, {
|
|
37
47
|
encryption: {
|
|
38
48
|
saltBits: 256,
|
|
39
49
|
algorithm: 'aes-256-cbc',
|
|
@@ -67,28 +77,23 @@ export class SessionEncryption implements SessionEncryptionInterface {
|
|
|
67
77
|
const passwordMap = { 1: password };
|
|
68
78
|
|
|
69
79
|
// Use iron-webcrypto's unseal function
|
|
70
|
-
const data = await unsealData(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
saltBits: 256,
|
|
77
|
-
algorithm: 'aes-256-cbc',
|
|
78
|
-
iterations: 1,
|
|
79
|
-
minPasswordlength: 32,
|
|
80
|
-
},
|
|
81
|
-
integrity: {
|
|
82
|
-
saltBits: 256,
|
|
83
|
-
algorithm: 'sha256',
|
|
84
|
-
iterations: 1,
|
|
85
|
-
minPasswordlength: 32,
|
|
86
|
-
},
|
|
87
|
-
ttl: 0,
|
|
88
|
-
timestampSkewSec: 60,
|
|
89
|
-
localtimeOffsetMsec: 0,
|
|
80
|
+
const data = await unsealData(ironCrypto, sealWithoutVersion, passwordMap, {
|
|
81
|
+
encryption: {
|
|
82
|
+
saltBits: 256,
|
|
83
|
+
algorithm: 'aes-256-cbc',
|
|
84
|
+
iterations: 1,
|
|
85
|
+
minPasswordlength: 32,
|
|
90
86
|
},
|
|
91
|
-
|
|
87
|
+
integrity: {
|
|
88
|
+
saltBits: 256,
|
|
89
|
+
algorithm: 'sha256',
|
|
90
|
+
iterations: 1,
|
|
91
|
+
minPasswordlength: 32,
|
|
92
|
+
},
|
|
93
|
+
ttl: 0,
|
|
94
|
+
timestampSkewSec: 60,
|
|
95
|
+
localtimeOffsetMsec: 0,
|
|
96
|
+
});
|
|
92
97
|
|
|
93
98
|
// Check the token version if needed
|
|
94
99
|
if (tokenVersion === 2) {
|