@workos/authkit-session 0.2.0-beta.0 → 0.3.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 +96 -781
- package/dist/core/AuthKitCore.d.ts +16 -10
- package/dist/core/AuthKitCore.d.ts.map +1 -1
- package/dist/core/AuthKitCore.js +32 -22
- package/dist/core/AuthKitCore.js.map +1 -1
- package/dist/core/errors.d.ts +6 -1
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +5 -1
- package/dist/core/errors.js.map +1 -1
- package/dist/core/session/CookieSessionStorage.d.ts +2 -3
- package/dist/core/session/CookieSessionStorage.d.ts.map +1 -1
- package/dist/core/session/CookieSessionStorage.js +14 -20
- package/dist/core/session/CookieSessionStorage.js.map +1 -1
- package/dist/core/session/types.d.ts +8 -0
- package/dist/core/session/types.d.ts.map +1 -1
- package/dist/index.d.ts +18 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +29 -24
- package/dist/index.js.map +1 -1
- package/dist/operations/AuthOperations.d.ts +13 -14
- package/dist/operations/AuthOperations.d.ts.map +1 -1
- package/dist/operations/AuthOperations.js +37 -47
- package/dist/operations/AuthOperations.js.map +1 -1
- package/dist/service/AuthService.d.ts +22 -41
- package/dist/service/AuthService.d.ts.map +1 -1
- package/dist/service/AuthService.js +64 -65
- package/dist/service/AuthService.js.map +1 -1
- package/dist/service/factory.d.ts.map +1 -1
- package/dist/service/factory.js +24 -10
- package/dist/service/factory.js.map +1 -1
- package/package.json +2 -2
- package/src/core/AuthKitCore.spec.ts +12 -19
- package/src/core/AuthKitCore.ts +38 -23
- package/src/core/config/ConfigurationProvider.spec.ts +1 -0
- package/src/core/errors.spec.ts +47 -0
- package/src/core/errors.ts +10 -1
- package/src/core/session/CookieSessionStorage.spec.ts +51 -62
- package/src/core/session/CookieSessionStorage.ts +19 -22
- package/src/core/session/types.ts +8 -0
- package/src/index.ts +36 -25
- package/src/operations/AuthOperations.spec.ts +128 -69
- package/src/operations/AuthOperations.ts +53 -66
- package/src/service/AuthService.spec.ts +66 -40
- package/src/service/AuthService.ts +77 -82
- package/src/service/factory.spec.ts +20 -9
- package/src/service/factory.ts +33 -20
package/src/index.ts
CHANGED
|
@@ -1,42 +1,49 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @workos/authkit-session
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* Provides business logic primitives (crypto, JWT, refresh) that framework
|
|
6
|
-
* adapters use to implement authentication patterns.
|
|
4
|
+
* Framework-agnostic authentication library for WorkOS.
|
|
7
5
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* - Provide integration helpers → Cookie building, config management
|
|
11
|
-
* - Let frameworks own patterns → updateSession/withAuth are framework-specific
|
|
6
|
+
* Provides authentication business logic (JWT verification, token refresh)
|
|
7
|
+
* with a pluggable storage adapter pattern for framework integration.
|
|
12
8
|
*
|
|
13
|
-
* **
|
|
14
|
-
* -
|
|
15
|
-
* -
|
|
16
|
-
* -
|
|
17
|
-
* -
|
|
9
|
+
* **What frameworks do:**
|
|
10
|
+
* - Implement storage adapter (SessionStorage<TRequest, TResponse>)
|
|
11
|
+
* - Add middleware for auth validation and refresh
|
|
12
|
+
* - Export framework-specific helpers
|
|
13
|
+
* - Handle session encryption (or use our fallback)
|
|
18
14
|
*
|
|
19
|
-
* **
|
|
20
|
-
* -
|
|
15
|
+
* **What this library does:**
|
|
16
|
+
* - All authentication logic (AuthService)
|
|
17
|
+
* - JWT verification (JWKS with caching)
|
|
18
|
+
* - Token refresh orchestration
|
|
19
|
+
* - WorkOS API operations
|
|
20
|
+
* - Session encryption fallback (iron-webcrypto, AES-256-CBC)
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
// ============================================
|
|
24
|
-
//
|
|
24
|
+
// Public API
|
|
25
25
|
// ============================================
|
|
26
|
-
export {
|
|
26
|
+
export { AuthService } from './service/AuthService.js';
|
|
27
|
+
export { createAuthService } from './service/factory.js';
|
|
27
28
|
|
|
28
29
|
// ============================================
|
|
29
|
-
//
|
|
30
|
+
// Advanced (Internal Layers)
|
|
30
31
|
// ============================================
|
|
32
|
+
export { AuthKitCore } from './core/AuthKitCore.js';
|
|
31
33
|
export { AuthOperations } from './operations/AuthOperations.js';
|
|
32
34
|
|
|
33
35
|
// ============================================
|
|
34
|
-
//
|
|
36
|
+
// Storage Helpers
|
|
35
37
|
// ============================================
|
|
36
38
|
export { CookieSessionStorage } from './core/session/CookieSessionStorage.js';
|
|
37
39
|
|
|
38
40
|
// ============================================
|
|
39
|
-
//
|
|
41
|
+
// Encryption Fallback
|
|
42
|
+
// ============================================
|
|
43
|
+
export { default as sessionEncryption } from './core/encryption/ironWebcryptoEncryption.js';
|
|
44
|
+
|
|
45
|
+
// ============================================
|
|
46
|
+
// Configuration
|
|
40
47
|
// ============================================
|
|
41
48
|
export {
|
|
42
49
|
configure,
|
|
@@ -51,6 +58,16 @@ export { ConfigurationProvider } from './core/config/ConfigurationProvider.js';
|
|
|
51
58
|
// ============================================
|
|
52
59
|
export { getWorkOS } from './core/client/workos.js';
|
|
53
60
|
|
|
61
|
+
// ============================================
|
|
62
|
+
// Errors
|
|
63
|
+
// ============================================
|
|
64
|
+
export {
|
|
65
|
+
AuthKitError,
|
|
66
|
+
SessionEncryptionError,
|
|
67
|
+
TokenValidationError,
|
|
68
|
+
TokenRefreshError,
|
|
69
|
+
} from './core/errors.js';
|
|
70
|
+
|
|
54
71
|
// ============================================
|
|
55
72
|
// Type Exports
|
|
56
73
|
// ============================================
|
|
@@ -63,9 +80,3 @@ export type {
|
|
|
63
80
|
WorkOS,
|
|
64
81
|
AuthenticationResponse,
|
|
65
82
|
} from '@workos-inc/node';
|
|
66
|
-
|
|
67
|
-
// ============================================
|
|
68
|
-
// Optional Reference Implementation
|
|
69
|
-
// ============================================
|
|
70
|
-
export { AuthService } from './service/AuthService.js';
|
|
71
|
-
export { createAuthService } from './service/factory.js';
|
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { AuthOperations } from './AuthOperations.js';
|
|
2
2
|
|
|
3
3
|
const mockConfig = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
redirectUri: 'http://localhost:3000/callback',
|
|
8
|
-
cookieName: 'wos-session',
|
|
9
|
-
};
|
|
10
|
-
return values[key as keyof typeof values];
|
|
11
|
-
},
|
|
4
|
+
clientId: 'test-client-id',
|
|
5
|
+
redirectUri: 'http://localhost:3000/callback',
|
|
6
|
+
cookieName: 'wos-session',
|
|
12
7
|
};
|
|
13
8
|
|
|
14
9
|
const mockUser = {
|
|
@@ -22,24 +17,29 @@ const mockUser = {
|
|
|
22
17
|
createdAt: '2023-01-01T00:00:00Z',
|
|
23
18
|
updatedAt: '2023-01-01T00:00:00Z',
|
|
24
19
|
lastSignInAt: '2023-01-01T00:00:00Z',
|
|
20
|
+
locale: 'en-US',
|
|
25
21
|
externalId: null,
|
|
26
22
|
metadata: {},
|
|
27
23
|
} as const;
|
|
28
24
|
|
|
29
25
|
const mockCore = {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
26
|
+
validateAndRefresh: async () => ({
|
|
27
|
+
valid: true,
|
|
28
|
+
refreshed: true,
|
|
29
|
+
session: {
|
|
30
|
+
accessToken: 'new-access-token',
|
|
31
|
+
refreshToken: 'new-refresh-token',
|
|
32
|
+
user: mockUser,
|
|
33
|
+
impersonator: undefined,
|
|
34
|
+
},
|
|
35
|
+
claims: {
|
|
36
|
+
sub: 'user_123',
|
|
37
|
+
sid: 'session_123',
|
|
38
|
+
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
39
|
+
org_id: 'org_123',
|
|
40
|
+
},
|
|
35
41
|
}),
|
|
36
42
|
encryptSession: async () => 'encrypted-session-data',
|
|
37
|
-
parseTokenClaims: () => ({
|
|
38
|
-
sub: 'user_123',
|
|
39
|
-
sid: 'session_123',
|
|
40
|
-
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
41
|
-
org_id: 'org_123',
|
|
42
|
-
}),
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
const mockClient = {
|
|
@@ -68,21 +68,19 @@ describe('AuthOperations', () => {
|
|
|
68
68
|
});
|
|
69
69
|
});
|
|
70
70
|
|
|
71
|
-
describe('
|
|
72
|
-
it('returns logout URL
|
|
73
|
-
const result =
|
|
71
|
+
describe('getLogoutUrl()', () => {
|
|
72
|
+
it('returns logout URL with session ID', () => {
|
|
73
|
+
const result = operations.getLogoutUrl('session_123');
|
|
74
74
|
|
|
75
|
-
expect(result
|
|
76
|
-
expect(result.clearCookieHeader).toContain('wos-session=');
|
|
77
|
-
expect(result.clearCookieHeader).toContain('Max-Age=0');
|
|
75
|
+
expect(result).toContain('session_id=session_123');
|
|
78
76
|
});
|
|
79
77
|
|
|
80
|
-
it('includes returnTo in logout URL',
|
|
81
|
-
const result =
|
|
78
|
+
it('includes returnTo in logout URL', () => {
|
|
79
|
+
const result = operations.getLogoutUrl('session_123', {
|
|
82
80
|
returnTo: 'http://localhost:3000',
|
|
83
81
|
});
|
|
84
82
|
|
|
85
|
-
expect(result
|
|
83
|
+
expect(result).toContain('return_to=');
|
|
86
84
|
});
|
|
87
85
|
});
|
|
88
86
|
|
|
@@ -98,7 +96,9 @@ describe('AuthOperations', () => {
|
|
|
98
96
|
const result = await operations.switchOrganization(session, 'org_456');
|
|
99
97
|
|
|
100
98
|
expect(result.auth.user).toEqual(mockUser);
|
|
101
|
-
|
|
99
|
+
if (result.auth.user) {
|
|
100
|
+
expect(result.auth.accessToken).toBe('new-access-token');
|
|
101
|
+
}
|
|
102
102
|
expect(result.encryptedSession).toBe('encrypted-session-data');
|
|
103
103
|
});
|
|
104
104
|
});
|
|
@@ -113,29 +113,38 @@ describe('AuthOperations', () => {
|
|
|
113
113
|
};
|
|
114
114
|
|
|
115
115
|
const result = await operations.refreshSession(session);
|
|
116
|
+
const auth = result.auth as Extract<typeof result.auth, { user: object }>;
|
|
116
117
|
|
|
117
|
-
expect(
|
|
118
|
-
expect(
|
|
119
|
-
expect(
|
|
120
|
-
expect(
|
|
118
|
+
expect(auth.user).toEqual(mockUser);
|
|
119
|
+
expect(auth.accessToken).toBe('new-access-token');
|
|
120
|
+
expect(auth.refreshToken).toBe('new-refresh-token');
|
|
121
|
+
expect(auth.sessionId).toBe('session_123');
|
|
121
122
|
expect(result.encryptedSession).toBe('encrypted-session-data');
|
|
122
123
|
});
|
|
123
124
|
|
|
124
|
-
it('
|
|
125
|
+
it('passes organizationId to validateAndRefresh when provided', async () => {
|
|
126
|
+
let capturedOptions: any;
|
|
125
127
|
const coreWithSpy = {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
128
|
+
validateAndRefresh: async (_session: any, options?: any) => {
|
|
129
|
+
capturedOptions = options;
|
|
130
|
+
return {
|
|
131
|
+
valid: true,
|
|
132
|
+
refreshed: true,
|
|
133
|
+
session: {
|
|
134
|
+
accessToken: 'new-access-token',
|
|
135
|
+
refreshToken: 'new-refresh-token',
|
|
136
|
+
user: mockUser,
|
|
137
|
+
impersonator: undefined,
|
|
138
|
+
},
|
|
139
|
+
claims: {
|
|
140
|
+
sub: 'user_123',
|
|
141
|
+
sid: 'session_123',
|
|
142
|
+
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
143
|
+
org_id: 'org_456',
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
},
|
|
132
147
|
encryptSession: async () => 'encrypted-session-data',
|
|
133
|
-
parseTokenClaims: () => ({
|
|
134
|
-
sub: 'user_123',
|
|
135
|
-
sid: 'session_123',
|
|
136
|
-
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
137
|
-
org_id: 'org_123',
|
|
138
|
-
}),
|
|
139
148
|
};
|
|
140
149
|
const testOps = new AuthOperations(
|
|
141
150
|
coreWithSpy as any,
|
|
@@ -150,29 +159,37 @@ describe('AuthOperations', () => {
|
|
|
150
159
|
impersonator: undefined,
|
|
151
160
|
};
|
|
152
161
|
|
|
153
|
-
|
|
162
|
+
await testOps.refreshSession(session, 'org_456');
|
|
154
163
|
|
|
155
|
-
expect(
|
|
164
|
+
expect(capturedOptions).toEqual({
|
|
165
|
+
force: true,
|
|
166
|
+
organizationId: 'org_456',
|
|
167
|
+
});
|
|
156
168
|
});
|
|
157
169
|
|
|
158
|
-
it('
|
|
159
|
-
|
|
160
|
-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzEyMyIsInNpZCI6InNlc3Npb25fMTIzIiwiZXhwIjoxNzM2Mzc2MDAwLCJvcmdfaWQiOiJvcmdfZnJvbV90b2tlbiJ9.fake-signature';
|
|
161
|
-
|
|
170
|
+
it('calls validateAndRefresh with force: true and no organizationId when not provided', async () => {
|
|
171
|
+
let capturedOptions: any;
|
|
162
172
|
const coreWithSpy = {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
173
|
+
validateAndRefresh: async (_session: any, options?: any) => {
|
|
174
|
+
capturedOptions = options;
|
|
175
|
+
return {
|
|
176
|
+
valid: true,
|
|
177
|
+
refreshed: true,
|
|
178
|
+
session: {
|
|
179
|
+
accessToken: 'new-access-token',
|
|
180
|
+
refreshToken: 'new-refresh-token',
|
|
181
|
+
user: mockUser,
|
|
182
|
+
impersonator: undefined,
|
|
183
|
+
},
|
|
184
|
+
claims: {
|
|
185
|
+
sub: 'user_123',
|
|
186
|
+
sid: 'session_123',
|
|
187
|
+
exp: Math.floor(Date.now() / 1000) + 3600,
|
|
188
|
+
org_id: 'org_123',
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
},
|
|
169
192
|
encryptSession: async () => 'encrypted-session-data',
|
|
170
|
-
parseTokenClaims: () => ({
|
|
171
|
-
sub: 'user_123',
|
|
172
|
-
sid: 'session_123',
|
|
173
|
-
exp: 1736376000,
|
|
174
|
-
org_id: 'org_from_token',
|
|
175
|
-
}),
|
|
176
193
|
};
|
|
177
194
|
const testOps = new AuthOperations(
|
|
178
195
|
coreWithSpy as any,
|
|
@@ -181,15 +198,18 @@ describe('AuthOperations', () => {
|
|
|
181
198
|
);
|
|
182
199
|
|
|
183
200
|
const session = {
|
|
184
|
-
accessToken:
|
|
201
|
+
accessToken: 'test-token',
|
|
185
202
|
refreshToken: 'test-refresh',
|
|
186
203
|
user: mockUser,
|
|
187
204
|
impersonator: undefined,
|
|
188
205
|
};
|
|
189
206
|
|
|
190
|
-
|
|
207
|
+
await testOps.refreshSession(session);
|
|
191
208
|
|
|
192
|
-
expect(
|
|
209
|
+
expect(capturedOptions).toEqual({
|
|
210
|
+
force: true,
|
|
211
|
+
organizationId: undefined,
|
|
212
|
+
});
|
|
193
213
|
});
|
|
194
214
|
});
|
|
195
215
|
|
|
@@ -201,7 +221,7 @@ describe('AuthOperations', () => {
|
|
|
201
221
|
expect(result).toContain('redirect_uri');
|
|
202
222
|
});
|
|
203
223
|
|
|
204
|
-
it('encodes returnPathname in state', async () => {
|
|
224
|
+
it('encodes returnPathname in URL-safe base64 state', async () => {
|
|
205
225
|
const result = await operations.getAuthorizationUrl({
|
|
206
226
|
returnPathname: '/dashboard',
|
|
207
227
|
});
|
|
@@ -209,10 +229,49 @@ describe('AuthOperations', () => {
|
|
|
209
229
|
expect(result).toContain('state=');
|
|
210
230
|
const stateMatch = result.match(/state=([^&]+)/);
|
|
211
231
|
expect(stateMatch).toBeTruthy();
|
|
212
|
-
|
|
232
|
+
|
|
233
|
+
// Decode URL-safe base64: reverse - to +, _ to /
|
|
234
|
+
const urlSafeState = stateMatch![1]!;
|
|
235
|
+
const standardBase64 = urlSafeState.replace(/-/g, '+').replace(/_/g, '/');
|
|
236
|
+
const decoded = JSON.parse(atob(standardBase64));
|
|
213
237
|
expect(decoded.returnPathname).toBe('/dashboard');
|
|
214
238
|
});
|
|
215
239
|
|
|
240
|
+
it('combines internal state with custom user state', async () => {
|
|
241
|
+
const result = await operations.getAuthorizationUrl({
|
|
242
|
+
returnPathname: '/profile',
|
|
243
|
+
state: 'my-custom-state',
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
expect(result).toContain('state=');
|
|
247
|
+
const stateMatch = result.match(/state=([^&]+)/);
|
|
248
|
+
expect(stateMatch).toBeTruthy();
|
|
249
|
+
|
|
250
|
+
// State should be in format: internal.userState
|
|
251
|
+
const fullState = stateMatch![1]!;
|
|
252
|
+
expect(fullState).toContain('.');
|
|
253
|
+
const [internal, userState] = fullState.split('.');
|
|
254
|
+
expect(userState).toBe('my-custom-state');
|
|
255
|
+
|
|
256
|
+
// Decode internal part
|
|
257
|
+
const standardBase64 = internal!.replace(/-/g, '+').replace(/_/g, '/');
|
|
258
|
+
const decoded = JSON.parse(atob(standardBase64));
|
|
259
|
+
expect(decoded.returnPathname).toBe('/profile');
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('passes custom state as-is when no returnPathname', async () => {
|
|
263
|
+
const result = await operations.getAuthorizationUrl({
|
|
264
|
+
state: 'only-user-state',
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
expect(result).toContain('state=');
|
|
268
|
+
const stateMatch = result.match(/state=([^&]+)/);
|
|
269
|
+
expect(stateMatch).toBeTruthy();
|
|
270
|
+
|
|
271
|
+
// State should be passed through directly without internal wrapper
|
|
272
|
+
expect(stateMatch![1]).toBe('only-user-state');
|
|
273
|
+
});
|
|
274
|
+
|
|
216
275
|
it('includes screenHint when provided', async () => {
|
|
217
276
|
const result = await operations.getAuthorizationUrl({
|
|
218
277
|
screenHint: 'sign-up',
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import type { WorkOS } from '@workos-inc/node';
|
|
2
2
|
import type { AuthKitCore } from '../core/AuthKitCore.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { AuthKitConfig } from '../core/config/types.js';
|
|
4
4
|
import type {
|
|
5
5
|
AuthResult,
|
|
6
|
-
BaseTokenClaims,
|
|
7
6
|
GetAuthorizationUrlOptions,
|
|
8
7
|
Session,
|
|
9
8
|
} from '../core/session/types.js';
|
|
@@ -21,43 +20,31 @@ import type {
|
|
|
21
20
|
* - Get authorization URLs (for sign in/sign up flows)
|
|
22
21
|
*/
|
|
23
22
|
export class AuthOperations {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
) {
|
|
23
|
+
private core: AuthKitCore;
|
|
24
|
+
private client: WorkOS;
|
|
25
|
+
private config: AuthKitConfig;
|
|
26
|
+
|
|
27
|
+
constructor(core: AuthKitCore, client: WorkOS, config: AuthKitConfig) {
|
|
28
|
+
this.core = core;
|
|
29
|
+
this.client = client;
|
|
30
|
+
this.config = config;
|
|
31
|
+
}
|
|
29
32
|
|
|
30
33
|
/**
|
|
31
|
-
*
|
|
34
|
+
* Get the WorkOS logout URL.
|
|
32
35
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
36
|
+
* This only handles the WorkOS API part. Session clearing is handled
|
|
37
|
+
* by the storage layer in AuthService.
|
|
35
38
|
*
|
|
36
39
|
* @param sessionId - The session ID to terminate
|
|
37
40
|
* @param options - Optional return URL
|
|
38
|
-
* @returns Logout URL
|
|
41
|
+
* @returns Logout URL
|
|
39
42
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
options?: { returnTo?: string },
|
|
43
|
-
): Promise<{
|
|
44
|
-
logoutUrl: string;
|
|
45
|
-
clearCookieHeader: string;
|
|
46
|
-
}> {
|
|
47
|
-
// Generate WorkOS logout URL
|
|
48
|
-
const logoutUrl = this.client.userManagement.getLogoutUrl({
|
|
43
|
+
getLogoutUrl(sessionId: string, options?: { returnTo?: string }): string {
|
|
44
|
+
return this.client.userManagement.getLogoutUrl({
|
|
49
45
|
sessionId,
|
|
50
46
|
returnTo: options?.returnTo,
|
|
51
47
|
});
|
|
52
|
-
|
|
53
|
-
// Build cookie clear header
|
|
54
|
-
const cookieName = this.config.getValue('cookieName');
|
|
55
|
-
const clearCookieHeader = `${cookieName}=; Path=/; Max-Age=0; HttpOnly; Secure; SameSite=lax`;
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
logoutUrl,
|
|
59
|
-
clearCookieHeader,
|
|
60
|
-
};
|
|
61
48
|
}
|
|
62
49
|
|
|
63
50
|
/**
|
|
@@ -84,7 +71,7 @@ export class AuthOperations {
|
|
|
84
71
|
/**
|
|
85
72
|
* Refresh session operation.
|
|
86
73
|
*
|
|
87
|
-
*
|
|
74
|
+
* Forces a token refresh (for org switching or manual refresh),
|
|
88
75
|
* encrypts the new session, and returns the auth result.
|
|
89
76
|
*
|
|
90
77
|
* @param session - Current session with refresh token
|
|
@@ -98,41 +85,27 @@ export class AuthOperations {
|
|
|
98
85
|
auth: AuthResult;
|
|
99
86
|
encryptedSession: string;
|
|
100
87
|
}> {
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const claims = this.core.parseTokenClaims<BaseTokenClaims>(
|
|
107
|
-
session.accessToken,
|
|
108
|
-
);
|
|
109
|
-
orgId = claims.org_id;
|
|
110
|
-
} catch {
|
|
111
|
-
// Token too malformed to parse - refresh without org context
|
|
112
|
-
// WorkOS will use whatever org is embedded in the refresh token
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const result = await this.core.refreshTokens(session.refreshToken, orgId);
|
|
117
|
-
|
|
118
|
-
const newSession: Session = {
|
|
119
|
-
accessToken: result.accessToken,
|
|
120
|
-
refreshToken: result.refreshToken,
|
|
121
|
-
user: result.user,
|
|
122
|
-
impersonator: result.impersonator,
|
|
123
|
-
};
|
|
88
|
+
// Force refresh via core, optionally switching organizations
|
|
89
|
+
const { session: newSession, claims } = await this.core.validateAndRefresh(
|
|
90
|
+
session,
|
|
91
|
+
{ force: true, organizationId },
|
|
92
|
+
);
|
|
124
93
|
|
|
125
94
|
const encryptedSession = await this.core.encryptSession(newSession);
|
|
126
95
|
|
|
127
|
-
const claims = this.core.parseTokenClaims(result.accessToken);
|
|
128
|
-
|
|
129
96
|
const auth: AuthResult = {
|
|
130
|
-
user:
|
|
97
|
+
user: newSession.user,
|
|
131
98
|
sessionId: claims.sid,
|
|
132
|
-
impersonator:
|
|
133
|
-
accessToken:
|
|
134
|
-
refreshToken:
|
|
99
|
+
impersonator: newSession.impersonator,
|
|
100
|
+
accessToken: newSession.accessToken,
|
|
101
|
+
refreshToken: newSession.refreshToken,
|
|
135
102
|
claims,
|
|
103
|
+
organizationId: claims.org_id,
|
|
104
|
+
role: claims.role,
|
|
105
|
+
roles: claims.roles,
|
|
106
|
+
permissions: claims.permissions,
|
|
107
|
+
entitlements: claims.entitlements,
|
|
108
|
+
featureFlags: claims.feature_flags,
|
|
136
109
|
};
|
|
137
110
|
|
|
138
111
|
return {
|
|
@@ -144,25 +117,39 @@ export class AuthOperations {
|
|
|
144
117
|
/**
|
|
145
118
|
* Get authorization URL for WorkOS authentication.
|
|
146
119
|
*
|
|
147
|
-
*
|
|
120
|
+
* State encoding format: `{internal}.{userState}` where internal is URL-safe
|
|
121
|
+
* base64 encoded JSON containing returnPathname. This allows customers to
|
|
122
|
+
* pass their own state through the OAuth flow.
|
|
148
123
|
*
|
|
149
|
-
* @param options - Authorization URL options (returnPathname, screenHint, etc.)
|
|
124
|
+
* @param options - Authorization URL options (returnPathname, screenHint, state, etc.)
|
|
150
125
|
* @returns The authorization URL
|
|
151
126
|
*/
|
|
152
127
|
async getAuthorizationUrl(
|
|
153
128
|
options: GetAuthorizationUrlOptions = {},
|
|
154
129
|
): Promise<string> {
|
|
130
|
+
// Build the combined state parameter (matches authkit-nextjs format)
|
|
131
|
+
const internalState = options.returnPathname
|
|
132
|
+
? btoa(JSON.stringify({ returnPathname: options.returnPathname }))
|
|
133
|
+
.replace(/\+/g, '-')
|
|
134
|
+
.replace(/\//g, '_')
|
|
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
|
+
|
|
155
144
|
return this.client.userManagement.getAuthorizationUrl({
|
|
156
145
|
provider: 'authkit',
|
|
157
|
-
redirectUri: options.redirectUri ?? this.config.
|
|
146
|
+
redirectUri: options.redirectUri ?? this.config.redirectUri,
|
|
158
147
|
screenHint: options.screenHint,
|
|
159
148
|
organizationId: options.organizationId,
|
|
160
149
|
loginHint: options.loginHint,
|
|
161
150
|
prompt: options.prompt,
|
|
162
|
-
clientId: this.config.
|
|
163
|
-
state
|
|
164
|
-
? btoa(JSON.stringify({ returnPathname: options.returnPathname }))
|
|
165
|
-
: undefined,
|
|
151
|
+
clientId: this.config.clientId,
|
|
152
|
+
state,
|
|
166
153
|
});
|
|
167
154
|
}
|
|
168
155
|
|