@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
package/src/HttpClient.ts
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
2
|
-
|
|
3
|
-
interface RequestOptions {
|
|
4
|
-
method: HttpMethod;
|
|
5
|
-
params?: Record<string, string>;
|
|
6
|
-
headers?: Record<string, string>;
|
|
7
|
-
body?: unknown;
|
|
8
|
-
credentials?: RequestCredentials;
|
|
9
|
-
mode?: RequestMode;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface ApiResponse<T> {
|
|
13
|
-
data: T;
|
|
14
|
-
status: number;
|
|
15
|
-
headers: Record<string, string>;
|
|
16
|
-
ok: boolean;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface ApiError {
|
|
20
|
-
message: string;
|
|
21
|
-
status?: number;
|
|
22
|
-
details?: unknown;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
interface ClientConfig {
|
|
26
|
-
baseUrl: string;
|
|
27
|
-
defaultHeaders?: Record<string, string>;
|
|
28
|
-
timeout?: number;
|
|
29
|
-
credentials?: RequestCredentials;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class HttpClient {
|
|
33
|
-
private baseUrl: string;
|
|
34
|
-
private defaultHeaders: Record<string, string>;
|
|
35
|
-
private timeout: number;
|
|
36
|
-
private credentials: RequestCredentials;
|
|
37
|
-
|
|
38
|
-
constructor(config: ClientConfig) {
|
|
39
|
-
this.baseUrl = config.baseUrl.replace(/\/$/, '');
|
|
40
|
-
this.defaultHeaders = {
|
|
41
|
-
'Content-Type': 'application/json',
|
|
42
|
-
Accept: 'application/json',
|
|
43
|
-
...config.defaultHeaders,
|
|
44
|
-
};
|
|
45
|
-
this.timeout = config.timeout || 30_000; // Default timeout of 30 seconds
|
|
46
|
-
this.credentials = config.credentials || 'same-origin';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private createError(
|
|
50
|
-
message: string,
|
|
51
|
-
status?: number,
|
|
52
|
-
details?: unknown,
|
|
53
|
-
): ApiError {
|
|
54
|
-
return { message, status, details };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private async request<T>(
|
|
58
|
-
endpoint: string,
|
|
59
|
-
options: RequestOptions,
|
|
60
|
-
): Promise<ApiResponse<T>> {
|
|
61
|
-
let url = `${this.baseUrl}${endpoint}`;
|
|
62
|
-
const headers = { ...this.defaultHeaders, ...options.headers };
|
|
63
|
-
const fetchOptions: RequestInit = {
|
|
64
|
-
method: options.method,
|
|
65
|
-
headers,
|
|
66
|
-
credentials: options.credentials ?? this.credentials,
|
|
67
|
-
mode: options.mode ?? 'cors',
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
if (options.params) {
|
|
71
|
-
url += new URLSearchParams(options.params).toString();
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (options.body) {
|
|
75
|
-
if (options.body instanceof FormData) {
|
|
76
|
-
fetchOptions.body = options.body;
|
|
77
|
-
const headerRecord = fetchOptions.headers as Record<string, string>;
|
|
78
|
-
delete headerRecord['Content-Type'];
|
|
79
|
-
} else {
|
|
80
|
-
fetchOptions.body =
|
|
81
|
-
typeof options.body === 'string'
|
|
82
|
-
? options.body
|
|
83
|
-
: JSON.stringify(options.body);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
const controller = new AbortController();
|
|
89
|
-
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
90
|
-
|
|
91
|
-
const response = await fetch(url, {
|
|
92
|
-
...fetchOptions,
|
|
93
|
-
signal: controller.signal,
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
clearTimeout(timeoutId);
|
|
97
|
-
|
|
98
|
-
const responseData = await response.json();
|
|
99
|
-
const responseHeaders: Record<string, string> = {};
|
|
100
|
-
response.headers.forEach((value, key) => {
|
|
101
|
-
responseHeaders[key] = value;
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
return {
|
|
105
|
-
data: responseData,
|
|
106
|
-
status: response.status,
|
|
107
|
-
headers: responseHeaders,
|
|
108
|
-
ok: response.ok,
|
|
109
|
-
} satisfies ApiResponse<T>;
|
|
110
|
-
} catch (error) {
|
|
111
|
-
if (error instanceof Error) {
|
|
112
|
-
if (error.name === 'AbortController') {
|
|
113
|
-
throw this.createError('Request timeout', 408);
|
|
114
|
-
}
|
|
115
|
-
throw this.createError(error.message);
|
|
116
|
-
}
|
|
117
|
-
throw this.createError('Unknown error occurred');
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async get<T>(
|
|
122
|
-
endpoint: string,
|
|
123
|
-
options: Partial<RequestOptions> = {},
|
|
124
|
-
): Promise<ApiResponse<T>> {
|
|
125
|
-
return this.request<T>(endpoint, { ...options, method: 'GET' });
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async post<T>(
|
|
129
|
-
endpoint: string,
|
|
130
|
-
body?: unknown,
|
|
131
|
-
options: Partial<RequestOptions> = {},
|
|
132
|
-
): Promise<ApiResponse<T>> {
|
|
133
|
-
return this.request<T>(endpoint, { ...options, method: 'POST', body });
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
async put<T>(
|
|
137
|
-
endpoint: string,
|
|
138
|
-
body?: unknown,
|
|
139
|
-
options: Partial<RequestOptions> = {},
|
|
140
|
-
): Promise<ApiResponse<T>> {
|
|
141
|
-
return this.request<T>(endpoint, { ...options, method: 'PUT', body });
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async delete<T>(
|
|
145
|
-
endpoint: string,
|
|
146
|
-
options: Partial<RequestOptions> = {},
|
|
147
|
-
): Promise<ApiResponse<T>> {
|
|
148
|
-
return this.request<T>(endpoint, { ...options, method: 'DELETE' });
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async patch<T>(
|
|
152
|
-
endpoint: string,
|
|
153
|
-
body?: unknown,
|
|
154
|
-
options: Partial<RequestOptions> = {},
|
|
155
|
-
): Promise<ApiResponse<T>> {
|
|
156
|
-
return this.request<T>(endpoint, { ...options, method: 'PATCH', body });
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export default HttpClient;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { SessionStorage } from '../../core/session/types.js';
|
|
2
|
-
export class ChromeExtensionStorage implements SessionStorage<void, void> {
|
|
3
|
-
cookieName: string;
|
|
4
|
-
domain: string;
|
|
5
|
-
|
|
6
|
-
constructor(domain: string, cookeName = 'wos-session') {
|
|
7
|
-
this.cookieName = cookeName;
|
|
8
|
-
this.domain = domain;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async getSession(): Promise<string | null> {
|
|
12
|
-
const cookie = await chrome.cookies.get({
|
|
13
|
-
name: this.cookieName,
|
|
14
|
-
url: this.domain,
|
|
15
|
-
});
|
|
16
|
-
return cookie?.value ?? null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async saveSession(_: unknown, sessionData: string): Promise<void> {
|
|
20
|
-
await chrome.cookies.set({
|
|
21
|
-
name: this.cookieName,
|
|
22
|
-
url: this.domain,
|
|
23
|
-
value: sessionData,
|
|
24
|
-
expirationDate: Date.now() / 1000 + 60 * 60 * 24 * 400, // 400 days
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async clearSession(): Promise<void> {
|
|
29
|
-
await chrome.cookies.remove({
|
|
30
|
-
name: this.cookieName,
|
|
31
|
-
url: this.domain,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
import type { WorkOSOptions } from '@workos-inc/node';
|
|
2
|
-
import { version } from '../../../package.json';
|
|
3
|
-
import HttpClient from '../../HttpClient';
|
|
4
|
-
import { once } from '../../utils';
|
|
5
|
-
import { getConfig } from '../config';
|
|
6
|
-
import type {
|
|
7
|
-
AuthenticateWithCodeOptions,
|
|
8
|
-
AuthenticateWithEmailVerificationOptions,
|
|
9
|
-
AuthenticateWithRefreshTokenOptions,
|
|
10
|
-
AuthenticationResponse,
|
|
11
|
-
AuthorizationURLOptions,
|
|
12
|
-
UserManagementInterface,
|
|
13
|
-
WorkOSClient,
|
|
14
|
-
} from './types';
|
|
15
|
-
|
|
16
|
-
export class UserManagement implements UserManagementInterface {
|
|
17
|
-
private client: HttpClient;
|
|
18
|
-
private baseURL: string;
|
|
19
|
-
private key: string;
|
|
20
|
-
|
|
21
|
-
constructor(
|
|
22
|
-
apiKey: string,
|
|
23
|
-
baseURL: string,
|
|
24
|
-
options: {
|
|
25
|
-
appInfo?: { name: string; version: string };
|
|
26
|
-
} = {},
|
|
27
|
-
) {
|
|
28
|
-
this.key = apiKey;
|
|
29
|
-
this.baseURL = baseURL;
|
|
30
|
-
|
|
31
|
-
this.client = new HttpClient({
|
|
32
|
-
baseUrl: this.baseURL,
|
|
33
|
-
defaultHeaders: {
|
|
34
|
-
Authorization: `Bearer ${apiKey}`,
|
|
35
|
-
'User-Agent': options.appInfo
|
|
36
|
-
? `${options.appInfo.name}/${options.appInfo.version}`
|
|
37
|
-
: 'authkit-session',
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Helper method to create a query string
|
|
43
|
-
private toQueryString(params: Record<string, string | undefined>): string {
|
|
44
|
-
const searchParams = new URLSearchParams();
|
|
45
|
-
const keys = Object.keys(params).sort();
|
|
46
|
-
|
|
47
|
-
for (const key of keys) {
|
|
48
|
-
const value = params[key];
|
|
49
|
-
if (value) {
|
|
50
|
-
searchParams.append(key, value);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return searchParams.toString();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async getJwksUrl(clientId: string): Promise<string> {
|
|
58
|
-
if (!clientId) {
|
|
59
|
-
throw TypeError('clientId must be a valid clientId');
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return `${this.baseURL}/sso/jwks/${clientId}`;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
getLogoutUrl({
|
|
66
|
-
sessionId,
|
|
67
|
-
returnTo,
|
|
68
|
-
}: {
|
|
69
|
-
sessionId: string;
|
|
70
|
-
returnTo?: string;
|
|
71
|
-
}): string {
|
|
72
|
-
if (!sessionId) {
|
|
73
|
-
throw new TypeError(`Incomplete arguments. Need to specify 'sessionId'.`);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const url = new URL(`${this.baseURL}/user_management/sessions/logout`);
|
|
77
|
-
url.searchParams.set('session_id', sessionId);
|
|
78
|
-
|
|
79
|
-
if (returnTo) {
|
|
80
|
-
url.searchParams.set('return_to', returnTo);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return url.toString();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async revokeSession({ sessionId }: { sessionId: string }): Promise<void> {
|
|
87
|
-
if (!sessionId) {
|
|
88
|
-
throw new TypeError('sessionId is required');
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
await this.client.post('/user_management/sessions/revoke', {
|
|
92
|
-
session_id: sessionId,
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
getAuthorizationUrl({
|
|
97
|
-
connectionId,
|
|
98
|
-
codeChallenge,
|
|
99
|
-
codeChallengeMethod,
|
|
100
|
-
context,
|
|
101
|
-
clientId,
|
|
102
|
-
domainHint,
|
|
103
|
-
loginHint,
|
|
104
|
-
organizationId,
|
|
105
|
-
provider,
|
|
106
|
-
redirectUri,
|
|
107
|
-
state,
|
|
108
|
-
screenHint,
|
|
109
|
-
}: AuthorizationURLOptions): string {
|
|
110
|
-
if (!provider && !connectionId && !organizationId) {
|
|
111
|
-
throw new TypeError(
|
|
112
|
-
`Incomplete arguments. Need to specify either a 'connectionId', 'organizationId', or 'provider'.`,
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (provider !== 'authkit' && screenHint) {
|
|
117
|
-
throw new TypeError(
|
|
118
|
-
`'screenHint' is only supported for 'authkit' provider`,
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const query = this.toQueryString({
|
|
123
|
-
connection_id: connectionId,
|
|
124
|
-
code_challenge: codeChallenge,
|
|
125
|
-
code_challenge_method: codeChallengeMethod,
|
|
126
|
-
context,
|
|
127
|
-
organization_id: organizationId,
|
|
128
|
-
domain_hint: domainHint,
|
|
129
|
-
login_hint: loginHint,
|
|
130
|
-
provider,
|
|
131
|
-
client_id: clientId,
|
|
132
|
-
redirect_uri: redirectUri,
|
|
133
|
-
response_type: 'code',
|
|
134
|
-
state,
|
|
135
|
-
screen_hint: screenHint,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
return `${this.baseURL}/user_management/authorize?${query}`;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async authenticateWithRefreshToken(
|
|
142
|
-
options: AuthenticateWithRefreshTokenOptions,
|
|
143
|
-
): Promise<AuthenticationResponse> {
|
|
144
|
-
const { data } = await this.client.post<any>(
|
|
145
|
-
'/user_management/authenticate',
|
|
146
|
-
{
|
|
147
|
-
grant_type: 'refresh_token',
|
|
148
|
-
client_id: options.clientId,
|
|
149
|
-
client_secret: this.key,
|
|
150
|
-
refresh_token: options.refreshToken,
|
|
151
|
-
organization_id: options.organizationId,
|
|
152
|
-
ip_address: options.ipAddress,
|
|
153
|
-
user_agent: options.userAgent,
|
|
154
|
-
},
|
|
155
|
-
);
|
|
156
|
-
|
|
157
|
-
return this.deserializeAuthResponse(data);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async authenticateWithCode(
|
|
161
|
-
options: AuthenticateWithCodeOptions,
|
|
162
|
-
): Promise<AuthenticationResponse> {
|
|
163
|
-
const { data } = await this.client.post<any>(
|
|
164
|
-
'/user_management/authenticate',
|
|
165
|
-
{
|
|
166
|
-
grant_type: 'authorization_code',
|
|
167
|
-
client_id: options.clientId,
|
|
168
|
-
client_secret: this.key,
|
|
169
|
-
code: options.code,
|
|
170
|
-
code_verifier: options.codeVerifier,
|
|
171
|
-
invitation_token: options.invitationToken,
|
|
172
|
-
ip_address: options.ipAddress,
|
|
173
|
-
user_agent: options.userAgent,
|
|
174
|
-
},
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
return this.deserializeAuthResponse(data);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
async authenticateWithEmailVerification(
|
|
181
|
-
options: AuthenticateWithEmailVerificationOptions,
|
|
182
|
-
): Promise<AuthenticationResponse> {
|
|
183
|
-
const { data } = await this.client.post<any>(
|
|
184
|
-
'/user_management/authenticate',
|
|
185
|
-
{
|
|
186
|
-
grant_type: 'urn:workos:oauth:grant-type:email-verification:code',
|
|
187
|
-
client_id: options.clientId,
|
|
188
|
-
client_secret: this.key,
|
|
189
|
-
pending_authentication_token: options.pendingAuthenticationToken,
|
|
190
|
-
code: options.code,
|
|
191
|
-
ip_address: options.ipAddress,
|
|
192
|
-
user_agent: options.userAgent,
|
|
193
|
-
},
|
|
194
|
-
);
|
|
195
|
-
|
|
196
|
-
return this.deserializeAuthResponse(data);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Helper to deserialize auth response
|
|
200
|
-
private deserializeAuthResponse(data: any): AuthenticationResponse {
|
|
201
|
-
return {
|
|
202
|
-
user: {
|
|
203
|
-
object: data.user.object,
|
|
204
|
-
id: data.user.id,
|
|
205
|
-
email: data.user.email,
|
|
206
|
-
emailVerified: data.user.email_verified,
|
|
207
|
-
firstName: data.user.first_name,
|
|
208
|
-
profilePictureUrl: data.user.profile_picture_url,
|
|
209
|
-
lastName: data.user.last_name,
|
|
210
|
-
lastSignInAt: data.user.last_sign_in_at,
|
|
211
|
-
createdAt: data.user.created_at,
|
|
212
|
-
updatedAt: data.user.updated_at,
|
|
213
|
-
externalId: data.user.external_id ?? null,
|
|
214
|
-
metadata: data.user.metadata ?? {},
|
|
215
|
-
},
|
|
216
|
-
organizationId: data.organization_id,
|
|
217
|
-
accessToken: data.access_token,
|
|
218
|
-
refreshToken: data.refresh_token,
|
|
219
|
-
impersonator: data.impersonator,
|
|
220
|
-
authenticationMethod: data.authentication_method,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export class WorkOSLite implements WorkOSClient {
|
|
226
|
-
protected apiKey: string;
|
|
227
|
-
protected baseURL: string;
|
|
228
|
-
public userManagement: UserManagementInterface;
|
|
229
|
-
|
|
230
|
-
constructor(apiKey: string, options: WorkOSOptions) {
|
|
231
|
-
this.apiKey = apiKey;
|
|
232
|
-
const hostname = options.apiHostname || 'api.workos.com';
|
|
233
|
-
const protocol = options.https !== false ? 'https' : 'http';
|
|
234
|
-
const port = options.port ? `:${options.port}` : '';
|
|
235
|
-
this.baseURL = `${protocol}://${hostname}${port}`;
|
|
236
|
-
this.userManagement = new UserManagement(this.apiKey, this.baseURL);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
getJwksUrl(clientId: string): string {
|
|
240
|
-
if (!clientId) {
|
|
241
|
-
throw TypeError('clientId must be a valid clientId');
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return `${this.baseURL}/sso/jwks/${clientId}`;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Create a WorkOS instance with the provided API key and optional settings.
|
|
250
|
-
*/
|
|
251
|
-
export function createWorkOSInstance() {
|
|
252
|
-
// Get required API key from config
|
|
253
|
-
const apiKey = getConfig('apiKey');
|
|
254
|
-
|
|
255
|
-
// Get optional settings
|
|
256
|
-
const apiHostname = getConfig('apiHostname');
|
|
257
|
-
const apiHttps = getConfig('apiHttps');
|
|
258
|
-
const apiPort = getConfig('apiPort');
|
|
259
|
-
const clientId = getConfig('clientId');
|
|
260
|
-
|
|
261
|
-
const options = {
|
|
262
|
-
apiHostname,
|
|
263
|
-
https: apiHttps,
|
|
264
|
-
port: apiPort,
|
|
265
|
-
clientId,
|
|
266
|
-
appInfo: {
|
|
267
|
-
name: 'authkit-session',
|
|
268
|
-
version,
|
|
269
|
-
},
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
// Initialize the WorkOS client with config values
|
|
273
|
-
// TODO: allow this to use the client from @workos-inc/node
|
|
274
|
-
const workos = new WorkOSLite(apiKey, options);
|
|
275
|
-
|
|
276
|
-
return workos;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Create a WorkOS instance with the provided API key and optional settings.
|
|
281
|
-
* This function is lazy loaded to avoid loading the WorkOS SDK when it's not needed.
|
|
282
|
-
*/
|
|
283
|
-
export const getWorkOS = once(createWorkOSInstance);
|
|
284
|
-
export default getWorkOS;
|
package/src/core/iron.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
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
|
-
}
|