@workos/authkit-session 0.1.2 → 0.2.0-beta.0
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 +507 -232
- package/dist/core/AuthKitCore.d.ts +107 -0
- package/dist/core/AuthKitCore.d.ts.map +1 -0
- package/dist/core/AuthKitCore.js +189 -0
- package/dist/core/AuthKitCore.js.map +1 -0
- package/dist/core/session/CookieSessionStorage.d.ts.map +1 -1
- package/dist/core/session/CookieSessionStorage.js +22 -3
- package/dist/core/session/CookieSessionStorage.js.map +1 -1
- package/dist/core/session/types.d.ts +27 -7
- package/dist/core/session/types.d.ts.map +1 -1
- package/dist/index.d.ts +27 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +48 -4
- package/dist/index.js.map +1 -1
- package/dist/operations/AuthOperations.d.ts +84 -0
- package/dist/operations/AuthOperations.d.ts.map +1 -0
- package/dist/operations/AuthOperations.js +147 -0
- package/dist/operations/AuthOperations.js.map +1 -0
- package/dist/service/AuthService.d.ts +156 -0
- package/dist/service/AuthService.d.ts.map +1 -0
- package/dist/service/AuthService.js +225 -0
- package/dist/service/AuthService.js.map +1 -0
- package/dist/service/factory.d.ts +37 -0
- package/dist/service/factory.d.ts.map +1 -0
- package/dist/service/factory.js +46 -0
- package/dist/service/factory.js.map +1 -0
- package/package.json +6 -6
- package/src/core/AuthKitCore.spec.ts +269 -0
- package/src/core/AuthKitCore.ts +239 -0
- package/src/core/session/CookieSessionStorage.spec.ts +17 -0
- package/src/core/session/CookieSessionStorage.ts +22 -2
- package/src/core/session/types.ts +30 -8
- package/src/index.ts +55 -4
- package/src/operations/AuthOperations.spec.ts +240 -0
- package/src/operations/AuthOperations.ts +192 -0
- package/src/service/AuthService.spec.ts +241 -0
- package/src/service/AuthService.ts +293 -0
- package/src/service/factory.spec.ts +109 -0
- package/src/service/factory.ts +69 -0
- package/dist/core/createAuthKitFactory.d.ts +0 -64
- package/dist/core/createAuthKitFactory.d.ts.map +0 -1
- package/dist/core/createAuthKitFactory.js +0 -95
- package/dist/core/createAuthKitFactory.js.map +0 -1
- package/dist/core/session/SessionManager.d.ts +0 -51
- package/dist/core/session/SessionManager.d.ts.map +0 -1
- package/dist/core/session/SessionManager.js +0 -238
- package/dist/core/session/SessionManager.js.map +0 -1
- package/src/core/createAuthKitFactory.spec.ts +0 -161
- package/src/core/createAuthKitFactory.ts +0 -195
- package/src/core/session/SessionManager.spec.ts +0 -357
- package/src/core/session/SessionManager.ts +0 -357
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthOperations provides high-level authentication operations.
|
|
3
|
+
*
|
|
4
|
+
* This class orchestrates between AuthKitCore (pure logic) and WorkOS API.
|
|
5
|
+
* It doesn't know about requests, responses, or cookies - that's the framework's job.
|
|
6
|
+
*
|
|
7
|
+
* Responsibilities:
|
|
8
|
+
* - Sign out (generate logout URL + clear cookie header)
|
|
9
|
+
* - Switch organization (refresh with new org + return auth result)
|
|
10
|
+
* - Refresh session (refresh tokens + return auth result)
|
|
11
|
+
* - Get authorization URLs (for sign in/sign up flows)
|
|
12
|
+
*/
|
|
13
|
+
export class AuthOperations {
|
|
14
|
+
core;
|
|
15
|
+
client;
|
|
16
|
+
config;
|
|
17
|
+
constructor(core, client, config) {
|
|
18
|
+
this.core = core;
|
|
19
|
+
this.client = client;
|
|
20
|
+
this.config = config;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Sign out operation.
|
|
24
|
+
*
|
|
25
|
+
* Returns the WorkOS logout URL and a cookie clear header string.
|
|
26
|
+
* The framework is responsible for applying the header and redirecting.
|
|
27
|
+
*
|
|
28
|
+
* @param sessionId - The session ID to terminate
|
|
29
|
+
* @param options - Optional return URL
|
|
30
|
+
* @returns Logout URL and clear cookie header
|
|
31
|
+
*/
|
|
32
|
+
async signOut(sessionId, options) {
|
|
33
|
+
// Generate WorkOS logout URL
|
|
34
|
+
const logoutUrl = this.client.userManagement.getLogoutUrl({
|
|
35
|
+
sessionId,
|
|
36
|
+
returnTo: options?.returnTo,
|
|
37
|
+
});
|
|
38
|
+
// Build cookie clear header
|
|
39
|
+
const cookieName = this.config.getValue('cookieName');
|
|
40
|
+
const clearCookieHeader = `${cookieName}=; Path=/; Max-Age=0; HttpOnly; Secure; SameSite=lax`;
|
|
41
|
+
return {
|
|
42
|
+
logoutUrl,
|
|
43
|
+
clearCookieHeader,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Switch to a different organization.
|
|
48
|
+
*
|
|
49
|
+
* This is a convenience wrapper around refreshSession() that enforces
|
|
50
|
+
* an organization ID must be provided.
|
|
51
|
+
*
|
|
52
|
+
* @param session - Current session
|
|
53
|
+
* @param organizationId - Organization ID to switch to (required)
|
|
54
|
+
* @returns Auth result and encrypted session data
|
|
55
|
+
*/
|
|
56
|
+
async switchOrganization(session, organizationId) {
|
|
57
|
+
// Delegate to refreshSession with explicit organization
|
|
58
|
+
return this.refreshSession(session, organizationId);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Refresh session operation.
|
|
62
|
+
*
|
|
63
|
+
* Calls WorkOS to refresh tokens (optionally switching organizations),
|
|
64
|
+
* encrypts the new session, and returns the auth result.
|
|
65
|
+
*
|
|
66
|
+
* @param session - Current session with refresh token
|
|
67
|
+
* @param organizationId - Optional organization ID to switch to during refresh
|
|
68
|
+
* @returns Auth result and encrypted session data
|
|
69
|
+
*/
|
|
70
|
+
async refreshSession(session, organizationId) {
|
|
71
|
+
// Determine which organization to use
|
|
72
|
+
let orgId = organizationId;
|
|
73
|
+
if (!orgId) {
|
|
74
|
+
// Extract org from current token (decodeJwt works even on expired tokens)
|
|
75
|
+
try {
|
|
76
|
+
const claims = this.core.parseTokenClaims(session.accessToken);
|
|
77
|
+
orgId = claims.org_id;
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
// Token too malformed to parse - refresh without org context
|
|
81
|
+
// WorkOS will use whatever org is embedded in the refresh token
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const result = await this.core.refreshTokens(session.refreshToken, orgId);
|
|
85
|
+
const newSession = {
|
|
86
|
+
accessToken: result.accessToken,
|
|
87
|
+
refreshToken: result.refreshToken,
|
|
88
|
+
user: result.user,
|
|
89
|
+
impersonator: result.impersonator,
|
|
90
|
+
};
|
|
91
|
+
const encryptedSession = await this.core.encryptSession(newSession);
|
|
92
|
+
const claims = this.core.parseTokenClaims(result.accessToken);
|
|
93
|
+
const auth = {
|
|
94
|
+
user: result.user,
|
|
95
|
+
sessionId: claims.sid,
|
|
96
|
+
impersonator: result.impersonator,
|
|
97
|
+
accessToken: result.accessToken,
|
|
98
|
+
refreshToken: result.refreshToken,
|
|
99
|
+
claims,
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
auth,
|
|
103
|
+
encryptedSession,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get authorization URL for WorkOS authentication.
|
|
108
|
+
*
|
|
109
|
+
* Builds the WorkOS authorization URL with proper state encoding.
|
|
110
|
+
*
|
|
111
|
+
* @param options - Authorization URL options (returnPathname, screenHint, etc.)
|
|
112
|
+
* @returns The authorization URL
|
|
113
|
+
*/
|
|
114
|
+
async getAuthorizationUrl(options = {}) {
|
|
115
|
+
return this.client.userManagement.getAuthorizationUrl({
|
|
116
|
+
provider: 'authkit',
|
|
117
|
+
redirectUri: options.redirectUri ?? this.config.getValue('redirectUri'),
|
|
118
|
+
screenHint: options.screenHint,
|
|
119
|
+
organizationId: options.organizationId,
|
|
120
|
+
loginHint: options.loginHint,
|
|
121
|
+
prompt: options.prompt,
|
|
122
|
+
clientId: this.config.getValue('clientId'),
|
|
123
|
+
state: options.returnPathname
|
|
124
|
+
? btoa(JSON.stringify({ returnPathname: options.returnPathname }))
|
|
125
|
+
: undefined,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Convenience method: Get sign-in URL.
|
|
130
|
+
*/
|
|
131
|
+
async getSignInUrl(options = {}) {
|
|
132
|
+
return this.getAuthorizationUrl({
|
|
133
|
+
...options,
|
|
134
|
+
screenHint: 'sign-in',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Convenience method: Get sign-up URL.
|
|
139
|
+
*/
|
|
140
|
+
async getSignUpUrl(options = {}) {
|
|
141
|
+
return this.getAuthorizationUrl({
|
|
142
|
+
...options,
|
|
143
|
+
screenHint: 'sign-up',
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=AuthOperations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthOperations.js","sourceRoot":"","sources":["../../src/operations/AuthOperations.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,cAAc;IAEf;IACA;IACA;IAHV,YACU,IAAiB,EACjB,MAAc,EACd,MAA6B;QAF7B,SAAI,GAAJ,IAAI,CAAa;QACjB,WAAM,GAAN,MAAM,CAAQ;QACd,WAAM,GAAN,MAAM,CAAuB;IACpC,CAAC;IAEJ;;;;;;;;;OASG;IACH,KAAK,CAAC,OAAO,CACX,SAAiB,EACjB,OAA+B;QAK/B,6BAA6B;QAC7B,MAAM,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,YAAY,CAAC;YACxD,SAAS;YACT,QAAQ,EAAE,OAAO,EAAE,QAAQ;SAC5B,CAAC,CAAC;QAEH,4BAA4B;QAC5B,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;QACtD,MAAM,iBAAiB,GAAG,GAAG,UAAU,sDAAsD,CAAC;QAE9F,OAAO;YACL,SAAS;YACT,iBAAiB;SAClB,CAAC;IACJ,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,kBAAkB,CACtB,OAAgB,EA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IAAI;YACJ,gBAAgB;SACjB,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,mBAAmB,CACvB,UAAsC,EAAE;QAExC,OAAO,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,mBAAmB,CAAC;YACpD,QAAQ,EAAE,SAAS;YACnB,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAC;YACvE,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,SAAS,EAAE,OAAO,CAAC,SAAS;YAC5B,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC;YAC1C,KAAK,EAAE,OAAO,CAAC,cAAc;gBAC3B,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,cAAc,EAAE,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;gBAClE,CAAC,CAAC,SAAS;SACd,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY,CAChB,UAA0D,EAAE;QAE5D,OAAO,IAAI,CAAC,mBAAmB,CAAC;YAC9B,GAAG,OAAO;YACV,UAAU,EAAE,SAAS;SACtB,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,YAAY,CAChB,UAA0D,EAAE;QAE5D,OAAO,IAAI,CAAC,mBAAmB,CAAC;YAC9B,GAAG,OAAO;YACV,UAAU,EAAE,SAAS;SACtB,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { WorkOS } from '@workos-inc/node';
|
|
2
|
+
import type { ConfigurationProvider } from '../core/config/ConfigurationProvider.js';
|
|
3
|
+
import type { AuthKitConfig } from '../core/config/types.js';
|
|
4
|
+
import type { AuthResult, CustomClaims, GetAuthorizationUrlOptions, HeadersBag, Session, SessionEncryption, SessionStorage } from '../core/session/types.js';
|
|
5
|
+
/**
|
|
6
|
+
* AuthService is a REFERENCE IMPLEMENTATION for framework adapters.
|
|
7
|
+
*
|
|
8
|
+
* **IMPORTANT:** This class is optional. Framework adapters can use AuthKitCore,
|
|
9
|
+
* AuthOperations, and CookieSessionStorage directly to implement their own patterns.
|
|
10
|
+
*
|
|
11
|
+
* This class provides a pattern for coordinating between:
|
|
12
|
+
* - AuthKitCore (pure business logic: crypto, JWT, refresh)
|
|
13
|
+
* - AuthOperations (WorkOS API operations: signOut, refreshSession, URLs)
|
|
14
|
+
* - SessionStorage<TRequest, TResponse> (framework-specific storage)
|
|
15
|
+
*
|
|
16
|
+
* **Why this is optional:**
|
|
17
|
+
* - Frameworks have different middleware/route patterns (Next.js vs TanStack vs Remix)
|
|
18
|
+
* - Request context handling differs by framework (headers vs locals vs WeakMap)
|
|
19
|
+
* - Framework-specific features (callbacks, eagerAuth) don't fit generic patterns
|
|
20
|
+
*
|
|
21
|
+
* **Recommended usage:**
|
|
22
|
+
* - Use AuthKitCore directly for crypto, token validation, and refresh logic
|
|
23
|
+
* - Use AuthOperations for WorkOS API calls (refreshSession, signOut, URLs)
|
|
24
|
+
* - Use CookieSessionStorage.buildSetCookie() for cookie string generation
|
|
25
|
+
* - Implement your own updateSession/withAuth patterns that fit your framework
|
|
26
|
+
*
|
|
27
|
+
* **See also:**
|
|
28
|
+
* - authkit-nextjs: Uses Core + Operations directly, implements Next.js patterns
|
|
29
|
+
* - authkit-tanstack-start: Uses Core + Operations, implements TanStack patterns
|
|
30
|
+
*
|
|
31
|
+
* **Lazy initialization:**
|
|
32
|
+
* This class uses lazy initialization via private getters, which allows
|
|
33
|
+
* configuration to be set AFTER instantiation but BEFORE first use.
|
|
34
|
+
* This pattern may be useful for your framework adapter.
|
|
35
|
+
*/
|
|
36
|
+
export declare class AuthService<TRequest, TResponse> {
|
|
37
|
+
private _core?;
|
|
38
|
+
private _operations?;
|
|
39
|
+
private readonly storage;
|
|
40
|
+
private readonly config;
|
|
41
|
+
private readonly clientFactory;
|
|
42
|
+
private readonly encryptionFactory;
|
|
43
|
+
constructor(config: ConfigurationProvider, storage: SessionStorage<TRequest, TResponse>, clientFactory: (config: AuthKitConfig) => WorkOS, encryptionFactory: (config: AuthKitConfig) => SessionEncryption);
|
|
44
|
+
/**
|
|
45
|
+
* Lazy getter for AuthKitCore.
|
|
46
|
+
* Instantiates on first access, allowing config to be set beforehand.
|
|
47
|
+
*/
|
|
48
|
+
private get core();
|
|
49
|
+
/**
|
|
50
|
+
* Lazy getter for AuthOperations.
|
|
51
|
+
* Instantiates on first access, allowing config to be set beforehand.
|
|
52
|
+
*/
|
|
53
|
+
private get operations();
|
|
54
|
+
/**
|
|
55
|
+
* Main authentication check method.
|
|
56
|
+
*
|
|
57
|
+
* This method:
|
|
58
|
+
* 1. Reads encrypted session from request (via storage)
|
|
59
|
+
* 2. Validates and potentially refreshes the session (via core)
|
|
60
|
+
* 3. Returns auth result + optionally refreshed session data
|
|
61
|
+
*
|
|
62
|
+
* @param request - Framework-specific request object
|
|
63
|
+
* @returns Auth result and optional refreshed session data
|
|
64
|
+
*/
|
|
65
|
+
withAuth<TCustomClaims = CustomClaims>(request: TRequest): Promise<{
|
|
66
|
+
auth: AuthResult<TCustomClaims>;
|
|
67
|
+
refreshedSessionData?: string;
|
|
68
|
+
}>;
|
|
69
|
+
/**
|
|
70
|
+
* Get a session from a request.
|
|
71
|
+
*
|
|
72
|
+
* @param request - Framework-specific request object
|
|
73
|
+
* @returns Decrypted session or null
|
|
74
|
+
*/
|
|
75
|
+
getSession(request: TRequest): Promise<Session | null>;
|
|
76
|
+
/**
|
|
77
|
+
* Save a session to storage.
|
|
78
|
+
*
|
|
79
|
+
* @param response - Framework-specific response object (may be undefined)
|
|
80
|
+
* @param sessionData - Encrypted session string
|
|
81
|
+
* @returns Updated response and/or headers
|
|
82
|
+
*/
|
|
83
|
+
saveSession(response: TResponse | undefined, sessionData: string): Promise<{
|
|
84
|
+
response?: TResponse;
|
|
85
|
+
headers?: HeadersBag;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Clear a session from storage.
|
|
89
|
+
*
|
|
90
|
+
* @param response - Framework-specific response object
|
|
91
|
+
* @returns Updated response and/or headers
|
|
92
|
+
*/
|
|
93
|
+
clearSession(response: TResponse): Promise<{
|
|
94
|
+
response?: TResponse;
|
|
95
|
+
headers?: HeadersBag;
|
|
96
|
+
}>;
|
|
97
|
+
/**
|
|
98
|
+
* Sign out operation - delegates to AuthOperations.
|
|
99
|
+
*/
|
|
100
|
+
signOut(sessionId: string, options?: {
|
|
101
|
+
returnTo?: string;
|
|
102
|
+
}): Promise<{
|
|
103
|
+
logoutUrl: string;
|
|
104
|
+
clearCookieHeader: string;
|
|
105
|
+
}>;
|
|
106
|
+
/**
|
|
107
|
+
* Switch organization - delegates to AuthOperations.
|
|
108
|
+
*/
|
|
109
|
+
switchOrganization(session: Session, organizationId: string): Promise<{
|
|
110
|
+
auth: AuthResult;
|
|
111
|
+
encryptedSession: string;
|
|
112
|
+
}>;
|
|
113
|
+
/**
|
|
114
|
+
* Refresh session - delegates to AuthOperations.
|
|
115
|
+
*/
|
|
116
|
+
refreshSession(session: Session, organizationId?: string): Promise<{
|
|
117
|
+
auth: AuthResult;
|
|
118
|
+
encryptedSession: string;
|
|
119
|
+
}>;
|
|
120
|
+
/**
|
|
121
|
+
* Get authorization URL - delegates to AuthOperations.
|
|
122
|
+
*/
|
|
123
|
+
getAuthorizationUrl(options?: GetAuthorizationUrlOptions): Promise<string>;
|
|
124
|
+
/**
|
|
125
|
+
* Convenience: Get sign-in URL.
|
|
126
|
+
*/
|
|
127
|
+
getSignInUrl(options?: Omit<GetAuthorizationUrlOptions, 'screenHint'>): Promise<string>;
|
|
128
|
+
/**
|
|
129
|
+
* Convenience: Get sign-up URL.
|
|
130
|
+
*/
|
|
131
|
+
getSignUpUrl(options?: Omit<GetAuthorizationUrlOptions, 'screenHint'>): Promise<string>;
|
|
132
|
+
/**
|
|
133
|
+
* Get the WorkOS client instance.
|
|
134
|
+
* Useful for direct API calls not covered by AuthKit.
|
|
135
|
+
*/
|
|
136
|
+
getWorkOS(): WorkOS;
|
|
137
|
+
/**
|
|
138
|
+
* Handle OAuth callback.
|
|
139
|
+
* This creates a new session after successful authentication.
|
|
140
|
+
*
|
|
141
|
+
* @param request - Framework-specific request (not currently used)
|
|
142
|
+
* @param response - Framework-specific response
|
|
143
|
+
* @param options - OAuth callback options (code, state)
|
|
144
|
+
* @returns Updated response, return pathname, and auth response
|
|
145
|
+
*/
|
|
146
|
+
handleCallback(_request: TRequest, response: TResponse, options: {
|
|
147
|
+
code: string;
|
|
148
|
+
state?: string;
|
|
149
|
+
}): Promise<{
|
|
150
|
+
response: TResponse | undefined;
|
|
151
|
+
headers: HeadersBag | undefined;
|
|
152
|
+
returnPathname: string;
|
|
153
|
+
authResponse: import("@workos-inc/node").AuthenticationResponse;
|
|
154
|
+
}>;
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=AuthService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthService.d.ts","sourceRoot":"","sources":["../../src/service/AuthService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,yCAAyC,CAAC;AACrF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAE7D,OAAO,KAAK,EACV,UAAU,EACV,YAAY,EACZ,0BAA0B,EAC1B,UAAU,EACV,OAAO,EACP,iBAAiB,EACjB,cAAc,EACf,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,WAAW,CAAC,QAAQ,EAAE,SAAS;IAC1C,OAAO,CAAC,KAAK,CAAC,CAAc;IAC5B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AQ,CAAC,EAAE,MAAM,CAAA;KAAE;;;;IAIhE;;OAEG;IACG,kBAAkB,CAAC,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM;;;;IAIjE;;OAEG;IACG,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,cAAc,CAAC,EAAE,MAAM;;;;IAI9D;;OAEG;IACG,mBAAmB,CAAC,OAAO,GAAE,0BAA+B;IAIlE;;OAEG;IACG,YAAY,CAChB,OAAO,GAAE,IAAI,CAAC,0BAA0B,EAAE,YAAY,CAAM;IAK9D;;OAEG;IACG,YAAY,CAChB,OAAO,GAAE,IAAI,CAAC,0BAA0B,EAAE,YAAY,CAAM;IAK9D;;;OAGG;IACH,SAAS,IAAI,MAAM;IAInB;;;;;;;;OAQG;IACG,cAAc,CAClB,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,SAAS,EACnB,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE;;;;;;CAwC5C"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { AuthKitCore } from '../core/AuthKitCore.js';
|
|
2
|
+
import { AuthOperations } from '../operations/AuthOperations.js';
|
|
3
|
+
/**
|
|
4
|
+
* AuthService is a REFERENCE IMPLEMENTATION for framework adapters.
|
|
5
|
+
*
|
|
6
|
+
* **IMPORTANT:** This class is optional. Framework adapters can use AuthKitCore,
|
|
7
|
+
* AuthOperations, and CookieSessionStorage directly to implement their own patterns.
|
|
8
|
+
*
|
|
9
|
+
* This class provides a pattern for coordinating between:
|
|
10
|
+
* - AuthKitCore (pure business logic: crypto, JWT, refresh)
|
|
11
|
+
* - AuthOperations (WorkOS API operations: signOut, refreshSession, URLs)
|
|
12
|
+
* - SessionStorage<TRequest, TResponse> (framework-specific storage)
|
|
13
|
+
*
|
|
14
|
+
* **Why this is optional:**
|
|
15
|
+
* - Frameworks have different middleware/route patterns (Next.js vs TanStack vs Remix)
|
|
16
|
+
* - Request context handling differs by framework (headers vs locals vs WeakMap)
|
|
17
|
+
* - Framework-specific features (callbacks, eagerAuth) don't fit generic patterns
|
|
18
|
+
*
|
|
19
|
+
* **Recommended usage:**
|
|
20
|
+
* - Use AuthKitCore directly for crypto, token validation, and refresh logic
|
|
21
|
+
* - Use AuthOperations for WorkOS API calls (refreshSession, signOut, URLs)
|
|
22
|
+
* - Use CookieSessionStorage.buildSetCookie() for cookie string generation
|
|
23
|
+
* - Implement your own updateSession/withAuth patterns that fit your framework
|
|
24
|
+
*
|
|
25
|
+
* **See also:**
|
|
26
|
+
* - authkit-nextjs: Uses Core + Operations directly, implements Next.js patterns
|
|
27
|
+
* - authkit-tanstack-start: Uses Core + Operations, implements TanStack patterns
|
|
28
|
+
*
|
|
29
|
+
* **Lazy initialization:**
|
|
30
|
+
* This class uses lazy initialization via private getters, which allows
|
|
31
|
+
* configuration to be set AFTER instantiation but BEFORE first use.
|
|
32
|
+
* This pattern may be useful for your framework adapter.
|
|
33
|
+
*/
|
|
34
|
+
export class AuthService {
|
|
35
|
+
_core;
|
|
36
|
+
_operations;
|
|
37
|
+
storage;
|
|
38
|
+
config;
|
|
39
|
+
clientFactory;
|
|
40
|
+
encryptionFactory;
|
|
41
|
+
constructor(config, storage, clientFactory, encryptionFactory) {
|
|
42
|
+
this.config = config;
|
|
43
|
+
this.storage = storage;
|
|
44
|
+
this.clientFactory = clientFactory;
|
|
45
|
+
this.encryptionFactory = encryptionFactory;
|
|
46
|
+
// NOTE: core and operations are NOT instantiated here
|
|
47
|
+
// They're created lazily on first access via getters below
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Lazy getter for AuthKitCore.
|
|
51
|
+
* Instantiates on first access, allowing config to be set beforehand.
|
|
52
|
+
*/
|
|
53
|
+
get core() {
|
|
54
|
+
if (!this._core) {
|
|
55
|
+
this._core = new AuthKitCore(this.config, this.clientFactory(this.config.getConfig()), this.encryptionFactory(this.config.getConfig()));
|
|
56
|
+
}
|
|
57
|
+
return this._core;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Lazy getter for AuthOperations.
|
|
61
|
+
* Instantiates on first access, allowing config to be set beforehand.
|
|
62
|
+
*/
|
|
63
|
+
get operations() {
|
|
64
|
+
if (!this._operations) {
|
|
65
|
+
this._operations = new AuthOperations(this.core, this.clientFactory(this.config.getConfig()), this.config);
|
|
66
|
+
}
|
|
67
|
+
return this._operations;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Main authentication check method.
|
|
71
|
+
*
|
|
72
|
+
* This method:
|
|
73
|
+
* 1. Reads encrypted session from request (via storage)
|
|
74
|
+
* 2. Validates and potentially refreshes the session (via core)
|
|
75
|
+
* 3. Returns auth result + optionally refreshed session data
|
|
76
|
+
*
|
|
77
|
+
* @param request - Framework-specific request object
|
|
78
|
+
* @returns Auth result and optional refreshed session data
|
|
79
|
+
*/
|
|
80
|
+
async withAuth(request) {
|
|
81
|
+
try {
|
|
82
|
+
const encryptedSession = await this.storage.getSession(request);
|
|
83
|
+
if (!encryptedSession) {
|
|
84
|
+
return { auth: { user: null } };
|
|
85
|
+
}
|
|
86
|
+
const { claims, session, refreshed } = await this.core.validateAndRefresh(await this.core.decryptSession(encryptedSession));
|
|
87
|
+
const auth = {
|
|
88
|
+
refreshToken: session.refreshToken,
|
|
89
|
+
user: session.user,
|
|
90
|
+
claims,
|
|
91
|
+
impersonator: session.impersonator,
|
|
92
|
+
accessToken: session.accessToken,
|
|
93
|
+
sessionId: claims.sid,
|
|
94
|
+
};
|
|
95
|
+
if (refreshed) {
|
|
96
|
+
const refreshedSessionData = await this.core.encryptSession(session);
|
|
97
|
+
return { auth, refreshedSessionData };
|
|
98
|
+
}
|
|
99
|
+
return { auth };
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return { auth: { user: null } };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get a session from a request.
|
|
107
|
+
*
|
|
108
|
+
* @param request - Framework-specific request object
|
|
109
|
+
* @returns Decrypted session or null
|
|
110
|
+
*/
|
|
111
|
+
async getSession(request) {
|
|
112
|
+
const encryptedSession = await this.storage.getSession(request);
|
|
113
|
+
if (!encryptedSession) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
return this.core.decryptSession(encryptedSession);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Save a session to storage.
|
|
120
|
+
*
|
|
121
|
+
* @param response - Framework-specific response object (may be undefined)
|
|
122
|
+
* @param sessionData - Encrypted session string
|
|
123
|
+
* @returns Updated response and/or headers
|
|
124
|
+
*/
|
|
125
|
+
async saveSession(response, sessionData) {
|
|
126
|
+
return this.storage.saveSession(response, sessionData);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Clear a session from storage.
|
|
130
|
+
*
|
|
131
|
+
* @param response - Framework-specific response object
|
|
132
|
+
* @returns Updated response and/or headers
|
|
133
|
+
*/
|
|
134
|
+
async clearSession(response) {
|
|
135
|
+
return this.storage.clearSession(response);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Sign out operation - delegates to AuthOperations.
|
|
139
|
+
*/
|
|
140
|
+
async signOut(sessionId, options) {
|
|
141
|
+
return this.operations.signOut(sessionId, options);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Switch organization - delegates to AuthOperations.
|
|
145
|
+
*/
|
|
146
|
+
async switchOrganization(session, organizationId) {
|
|
147
|
+
return this.operations.switchOrganization(session, organizationId);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Refresh session - delegates to AuthOperations.
|
|
151
|
+
*/
|
|
152
|
+
async refreshSession(session, organizationId) {
|
|
153
|
+
return this.operations.refreshSession(session, organizationId);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Get authorization URL - delegates to AuthOperations.
|
|
157
|
+
*/
|
|
158
|
+
async getAuthorizationUrl(options = {}) {
|
|
159
|
+
return this.operations.getAuthorizationUrl(options);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Convenience: Get sign-in URL.
|
|
163
|
+
*/
|
|
164
|
+
async getSignInUrl(options = {}) {
|
|
165
|
+
return this.operations.getSignInUrl(options);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Convenience: Get sign-up URL.
|
|
169
|
+
*/
|
|
170
|
+
async getSignUpUrl(options = {}) {
|
|
171
|
+
return this.operations.getSignUpUrl(options);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Get the WorkOS client instance.
|
|
175
|
+
* Useful for direct API calls not covered by AuthKit.
|
|
176
|
+
*/
|
|
177
|
+
getWorkOS() {
|
|
178
|
+
return this.clientFactory(this.config.getConfig());
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Handle OAuth callback.
|
|
182
|
+
* This creates a new session after successful authentication.
|
|
183
|
+
*
|
|
184
|
+
* @param request - Framework-specific request (not currently used)
|
|
185
|
+
* @param response - Framework-specific response
|
|
186
|
+
* @param options - OAuth callback options (code, state)
|
|
187
|
+
* @returns Updated response, return pathname, and auth response
|
|
188
|
+
*/
|
|
189
|
+
async handleCallback(_request, response, options) {
|
|
190
|
+
const clientId = this.config.getValue('clientId');
|
|
191
|
+
const client = this.clientFactory(this.config.getConfig());
|
|
192
|
+
// Authenticate with WorkOS using the OAuth code
|
|
193
|
+
const authResponse = await client.userManagement.authenticateWithCode({
|
|
194
|
+
code: options.code,
|
|
195
|
+
clientId,
|
|
196
|
+
});
|
|
197
|
+
// Create and save the new session
|
|
198
|
+
const session = {
|
|
199
|
+
accessToken: authResponse.accessToken,
|
|
200
|
+
refreshToken: authResponse.refreshToken,
|
|
201
|
+
user: authResponse.user,
|
|
202
|
+
impersonator: authResponse.impersonator,
|
|
203
|
+
};
|
|
204
|
+
const encryptedSession = await this.core.encryptSession(session);
|
|
205
|
+
const { response: updatedResponse, headers } = await this.saveSession(response, encryptedSession);
|
|
206
|
+
// Decode return pathname from state
|
|
207
|
+
let returnPathname = '/';
|
|
208
|
+
if (options.state) {
|
|
209
|
+
try {
|
|
210
|
+
const decoded = JSON.parse(atob(options.state));
|
|
211
|
+
returnPathname = decoded.returnPathname || '/';
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
// Invalid state, use default
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return {
|
|
218
|
+
response: updatedResponse,
|
|
219
|
+
headers,
|
|
220
|
+
returnPathname,
|
|
221
|
+
authResponse,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=AuthService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AuthService.js","sourceRoot":"","sources":["../../src/service/AuthService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAGrD,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AAWjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,OAAO,WAAW;IACd,KAAK,CAAe;IACpB,WAAW,CAAkB;IACpB,OAAO,CAAsC;IAC7C,MAAM,CAAwB;IAC9B,aAAa,CAAoC;IACjD,iBAAiB,CAA+C;IAEjF,YACE,MAA6B,EAC7B,OAA4C,EAC5C,aAAgD,EAChD,iBAA+D;QAE/D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,sDAAsD;QACtD,2DAA2D;IAC7D,CAAC;IAED;;;OAGG;IACH,IAAY,IAAI;QACd,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YAChB,IAAI,CAAC,KAAK,GAAG,IAAI,WAAW,CAC1B,IAAI,CAAC,MAA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I;YACvB,YAAY,EAAE,YAAY,CAAC,YAAY;SACxC,CAAC;QAEF,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QACjE,MAAM,EAAE,QAAQ,EAAE,eAAe,EAAE,OAAO,EAAE,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC;QAElG,oCAAoC;QACpC,IAAI,cAAc,GAAG,GAAG,CAAC;QACzB,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC;YAClB,IAAI,CAAC;gBACH,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;gBAChD,cAAc,GAAG,OAAO,CAAC,cAAc,IAAI,GAAG,CAAC;YACjD,CAAC;YAAC,MAAM,CAAC;gBACP,6BAA6B;YAC/B,CAAC;QACH,CAAC;QAED,OAAO;YACL,QAAQ,EAAE,eAAe;YACzB,OAAO;YACP,cAAc;YACd,YAAY;SACb,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { WorkOS } from '@workos-inc/node';
|
|
2
|
+
import type { AuthKitConfig } from '../core/config/types.js';
|
|
3
|
+
import type { SessionEncryption, SessionStorage } from '../core/session/types.js';
|
|
4
|
+
import { AuthService } from './AuthService.js';
|
|
5
|
+
/**
|
|
6
|
+
* Creates an AuthService instance with lazy initialization support.
|
|
7
|
+
*
|
|
8
|
+
* This factory function provides ergonomic AuthService creation with sensible defaults.
|
|
9
|
+
* The returned service uses lazy initialization, allowing configure() to be called
|
|
10
|
+
* after instantiation but before first use.
|
|
11
|
+
*
|
|
12
|
+
* @param options - Configuration options
|
|
13
|
+
* @param options.sessionStorageFactory - Factory function to create storage adapter
|
|
14
|
+
* @param options.clientFactory - Optional factory for WorkOS client (defaults to getWorkOS)
|
|
15
|
+
* @param options.encryptionFactory - Optional factory for encryption (defaults to iron-webcrypto)
|
|
16
|
+
* @returns AuthService instance with lazy initialization
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* // Create service (config not needed yet)
|
|
21
|
+
* export const authService = createAuthService({
|
|
22
|
+
* sessionStorageFactory: (config) => new MyFrameworkStorage(config),
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Configure later
|
|
26
|
+
* configure({ clientId: '...' });
|
|
27
|
+
*
|
|
28
|
+
* // Use (triggers lazy init)
|
|
29
|
+
* await authService.withAuth(request);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare function createAuthService<TRequest, TResponse>(options: {
|
|
33
|
+
sessionStorageFactory: (config: AuthKitConfig) => SessionStorage<TRequest, TResponse>;
|
|
34
|
+
clientFactory?: (config: AuthKitConfig) => WorkOS;
|
|
35
|
+
encryptionFactory?: (config: AuthKitConfig) => SessionEncryption;
|
|
36
|
+
}): AuthService<TRequest, TResponse>;
|
|
37
|
+
//# sourceMappingURL=factory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../../src/service/factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAG7D,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAClF,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE;IAC9D,qBAAqB,EAAE,CACrB,MAAM,EAAE,aAAa,KAClB,cAAc,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;IACzC,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,MAAM,CAAC;IAClD,iBAAiB,CAAC,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,iBAAiB,CAAC;CAClE,GAAG,WAAW,CAAC,QAAQ,EAAE,SAAS,CAAC,CA0BnC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { once } from '../utils.js';
|
|
2
|
+
import { getConfigurationProvider, getFullConfig } from '../core/config.js';
|
|
3
|
+
import { getWorkOS } from '../core/client/workos.js';
|
|
4
|
+
import sessionEncryption from '../core/encryption/ironWebcryptoEncryption.js';
|
|
5
|
+
import { AuthService } from './AuthService.js';
|
|
6
|
+
/**
|
|
7
|
+
* Creates an AuthService instance with lazy initialization support.
|
|
8
|
+
*
|
|
9
|
+
* This factory function provides ergonomic AuthService creation with sensible defaults.
|
|
10
|
+
* The returned service uses lazy initialization, allowing configure() to be called
|
|
11
|
+
* after instantiation but before first use.
|
|
12
|
+
*
|
|
13
|
+
* @param options - Configuration options
|
|
14
|
+
* @param options.sessionStorageFactory - Factory function to create storage adapter
|
|
15
|
+
* @param options.clientFactory - Optional factory for WorkOS client (defaults to getWorkOS)
|
|
16
|
+
* @param options.encryptionFactory - Optional factory for encryption (defaults to iron-webcrypto)
|
|
17
|
+
* @returns AuthService instance with lazy initialization
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Create service (config not needed yet)
|
|
22
|
+
* export const authService = createAuthService({
|
|
23
|
+
* sessionStorageFactory: (config) => new MyFrameworkStorage(config),
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Configure later
|
|
27
|
+
* configure({ clientId: '...' });
|
|
28
|
+
*
|
|
29
|
+
* // Use (triggers lazy init)
|
|
30
|
+
* await authService.withAuth(request);
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function createAuthService(options) {
|
|
34
|
+
const { sessionStorageFactory, clientFactory = () => getWorkOS(), encryptionFactory = () => sessionEncryption, } = options;
|
|
35
|
+
// Storage needs to be created with lazy config evaluation
|
|
36
|
+
// We create a wrapper storage that delegates to the real storage lazily
|
|
37
|
+
const getStorage = once(() => sessionStorageFactory(getFullConfig()));
|
|
38
|
+
// Create a proxy storage that lazily instantiates the real storage
|
|
39
|
+
const lazyStorage = {
|
|
40
|
+
getSession: async (request) => getStorage().getSession(request),
|
|
41
|
+
saveSession: async (response, sessionData) => getStorage().saveSession(response, sessionData),
|
|
42
|
+
clearSession: async (response) => getStorage().clearSession(response),
|
|
43
|
+
};
|
|
44
|
+
return new AuthService(getConfigurationProvider(), lazyStorage, clientFactory, encryptionFactory);
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../src/service/factory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,wBAAwB,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAE5E,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AACrD,OAAO,iBAAiB,MAAM,+CAA+C,CAAC;AAE9E,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,iBAAiB,CAAsB,OAMtD;IACC,MAAM,EACJ,qBAAqB,EACrB,aAAa,GAAG,GAAG,EAAE,CAAC,SAAS,EAAE,EACjC,iBAAiB,GAAG,GAAG,EAAE,CAAC,iBAAiB,GAC5C,GAAG,OAAO,CAAC;IAEZ,0DAA0D;IAC1D,wEAAwE;IACxE,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,qBAAqB,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC;IAEtE,mEAAmE;IACnE,MAAM,WAAW,GAAwC;QACvD,UAAU,EAAE,KAAK,EAAE,OAAiB,EAAE,EAAE,CAAC,UAAU,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC;QACzE,WAAW,EAAE,KAAK,EAAE,QAA+B,EAAE,WAAmB,EAAE,EAAE,CAC1E,UAAU,EAAE,CAAC,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC;QACjD,YAAY,EAAE,KAAK,EAAE,QAAmB,EAAE,EAAE,CAC1C,UAAU,EAAE,CAAC,YAAY,CAAC,QAAQ,CAAC;KACtC,CAAC;IAEF,OAAO,IAAI,WAAW,CACpB,wBAAwB,EAAE,EAC1B,WAAW,EACX,aAAa,EACb,iBAAiB,CAClB,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workos/authkit-session",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-beta.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -29,16 +29,16 @@
|
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^20.17.0",
|
|
32
|
-
"@vitest/coverage-v8": "^
|
|
32
|
+
"@vitest/coverage-v8": "^4.0.10",
|
|
33
33
|
"prettier": "^3.6.2",
|
|
34
|
-
"typescript": "^5.9.
|
|
35
|
-
"vitest": "^
|
|
34
|
+
"typescript": "^5.9.3",
|
|
35
|
+
"vitest": "^4.0.10"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@workos-inc/node": "^8.0.0-
|
|
38
|
+
"@workos-inc/node": "^8.0.0-rc.3",
|
|
39
39
|
"iron-session": "^8.0.4",
|
|
40
40
|
"iron-webcrypto": "^1.2.1",
|
|
41
|
-
"jose": "^6.
|
|
41
|
+
"jose": "^6.1.2"
|
|
42
42
|
},
|
|
43
43
|
"engines": {
|
|
44
44
|
"node": ">=20.0.0"
|