@workos/authkit-session 0.0.1-alpha.2 → 0.1.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/README.md +184 -11
- package/dist/index.cjs +229 -356
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +225 -352
- package/dist/index.js.map +1 -1
- package/package.json +20 -12
- 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 +80 -12
- 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 +33 -25
- 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 +78 -75
- 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.d.cts +0 -406
- package/dist/index.d.ts +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
|
@@ -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
|
-
}
|