@workos/authkit-session 0.0.1-alpha.1
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/LICENSE +21 -0
- package/README.md +182 -0
- package/dist/index.cjs +878 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +403 -0
- package/dist/index.d.ts +403 -0
- package/dist/index.js +878 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
- package/src/HttpClient.ts +160 -0
- package/src/adapters/chrome/ChromeExtensionStorage.ts +34 -0
- package/src/core/UserManagement.ts +8 -0
- package/src/core/client/WorkOSLite.ts +274 -0
- package/src/core/client/types.ts +108 -0
- package/src/core/client/workos.ts +38 -0
- package/src/core/config/ConfigurationProvider.ts +127 -0
- package/src/core/config/types.ts +73 -0
- package/src/core/config.ts +72 -0
- package/src/core/createAuthKitFactory.ts +124 -0
- package/src/core/encryption/ironSessionEncryption.ts +6 -0
- package/src/core/encryption/ironWebcryptoEncryption.ts +107 -0
- package/src/core/errors.ts +31 -0
- package/src/core/iron.ts +103 -0
- package/src/core/session/CookieSessionStorage.ts +42 -0
- package/src/core/session/SessionManager.ts +348 -0
- package/src/core/session/TokenManager.ts +55 -0
- package/src/core/session/types.ts +88 -0
- package/src/index.ts +7 -0
- package/src/utils.ts +21 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
export interface UserManagementInterface {
|
|
2
|
+
getAuthorizationUrl: (options: AuthorizationURLOptions) => string;
|
|
3
|
+
authenticateWithCode: (
|
|
4
|
+
options: AuthenticateWithCodeOptions,
|
|
5
|
+
) => Promise<AuthenticationResponse>;
|
|
6
|
+
authenticateWithRefreshToken: (
|
|
7
|
+
options: AuthenticateWithRefreshTokenOptions,
|
|
8
|
+
) => Promise<AuthenticationResponse>;
|
|
9
|
+
getLogoutUrl: (optoions: { sessionId: string; returnTo?: string }) => string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface WorkOSClientConstructor {
|
|
13
|
+
new (apiKey: string, options?: WorkOSOptions): WorkOSClient;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface WorkOSClient {
|
|
17
|
+
userManagement: UserManagementInterface;
|
|
18
|
+
getJwksUrl: (clientId: string) => string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AppInfo {
|
|
22
|
+
name: string;
|
|
23
|
+
version: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface WorkOSOptions {
|
|
27
|
+
apiHostname?: string;
|
|
28
|
+
https?: boolean;
|
|
29
|
+
port?: number;
|
|
30
|
+
config?: RequestInit;
|
|
31
|
+
appInfo?: AppInfo;
|
|
32
|
+
fetchFn?: typeof fetch;
|
|
33
|
+
clientId?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface User {
|
|
37
|
+
object: string;
|
|
38
|
+
id: string;
|
|
39
|
+
email: string;
|
|
40
|
+
emailVerified: boolean;
|
|
41
|
+
profilePictureUrl: string | null;
|
|
42
|
+
firstName: string | null;
|
|
43
|
+
lastName: string | null;
|
|
44
|
+
lastSignInAt: string | null;
|
|
45
|
+
createdAt: string;
|
|
46
|
+
updatedAt: string;
|
|
47
|
+
externalId: string | null;
|
|
48
|
+
metadata: Record<string, string>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface Impersonator {
|
|
52
|
+
email: string;
|
|
53
|
+
reason: string | null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface AuthenticationResponse {
|
|
57
|
+
user: User;
|
|
58
|
+
organizationId?: string;
|
|
59
|
+
accessToken: string;
|
|
60
|
+
refreshToken: string;
|
|
61
|
+
impersonator?: Impersonator;
|
|
62
|
+
authenticationMethod?: string;
|
|
63
|
+
sealedSession?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface AuthenticateWithOptionsBase {
|
|
67
|
+
clientId: string;
|
|
68
|
+
ipAddress?: string;
|
|
69
|
+
userAgent?: string;
|
|
70
|
+
session?: {
|
|
71
|
+
cookiePassword?: string;
|
|
72
|
+
sealSession: boolean;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface AuthenticateWithRefreshTokenOptions
|
|
77
|
+
extends AuthenticateWithOptionsBase {
|
|
78
|
+
refreshToken: string;
|
|
79
|
+
organizationId?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface AuthenticateWithCodeOptions
|
|
83
|
+
extends AuthenticateWithOptionsBase {
|
|
84
|
+
codeVerifier?: string;
|
|
85
|
+
code: string;
|
|
86
|
+
invitationToken?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface AuthenticateWithEmailVerificationOptions
|
|
90
|
+
extends AuthenticateWithOptionsBase {
|
|
91
|
+
code: string;
|
|
92
|
+
pendingAuthenticationToken: string;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface AuthorizationURLOptions {
|
|
96
|
+
clientId: string;
|
|
97
|
+
codeChallenge?: string;
|
|
98
|
+
codeChallengeMethod?: 'S256';
|
|
99
|
+
connectionId?: string;
|
|
100
|
+
context?: string;
|
|
101
|
+
organizationId?: string;
|
|
102
|
+
domainHint?: string;
|
|
103
|
+
loginHint?: string;
|
|
104
|
+
provider?: string;
|
|
105
|
+
redirectUri: string;
|
|
106
|
+
state?: string;
|
|
107
|
+
screenHint?: 'sign-up' | 'sign-in';
|
|
108
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { WorkOS } from '@workos-inc/node';
|
|
2
|
+
import { getConfig } from '../config';
|
|
3
|
+
import { once } from '../../utils';
|
|
4
|
+
import { version } from '../../../package.json';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a WorkOS instance with the provided API key and optional settings.
|
|
8
|
+
*/
|
|
9
|
+
export function createWorkOSInstance() {
|
|
10
|
+
// Get required API key from config
|
|
11
|
+
const apiKey = getConfig('apiKey');
|
|
12
|
+
|
|
13
|
+
// Get optional settings
|
|
14
|
+
const apiHostname = getConfig('apiHostname');
|
|
15
|
+
const apiHttps = getConfig('apiHttps');
|
|
16
|
+
const apiPort = getConfig('apiPort');
|
|
17
|
+
|
|
18
|
+
const options = {
|
|
19
|
+
apiHostname,
|
|
20
|
+
https: apiHttps,
|
|
21
|
+
port: apiPort,
|
|
22
|
+
appInfo: {
|
|
23
|
+
name: 'authkit-react-router',
|
|
24
|
+
version,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Initialize the WorkOS client with config values
|
|
29
|
+
const workos = new WorkOS(apiKey, options);
|
|
30
|
+
|
|
31
|
+
return workos;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Create a WorkOS instance with the provided API key and optional settings.
|
|
36
|
+
* This function is lazy loaded to avoid loading the WorkOS SDK when it's not needed.
|
|
37
|
+
*/
|
|
38
|
+
export const getWorkOS = once(createWorkOSInstance);
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { AuthKitConfig, ValueSource } from './types.js';
|
|
2
|
+
import { once } from '../../utils.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Default environment variable source that uses process.env
|
|
6
|
+
*/
|
|
7
|
+
const defaultSource: ValueSource = (key: string): string | undefined => {
|
|
8
|
+
try {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
const processEnv: Record<string, string> | undefined = (globalThis as any)
|
|
11
|
+
?.process?.env;
|
|
12
|
+
return processEnv?.[key];
|
|
13
|
+
} catch {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Configuration class for AuthKit.
|
|
20
|
+
* This class is used to manage configuration values and provide defaults.
|
|
21
|
+
* It also provides a way to get configuration values from environment variables.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export class ConfigurationProvider {
|
|
25
|
+
private config: Partial<AuthKitConfig> = {
|
|
26
|
+
cookieName: 'wos-session',
|
|
27
|
+
apiHttps: true,
|
|
28
|
+
// Defaults to 400 days, the maximum allowed by Chrome
|
|
29
|
+
// It's fine to have a long cookie expiry date as the access/refresh tokens
|
|
30
|
+
// act as the actual time-limited aspects of the session.
|
|
31
|
+
cookieMaxAge: 60 * 60 * 24 * 400,
|
|
32
|
+
apiHostname: 'api.workos.com',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
private valueSource: ValueSource = defaultSource;
|
|
36
|
+
|
|
37
|
+
private readonly requiredKeys: (keyof AuthKitConfig)[] = [
|
|
38
|
+
'clientId',
|
|
39
|
+
'apiKey',
|
|
40
|
+
'redirectUri',
|
|
41
|
+
'cookiePassword',
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Convert a camelCase string to an uppercase, underscore-separated environment variable name.
|
|
46
|
+
* @param str The string to convert
|
|
47
|
+
* @returns The environment variable name
|
|
48
|
+
*/
|
|
49
|
+
protected getEnvironmentVariableName(str: string) {
|
|
50
|
+
return `WORKOS_${str.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase()}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private updateConfig(config: Partial<AuthKitConfig>): void {
|
|
54
|
+
this.config = { ...this.config, ...config };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
setValueSource(source: ValueSource): void {
|
|
58
|
+
this.valueSource = source;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
configure(
|
|
62
|
+
configOrSource: Partial<AuthKitConfig> | ValueSource,
|
|
63
|
+
source?: ValueSource,
|
|
64
|
+
): void {
|
|
65
|
+
if (typeof configOrSource === 'function') {
|
|
66
|
+
this.setValueSource(configOrSource);
|
|
67
|
+
} else if (typeof configOrSource === 'object' && !source) {
|
|
68
|
+
this.updateConfig(configOrSource);
|
|
69
|
+
} else if (typeof configOrSource === 'object' && source) {
|
|
70
|
+
this.updateConfig(configOrSource);
|
|
71
|
+
this.setValueSource(source);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Validate the cookiePassword if provided
|
|
75
|
+
if (this.config.cookiePassword && this.config.cookiePassword.length < 32) {
|
|
76
|
+
throw new Error('cookiePassword must be at least 32 characters long');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getValue<K extends keyof AuthKitConfig>(key: K): AuthKitConfig[K] {
|
|
81
|
+
// First check environment variables
|
|
82
|
+
const envKey = this.getEnvironmentVariableName(key);
|
|
83
|
+
let envValue: AuthKitConfig[K] | undefined = undefined;
|
|
84
|
+
|
|
85
|
+
const { valueSource, config } = this;
|
|
86
|
+
if (typeof valueSource === 'function') {
|
|
87
|
+
envValue = valueSource(envKey);
|
|
88
|
+
} else if (valueSource && envKey in valueSource) {
|
|
89
|
+
envValue = valueSource[envKey];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// If environment variable exists, use it
|
|
93
|
+
if (envValue != null) {
|
|
94
|
+
// Convert string values to appropriate types
|
|
95
|
+
if (key === 'apiHttps' && typeof envValue === 'string') {
|
|
96
|
+
return (envValue === 'true') as AuthKitConfig[K];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (
|
|
100
|
+
(key === 'apiPort' || key === 'cookieMaxAge') &&
|
|
101
|
+
typeof envValue === 'string'
|
|
102
|
+
) {
|
|
103
|
+
const num = parseInt(envValue, 10);
|
|
104
|
+
return (isNaN(num) ? undefined : num) as AuthKitConfig[K];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return envValue as AuthKitConfig[K];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Then check programmatically provided config
|
|
111
|
+
if (key in config && config[key] != undefined) {
|
|
112
|
+
return config[key] as AuthKitConfig[K];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (this.requiredKeys.includes(key)) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
`Missing required configuration value for ${key} (${envKey}).`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return undefined as AuthKitConfig[K];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
getConfig(): AuthKitConfig {
|
|
125
|
+
return this.config as AuthKitConfig;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthKit Configuration Options
|
|
3
|
+
*/
|
|
4
|
+
export interface AuthKitConfig {
|
|
5
|
+
/**
|
|
6
|
+
* The WorkOS Client ID
|
|
7
|
+
* Equivalent to the WORKOS_CLIENT_ID environment variable
|
|
8
|
+
*/
|
|
9
|
+
clientId: string;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The WorkOS API Key
|
|
13
|
+
* Equivalent to the WORKOS_API_KEY environment variable
|
|
14
|
+
*/
|
|
15
|
+
apiKey: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The redirect URI for the authentication callback
|
|
19
|
+
* Equivalent to the WORKOS_REDIRECT_URI environment variable
|
|
20
|
+
*/
|
|
21
|
+
redirectUri: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The password used to encrypt the session cookie
|
|
25
|
+
* Equivalent to the WORKOS_COOKIE_PASSWORD environment variable
|
|
26
|
+
* Must be at least 32 characters long
|
|
27
|
+
*/
|
|
28
|
+
cookiePassword: string;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The hostname of the API to use
|
|
32
|
+
* Equivalent to the WORKOS_API_HOSTNAME environment variable
|
|
33
|
+
*/
|
|
34
|
+
apiHostname?: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether to use HTTPS for API requests
|
|
38
|
+
* Equivalent to the WORKOS_API_HTTPS environment variable
|
|
39
|
+
*/
|
|
40
|
+
apiHttps: boolean;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The port to use for the API
|
|
44
|
+
* Equivalent to the WORKOS_API_PORT environment variable
|
|
45
|
+
*/
|
|
46
|
+
apiPort?: number;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The maximum age of the session cookie in seconds
|
|
50
|
+
* Equivalent to the WORKOS_COOKIE_MAX_AGE environment variable
|
|
51
|
+
*/
|
|
52
|
+
cookieMaxAge: number;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The sameSite attribute for the session cookie
|
|
56
|
+
*/
|
|
57
|
+
cookieSameSite?: 'strict' | 'lax' | 'none';
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The name of the session cookie
|
|
61
|
+
* Equivalent to the WORKOS_COOKIE_NAME environment variable
|
|
62
|
+
* Defaults to "wos-session"
|
|
63
|
+
*/
|
|
64
|
+
cookieName: string;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The domain for the session cookie
|
|
68
|
+
*/
|
|
69
|
+
cookieDomain?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
+
export type ValueSource = Record<string, any> | ((key: string) => any);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { once } from '../utils';
|
|
2
|
+
import { ConfigurationProvider } from './config/ConfigurationProvider';
|
|
3
|
+
import type { AuthKitConfig, ValueSource } from './config/types';
|
|
4
|
+
|
|
5
|
+
const getConfigurationInstance = once(() => new ConfigurationProvider());
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Configure AuthKit with a custom value source.
|
|
9
|
+
* @param source The source of configuration values
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* configure(key => Deno.env.get(key));
|
|
13
|
+
*/
|
|
14
|
+
export function configure(source: ValueSource): void;
|
|
15
|
+
/**
|
|
16
|
+
* Configure AuthKit with custom values.
|
|
17
|
+
* @param config The configuration values
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* configure({
|
|
21
|
+
* clientId: 'your-client-id',
|
|
22
|
+
* redirectUri: 'https://your-app.com/auth/callback',
|
|
23
|
+
* apiKey: 'your-api-key',
|
|
24
|
+
* cookiePassword: 'your-cookie-password',
|
|
25
|
+
* });
|
|
26
|
+
*/
|
|
27
|
+
export function configure(config: Partial<AuthKitConfig>): void;
|
|
28
|
+
/**
|
|
29
|
+
* Configure AuthKit with custom values and a custom value source.
|
|
30
|
+
* @param config The configuration values
|
|
31
|
+
* @param source The source of configuration values
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* configure({
|
|
35
|
+
* clientId: 'your-client-id',
|
|
36
|
+
* }, env);
|
|
37
|
+
*/
|
|
38
|
+
export function configure(
|
|
39
|
+
config: Partial<AuthKitConfig>,
|
|
40
|
+
source: ValueSource,
|
|
41
|
+
): void;
|
|
42
|
+
export function configure(
|
|
43
|
+
configOrSource: Partial<AuthKitConfig> | ValueSource,
|
|
44
|
+
source?: ValueSource,
|
|
45
|
+
): void {
|
|
46
|
+
const config = getConfigurationInstance();
|
|
47
|
+
config.configure(configOrSource, source);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Get a configuration value by key.
|
|
52
|
+
* This function will first check environment variables, then programmatically provided config,
|
|
53
|
+
* and finally fall back to defaults for optional settings.
|
|
54
|
+
* If a required setting is missing, an error will be thrown.
|
|
55
|
+
* @param key The configuration key
|
|
56
|
+
* @returns The configuration value
|
|
57
|
+
*/
|
|
58
|
+
export function getConfig<K extends keyof AuthKitConfig>(
|
|
59
|
+
key: K,
|
|
60
|
+
): AuthKitConfig[K] {
|
|
61
|
+
const config = getConfigurationInstance();
|
|
62
|
+
return config.getValue(key);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function getConfigurationProvider(): ConfigurationProvider {
|
|
66
|
+
return getConfigurationInstance();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function getFullConfig(): AuthKitConfig {
|
|
70
|
+
const config = getConfigurationInstance();
|
|
71
|
+
return config.getConfig();
|
|
72
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { once } from '../utils';
|
|
2
|
+
import { getWorkOS } from './client/WorkOSLite';
|
|
3
|
+
import type { WorkOSClient } from './client/types';
|
|
4
|
+
import { getConfig, getConfigurationProvider, getFullConfig } from './config';
|
|
5
|
+
import type { AuthKitConfig } from './config/types';
|
|
6
|
+
import sessionEncryption from './encryption/ironWebcryptoEncryption';
|
|
7
|
+
import { SessionManager } from './session/SessionManager';
|
|
8
|
+
import TokenManager from './session/TokenManager';
|
|
9
|
+
import type {
|
|
10
|
+
BaseTokenClaims,
|
|
11
|
+
CustomClaims,
|
|
12
|
+
SessionEncryption,
|
|
13
|
+
SessionStorage,
|
|
14
|
+
} from './session/types';
|
|
15
|
+
|
|
16
|
+
export const createAuthKitFactory = once(function createAuthKit<
|
|
17
|
+
TRequest,
|
|
18
|
+
TResponse,
|
|
19
|
+
>(options: {
|
|
20
|
+
sessionStorageFactory: (
|
|
21
|
+
config: AuthKitConfig,
|
|
22
|
+
) => SessionStorage<TRequest, TResponse>;
|
|
23
|
+
sessionEncryptionFactory?: (confg: AuthKitConfig) => SessionEncryption;
|
|
24
|
+
clientFactory?: (config: AuthKitConfig) => WorkOSClient;
|
|
25
|
+
}) {
|
|
26
|
+
const {
|
|
27
|
+
sessionStorageFactory,
|
|
28
|
+
clientFactory = () => getWorkOS(),
|
|
29
|
+
sessionEncryptionFactory = () => sessionEncryption,
|
|
30
|
+
} = options;
|
|
31
|
+
|
|
32
|
+
const getTokenManager = once(
|
|
33
|
+
() =>
|
|
34
|
+
new TokenManager(getConfig('clientId'), clientFactory(getFullConfig())),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const getSessionManager = once(
|
|
38
|
+
() =>
|
|
39
|
+
new SessionManager<TRequest, TResponse>(
|
|
40
|
+
getConfigurationProvider(),
|
|
41
|
+
sessionStorageFactory(getFullConfig()),
|
|
42
|
+
getTokenManager(),
|
|
43
|
+
clientFactory(getFullConfig()),
|
|
44
|
+
sessionEncryptionFactory(getFullConfig()),
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
withAuth: (
|
|
50
|
+
...args: Parameters<(typeof SessionManager.prototype)['withAuth']>
|
|
51
|
+
) => getSessionManager().withAuth(...args),
|
|
52
|
+
|
|
53
|
+
getAuthorizationUrl: (
|
|
54
|
+
...args: Parameters<
|
|
55
|
+
(typeof SessionManager.prototype)['getAuthorizationUrl']
|
|
56
|
+
>
|
|
57
|
+
) => getSessionManager().getAuthorizationUrl(...args),
|
|
58
|
+
|
|
59
|
+
refreshSession: (
|
|
60
|
+
...args: Parameters<(typeof SessionManager.prototype)['refreshSession']>
|
|
61
|
+
) => getSessionManager().refreshSession(...args),
|
|
62
|
+
|
|
63
|
+
saveSession: (
|
|
64
|
+
...args: Parameters<SessionStorage<TRequest, TResponse>['saveSession']>
|
|
65
|
+
) => sessionStorageFactory(getFullConfig()).saveSession(...args),
|
|
66
|
+
|
|
67
|
+
getSignInUrl: (options: {
|
|
68
|
+
organizationId?: string;
|
|
69
|
+
loginHint?: string;
|
|
70
|
+
redirectUri?: string;
|
|
71
|
+
}) =>
|
|
72
|
+
getSessionManager().getAuthorizationUrl({
|
|
73
|
+
...options,
|
|
74
|
+
screenHint: 'sign-in',
|
|
75
|
+
}),
|
|
76
|
+
|
|
77
|
+
getSignUpUrl: (options: {
|
|
78
|
+
organizationId?: string;
|
|
79
|
+
loginHint?: string;
|
|
80
|
+
redirectUri?: string;
|
|
81
|
+
}) =>
|
|
82
|
+
getSessionManager().getAuthorizationUrl({
|
|
83
|
+
...options,
|
|
84
|
+
screenHint: 'sign-up',
|
|
85
|
+
}),
|
|
86
|
+
|
|
87
|
+
getLogoutUrl: (
|
|
88
|
+
...args: Parameters<(typeof SessionManager.prototype)['terminateSession']>
|
|
89
|
+
) => getSessionManager().terminateSession(...args),
|
|
90
|
+
|
|
91
|
+
switchToOrganization: (
|
|
92
|
+
...args: Parameters<
|
|
93
|
+
(typeof SessionManager.prototype)['switchToOrganization']
|
|
94
|
+
>
|
|
95
|
+
) => getSessionManager().switchToOrganization(...args),
|
|
96
|
+
|
|
97
|
+
getTokenClaims: async <TCustomClaims = CustomClaims>(
|
|
98
|
+
request: TRequest,
|
|
99
|
+
accessToken?: string,
|
|
100
|
+
): Promise<Partial<BaseTokenClaims & TCustomClaims>> => {
|
|
101
|
+
if (accessToken) {
|
|
102
|
+
try {
|
|
103
|
+
return getTokenManager().parseTokenClaims<TCustomClaims>(accessToken);
|
|
104
|
+
} catch {
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const authResult =
|
|
110
|
+
await getSessionManager().withAuth<TCustomClaims>(request);
|
|
111
|
+
if (!authResult.accessToken) {
|
|
112
|
+
return {};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
return getTokenManager().parseTokenClaims<TCustomClaims>(
|
|
117
|
+
authResult.accessToken,
|
|
118
|
+
);
|
|
119
|
+
} catch {
|
|
120
|
+
return {};
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { seal as sealData, unseal as unsealData } from 'iron-webcrypto';
|
|
2
|
+
import type { SessionEncryption as SessionEncryptionInterface } from '../session/types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A compatible implementation that works with iron-session
|
|
6
|
+
*/
|
|
7
|
+
export class SessionEncryption implements SessionEncryptionInterface {
|
|
8
|
+
private readonly versionDelimiter = '~';
|
|
9
|
+
private readonly currentMajorVersion = 2;
|
|
10
|
+
|
|
11
|
+
// Parse an iron-session seal to extract the version
|
|
12
|
+
private parseSeal(seal: string): {
|
|
13
|
+
sealWithoutVersion: string;
|
|
14
|
+
tokenVersion: number | null;
|
|
15
|
+
} {
|
|
16
|
+
const [sealWithoutVersion = '', tokenVersionAsString] = seal.split(
|
|
17
|
+
this.versionDelimiter,
|
|
18
|
+
);
|
|
19
|
+
const tokenVersion =
|
|
20
|
+
tokenVersionAsString == null ? null : parseInt(tokenVersionAsString, 10);
|
|
21
|
+
return { sealWithoutVersion, tokenVersion };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Encrypt data in a way that's compatible with iron-session
|
|
25
|
+
async sealData(
|
|
26
|
+
data: unknown,
|
|
27
|
+
{ password, ttl = 0 }: { password: string; ttl?: number | undefined },
|
|
28
|
+
) {
|
|
29
|
+
// Format password as iron-session expects
|
|
30
|
+
const passwordObj = {
|
|
31
|
+
id: '1',
|
|
32
|
+
secret: password,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// Seal the data using iron-webcrypto with properly formatted password
|
|
36
|
+
const seal = await sealData(globalThis.crypto, data, passwordObj, {
|
|
37
|
+
encryption: {
|
|
38
|
+
saltBits: 256,
|
|
39
|
+
algorithm: 'aes-256-cbc',
|
|
40
|
+
iterations: 1,
|
|
41
|
+
minPasswordlength: 32,
|
|
42
|
+
},
|
|
43
|
+
integrity: {
|
|
44
|
+
saltBits: 256,
|
|
45
|
+
algorithm: 'sha256',
|
|
46
|
+
iterations: 1,
|
|
47
|
+
minPasswordlength: 32,
|
|
48
|
+
},
|
|
49
|
+
ttl: ttl * 1000, // Convert seconds to milliseconds
|
|
50
|
+
timestampSkewSec: 60,
|
|
51
|
+
localtimeOffsetMsec: 0,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Add the version delimiter exactly like iron-session does
|
|
55
|
+
return `${seal}${this.versionDelimiter}${this.currentMajorVersion}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Decrypt data from iron-session with HMAC verification
|
|
59
|
+
async unsealData<T = unknown>(
|
|
60
|
+
encryptedData: string,
|
|
61
|
+
{ password }: { password: string },
|
|
62
|
+
): Promise<T> {
|
|
63
|
+
// First, parse the seal to extract the version
|
|
64
|
+
const { sealWithoutVersion, tokenVersion } = this.parseSeal(encryptedData);
|
|
65
|
+
|
|
66
|
+
// Format password as a map like iron-session expects
|
|
67
|
+
const passwordMap = { 1: password };
|
|
68
|
+
|
|
69
|
+
// Use iron-webcrypto's unseal function
|
|
70
|
+
const data = await unsealData(
|
|
71
|
+
globalThis.crypto,
|
|
72
|
+
sealWithoutVersion,
|
|
73
|
+
passwordMap,
|
|
74
|
+
{
|
|
75
|
+
encryption: {
|
|
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,
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// Check the token version if needed
|
|
94
|
+
if (tokenVersion === 2) {
|
|
95
|
+
return data as T;
|
|
96
|
+
} else if (tokenVersion !== null) {
|
|
97
|
+
// For older token versions, extract the persistent property
|
|
98
|
+
return { ...(data as any).persistent } as T;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return data as T;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const ironWebcryptoEncryption = new SessionEncryption();
|
|
106
|
+
|
|
107
|
+
export default ironWebcryptoEncryption;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export class AuthKitError extends Error {
|
|
2
|
+
data?: Record<string, any>;
|
|
3
|
+
|
|
4
|
+
constructor(message: string, cause?: unknown, data?: Record<string, any>) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'AuthKitError';
|
|
7
|
+
this.cause = cause;
|
|
8
|
+
this.data = data;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class SessionEcnryptionError extends AuthKitError {
|
|
13
|
+
constructor(message: string, cause?: unknown) {
|
|
14
|
+
super(message, cause);
|
|
15
|
+
this.name = 'SessionEncryptionError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class TokenValidationError extends AuthKitError {
|
|
20
|
+
constructor(message: string, cause?: unknown) {
|
|
21
|
+
super(message, cause);
|
|
22
|
+
this.name = 'TokenValidationError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class TokenRefreshError extends AuthKitError {
|
|
27
|
+
constructor(message: string, cause?: unknown) {
|
|
28
|
+
super(message, cause);
|
|
29
|
+
this.name = 'TokenRefreshError';
|
|
30
|
+
}
|
|
31
|
+
}
|