@workos/authkit-session 0.0.1-alpha.3 → 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.
Files changed (35) hide show
  1. package/README.md +80 -5
  2. package/dist/index.cjs +229 -356
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.js +225 -352
  5. package/dist/index.js.map +1 -1
  6. package/package.json +18 -11
  7. package/src/core/client/types.ts +2 -7
  8. package/src/core/client/workos.ts +5 -5
  9. package/src/core/config/ConfigurationProvider.spec.ts +201 -0
  10. package/src/core/config/ConfigurationProvider.ts +75 -7
  11. package/src/core/config.spec.ts +160 -0
  12. package/src/core/config.ts +29 -3
  13. package/src/core/createAuthKitFactory.spec.ts +161 -0
  14. package/src/core/createAuthKitFactory.ts +28 -23
  15. package/src/core/encryption/ironWebcryptoEncryption.spec.ts +129 -0
  16. package/src/core/encryption/ironWebcryptoEncryption.ts +28 -23
  17. package/src/core/encryption/pureWebcryptoEncryption.spec.ts +177 -0
  18. package/src/core/encryption/pureWebcryptoEncryption.ts +339 -0
  19. package/src/core/errors.spec.ts +130 -0
  20. package/src/core/session/CookieSessionStorage.spec.ts +136 -0
  21. package/src/core/session/CookieSessionStorage.ts +40 -7
  22. package/src/core/session/SessionManager.spec.ts +357 -0
  23. package/src/core/session/SessionManager.ts +71 -72
  24. package/src/core/session/TokenManager.spec.ts +132 -0
  25. package/src/core/session/TokenManager.ts +8 -6
  26. package/src/core/session/types.ts +39 -3
  27. package/src/index.ts +20 -8
  28. package/src/utils.spec.ts +12 -0
  29. package/dist/index.d.cts +0 -406
  30. package/dist/index.d.ts +0 -406
  31. package/src/HttpClient.ts +0 -160
  32. package/src/adapters/chrome/ChromeExtensionStorage.ts +0 -34
  33. package/src/core/UserManagement.ts +0 -8
  34. package/src/core/client/WorkOSLite.ts +0 -284
  35. package/src/core/iron.ts +0 -103
