@workos/authkit-session 0.0.1-alpha.1 → 0.0.1-alpha.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 +317 -36
- package/dist/index.cjs +150 -104
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -25
- package/dist/index.d.ts +28 -25
- package/dist/index.js +149 -103
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
- package/src/core/client/WorkOSLite.ts +10 -0
- package/src/core/client/types.ts +3 -21
- package/src/core/config/ConfigurationProvider.ts +42 -30
- package/src/core/config.ts +3 -6
- package/src/core/createAuthKitFactory.ts +76 -13
- package/src/core/errors.ts +1 -1
- package/src/core/session/SessionManager.ts +90 -84
- package/src/core/session/TokenManager.ts +1 -1
- package/src/core/session/types.ts +1 -1
- package/src/index.ts +1 -0
package/dist/index.d.cts
CHANGED
|
@@ -1,37 +1,24 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _workos_inc_node from '@workos-inc/node';
|
|
2
|
+
import { User, Impersonator, WorkOSOptions } from '@workos-inc/node';
|
|
3
|
+
export { Impersonator, Organization, User } from '@workos-inc/node';
|
|
2
4
|
import { JWTPayload } from 'jose';
|
|
3
5
|
|
|
4
6
|
interface UserManagementInterface {
|
|
5
7
|
getAuthorizationUrl: (options: AuthorizationURLOptions) => string;
|
|
6
8
|
authenticateWithCode: (options: AuthenticateWithCodeOptions) => Promise<AuthenticationResponse>;
|
|
7
9
|
authenticateWithRefreshToken: (options: AuthenticateWithRefreshTokenOptions) => Promise<AuthenticationResponse>;
|
|
8
|
-
getLogoutUrl: (
|
|
10
|
+
getLogoutUrl: (options: {
|
|
9
11
|
sessionId: string;
|
|
10
12
|
returnTo?: string;
|
|
11
13
|
}) => string;
|
|
14
|
+
revokeSession: (options: {
|
|
15
|
+
sessionId: string;
|
|
16
|
+
}) => Promise<void>;
|
|
12
17
|
}
|
|
13
18
|
interface WorkOSClient {
|
|
14
19
|
userManagement: UserManagementInterface;
|
|
15
20
|
getJwksUrl: (clientId: string) => string;
|
|
16
21
|
}
|
|
17
|
-
interface User {
|
|
18
|
-
object: string;
|
|
19
|
-
id: string;
|
|
20
|
-
email: string;
|
|
21
|
-
emailVerified: boolean;
|
|
22
|
-
profilePictureUrl: string | null;
|
|
23
|
-
firstName: string | null;
|
|
24
|
-
lastName: string | null;
|
|
25
|
-
lastSignInAt: string | null;
|
|
26
|
-
createdAt: string;
|
|
27
|
-
updatedAt: string;
|
|
28
|
-
externalId: string | null;
|
|
29
|
-
metadata: Record<string, string>;
|
|
30
|
-
}
|
|
31
|
-
interface Impersonator {
|
|
32
|
-
email: string;
|
|
33
|
-
reason: string | null;
|
|
34
|
-
}
|
|
35
22
|
interface AuthenticationResponse {
|
|
36
23
|
user: User;
|
|
37
24
|
organizationId?: string;
|
|
@@ -169,6 +156,8 @@ declare class ConfigurationProvider {
|
|
|
169
156
|
setValueSource(source: ValueSource): void;
|
|
170
157
|
configure(configOrSource: Partial<AuthKitConfig> | ValueSource, source?: ValueSource): void;
|
|
171
158
|
getValue<K extends keyof AuthKitConfig>(key: K): AuthKitConfig[K];
|
|
159
|
+
private getEnvironmentValue;
|
|
160
|
+
private convertValueType;
|
|
172
161
|
getConfig(): AuthKitConfig;
|
|
173
162
|
}
|
|
174
163
|
|
|
@@ -300,15 +289,17 @@ declare class SessionManager<TRequest, TResponse> {
|
|
|
300
289
|
private decryptSession;
|
|
301
290
|
private getSession;
|
|
302
291
|
private validateSession;
|
|
292
|
+
private validateAndRefreshIfNeeded;
|
|
293
|
+
private attemptTokenRefresh;
|
|
303
294
|
withAuth<TCustomClaims = CustomClaims>(request: TRequest): Promise<AuthResult<TCustomClaims>>;
|
|
304
295
|
refreshSession<TCustomClaims = CustomClaims>(session: Session, organizationId?: string): Promise<{
|
|
305
|
-
user: User;
|
|
296
|
+
user: _workos_inc_node.User;
|
|
306
297
|
sessionId: string;
|
|
307
298
|
organizationId: string | undefined;
|
|
308
299
|
role: string | undefined;
|
|
309
300
|
permissions: string[] | undefined;
|
|
310
301
|
entitlements: string[] | undefined;
|
|
311
|
-
impersonator: Impersonator | undefined;
|
|
302
|
+
impersonator: _workos_inc_node.Impersonator | undefined;
|
|
312
303
|
accessToken: string;
|
|
313
304
|
claims: BaseTokenClaims & TCustomClaims;
|
|
314
305
|
sessionData: string;
|
|
@@ -324,6 +315,7 @@ declare class SessionManager<TRequest, TResponse> {
|
|
|
324
315
|
response: TResponse;
|
|
325
316
|
authResult: AuthResult<TCustomClaims>;
|
|
326
317
|
}>;
|
|
318
|
+
private handleSwitchOrganizationError;
|
|
327
319
|
terminateSession(session: Session, response: TResponse, options?: {
|
|
328
320
|
returnTo?: string;
|
|
329
321
|
}): Promise<{
|
|
@@ -364,13 +356,13 @@ declare const createAuthKitFactory: <TRequest, TResponse>(options: {
|
|
|
364
356
|
screenHint?: "sign-up" | "sign-in";
|
|
365
357
|
}) => Promise<string>;
|
|
366
358
|
refreshSession: (session: Session, organizationId?: string | undefined) => Promise<{
|
|
367
|
-
user: User;
|
|
359
|
+
user: _workos_inc_node.User;
|
|
368
360
|
sessionId: string;
|
|
369
361
|
organizationId: string | undefined;
|
|
370
362
|
role: string | undefined;
|
|
371
363
|
permissions: string[] | undefined;
|
|
372
364
|
entitlements: string[] | undefined;
|
|
373
|
-
impersonator: Impersonator | undefined;
|
|
365
|
+
impersonator: _workos_inc_node.Impersonator | undefined;
|
|
374
366
|
accessToken: string;
|
|
375
367
|
claims: BaseTokenClaims & CustomClaims;
|
|
376
368
|
sessionData: string;
|
|
@@ -387,16 +379,27 @@ declare const createAuthKitFactory: <TRequest, TResponse>(options: {
|
|
|
387
379
|
loginHint?: string;
|
|
388
380
|
redirectUri?: string;
|
|
389
381
|
}) => Promise<string>;
|
|
390
|
-
|
|
382
|
+
terminateSession: (session: Session, response: any, options?: {
|
|
391
383
|
returnTo?: string;
|
|
392
384
|
} | undefined) => Promise<{
|
|
393
385
|
response: TResponse;
|
|
394
386
|
logoutUrl: string;
|
|
395
387
|
}>;
|
|
388
|
+
signOut: (request: TRequest, response: TResponse, options?: {
|
|
389
|
+
returnTo?: string;
|
|
390
|
+
}) => Promise<TResponse>;
|
|
396
391
|
switchToOrganization: (request: any, response: any, organizationId: string) => Promise<{
|
|
397
392
|
response: TResponse;
|
|
398
393
|
authResult: AuthResult<CustomClaims>;
|
|
399
394
|
}>;
|
|
395
|
+
handleCallback: (request: TRequest, response: TResponse, options: {
|
|
396
|
+
code: string;
|
|
397
|
+
state?: string;
|
|
398
|
+
}) => Promise<{
|
|
399
|
+
response: Awaited<TResponse>;
|
|
400
|
+
returnPathname: string;
|
|
401
|
+
authResponse: AuthenticationResponse;
|
|
402
|
+
}>;
|
|
400
403
|
getTokenClaims: <TCustomClaims = CustomClaims>(request: TRequest, accessToken?: string) => Promise<Partial<BaseTokenClaims & TCustomClaims>>;
|
|
401
404
|
};
|
|
402
405
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,37 +1,24 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as _workos_inc_node from '@workos-inc/node';
|
|
2
|
+
import { User, Impersonator, WorkOSOptions } from '@workos-inc/node';
|
|
3
|
+
export { Impersonator, Organization, User } from '@workos-inc/node';
|
|
2
4
|
import { JWTPayload } from 'jose';
|
|
3
5
|
|
|
4
6
|
interface UserManagementInterface {
|
|
5
7
|
getAuthorizationUrl: (options: AuthorizationURLOptions) => string;
|
|
6
8
|
authenticateWithCode: (options: AuthenticateWithCodeOptions) => Promise<AuthenticationResponse>;
|
|
7
9
|
authenticateWithRefreshToken: (options: AuthenticateWithRefreshTokenOptions) => Promise<AuthenticationResponse>;
|
|
8
|
-
getLogoutUrl: (
|
|
10
|
+
getLogoutUrl: (options: {
|
|
9
11
|
sessionId: string;
|
|
10
12
|
returnTo?: string;
|
|
11
13
|
}) => string;
|
|
14
|
+
revokeSession: (options: {
|
|
15
|
+
sessionId: string;
|
|
16
|
+
}) => Promise<void>;
|
|
12
17
|
}
|
|
13
18
|
interface WorkOSClient {
|
|
14
19
|
userManagement: UserManagementInterface;
|
|
15
20
|
getJwksUrl: (clientId: string) => string;
|
|
16
21
|
}
|
|
17
|
-
interface User {
|
|
18
|
-
object: string;
|
|
19
|
-
id: string;
|
|
20
|
-
email: string;
|
|
21
|
-
emailVerified: boolean;
|
|
22
|
-
profilePictureUrl: string | null;
|
|
23
|
-
firstName: string | null;
|
|
24
|
-
lastName: string | null;
|
|
25
|
-
lastSignInAt: string | null;
|
|
26
|
-
createdAt: string;
|
|
27
|
-
updatedAt: string;
|
|
28
|
-
externalId: string | null;
|
|
29
|
-
metadata: Record<string, string>;
|
|
30
|
-
}
|
|
31
|
-
interface Impersonator {
|
|
32
|
-
email: string;
|
|
33
|
-
reason: string | null;
|
|
34
|
-
}
|
|
35
22
|
interface AuthenticationResponse {
|
|
36
23
|
user: User;
|
|
37
24
|
organizationId?: string;
|
|
@@ -169,6 +156,8 @@ declare class ConfigurationProvider {
|
|
|
169
156
|
setValueSource(source: ValueSource): void;
|
|
170
157
|
configure(configOrSource: Partial<AuthKitConfig> | ValueSource, source?: ValueSource): void;
|
|
171
158
|
getValue<K extends keyof AuthKitConfig>(key: K): AuthKitConfig[K];
|
|
159
|
+
private getEnvironmentValue;
|
|
160
|
+
private convertValueType;
|
|
172
161
|
getConfig(): AuthKitConfig;
|
|
173
162
|
}
|
|
174
163
|
|
|
@@ -300,15 +289,17 @@ declare class SessionManager<TRequest, TResponse> {
|
|
|
300
289
|
private decryptSession;
|
|
301
290
|
private getSession;
|
|
302
291
|
private validateSession;
|
|
292
|
+
private validateAndRefreshIfNeeded;
|
|
293
|
+
private attemptTokenRefresh;
|
|
303
294
|
withAuth<TCustomClaims = CustomClaims>(request: TRequest): Promise<AuthResult<TCustomClaims>>;
|
|
304
295
|
refreshSession<TCustomClaims = CustomClaims>(session: Session, organizationId?: string): Promise<{
|
|
305
|
-
user: User;
|
|
296
|
+
user: _workos_inc_node.User;
|
|
306
297
|
sessionId: string;
|
|
307
298
|
organizationId: string | undefined;
|
|
308
299
|
role: string | undefined;
|
|
309
300
|
permissions: string[] | undefined;
|
|
310
301
|
entitlements: string[] | undefined;
|
|
311
|
-
impersonator: Impersonator | undefined;
|
|
302
|
+
impersonator: _workos_inc_node.Impersonator | undefined;
|
|
312
303
|
accessToken: string;
|
|
313
304
|
claims: BaseTokenClaims & TCustomClaims;
|
|
314
305
|
sessionData: string;
|
|
@@ -324,6 +315,7 @@ declare class SessionManager<TRequest, TResponse> {
|
|
|
324
315
|
response: TResponse;
|
|
325
316
|
authResult: AuthResult<TCustomClaims>;
|
|
326
317
|
}>;
|
|
318
|
+
private handleSwitchOrganizationError;
|
|
327
319
|
terminateSession(session: Session, response: TResponse, options?: {
|
|
328
320
|
returnTo?: string;
|
|
329
321
|
}): Promise<{
|
|
@@ -364,13 +356,13 @@ declare const createAuthKitFactory: <TRequest, TResponse>(options: {
|
|
|
364
356
|
screenHint?: "sign-up" | "sign-in";
|
|
365
357
|
}) => Promise<string>;
|
|
366
358
|
refreshSession: (session: Session, organizationId?: string | undefined) => Promise<{
|
|
367
|
-
user: User;
|
|
359
|
+
user: _workos_inc_node.User;
|
|
368
360
|
sessionId: string;
|
|
369
361
|
organizationId: string | undefined;
|
|
370
362
|
role: string | undefined;
|
|
371
363
|
permissions: string[] | undefined;
|
|
372
364
|
entitlements: string[] | undefined;
|
|
373
|
-
impersonator: Impersonator | undefined;
|
|
365
|
+
impersonator: _workos_inc_node.Impersonator | undefined;
|
|
374
366
|
accessToken: string;
|
|
375
367
|
claims: BaseTokenClaims & CustomClaims;
|
|
376
368
|
sessionData: string;
|
|
@@ -387,16 +379,27 @@ declare const createAuthKitFactory: <TRequest, TResponse>(options: {
|
|
|
387
379
|
loginHint?: string;
|
|
388
380
|
redirectUri?: string;
|
|
389
381
|
}) => Promise<string>;
|
|
390
|
-
|
|
382
|
+
terminateSession: (session: Session, response: any, options?: {
|
|
391
383
|
returnTo?: string;
|
|
392
384
|
} | undefined) => Promise<{
|
|
393
385
|
response: TResponse;
|
|
394
386
|
logoutUrl: string;
|
|
395
387
|
}>;
|
|
388
|
+
signOut: (request: TRequest, response: TResponse, options?: {
|
|
389
|
+
returnTo?: string;
|
|
390
|
+
}) => Promise<TResponse>;
|
|
396
391
|
switchToOrganization: (request: any, response: any, organizationId: string) => Promise<{
|
|
397
392
|
response: TResponse;
|
|
398
393
|
authResult: AuthResult<CustomClaims>;
|
|
399
394
|
}>;
|
|
395
|
+
handleCallback: (request: TRequest, response: TResponse, options: {
|
|
396
|
+
code: string;
|
|
397
|
+
state?: string;
|
|
398
|
+
}) => Promise<{
|
|
399
|
+
response: Awaited<TResponse>;
|
|
400
|
+
returnPathname: string;
|
|
401
|
+
authResponse: AuthenticationResponse;
|
|
402
|
+
}>;
|
|
400
403
|
getTokenClaims: <TCustomClaims = CustomClaims>(request: TRequest, accessToken?: string) => Promise<Partial<BaseTokenClaims & TCustomClaims>>;
|
|
401
404
|
};
|
|
402
405
|
|
package/dist/index.js
CHANGED
|
@@ -156,25 +156,10 @@ var ConfigurationProvider = class {
|
|
|
156
156
|
}
|
|
157
157
|
getValue(key) {
|
|
158
158
|
const envKey = this.getEnvironmentVariableName(key);
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
if (
|
|
162
|
-
|
|
163
|
-
} else if (valueSource && envKey in valueSource) {
|
|
164
|
-
envValue = valueSource[envKey];
|
|
165
|
-
}
|
|
166
|
-
if (envValue != null) {
|
|
167
|
-
if (key === "apiHttps" && typeof envValue === "string") {
|
|
168
|
-
return envValue === "true";
|
|
169
|
-
}
|
|
170
|
-
if ((key === "apiPort" || key === "cookieMaxAge") && typeof envValue === "string") {
|
|
171
|
-
const num = parseInt(envValue, 10);
|
|
172
|
-
return isNaN(num) ? void 0 : num;
|
|
173
|
-
}
|
|
174
|
-
return envValue;
|
|
175
|
-
}
|
|
176
|
-
if (key in config && config[key] != void 0) {
|
|
177
|
-
return config[key];
|
|
159
|
+
const envValue = this.getEnvironmentValue(envKey);
|
|
160
|
+
const rawValue = envValue ?? this.config[key];
|
|
161
|
+
if (rawValue != null) {
|
|
162
|
+
return this.convertValueType(key, rawValue);
|
|
178
163
|
}
|
|
179
164
|
if (this.requiredKeys.includes(key)) {
|
|
180
165
|
throw new Error(
|
|
@@ -183,6 +168,29 @@ var ConfigurationProvider = class {
|
|
|
183
168
|
}
|
|
184
169
|
return void 0;
|
|
185
170
|
}
|
|
171
|
+
getEnvironmentValue(envKey) {
|
|
172
|
+
const { valueSource } = this;
|
|
173
|
+
if (typeof valueSource === "function") {
|
|
174
|
+
return valueSource(envKey);
|
|
175
|
+
}
|
|
176
|
+
if (valueSource && envKey in valueSource) {
|
|
177
|
+
return valueSource[envKey];
|
|
178
|
+
}
|
|
179
|
+
return void 0;
|
|
180
|
+
}
|
|
181
|
+
convertValueType(key, value) {
|
|
182
|
+
if (typeof value !== "string") {
|
|
183
|
+
return value;
|
|
184
|
+
}
|
|
185
|
+
if (key === "apiHttps") {
|
|
186
|
+
return value === "true";
|
|
187
|
+
}
|
|
188
|
+
if (key === "apiPort" || key === "cookieMaxAge") {
|
|
189
|
+
const num = parseInt(value, 10);
|
|
190
|
+
return isNaN(num) ? void 0 : num;
|
|
191
|
+
}
|
|
192
|
+
return value;
|
|
193
|
+
}
|
|
186
194
|
getConfig() {
|
|
187
195
|
return this.config;
|
|
188
196
|
}
|
|
@@ -191,19 +199,16 @@ var ConfigurationProvider = class {
|
|
|
191
199
|
// src/core/config.ts
|
|
192
200
|
var getConfigurationInstance = once(() => new ConfigurationProvider());
|
|
193
201
|
function configure(configOrSource, source) {
|
|
194
|
-
|
|
195
|
-
config.configure(configOrSource, source);
|
|
202
|
+
getConfigurationInstance().configure(configOrSource, source);
|
|
196
203
|
}
|
|
197
204
|
function getConfig(key) {
|
|
198
|
-
|
|
199
|
-
return config.getValue(key);
|
|
205
|
+
return getConfigurationInstance().getValue(key);
|
|
200
206
|
}
|
|
201
207
|
function getConfigurationProvider() {
|
|
202
208
|
return getConfigurationInstance();
|
|
203
209
|
}
|
|
204
210
|
function getFullConfig() {
|
|
205
|
-
|
|
206
|
-
return config.getConfig();
|
|
211
|
+
return getConfigurationInstance().getConfig();
|
|
207
212
|
}
|
|
208
213
|
|
|
209
214
|
// src/core/client/WorkOSLite.ts
|
|
@@ -254,6 +259,14 @@ var UserManagement = class {
|
|
|
254
259
|
}
|
|
255
260
|
return url.toString();
|
|
256
261
|
}
|
|
262
|
+
async revokeSession({ sessionId }) {
|
|
263
|
+
if (!sessionId) {
|
|
264
|
+
throw new TypeError("sessionId is required");
|
|
265
|
+
}
|
|
266
|
+
await this.client.post("/user_management/sessions/revoke", {
|
|
267
|
+
session_id: sessionId
|
|
268
|
+
});
|
|
269
|
+
}
|
|
257
270
|
getAuthorizationUrl({
|
|
258
271
|
connectionId,
|
|
259
272
|
codeChallenge,
|
|
@@ -416,7 +429,7 @@ var AuthKitError = class extends Error {
|
|
|
416
429
|
this.data = data;
|
|
417
430
|
}
|
|
418
431
|
};
|
|
419
|
-
var
|
|
432
|
+
var SessionEncryptionError = class extends AuthKitError {
|
|
420
433
|
constructor(message, cause) {
|
|
421
434
|
super(message, cause);
|
|
422
435
|
this.name = "SessionEncryptionError";
|
|
@@ -452,7 +465,7 @@ var SessionManager = class {
|
|
|
452
465
|
});
|
|
453
466
|
return encryptedSession;
|
|
454
467
|
} catch (error) {
|
|
455
|
-
throw new
|
|
468
|
+
throw new SessionEncryptionError("Failed to encrypt session", error);
|
|
456
469
|
}
|
|
457
470
|
}
|
|
458
471
|
async decryptSession(encryptedSession) {
|
|
@@ -465,7 +478,7 @@ var SessionManager = class {
|
|
|
465
478
|
return session;
|
|
466
479
|
} catch (error) {
|
|
467
480
|
console.error("Detailed error:", error);
|
|
468
|
-
throw new
|
|
481
|
+
throw new SessionEncryptionError("Failed to decrypt session", error);
|
|
469
482
|
}
|
|
470
483
|
}
|
|
471
484
|
async getSession(request) {
|
|
@@ -478,39 +491,7 @@ var SessionManager = class {
|
|
|
478
491
|
async validateSession(encryptedSession) {
|
|
479
492
|
try {
|
|
480
493
|
const session = await this.decryptSession(encryptedSession);
|
|
481
|
-
|
|
482
|
-
if (isValid) {
|
|
483
|
-
if (this.tokenManager.isTokenExpiring(session.accessToken)) {
|
|
484
|
-
const refreshResult = await this.refreshSession(session);
|
|
485
|
-
return {
|
|
486
|
-
valid: true,
|
|
487
|
-
session: refreshResult.session,
|
|
488
|
-
claims: refreshResult.claims
|
|
489
|
-
};
|
|
490
|
-
}
|
|
491
|
-
const claims = this.tokenManager.parseTokenClaims(
|
|
492
|
-
session.accessToken
|
|
493
|
-
);
|
|
494
|
-
return {
|
|
495
|
-
valid: true,
|
|
496
|
-
session,
|
|
497
|
-
claims
|
|
498
|
-
};
|
|
499
|
-
} else {
|
|
500
|
-
try {
|
|
501
|
-
const refreshResult = await this.refreshSession(session);
|
|
502
|
-
return {
|
|
503
|
-
valid: true,
|
|
504
|
-
session: refreshResult.session,
|
|
505
|
-
claims: refreshResult.claims
|
|
506
|
-
};
|
|
507
|
-
} catch (error) {
|
|
508
|
-
return {
|
|
509
|
-
valid: false,
|
|
510
|
-
error: error instanceof Error ? error : new TokenRefreshError("Failed to refresh session", error)
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
}
|
|
494
|
+
return await this.validateAndRefreshIfNeeded(session);
|
|
514
495
|
} catch (error) {
|
|
515
496
|
return {
|
|
516
497
|
valid: false,
|
|
@@ -518,6 +499,36 @@ var SessionManager = class {
|
|
|
518
499
|
};
|
|
519
500
|
}
|
|
520
501
|
}
|
|
502
|
+
async validateAndRefreshIfNeeded(session) {
|
|
503
|
+
const isValid = await this.tokenManager.verifyToken(session.accessToken);
|
|
504
|
+
const isExpiring = this.tokenManager.isTokenExpiring(session.accessToken);
|
|
505
|
+
if (!isValid || isExpiring) {
|
|
506
|
+
return await this.attemptTokenRefresh(session);
|
|
507
|
+
}
|
|
508
|
+
const claims = this.tokenManager.parseTokenClaims(
|
|
509
|
+
session.accessToken
|
|
510
|
+
);
|
|
511
|
+
return {
|
|
512
|
+
valid: true,
|
|
513
|
+
session,
|
|
514
|
+
claims
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
async attemptTokenRefresh(session) {
|
|
518
|
+
try {
|
|
519
|
+
const refreshResult = await this.refreshSession(session);
|
|
520
|
+
return {
|
|
521
|
+
valid: true,
|
|
522
|
+
session: refreshResult.session,
|
|
523
|
+
claims: refreshResult.claims
|
|
524
|
+
};
|
|
525
|
+
} catch (error) {
|
|
526
|
+
return {
|
|
527
|
+
valid: false,
|
|
528
|
+
error: error instanceof Error ? error : new TokenRefreshError("Failed to refresh session", error)
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
}
|
|
521
532
|
async withAuth(request) {
|
|
522
533
|
const encryptedSession = await this.storage.getSession(request);
|
|
523
534
|
if (!encryptedSession) {
|
|
@@ -627,42 +638,43 @@ var SessionManager = class {
|
|
|
627
638
|
}
|
|
628
639
|
};
|
|
629
640
|
} catch (error) {
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
if (error?.authkit_redirect_url) {
|
|
651
|
-
throw new AuthKitError(
|
|
652
|
-
"Organization switch requires authentication",
|
|
653
|
-
error,
|
|
654
|
-
{ authUrl: error.authkit_redirect_url }
|
|
655
|
-
);
|
|
656
|
-
}
|
|
657
|
-
throw new AuthKitError(
|
|
658
|
-
`Failed to switch to organization ${organizationId}`,
|
|
659
|
-
error
|
|
641
|
+
throw await this.handleSwitchOrganizationError(error, organizationId);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
async handleSwitchOrganizationError(error, organizationId) {
|
|
645
|
+
const errorCodeToMessage = {
|
|
646
|
+
sso_required: `SSO required for organization ${organizationId}`,
|
|
647
|
+
mfa_enrollment: `MFA enrollment required for organization ${organizationId}`
|
|
648
|
+
};
|
|
649
|
+
const errorMessage = errorCodeToMessage[error?.code];
|
|
650
|
+
if (errorMessage) {
|
|
651
|
+
const authUrl = await this.getAuthorizationUrl({
|
|
652
|
+
redirectUri: this.config.getValue("redirectUri")
|
|
653
|
+
});
|
|
654
|
+
return new AuthKitError(errorMessage, error, { authUrl });
|
|
655
|
+
}
|
|
656
|
+
if (error?.authkit_redirect_url) {
|
|
657
|
+
return new AuthKitError(
|
|
658
|
+
"Organization switch requires authentication",
|
|
659
|
+
error,
|
|
660
|
+
{ authUrl: error.authkit_redirect_url }
|
|
660
661
|
);
|
|
661
662
|
}
|
|
663
|
+
return new AuthKitError(
|
|
664
|
+
`Failed to switch to organization ${organizationId}`,
|
|
665
|
+
error
|
|
666
|
+
);
|
|
662
667
|
}
|
|
663
668
|
async terminateSession(session, response, options) {
|
|
664
|
-
const clearedResponse = await this.storage.clearSession(response);
|
|
665
669
|
const claims = this.tokenManager.parseTokenClaims(session.accessToken);
|
|
670
|
+
try {
|
|
671
|
+
await this.client.userManagement.revokeSession({
|
|
672
|
+
sessionId: claims.sid
|
|
673
|
+
});
|
|
674
|
+
} catch (error) {
|
|
675
|
+
console.error("Failed to revoke session on WorkOS:", error);
|
|
676
|
+
}
|
|
677
|
+
const clearedResponse = await this.storage.clearSession(response);
|
|
666
678
|
const logoutUrl = this.client.userManagement.getLogoutUrl({
|
|
667
679
|
sessionId: claims.sid,
|
|
668
680
|
returnTo: options?.returnTo
|
|
@@ -806,7 +818,7 @@ var TokenManager = class {
|
|
|
806
818
|
}
|
|
807
819
|
getTokenExpiryTime(token) {
|
|
808
820
|
const claims = this.parseTokenClaims(token);
|
|
809
|
-
return claims.exp
|
|
821
|
+
return claims.exp ?? null;
|
|
810
822
|
}
|
|
811
823
|
};
|
|
812
824
|
var TokenManager_default = TokenManager;
|
|
@@ -843,24 +855,58 @@ var createAuthKitFactory = once(function createAuthKit(options) {
|
|
|
843
855
|
...options2,
|
|
844
856
|
screenHint: "sign-up"
|
|
845
857
|
}),
|
|
846
|
-
|
|
858
|
+
terminateSession: (...args) => getSessionManager().terminateSession(...args),
|
|
859
|
+
signOut: async (request, response, options2) => {
|
|
860
|
+
const authResult = await getSessionManager().withAuth(request);
|
|
861
|
+
if (!authResult.user || !authResult.accessToken || !authResult.refreshToken) {
|
|
862
|
+
return sessionStorageFactory(getFullConfig()).clearSession(response);
|
|
863
|
+
}
|
|
864
|
+
const session = {
|
|
865
|
+
accessToken: authResult.accessToken,
|
|
866
|
+
refreshToken: authResult.refreshToken,
|
|
867
|
+
user: authResult.user,
|
|
868
|
+
impersonator: authResult.impersonator
|
|
869
|
+
};
|
|
870
|
+
const result = await getSessionManager().terminateSession(
|
|
871
|
+
session,
|
|
872
|
+
response,
|
|
873
|
+
options2
|
|
874
|
+
);
|
|
875
|
+
return result.response;
|
|
876
|
+
},
|
|
847
877
|
switchToOrganization: (...args) => getSessionManager().switchToOrganization(...args),
|
|
848
|
-
|
|
849
|
-
|
|
878
|
+
handleCallback: async (request, response, options2) => {
|
|
879
|
+
const client = clientFactory(getFullConfig());
|
|
880
|
+
const config = getFullConfig();
|
|
881
|
+
const authResponse = await client.userManagement.authenticateWithCode({
|
|
882
|
+
code: options2.code,
|
|
883
|
+
clientId: config.clientId
|
|
884
|
+
});
|
|
885
|
+
const updatedResponse = await getSessionManager().createSession(
|
|
886
|
+
authResponse,
|
|
887
|
+
response
|
|
888
|
+
);
|
|
889
|
+
let returnPathname = "/";
|
|
890
|
+
if (options2.state) {
|
|
850
891
|
try {
|
|
851
|
-
|
|
892
|
+
const decoded = JSON.parse(atob(options2.state));
|
|
893
|
+
returnPathname = decoded.returnPathname || "/";
|
|
852
894
|
} catch {
|
|
853
|
-
return {};
|
|
854
895
|
}
|
|
855
896
|
}
|
|
856
|
-
|
|
857
|
-
|
|
897
|
+
return {
|
|
898
|
+
response: updatedResponse,
|
|
899
|
+
returnPathname,
|
|
900
|
+
authResponse
|
|
901
|
+
};
|
|
902
|
+
},
|
|
903
|
+
getTokenClaims: async (request, accessToken) => {
|
|
904
|
+
const tokenToUse = accessToken || (await getSessionManager().withAuth(request)).accessToken;
|
|
905
|
+
if (!tokenToUse) {
|
|
858
906
|
return {};
|
|
859
907
|
}
|
|
860
908
|
try {
|
|
861
|
-
return getTokenManager().parseTokenClaims(
|
|
862
|
-
authResult.accessToken
|
|
863
|
-
);
|
|
909
|
+
return getTokenManager().parseTokenClaims(tokenToUse);
|
|
864
910
|
} catch {
|
|
865
911
|
return {};
|
|
866
912
|
}
|