@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 WorkOS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# @workos/authkit-session
|
|
2
|
+
|
|
3
|
+
> [!WARNING]
|
|
4
|
+
>This is prerelease software. APIs may change without notice.
|
|
5
|
+
|
|
6
|
+
A framework-agnostic authentication library for WorkOS with a modular adapter system for server-side rendered applications.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Framework-agnostic core**: Common authentication logic that works across platforms
|
|
11
|
+
- **Adapter pattern**: Simple interface for framework-specific implementations
|
|
12
|
+
- **Session management**: Secure cookie-based authentication
|
|
13
|
+
- **JWT handling**: Token validation, parsing, and refresh
|
|
14
|
+
- **Type-safe API**: Full TypeScript support
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# Using npm
|
|
20
|
+
npm install @workos/authkit-session
|
|
21
|
+
|
|
22
|
+
# Using pnpm
|
|
23
|
+
pnpm add @workos/authkit-session
|
|
24
|
+
|
|
25
|
+
# Using yarn
|
|
26
|
+
yarn add @workos/authkit-session
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
1. Configure AuthKit with your WorkOS credentials:
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { configure, createAuthKitFactory } from '@workos/authkit-session';
|
|
35
|
+
|
|
36
|
+
configure({
|
|
37
|
+
clientId: 'your-client-id',
|
|
38
|
+
apiKey: 'your-workos-api-key',
|
|
39
|
+
redirectUri: 'https://yourdomain.com/auth/callback',
|
|
40
|
+
cookiePassword: 'must-be-at-least-32-characters-long-secret',
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
2. Create a storage adapter for your framework:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { createAuthKitFactory } from '@workos/authkit-session';
|
|
48
|
+
import type { SessionStorage } from '@workos/authkit-session';
|
|
49
|
+
|
|
50
|
+
// Create your framework-specific storage adapter
|
|
51
|
+
class MyFrameworkStorage implements SessionStorage<MyRequest, MyResponse> {
|
|
52
|
+
cookieName: string;
|
|
53
|
+
|
|
54
|
+
constructor(cookieName = 'wos-session') {
|
|
55
|
+
this.cookieName = cookieName;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async getSession(request: MyRequest): Promise<string | null> {
|
|
59
|
+
// Framework-specific implementation to get cookie
|
|
60
|
+
return getCookieFromRequest(request, this.cookieName);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async saveSession(response: MyResponse, sessionData: string): Promise<MyResponse> {
|
|
64
|
+
// Framework-specific implementation to set cookie
|
|
65
|
+
return setCookieOnResponse(response, this.cookieName, sessionData);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async clearSession(response: MyResponse): Promise<MyResponse> {
|
|
69
|
+
// Framework-specific implementation to clear cookie
|
|
70
|
+
return clearCookieOnResponse(response, this.cookieName);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Create your AuthKit instance
|
|
75
|
+
const authKit = createAuthKitFactory<MyRequest, MyResponse>({
|
|
76
|
+
sessionStorageFactory: (config) => new MyFrameworkStorage(),
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
3. Use AuthKit in your application:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
// Validate a session
|
|
84
|
+
const authResult = await authKit.withAuth(request);
|
|
85
|
+
const { user, claims, accessToken, refreshToken, sessionId, impersonator } = authResult;
|
|
86
|
+
|
|
87
|
+
// Generate an authorization URL
|
|
88
|
+
const authUrl = await authKit.getAuthorizationUrl({
|
|
89
|
+
returnPathname: '/dashboard',
|
|
90
|
+
redirectUri: 'https://yourdomain.com/auth/callback',
|
|
91
|
+
screenHint: 'sign-in', // or 'sign-up'
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Refresh a session
|
|
95
|
+
const refreshResult = await authKit.refreshSession(session);
|
|
96
|
+
const { user, sessionId, organizationId, role, permissions, entitlements, impersonator, accessToken, claims, sessionData, session: newSession } = refreshResult;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Core Concepts
|
|
100
|
+
|
|
101
|
+
### Session Management
|
|
102
|
+
|
|
103
|
+
AuthKit SSR uses encrypted cookies to store session information. It handles:
|
|
104
|
+
|
|
105
|
+
- Token encryption/decryption (using iron-webcrypto)
|
|
106
|
+
- JWT validation and parsing
|
|
107
|
+
- Session refresh logic
|
|
108
|
+
- Session termination
|
|
109
|
+
|
|
110
|
+
### Adapter System
|
|
111
|
+
|
|
112
|
+
The adapter pattern uses a storage interface to abstract framework-specific concepts:
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
interface SessionStorage<TRequest, TResponse, TOptions = unknown> {
|
|
116
|
+
getSession(request: TRequest): Promise<string | null>;
|
|
117
|
+
saveSession(response: TResponse, sessionData: string, options?: TOptions): Promise<TResponse>;
|
|
118
|
+
clearSession(response: TResponse, options?: TOptions): Promise<TResponse>;
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Each framework adapter implements this interface to handle its specific request/response objects.
|
|
123
|
+
|
|
124
|
+
## Configuration
|
|
125
|
+
|
|
126
|
+
AuthKit can be configured in multiple ways:
|
|
127
|
+
|
|
128
|
+
### Environment Variables
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
WORKOS_CLIENT_ID=your-client-id
|
|
132
|
+
WORKOS_API_KEY=your-api-key
|
|
133
|
+
WORKOS_REDIRECT_URI=https://yourdomain.com/auth/callback
|
|
134
|
+
WORKOS_COOKIE_PASSWORD=must-be-at-least-32-characters-long
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Programmatic Configuration
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { configure } from '@workos/authkit-session';
|
|
141
|
+
|
|
142
|
+
configure({
|
|
143
|
+
clientId: 'your-client-id',
|
|
144
|
+
apiKey: 'your-api-key',
|
|
145
|
+
redirectUri: 'https://yourdomain.com/auth/callback',
|
|
146
|
+
cookiePassword: 'must-be-at-least-32-characters-long',
|
|
147
|
+
cookieName: 'your-custom-cookie-name', // Default: 'wos-session'
|
|
148
|
+
cookieMaxAge: 60 * 60 * 24 * 30, // 30 days in seconds
|
|
149
|
+
cookieSameSite: 'lax', // 'strict', 'lax', or 'none'
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## API Reference
|
|
154
|
+
|
|
155
|
+
### Core API
|
|
156
|
+
|
|
157
|
+
- `configure(config)`: Set up AuthKit with your WorkOS configuration
|
|
158
|
+
- `getConfig(key)`: Get a specific configuration value
|
|
159
|
+
- `createAuthKitFactory(options)`: Create an instance of AuthKit for your framework
|
|
160
|
+
|
|
161
|
+
### AuthKit Instance API
|
|
162
|
+
|
|
163
|
+
- `withAuth(request)`: Validate the current session and return `AuthResult` with user, claims, tokens, and session info
|
|
164
|
+
- `getAuthorizationUrl(options)`: Generate a WorkOS authorization URL with `returnPathname`, `redirectUri`, and `screenHint`
|
|
165
|
+
- `getSignInUrl(options)`: Generate a sign-in URL (calls `getAuthorizationUrl` with `screenHint: 'sign-in'`)
|
|
166
|
+
- `getSignUpUrl(options)`: Generate a sign-up URL (calls `getAuthorizationUrl` with `screenHint: 'sign-up'`)
|
|
167
|
+
- `refreshSession(session)`: Refresh an existing session and return updated session data
|
|
168
|
+
- `saveSession(response, sessionData)`: Save session data to a response
|
|
169
|
+
- `getLogoutUrl(session, response, options)`: End a user session and return logout URL with updated response
|
|
170
|
+
|
|
171
|
+
## Security
|
|
172
|
+
|
|
173
|
+
AuthKit uses iron-webcrypto for secure, encrypted cookies with the following security features:
|
|
174
|
+
|
|
175
|
+
- Encrypted cookies (AES-256-CBC)
|
|
176
|
+
- HMAC validation (SHA-256)
|
|
177
|
+
- Customizable cookie settings (HttpOnly, SameSite, etc.)
|
|
178
|
+
- Token refresh mechanism
|
|
179
|
+
|
|
180
|
+
## License
|
|
181
|
+
|
|
182
|
+
MIT
|