@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
package/README.md
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
A framework-agnostic authentication library for WorkOS with a modular adapter system for server-side rendered applications.
|
|
7
7
|
|
|
8
|
+
This library serves as the foundation for framework-specific WorkOS authentication packages like `@workos-inc/authkit-nextjs`, `@workos-inc/authkit-remix`, and `@workos-inc/authkit-sveltekit`. By implementing the `SessionStorage` interface for your framework, you can provide WorkOS authentication capabilities with minimal framework-specific code.
|
|
9
|
+
|
|
8
10
|
## Features
|
|
9
11
|
|
|
10
12
|
- **Framework-agnostic core**: Common authentication logic that works across platforms
|
|
@@ -134,22 +136,31 @@ const authKit = createAuthKitFactory<MyRequest, MyResponse>({
|
|
|
134
136
|
const { user, claims, sessionId, impersonator, accessToken, refreshToken } =
|
|
135
137
|
await authKit.withAuth(request);
|
|
136
138
|
|
|
139
|
+
// Note: ensureSignedIn option not yet implemented
|
|
140
|
+
// Framework adapters should handle redirects when user is null
|
|
141
|
+
|
|
137
142
|
// Generate an authorization URL
|
|
138
143
|
const authUrl = await authKit.getAuthorizationUrl({
|
|
139
144
|
returnPathname: '/dashboard',
|
|
140
145
|
redirectUri: 'https://yourdomain.com/auth/callback',
|
|
141
146
|
screenHint: 'sign-in', // or 'sign-up'
|
|
147
|
+
organizationId: 'org_123', // Optional: pre-select organization
|
|
148
|
+
loginHint: 'user@example.com', // Optional: pre-fill email
|
|
149
|
+
prompt: 'login', // Optional: force re-authentication
|
|
142
150
|
});
|
|
143
151
|
|
|
144
|
-
// Generate sign-in/sign-up URLs
|
|
152
|
+
// Generate sign-in/sign-up URLs (convenience methods)
|
|
145
153
|
const signInUrl = await authKit.getSignInUrl({
|
|
154
|
+
returnPathname: '/dashboard',
|
|
146
155
|
redirectUri: 'https://yourdomain.com/auth/callback',
|
|
156
|
+
organizationId: 'org_123', // Optional
|
|
147
157
|
});
|
|
148
158
|
const signUpUrl = await authKit.getSignUpUrl({
|
|
159
|
+
returnPathname: '/welcome',
|
|
149
160
|
redirectUri: 'https://yourdomain.com/auth/callback',
|
|
150
161
|
});
|
|
151
162
|
|
|
152
|
-
// Get token claims
|
|
163
|
+
// Get token claims from current session
|
|
153
164
|
const claims = await authKit.getTokenClaims(request);
|
|
154
165
|
// Or parse specific access token
|
|
155
166
|
const specificClaims = await authKit.getTokenClaims(request, accessToken);
|
|
@@ -158,12 +169,126 @@ const specificClaims = await authKit.getTokenClaims(request, accessToken);
|
|
|
158
169
|
const { response: updatedResponse, authResult: newAuth } =
|
|
159
170
|
await authKit.switchToOrganization(request, response, 'org_123');
|
|
160
171
|
|
|
161
|
-
//
|
|
162
|
-
const {
|
|
172
|
+
// Handle OAuth callback
|
|
173
|
+
const {
|
|
174
|
+
response: updatedResponse,
|
|
175
|
+
returnPathname,
|
|
176
|
+
authResponse,
|
|
177
|
+
} = await authKit.handleCallback(request, response, {
|
|
178
|
+
code: 'oauth_code_from_query',
|
|
179
|
+
state: 'optional_state_from_query',
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Sign out (simple session termination)
|
|
183
|
+
const clearedResponse = await authKit.signOut(request, response, {
|
|
184
|
+
returnTo: 'https://yourdomain.com',
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// Terminate session and get logout URL (advanced)
|
|
188
|
+
const { response: clearedResponse, logoutUrl } = await authKit.terminateSession(
|
|
163
189
|
session,
|
|
164
190
|
response,
|
|
165
191
|
{ returnTo: 'https://yourdomain.com' },
|
|
166
192
|
);
|
|
193
|
+
|
|
194
|
+
// Manually save a session (for custom auth flows)
|
|
195
|
+
await authKit.saveSession(response, authResponse);
|
|
196
|
+
|
|
197
|
+
// Manually refresh session
|
|
198
|
+
const refreshedAuth = await authKit.refreshSession(session, organizationId);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## For Framework Implementers
|
|
202
|
+
|
|
203
|
+
If you're building a framework-specific package (like `@workos-inc/authkit-sveltekit`), this library provides everything needed for WorkOS authentication:
|
|
204
|
+
|
|
205
|
+
### Implementation Steps
|
|
206
|
+
|
|
207
|
+
1. **Install and configure** `@workos/authkit-session` as a dependency
|
|
208
|
+
2. **Create a SessionStorage adapter** for your framework's request/response objects
|
|
209
|
+
3. **Export framework-specific helpers** that wrap the core AuthKit functionality
|
|
210
|
+
4. **Handle routing** for sign-in, callback, and sign-out endpoints
|
|
211
|
+
|
|
212
|
+
### Example Framework Package Structure
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
// src/storage.ts - Framework-specific storage adapter
|
|
216
|
+
import { CookieSessionStorage } from '@workos/authkit-session';
|
|
217
|
+
import type { ConfigurationProvider } from '@workos/authkit-session';
|
|
218
|
+
|
|
219
|
+
export class SvelteKitStorage extends CookieSessionStorage<RequestEvent, void> {
|
|
220
|
+
// Implement framework-specific cookie handling
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/index.ts - Main exports
|
|
224
|
+
import { configure, createAuthKitFactory } from '@workos/authkit-session';
|
|
225
|
+
import { SvelteKitStorage } from './storage';
|
|
226
|
+
|
|
227
|
+
export { configure };
|
|
228
|
+
|
|
229
|
+
export const authKit = createAuthKitFactory({
|
|
230
|
+
sessionStorageFactory: config => new SvelteKitStorage(config),
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// Framework-specific helpers
|
|
234
|
+
export async function withAuth(event: RequestEvent) {
|
|
235
|
+
return authKit.withAuth(event);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export async function handleCallback(event: RequestEvent) {
|
|
239
|
+
const url = new URL(event.request.url);
|
|
240
|
+
const code = url.searchParams.get('code');
|
|
241
|
+
const state = url.searchParams.get('state');
|
|
242
|
+
|
|
243
|
+
if (!code) throw new Error('Missing authorization code');
|
|
244
|
+
|
|
245
|
+
const result = await authKit.handleCallback(event, undefined, {
|
|
246
|
+
code,
|
|
247
|
+
state,
|
|
248
|
+
});
|
|
249
|
+
// Handle redirect to returnPathname
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Key Considerations
|
|
254
|
+
|
|
255
|
+
- **Request/Response Objects**: Adapt your framework's request/response objects to the `SessionStorage` interface
|
|
256
|
+
- **Cookie Handling**: Implement secure cookie setting/getting for your framework
|
|
257
|
+
- **Routing Integration**: Provide helpers for authentication routes (`/auth/signin`, `/auth/callback`, `/auth/signout`)
|
|
258
|
+
- **TypeScript Support**: Export proper types for your framework's patterns
|
|
259
|
+
- **Error Handling**: Wrap AuthKit errors in framework-appropriate error types
|
|
260
|
+
|
|
261
|
+
### Configuration Validation
|
|
262
|
+
|
|
263
|
+
Framework adapters should validate configuration on startup to provide clear error messages:
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
import { validateConfig } from '@workos/authkit-session';
|
|
267
|
+
|
|
268
|
+
// In your framework's initialization or middleware
|
|
269
|
+
try {
|
|
270
|
+
validateConfig();
|
|
271
|
+
} catch (error) {
|
|
272
|
+
// Framework will display helpful error message with all missing/invalid config
|
|
273
|
+
throw error;
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
The `validateConfig()` function performs batch validation, collecting all configuration errors before throwing. This provides developers with a complete picture of what needs to be fixed, rather than discovering issues one at a time.
|
|
278
|
+
|
|
279
|
+
### Accessing Configuration
|
|
280
|
+
|
|
281
|
+
Framework adapters can access configuration values directly:
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
import { getConfig, getConfigurationProvider } from '@workos/authkit-session';
|
|
285
|
+
|
|
286
|
+
// Get a single config value
|
|
287
|
+
const clientId = getConfig('clientId');
|
|
288
|
+
|
|
289
|
+
// Get the ConfigurationProvider for more advanced use
|
|
290
|
+
const provider = getConfigurationProvider();
|
|
291
|
+
const config = provider.getConfig(); // Returns full AuthKitConfig object
|
|
167
292
|
```
|
|
168
293
|
|
|
169
294
|
## Core Concepts
|
|
@@ -302,7 +427,7 @@ AuthKit can be configured in multiple ways:
|
|
|
302
427
|
|
|
303
428
|
### Environment Variables
|
|
304
429
|
|
|
305
|
-
```
|
|
430
|
+
```bash
|
|
306
431
|
WORKOS_CLIENT_ID=your-client-id
|
|
307
432
|
WORKOS_API_KEY=your-api-key
|
|
308
433
|
WORKOS_REDIRECT_URI=https://yourdomain.com/auth/callback
|
|
@@ -314,6 +439,18 @@ WORKOS_API_HTTPS=true
|
|
|
314
439
|
WORKOS_API_PORT=443
|
|
315
440
|
```
|
|
316
441
|
|
|
442
|
+
**Environment Variable Naming Convention:**
|
|
443
|
+
Environment variables follow the pattern: `WORKOS_{PROPERTY_NAME}` where the property name is converted from camelCase to UPPER_SNAKE_CASE.
|
|
444
|
+
|
|
445
|
+
Examples:
|
|
446
|
+
|
|
447
|
+
- `clientId` → `WORKOS_CLIENT_ID`
|
|
448
|
+
- `redirectUri` → `WORKOS_REDIRECT_URI`
|
|
449
|
+
- `cookiePassword` → `WORKOS_COOKIE_PASSWORD`
|
|
450
|
+
- `cookieMaxAge` → `WORKOS_COOKIE_MAX_AGE`
|
|
451
|
+
|
|
452
|
+
Environment variables always take precedence over programmatic configuration.
|
|
453
|
+
|
|
317
454
|
### Programmatic Configuration
|
|
318
455
|
|
|
319
456
|
```typescript
|
|
@@ -339,19 +476,36 @@ configure({
|
|
|
339
476
|
### Core API
|
|
340
477
|
|
|
341
478
|
- `configure(config)`: Set up AuthKit with your WorkOS configuration
|
|
479
|
+
- `validateConfig()`: Validate all required configuration values are present and valid (throws descriptive error if not)
|
|
342
480
|
- `getConfig(key)`: Get a specific configuration value
|
|
481
|
+
- `getConfigurationProvider()`: Get the ConfigurationProvider instance for advanced configuration access
|
|
343
482
|
- `createAuthKitFactory(options)`: Create an instance of AuthKit for your framework
|
|
344
483
|
|
|
345
484
|
### AuthKit Instance API
|
|
346
485
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
- `
|
|
350
|
-
- `
|
|
486
|
+
#### Authentication & Session Management
|
|
487
|
+
|
|
488
|
+
- `withAuth<TCustomClaims>(request, options?)`: Validate the current session and return `AuthResult<TCustomClaims>`
|
|
489
|
+
- Note: `ensureSignedIn` option not yet implemented - framework adapters should handle redirects
|
|
490
|
+
- `handleCallback(request, response, { code, state? })`: Process OAuth callback and create session
|
|
491
|
+
- `signOut(request, response, options?)`: Simple session termination with cookie clearing
|
|
492
|
+
- `terminateSession(session, response, options?)`: Advanced session termination with logout URL
|
|
493
|
+
- `saveSession(response, authResponse)`: Manually save session from `AuthenticationResponse`
|
|
494
|
+
- `refreshSession(session, organizationId?)`: Manually refresh session tokens
|
|
495
|
+
|
|
496
|
+
#### URL Generation
|
|
497
|
+
|
|
498
|
+
- `getAuthorizationUrl(options)`: Generate a WorkOS authorization URL
|
|
499
|
+
- Options: `returnPathname`, `redirectUri`, `screenHint` ('sign-in' | 'sign-up'), `organizationId`, `loginHint`, `prompt` ('login' | 'none' | 'consent' | 'select_account')
|
|
500
|
+
- `getSignInUrl(options)`: Generate a sign-in URL (convenience method with screenHint='sign-in')
|
|
501
|
+
- Options: `returnPathname`, `redirectUri`, `organizationId`, `loginHint`, `prompt`
|
|
502
|
+
- `getSignUpUrl(options)`: Generate a sign-up URL (convenience method with screenHint='sign-up')
|
|
503
|
+
- Options: `returnPathname`, `redirectUri`, `organizationId`, `loginHint`, `prompt`
|
|
504
|
+
|
|
505
|
+
#### Token & Organization Management
|
|
506
|
+
|
|
351
507
|
- `getTokenClaims<TCustomClaims>(request, accessToken?)`: Parse JWT token claims from session or specific token
|
|
352
508
|
- `switchToOrganization(request, response, organizationId)`: Switch user to different organization context
|
|
353
|
-
- `saveSession(response, sessionData)`: Save encrypted session data to response
|
|
354
|
-
- `getLogoutUrl(session, response, options?)`: Terminate session and return logout URL with cleared response
|
|
355
509
|
|
|
356
510
|
## Advanced Features
|
|
357
511
|
|
|
@@ -443,9 +597,28 @@ The library also exports these components for advanced use cases:
|
|
|
443
597
|
|
|
444
598
|
```typescript
|
|
445
599
|
import {
|
|
600
|
+
// Core configuration
|
|
601
|
+
configure, // Set up configuration
|
|
602
|
+
validateConfig, // Validate all required config values
|
|
603
|
+
getConfig, // Get specific config value
|
|
604
|
+
getConfigurationProvider, // Get ConfigurationProvider instance
|
|
605
|
+
ConfigurationProvider, // ConfigurationProvider class
|
|
606
|
+
|
|
607
|
+
// Session management
|
|
446
608
|
SessionManager, // Core session management class
|
|
447
609
|
CookieSessionStorage, // Abstract cookie storage base class
|
|
610
|
+
|
|
611
|
+
// WorkOS client
|
|
448
612
|
getWorkOS, // WorkOS client factory
|
|
613
|
+
|
|
614
|
+
// Types
|
|
615
|
+
type SessionStorage,
|
|
616
|
+
type AuthKitConfig,
|
|
617
|
+
type AuthResult,
|
|
618
|
+
type Session,
|
|
619
|
+
type User,
|
|
620
|
+
type Impersonator,
|
|
621
|
+
// ... and more
|
|
449
622
|
} from '@workos/authkit-session';
|
|
450
623
|
```
|
|
451
624
|
|