@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,136 @@
|
|
|
1
|
+
import { CookieSessionStorage } from './CookieSessionStorage.js';
|
|
2
|
+
|
|
3
|
+
// Mock config provider
|
|
4
|
+
const createMockConfig = (overrides: Record<string, any> = {}) => ({
|
|
5
|
+
getValue: (key: string) => {
|
|
6
|
+
const defaults = {
|
|
7
|
+
cookieName: 'wos-session',
|
|
8
|
+
cookieSameSite: 'lax',
|
|
9
|
+
apiHttps: true,
|
|
10
|
+
cookieMaxAge: 60 * 60 * 24 * 400,
|
|
11
|
+
cookieDomain: undefined,
|
|
12
|
+
};
|
|
13
|
+
return overrides[key] ?? defaults[key as keyof typeof defaults];
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// Concrete implementation for testing the abstract class
|
|
18
|
+
class TestCookieSessionStorage extends CookieSessionStorage<string, string> {
|
|
19
|
+
async getSession(): Promise<string | null> {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('CookieSessionStorage', () => {
|
|
25
|
+
describe('constructor', () => {
|
|
26
|
+
it('sets default cookie configuration', () => {
|
|
27
|
+
const storage = new TestCookieSessionStorage(createMockConfig() as any);
|
|
28
|
+
|
|
29
|
+
expect(storage['cookieName']).toBe('wos-session');
|
|
30
|
+
expect(storage['cookieOptions']).toEqual({
|
|
31
|
+
path: '/',
|
|
32
|
+
httpOnly: true,
|
|
33
|
+
sameSite: 'lax',
|
|
34
|
+
secure: true,
|
|
35
|
+
maxAge: 60 * 60 * 24 * 400,
|
|
36
|
+
domain: undefined,
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('uses custom cookie name from config', () => {
|
|
41
|
+
const config = createMockConfig({ cookieName: 'custom-session' });
|
|
42
|
+
const storage = new TestCookieSessionStorage(config as any);
|
|
43
|
+
|
|
44
|
+
expect(storage['cookieName']).toBe('custom-session');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('falls back to default cookie name when config returns falsy', () => {
|
|
48
|
+
const config = createMockConfig({ cookieName: '' });
|
|
49
|
+
const storage = new TestCookieSessionStorage(config as any);
|
|
50
|
+
|
|
51
|
+
expect(storage['cookieName']).toBe('wos_session');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('uses custom sameSite setting', () => {
|
|
55
|
+
const config = createMockConfig({ cookieSameSite: 'strict' });
|
|
56
|
+
const storage = new TestCookieSessionStorage(config as any);
|
|
57
|
+
|
|
58
|
+
expect(storage['cookieOptions'].sameSite).toBe('strict');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('uses custom security settings', () => {
|
|
62
|
+
const config = createMockConfig({ apiHttps: false });
|
|
63
|
+
const storage = new TestCookieSessionStorage(config as any);
|
|
64
|
+
|
|
65
|
+
expect(storage['cookieOptions'].secure).toBe(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('uses custom max age', () => {
|
|
69
|
+
const customMaxAge = 60 * 60 * 24; // 1 day
|
|
70
|
+
const config = createMockConfig({ cookieMaxAge: customMaxAge });
|
|
71
|
+
const storage = new TestCookieSessionStorage(config as any);
|
|
72
|
+
|
|
73
|
+
expect(storage['cookieOptions'].maxAge).toBe(customMaxAge);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('uses default max age when config returns null', () => {
|
|
77
|
+
const config = createMockConfig({ cookieMaxAge: null });
|
|
78
|
+
const storage = new TestCookieSessionStorage(config as any);
|
|
79
|
+
|
|
80
|
+
expect(storage['cookieOptions'].maxAge).toBe(60 * 60 * 24 * 400);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('sets custom cookie domain', () => {
|
|
84
|
+
const config = createMockConfig({ cookieDomain: '.example.com' });
|
|
85
|
+
const storage = new TestCookieSessionStorage(config as any);
|
|
86
|
+
|
|
87
|
+
expect(storage['cookieOptions'].domain).toBe('.example.com');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('uses all default fallbacks', () => {
|
|
91
|
+
const config = createMockConfig({
|
|
92
|
+
cookieName: '',
|
|
93
|
+
cookieSameSite: null,
|
|
94
|
+
apiHttps: null,
|
|
95
|
+
cookieMaxAge: null,
|
|
96
|
+
cookieDomain: null,
|
|
97
|
+
});
|
|
98
|
+
const storage = new TestCookieSessionStorage(config as any);
|
|
99
|
+
|
|
100
|
+
expect(storage['cookieName']).toBe('wos_session');
|
|
101
|
+
expect(storage['cookieOptions']).toEqual({
|
|
102
|
+
path: '/',
|
|
103
|
+
httpOnly: true,
|
|
104
|
+
sameSite: 'lax',
|
|
105
|
+
secure: true,
|
|
106
|
+
maxAge: 60 * 60 * 24 * 400,
|
|
107
|
+
domain: undefined,
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('abstract methods', () => {
|
|
113
|
+
it('implements SessionStorage interface', () => {
|
|
114
|
+
const storage = new TestCookieSessionStorage(createMockConfig() as any);
|
|
115
|
+
|
|
116
|
+
expect(typeof storage.getSession).toBe('function');
|
|
117
|
+
expect(typeof storage.saveSession).toBe('function');
|
|
118
|
+
expect(typeof storage.clearSession).toBe('function');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('concrete implementation works', async () => {
|
|
122
|
+
const storage = new TestCookieSessionStorage(createMockConfig() as any);
|
|
123
|
+
|
|
124
|
+
const session = await storage.getSession();
|
|
125
|
+
expect(session).toBeNull();
|
|
126
|
+
|
|
127
|
+
const savedResult = await storage.saveSession(undefined, 'session-data');
|
|
128
|
+
expect(savedResult).toHaveProperty('headers');
|
|
129
|
+
expect(savedResult.headers).toHaveProperty('Set-Cookie');
|
|
130
|
+
|
|
131
|
+
const clearedResult = await storage.clearSession('response');
|
|
132
|
+
expect(clearedResult).toHaveProperty('headers');
|
|
133
|
+
expect(clearedResult.headers).toHaveProperty('Set-Cookie');
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { ConfigurationProvider } from '../config/ConfigurationProvider';
|
|
2
|
-
import type { SessionStorage } from './types';
|
|
1
|
+
import type { ConfigurationProvider } from '../config/ConfigurationProvider.js';
|
|
2
|
+
import type { HeadersBag, SessionStorage } from './types.js';
|
|
3
3
|
|
|
4
4
|
export interface CookieOptions {
|
|
5
5
|
path?: string;
|
|
@@ -18,9 +18,11 @@ export abstract class CookieSessionStorage<TRequest, TResponse>
|
|
|
18
18
|
{
|
|
19
19
|
protected cookieName: string;
|
|
20
20
|
protected readonly cookieOptions: CookieOptions;
|
|
21
|
+
protected config: ConfigurationProvider;
|
|
21
22
|
|
|
22
23
|
constructor(config: ConfigurationProvider) {
|
|
23
|
-
this.
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.cookieName = config.getValue('cookieName') || 'wos_session';
|
|
24
26
|
this.cookieOptions = {
|
|
25
27
|
path: '/',
|
|
26
28
|
httpOnly: true,
|
|
@@ -31,12 +33,43 @@ export abstract class CookieSessionStorage<TRequest, TResponse>
|
|
|
31
33
|
};
|
|
32
34
|
}
|
|
33
35
|
|
|
36
|
+
protected async applyHeaders(
|
|
37
|
+
_response: TResponse | undefined,
|
|
38
|
+
_headers: HeadersBag,
|
|
39
|
+
): Promise<{ response: TResponse } | void> {
|
|
40
|
+
/* default no-op. Adapters can override if they CAN mutate a native response */
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected buildSetCookie(value: string, expired?: boolean): string {
|
|
44
|
+
const a = [`${this.cookieName}=${encodeURIComponent(value)}`];
|
|
45
|
+
const o = this.cookieOptions;
|
|
46
|
+
if (o.path) a.push(`Path=${o.path}`);
|
|
47
|
+
if (o.domain) a.push(`Domain=${o.domain}`);
|
|
48
|
+
if (o.maxAge || expired) a.push(`Max-Age=${expired ? 0 : o.maxAge}`);
|
|
49
|
+
if (o.httpOnly) a.push('HttpOnly');
|
|
50
|
+
if (o.secure) a.push('Secure');
|
|
51
|
+
if (o.sameSite) a.push(`SameSite=${o.sameSite}`);
|
|
52
|
+
if (o.priority) a.push(`Priority=${o.priority}`);
|
|
53
|
+
if (o.partitioned) a.push('Partitioned');
|
|
54
|
+
return a.join('; ');
|
|
55
|
+
}
|
|
56
|
+
|
|
34
57
|
abstract getSession(request: TRequest): Promise<string | null>;
|
|
35
58
|
|
|
36
|
-
|
|
37
|
-
response: TResponse,
|
|
59
|
+
async saveSession(
|
|
60
|
+
response: TResponse | undefined,
|
|
38
61
|
sessionData: string,
|
|
39
|
-
): Promise<TResponse
|
|
62
|
+
): Promise<{ response?: TResponse; headers?: HeadersBag }> {
|
|
63
|
+
const header = this.buildSetCookie(sessionData);
|
|
64
|
+
const mutated = await this.applyHeaders(response, { 'Set-Cookie': header });
|
|
65
|
+
return mutated ?? { headers: { 'Set-Cookie': header } };
|
|
66
|
+
}
|
|
40
67
|
|
|
41
|
-
|
|
68
|
+
async clearSession(
|
|
69
|
+
response: TResponse,
|
|
70
|
+
): Promise<{ response?: TResponse; headers?: HeadersBag }> {
|
|
71
|
+
const header = this.buildSetCookie('', true);
|
|
72
|
+
const mutated = await this.applyHeaders(response, { 'Set-Cookie': header });
|
|
73
|
+
return mutated ?? { headers: { 'Set-Cookie': header } };
|
|
74
|
+
}
|
|
42
75
|
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
import { SessionManager } from './SessionManager.js';
|
|
2
|
+
import {
|
|
3
|
+
SessionEncryptionError,
|
|
4
|
+
AuthKitError,
|
|
5
|
+
TokenRefreshError,
|
|
6
|
+
} from '../errors.js';
|
|
7
|
+
|
|
8
|
+
// Simple mocks for dependencies
|
|
9
|
+
const mockConfig = {
|
|
10
|
+
getValue: (key: string) => {
|
|
11
|
+
const values = {
|
|
12
|
+
cookiePassword: 'test-password-that-is-32-chars-long!!',
|
|
13
|
+
clientId: 'test-client-id',
|
|
14
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
15
|
+
};
|
|
16
|
+
return values[key as keyof typeof values];
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const mockUser = {
|
|
21
|
+
id: 'user_123',
|
|
22
|
+
email: 'test@example.com',
|
|
23
|
+
object: 'user',
|
|
24
|
+
firstName: 'Test',
|
|
25
|
+
lastName: 'User',
|
|
26
|
+
emailVerified: true,
|
|
27
|
+
profilePictureUrl: null,
|
|
28
|
+
createdAt: '2023-01-01T00:00:00Z',
|
|
29
|
+
updatedAt: '2023-01-01T00:00:00Z',
|
|
30
|
+
lastSignInAt: '2023-01-01T00:00:00Z',
|
|
31
|
+
externalId: null,
|
|
32
|
+
metadata: {},
|
|
33
|
+
} as const;
|
|
34
|
+
|
|
35
|
+
const mockStorage = {
|
|
36
|
+
getSession: async () => null,
|
|
37
|
+
saveSession: async (response: any) => ({ response }),
|
|
38
|
+
clearSession: async (response: any) => ({ response }),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const mockTokenManager = {
|
|
42
|
+
verifyToken: async () => true,
|
|
43
|
+
isTokenExpiring: () => false,
|
|
44
|
+
parseTokenClaims: () => ({
|
|
45
|
+
sub: 'user_123',
|
|
46
|
+
sid: 'session_123',
|
|
47
|
+
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
48
|
+
org_id: 'org_123',
|
|
49
|
+
}),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const mockClient = {
|
|
53
|
+
userManagement: {
|
|
54
|
+
getAuthorizationUrl: ({ redirectUri, clientId, state }: any) =>
|
|
55
|
+
`https://api.workos.com/sso/authorize?client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&state=${state || ''}`,
|
|
56
|
+
authenticateWithRefreshToken: async () => ({
|
|
57
|
+
accessToken: 'new-access-token',
|
|
58
|
+
refreshToken: 'new-refresh-token',
|
|
59
|
+
user: mockUser,
|
|
60
|
+
impersonator: undefined,
|
|
61
|
+
}),
|
|
62
|
+
revokeSession: async () => {},
|
|
63
|
+
getLogoutUrl: ({ sessionId, returnTo }: any) =>
|
|
64
|
+
`https://api.workos.com/sso/logout?session_id=${sessionId}&return_to=${returnTo || ''}`,
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const mockEncryption = {
|
|
69
|
+
sealData: async () => 'encrypted-session-data',
|
|
70
|
+
unsealData: async () => ({
|
|
71
|
+
accessToken: 'test-access-token',
|
|
72
|
+
refreshToken: 'test-refresh-token',
|
|
73
|
+
user: mockUser,
|
|
74
|
+
impersonator: undefined,
|
|
75
|
+
}),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
describe('SessionManager', () => {
|
|
79
|
+
let sessionManager: SessionManager<any, any>;
|
|
80
|
+
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
sessionManager = new SessionManager(
|
|
83
|
+
mockConfig as any,
|
|
84
|
+
mockStorage as any,
|
|
85
|
+
mockTokenManager as any,
|
|
86
|
+
mockClient as any,
|
|
87
|
+
mockEncryption as any,
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('constructor', () => {
|
|
92
|
+
it('creates instance with required dependencies', () => {
|
|
93
|
+
expect(sessionManager).toBeInstanceOf(SessionManager);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('withAuth()', () => {
|
|
98
|
+
it('returns null user when no session exists', async () => {
|
|
99
|
+
const result = await sessionManager.withAuth('request');
|
|
100
|
+
|
|
101
|
+
expect(result.user).toBeNull();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('returns user data when valid session exists', async () => {
|
|
105
|
+
const storageWithSession = {
|
|
106
|
+
...mockStorage,
|
|
107
|
+
getSession: async () => 'encrypted-session',
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const manager = new SessionManager(
|
|
111
|
+
mockConfig as any,
|
|
112
|
+
storageWithSession as any,
|
|
113
|
+
mockTokenManager as any,
|
|
114
|
+
mockClient as any,
|
|
115
|
+
mockEncryption as any,
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const result = await manager.withAuth('request');
|
|
119
|
+
|
|
120
|
+
expect(result.user).toEqual(mockUser);
|
|
121
|
+
expect(result.sessionId).toBe('session_123');
|
|
122
|
+
expect(result.accessToken).toBe('test-access-token');
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe('getAuthorizationUrl()', () => {
|
|
127
|
+
it('generates authorization URL with default values', async () => {
|
|
128
|
+
const url = await sessionManager.getAuthorizationUrl({});
|
|
129
|
+
|
|
130
|
+
expect(url).toContain('client_id=test-client-id');
|
|
131
|
+
expect(url).toContain(
|
|
132
|
+
'redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fcallback',
|
|
133
|
+
);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('includes return pathname in state', async () => {
|
|
137
|
+
const url = await sessionManager.getAuthorizationUrl({
|
|
138
|
+
returnPathname: '/dashboard',
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const stateParam = new URLSearchParams(url.split('?')[1]).get('state');
|
|
142
|
+
const decodedState = JSON.parse(atob(stateParam!));
|
|
143
|
+
|
|
144
|
+
expect(decodedState.returnPathname).toBe('/dashboard');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('uses custom redirect URI when provided', async () => {
|
|
148
|
+
const url = await sessionManager.getAuthorizationUrl({
|
|
149
|
+
redirectUri: 'https://example.com/auth/callback',
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
expect(url).toContain(
|
|
153
|
+
'redirect_uri=https%3A%2F%2Fexample.com%2Fauth%2Fcallback',
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe('createSession()', () => {
|
|
159
|
+
it('creates session from auth response', async () => {
|
|
160
|
+
const authResponse = {
|
|
161
|
+
accessToken: 'access-token',
|
|
162
|
+
refreshToken: 'refresh-token',
|
|
163
|
+
user: mockUser,
|
|
164
|
+
impersonator: undefined,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const result = await sessionManager.createSession(
|
|
168
|
+
authResponse,
|
|
169
|
+
'response',
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
expect(result.response).toBe('response');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('throws error when tokens are missing', async () => {
|
|
176
|
+
const authResponse = {
|
|
177
|
+
accessToken: null,
|
|
178
|
+
refreshToken: 'refresh-token',
|
|
179
|
+
user: mockUser,
|
|
180
|
+
impersonator: undefined,
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
await expect(
|
|
184
|
+
sessionManager.createSession(authResponse as any, 'response'),
|
|
185
|
+
).rejects.toThrow(AuthKitError);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe('refreshSession()', () => {
|
|
190
|
+
it('refreshes session with new tokens', async () => {
|
|
191
|
+
const session = {
|
|
192
|
+
accessToken: 'old-access-token',
|
|
193
|
+
refreshToken: 'old-refresh-token',
|
|
194
|
+
user: mockUser,
|
|
195
|
+
impersonator: undefined,
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const result = await sessionManager.refreshSession(session);
|
|
199
|
+
|
|
200
|
+
expect(result.accessToken).toBe('new-access-token');
|
|
201
|
+
expect(result.session.refreshToken).toBe('new-refresh-token');
|
|
202
|
+
expect(result.sessionId).toBe('session_123');
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('throws TokenRefreshError on failure', async () => {
|
|
206
|
+
const failingClient = {
|
|
207
|
+
userManagement: {
|
|
208
|
+
authenticateWithRefreshToken: async () => {
|
|
209
|
+
throw new Error('Refresh failed');
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const manager = new SessionManager(
|
|
215
|
+
mockConfig as any,
|
|
216
|
+
mockStorage as any,
|
|
217
|
+
mockTokenManager as any,
|
|
218
|
+
failingClient as any,
|
|
219
|
+
mockEncryption as any,
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
const session = {
|
|
223
|
+
accessToken: 'token',
|
|
224
|
+
refreshToken: 'refresh',
|
|
225
|
+
user: mockUser,
|
|
226
|
+
impersonator: undefined,
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
await expect(manager.refreshSession(session)).rejects.toThrow(
|
|
230
|
+
TokenRefreshError,
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
describe('terminateSession()', () => {
|
|
236
|
+
it('revokes session and returns logout URL', async () => {
|
|
237
|
+
const session = {
|
|
238
|
+
accessToken: 'access-token',
|
|
239
|
+
refreshToken: 'refresh-token',
|
|
240
|
+
user: mockUser,
|
|
241
|
+
impersonator: undefined,
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const result = await sessionManager.terminateSession(session, 'response');
|
|
245
|
+
|
|
246
|
+
expect(result.response).toBe('response');
|
|
247
|
+
expect(result.logoutUrl).toContain('session_id=session_123');
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('includes return URL in logout URL when provided', async () => {
|
|
251
|
+
const session = {
|
|
252
|
+
accessToken: 'access-token',
|
|
253
|
+
refreshToken: 'refresh-token',
|
|
254
|
+
user: mockUser,
|
|
255
|
+
impersonator: undefined,
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const result = await sessionManager.terminateSession(
|
|
259
|
+
session,
|
|
260
|
+
'response',
|
|
261
|
+
{
|
|
262
|
+
returnTo: 'https://example.com',
|
|
263
|
+
},
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
expect(result.logoutUrl).toContain('return_to=https://example.com');
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
describe('switchToOrganization()', () => {
|
|
271
|
+
it('switches organization and updates session', async () => {
|
|
272
|
+
const storageWithSession = {
|
|
273
|
+
...mockStorage,
|
|
274
|
+
getSession: async () => 'encrypted-session',
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const manager = new SessionManager(
|
|
278
|
+
mockConfig as any,
|
|
279
|
+
storageWithSession as any,
|
|
280
|
+
mockTokenManager as any,
|
|
281
|
+
mockClient as any,
|
|
282
|
+
mockEncryption as any,
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
const result = await manager.switchToOrganization(
|
|
286
|
+
'request',
|
|
287
|
+
'response',
|
|
288
|
+
'org_456',
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
expect(result.response).toBe('response');
|
|
292
|
+
expect(result.authResult.user).toEqual(mockUser);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('throws error when no session exists', async () => {
|
|
296
|
+
await expect(
|
|
297
|
+
sessionManager.switchToOrganization('request', 'response', 'org_456'),
|
|
298
|
+
).rejects.toThrow(AuthKitError);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
describe('error handling', () => {
|
|
303
|
+
it('handles encryption failures', async () => {
|
|
304
|
+
const failingEncryption = {
|
|
305
|
+
sealData: async () => {
|
|
306
|
+
throw new Error('Encryption failed');
|
|
307
|
+
},
|
|
308
|
+
unsealData: async () => ({}),
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const manager = new SessionManager(
|
|
312
|
+
mockConfig as any,
|
|
313
|
+
mockStorage as any,
|
|
314
|
+
mockTokenManager as any,
|
|
315
|
+
mockClient as any,
|
|
316
|
+
failingEncryption as any,
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
const session = {
|
|
320
|
+
accessToken: 'token',
|
|
321
|
+
refreshToken: 'refresh',
|
|
322
|
+
user: mockUser,
|
|
323
|
+
impersonator: undefined,
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
await expect(manager.createSession(session, 'response')).rejects.toThrow(
|
|
327
|
+
SessionEncryptionError,
|
|
328
|
+
);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it('handles decryption failures', async () => {
|
|
332
|
+
const storageWithSession = {
|
|
333
|
+
...mockStorage,
|
|
334
|
+
getSession: async () => 'invalid-encrypted-session',
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const failingEncryption = {
|
|
338
|
+
sealData: async () => 'encrypted',
|
|
339
|
+
unsealData: async () => {
|
|
340
|
+
throw new Error('Decryption failed');
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const manager = new SessionManager(
|
|
345
|
+
mockConfig as any,
|
|
346
|
+
storageWithSession as any,
|
|
347
|
+
mockTokenManager as any,
|
|
348
|
+
mockClient as any,
|
|
349
|
+
failingEncryption as any,
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
const result = await manager.withAuth('request');
|
|
353
|
+
|
|
354
|
+
expect(result.user).toBeNull();
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
});
|