package/dist/index.d.cts DELETED
@@ -1,406 +0,0 @@
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';
4
- import { JWTPayload } from 'jose';
5
-
6
- interface UserManagementInterface {
7
- getAuthorizationUrl: (options: AuthorizationURLOptions) => string;
8
- authenticateWithCode: (options: AuthenticateWithCodeOptions) => Promise<AuthenticationResponse>;
9
- authenticateWithRefreshToken: (options: AuthenticateWithRefreshTokenOptions) => Promise<AuthenticationResponse>;
10
- getLogoutUrl: (options: {
11
- sessionId: string;
12
- returnTo?: string;
13
- }) => string;
14
- revokeSession: (options: {
15
- sessionId: string;
16
- }) => Promise<void>;
17
- }
18
- interface WorkOSClient {
19
- userManagement: UserManagementInterface;
20
- getJwksUrl: (clientId: string) => string;
21
- }
22
- interface AuthenticationResponse {
23
- user: User;
24
- organizationId?: string;
25
- accessToken: string;
26
- refreshToken: string;
27
- impersonator?: Impersonator;
28
- authenticationMethod?: string;
29
- sealedSession?: string;
30
- }
31
- interface AuthenticateWithOptionsBase {
32
- clientId: string;
33
- ipAddress?: string;
34
- userAgent?: string;
35
- session?: {
36
- cookiePassword?: string;
37
- sealSession: boolean;
38
- };
39
- }
40
- interface AuthenticateWithRefreshTokenOptions extends AuthenticateWithOptionsBase {
41
- refreshToken: string;
42
- organizationId?: string;
43
- }
44
- interface AuthenticateWithCodeOptions extends AuthenticateWithOptionsBase {
45
- codeVerifier?: string;
46
- code: string;
47
- invitationToken?: string;
48
- }
49
- interface AuthorizationURLOptions {
50
- clientId: string;
51
- codeChallenge?: string;
52
- codeChallengeMethod?: 'S256';
53
- connectionId?: string;
54
- context?: string;
55
- organizationId?: string;
56
- domainHint?: string;
57
- loginHint?: string;
58
- provider?: string;
59
- redirectUri: string;
60
- state?: string;
61
- screenHint?: 'sign-up' | 'sign-in';
62
- }
63
-
64
- declare class WorkOSLite implements WorkOSClient {
65
- protected apiKey: string;
66
- protected baseURL: string;
67
- userManagement: UserManagementInterface;
68
- constructor(apiKey: string, options: WorkOSOptions);
69
- getJwksUrl(clientId: string): string;
70
- }
71
- /**
72
- * Create a WorkOS instance with the provided API key and optional settings.
73
- * This function is lazy loaded to avoid loading the WorkOS SDK when it's not needed.
74
- */
75
- declare const getWorkOS: () => WorkOSLite;
76
-
77
- /**
78
- * AuthKit Configuration Options
79
- */
80
- interface AuthKitConfig {
81
- /**
82
- * The WorkOS Client ID
83
- * Equivalent to the WORKOS_CLIENT_ID environment variable
84
- */
85
- clientId: string;
86
- /**
87
- * The WorkOS API Key
88
- * Equivalent to the WORKOS_API_KEY environment variable
89
- */
90
- apiKey: string;
91
- /**
92
- * The redirect URI for the authentication callback
93
- * Equivalent to the WORKOS_REDIRECT_URI environment variable
94
- */
95
- redirectUri: string;
96
- /**
97
- * The password used to encrypt the session cookie
98
- * Equivalent to the WORKOS_COOKIE_PASSWORD environment variable
99
- * Must be at least 32 characters long
100
- */
101
- cookiePassword: string;
102
- /**
103
- * The hostname of the API to use
104
- * Equivalent to the WORKOS_API_HOSTNAME environment variable
105
- */
106
- apiHostname?: string;
107
- /**
108
- * Whether to use HTTPS for API requests
109
- * Equivalent to the WORKOS_API_HTTPS environment variable
110
- */
111
- apiHttps: boolean;
112
- /**
113
- * The port to use for the API
114
- * Equivalent to the WORKOS_API_PORT environment variable
115
- */
116
- apiPort?: number;
117
- /**
118
- * The maximum age of the session cookie in seconds
119
- * Equivalent to the WORKOS_COOKIE_MAX_AGE environment variable
120
- */
121
- cookieMaxAge: number;
122
- /**
123
- * The sameSite attribute for the session cookie
124
- */
125
- cookieSameSite?: 'strict' | 'lax' | 'none';
126
- /**
127
- * The name of the session cookie
128
- * Equivalent to the WORKOS_COOKIE_NAME environment variable
129
- * Defaults to "wos-session"
130
- */
131
- cookieName: string;
132
- /**
133
- * The domain for the session cookie
134
- */
135
- cookieDomain?: string;
136
- }
137
- type ValueSource = Record<string, any> | ((key: string) => any);
138
-
139
- /**
140
- * Configuration class for AuthKit.
141
- * This class is used to manage configuration values and provide defaults.
142
- * It also provides a way to get configuration values from environment variables.
143
- * @internal
144
- */
145
- declare class ConfigurationProvider {
146
- private config;
147
- private valueSource;
148
- private readonly requiredKeys;
149
- /**
150
- * Convert a camelCase string to an uppercase, underscore-separated environment variable name.
151
- * @param str The string to convert
152
- * @returns The environment variable name
153
- */
154
- protected getEnvironmentVariableName(str: string): string;
155
- private updateConfig;
156
- setValueSource(source: ValueSource): void;
157
- configure(configOrSource: Partial<AuthKitConfig> | ValueSource, source?: ValueSource): void;
158
- getValue<K extends keyof AuthKitConfig>(key: K): AuthKitConfig[K];
159
- private getEnvironmentValue;
160
- private convertValueType;
161
- getConfig(): AuthKitConfig;
162
- }
163
-
164
- /**
165
- * Configure AuthKit with a custom value source.
166
- * @param source The source of configuration values
167
- *
168
- * @example
169
- * configure(key => Deno.env.get(key));
170
- */
171
- declare function configure(source: ValueSource): void;
172
- /**
173
- * Configure AuthKit with custom values.
174
- * @param config The configuration values
175
- *
176
- * @example
177
- * configure({
178
- * clientId: 'your-client-id',
179
- * redirectUri: 'https://your-app.com/auth/callback',
180
- * apiKey: 'your-api-key',
181
- * cookiePassword: 'your-cookie-password',
182
- * });
183
- */
184
- declare function configure(config: Partial<AuthKitConfig>): void;
185
- /**
186
- * Configure AuthKit with custom values and a custom value source.
187
- * @param config The configuration values
188
- * @param source The source of configuration values
189
- *
190
- * @example
191
- * configure({
192
- * clientId: 'your-client-id',
193
- * }, env);
194
- */
195
- declare function configure(config: Partial<AuthKitConfig>, source: ValueSource): void;
196
- /**
197
- * Get a configuration value by key.
198
- * This function will first check environment variables, then programmatically provided config,
199
- * and finally fall back to defaults for optional settings.
200
- * If a required setting is missing, an error will be thrown.
201
- * @param key The configuration key
202
- * @returns The configuration value
203
- */
204
- declare function getConfig<K extends keyof AuthKitConfig>(key: K): AuthKitConfig[K];
205
-
206
- interface BaseTokenClaims extends JWTPayload {
207
- sid: string;
208
- org_id?: string;
209
- role?: string;
210
- permissions?: string[];
211
- entitlements?: string[];
212
- feature_flags?: string[];
213
- }
214
- type CustomClaims = Record<string, unknown>;
215
- interface AuthResult<TCustomClaims = Record<string, unknown>> {
216
- refreshToken?: string;
217
- user?: User | null;
218
- claims?: BaseTokenClaims & TCustomClaims;
219
- impersonator?: Impersonator;
220
- accessToken?: string;
221
- sessionId?: string;
222
- }
223
- /**
224
- * AuthKit Session
225
- */
226
- interface Session {
227
- /**
228
- * The session access token
229
- */
230
- accessToken: string;
231
- /**
232
- * The session refresh token - used to refresh the access token
233
- */
234
- refreshToken: string;
235
- /**
236
- * The logged-in user
237
- */
238
- user: User;
239
- /**
240
- * The impersonator user, if any
241
- */
242
- impersonator?: Impersonator;
243
- }
244
- interface SessionStorage<TRequest, TResponse, TOptions = unknown> {
245
- getSession(request: TRequest): Promise<string | null>;
246
- /**
247
- * Save session data to a response object.
248
- * @param response The framework-specific response object.
249
- * @param sessionData The encrypted session string.
250
- * @param options Optional cookie options.
251
- * @returns The framework-specific response object with the session cookie set.
252
- */
253
- saveSession(response: TResponse, sessionData: string, options?: TOptions): Promise<TResponse>;
254
- /**
255
- * @param response The frmework-specific response object.
256
- * @returns The framework-specific response object with the session cookie removed.
257
- */
258
- clearSession(response: TResponse, options?: TOptions): Promise<TResponse>;
259
- }
260
- interface SessionEncryption {
261
- sealData: (data: unknown, options: {
262
- password: string;
263
- ttl?: number | undefined;
264
- }) => Promise<string>;
265
- unsealData: <T>(encryptedData: string, options: {
266
- password: string;
267
- }) => Promise<T>;
268
- }
269
-
270
- declare class TokenManager {
271
- private clientId;
272
- private client;
273
- constructor(clientId: string, client: WorkOSClient);
274
- private readonly getPublicKey;
275
- verifyToken(token: string): Promise<boolean>;
276
- parseTokenClaims<TCustomClaims = CustomClaims>(token: string): BaseTokenClaims & TCustomClaims;
277
- isTokenExpiring(token: string, bufferSeconds?: number): boolean;
278
- getTokenExpiryTime(token: string): number | null;
279
- }
280
-
281
- declare class SessionManager<TRequest, TResponse> {
282
- private readonly config;
283
- private readonly storage;
284
- private readonly tokenManager;
285
- private readonly client;
286
- private readonly encryption;
287
- constructor(config: ConfigurationProvider, storage: SessionStorage<TRequest, TResponse>, tokenManager: TokenManager, client: WorkOSClient, encryption: SessionEncryption);
288
- private encryptSession;
289
- private decryptSession;
290
- private getSession;
291
- private validateSession;
292
- private validateAndRefreshIfNeeded;
293
- private attemptTokenRefresh;
294
- withAuth<TCustomClaims = CustomClaims>(request: TRequest): Promise<AuthResult<TCustomClaims>>;
295
- refreshSession<TCustomClaims = CustomClaims>(session: Session, organizationId?: string): Promise<{
296
- user: _workos_inc_node.User;
297
- sessionId: string;
298
- organizationId: string | undefined;
299
- role: string | undefined;
300
- permissions: string[] | undefined;
301
- entitlements: string[] | undefined;
302
- impersonator: _workos_inc_node.Impersonator | undefined;
303
- accessToken: string;
304
- claims: BaseTokenClaims & TCustomClaims;
305
- sessionData: string;
306
- session: Session;
307
- }>;
308
- getAuthorizationUrl({ returnPathname, redirectUri, screenHint, }: {
309
- returnPathname?: string;
310
- redirectUri?: string;
311
- screenHint?: 'sign-up' | 'sign-in';
312
- }): Promise<string>;
313
- createSession(authResponse: AuthenticationResponse, response: TResponse): Promise<TResponse>;
314
- switchToOrganization<TCustomClaims = CustomClaims>(request: TRequest, response: TResponse, organizationId: string): Promise<{
315
- response: TResponse;
316
- authResult: AuthResult<TCustomClaims>;
317
- }>;
318
- private handleSwitchOrganizationError;
319
- terminateSession(session: Session, response: TResponse, options?: {
320
- returnTo?: string;
321
- }): Promise<{
322
- response: TResponse;
323
- logoutUrl: string;
324
- }>;
325
- }
326
-
327
- interface CookieOptions {
328
- path?: string;
329
- domain?: string;
330
- maxAge?: number;
331
- expires?: Date;
332
- httpOnly?: boolean;
333
- secure?: boolean;
334
- sameSite?: 'lax' | 'strict' | 'none';
335
- priority?: 'low' | 'medium' | 'high';
336
- partitioned?: boolean;
337
- }
338
- declare abstract class CookieSessionStorage<TRequest, TResponse> implements SessionStorage<TRequest, TResponse> {
339
- protected cookieName: string;
340
- protected readonly cookieOptions: CookieOptions;
341
- constructor(config: ConfigurationProvider);
342
- abstract getSession(request: TRequest): Promise<string | null>;
343
- abstract saveSession(response: TResponse, sessionData: string): Promise<TResponse>;
344
- abstract clearSession(response: TResponse): Promise<TResponse>;
345
- }
346
-
347
- declare const createAuthKitFactory: <TRequest, TResponse>(options: {
348
- sessionStorageFactory: (config: AuthKitConfig) => SessionStorage<TRequest, TResponse>;
349
- sessionEncryptionFactory?: (confg: AuthKitConfig) => SessionEncryption;
350
- clientFactory?: (config: AuthKitConfig) => WorkOSClient;
351
- }) => {
352
- withAuth: (request: any) => Promise<AuthResult<CustomClaims>>;
353
- getAuthorizationUrl: (args_0: {
354
- returnPathname?: string;
355
- redirectUri?: string;
356
- screenHint?: "sign-up" | "sign-in";
357
- }) => Promise<string>;
358
- refreshSession: (session: Session, organizationId?: string | undefined) => Promise<{
359
- user: _workos_inc_node.User;
360
- sessionId: string;
361
- organizationId: string | undefined;
362
- role: string | undefined;
363
- permissions: string[] | undefined;
364
- entitlements: string[] | undefined;
365
- impersonator: _workos_inc_node.Impersonator | undefined;
366
- accessToken: string;
367
- claims: BaseTokenClaims & CustomClaims;
368
- sessionData: string;
369
- session: Session;
370
- }>;
371
- saveSession: (response: TResponse, sessionData: string, options?: unknown) => Promise<TResponse>;
372
- getSignInUrl: (options: {
373
- organizationId?: string;
374
- loginHint?: string;
375
- redirectUri?: string;
376
- }) => Promise<string>;
377
- getSignUpUrl: (options: {
378
- organizationId?: string;
379
- loginHint?: string;
380
- redirectUri?: string;
381
- }) => Promise<string>;
382
- terminateSession: (session: Session, response: any, options?: {
383
- returnTo?: string;
384
- } | undefined) => Promise<{
385
- response: TResponse;
386
- logoutUrl: string;
387
- }>;
388
- signOut: (request: TRequest, response: TResponse, options?: {
389
- returnTo?: string;
390
- }) => Promise<TResponse>;
391
- switchToOrganization: (request: any, response: any, organizationId: string) => Promise<{
392
- response: TResponse;
393
- authResult: AuthResult<CustomClaims>;
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
- }>;
403
- getTokenClaims: <TCustomClaims = CustomClaims>(request: TRequest, accessToken?: string) => Promise<Partial<BaseTokenClaims & TCustomClaims>>;
404
- };
405
-
406
- export { type AuthKitConfig, type AuthResult, type BaseTokenClaims, CookieSessionStorage, type CustomClaims, type Session, type SessionEncryption, SessionManager, type SessionStorage, type ValueSource, configure, createAuthKitFactory, getConfig, getWorkOS };