@workos/authkit-session 0.0.1-alpha.3 → 0.1.2

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 (96) hide show
  1. package/README.md +80 -5
  2. package/dist/core/client/types.d.ts +75 -0
  3. package/dist/core/client/types.d.ts.map +1 -0
  4. package/dist/core/client/types.js +2 -0
  5. package/dist/core/client/types.js.map +1 -0
  6. package/dist/core/client/workos.d.ts +11 -0
  7. package/dist/core/client/workos.d.ts.map +1 -0
  8. package/dist/core/client/workos.js +33 -0
  9. package/dist/core/client/workos.js.map +1 -0
  10. package/dist/core/config/ConfigurationProvider.d.ts +43 -0
  11. package/dist/core/config/ConfigurationProvider.d.ts.map +1 -0
  12. package/dist/core/config/ConfigurationProvider.js +171 -0
  13. package/dist/core/config/ConfigurationProvider.js.map +1 -0
  14. package/dist/core/config/types.d.ts +62 -0
  15. package/dist/core/config/types.d.ts.map +1 -0
  16. package/dist/core/config/types.js +2 -0
  17. package/dist/core/config/types.js.map +1 -0
  18. package/dist/core/config.d.ts +69 -0
  19. package/dist/core/config.d.ts.map +1 -0
  20. package/dist/core/config.js +49 -0
  21. package/dist/core/config.js.map +1 -0
  22. package/dist/core/createAuthKitFactory.d.ts +64 -0
  23. package/dist/core/createAuthKitFactory.d.ts.map +1 -0
  24. package/dist/core/createAuthKitFactory.js +95 -0
  25. package/dist/core/createAuthKitFactory.js.map +1 -0
  26. package/dist/core/encryption/ironSessionEncryption.d.ts +16 -0
  27. package/dist/core/encryption/ironSessionEncryption.d.ts.map +1 -0
  28. package/dist/core/encryption/ironSessionEncryption.js +6 -0
  29. package/dist/core/encryption/ironSessionEncryption.js.map +1 -0
  30. package/dist/core/encryption/ironWebcryptoEncryption.d.ts +19 -0
  31. package/dist/core/encryption/ironWebcryptoEncryption.d.ts.map +1 -0
  32. package/dist/core/encryption/ironWebcryptoEncryption.js +84 -0
  33. package/dist/core/encryption/ironWebcryptoEncryption.js.map +1 -0
  34. package/dist/core/encryption/pureWebcryptoEncryption.d.ts +29 -0
  35. package/dist/core/encryption/pureWebcryptoEncryption.d.ts.map +1 -0
  36. package/dist/core/encryption/pureWebcryptoEncryption.js +182 -0
  37. package/dist/core/encryption/pureWebcryptoEncryption.js.map +1 -0
  38. package/dist/core/errors.d.ts +14 -0
  39. package/dist/core/errors.d.ts.map +1 -0
  40. package/dist/core/errors.js +28 -0
  41. package/dist/core/errors.js.map +1 -0
  42. package/dist/core/session/CookieSessionStorage.d.ts +33 -0
  43. package/dist/core/session/CookieSessionStorage.d.ts.map +1 -0
  44. package/dist/core/session/CookieSessionStorage.js +52 -0
  45. package/dist/core/session/CookieSessionStorage.js.map +1 -0
  46. package/dist/core/session/SessionManager.d.ts +51 -0
  47. package/dist/core/session/SessionManager.d.ts.map +1 -0
  48. package/dist/core/session/SessionManager.js +238 -0
  49. package/dist/core/session/SessionManager.js.map +1 -0
  50. package/dist/core/session/TokenManager.d.ts +14 -0
  51. package/dist/core/session/TokenManager.d.ts.map +1 -0
  52. package/dist/core/session/TokenManager.js +43 -0
  53. package/dist/core/session/TokenManager.js.map +1 -0
  54. package/dist/core/session/types.d.ts +101 -0
  55. package/dist/core/session/types.d.ts.map +1 -0
  56. package/dist/core/session/types.js +2 -0
  57. package/dist/core/session/types.js.map +1 -0
  58. package/dist/index.d.ts +10 -406
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +8 -923
  61. package/dist/index.js.map +1 -1
  62. package/dist/utils.d.ts +9 -0
  63. package/dist/utils.d.ts.map +1 -0
  64. package/dist/utils.js +19 -0
  65. package/dist/utils.js.map +1 -0
  66. package/package.json +19 -11
  67. package/src/core/client/types.ts +2 -7
  68. package/src/core/client/workos.ts +5 -5
  69. package/src/core/config/ConfigurationProvider.spec.ts +201 -0
  70. package/src/core/config/ConfigurationProvider.ts +75 -7
  71. package/src/core/config.spec.ts +160 -0
  72. package/src/core/config.ts +29 -3
  73. package/src/core/createAuthKitFactory.spec.ts +161 -0
  74. package/src/core/createAuthKitFactory.ts +28 -23
  75. package/src/core/encryption/ironWebcryptoEncryption.spec.ts +129 -0
  76. package/src/core/encryption/ironWebcryptoEncryption.ts +28 -23
  77. package/src/core/encryption/pureWebcryptoEncryption.spec.ts +177 -0
  78. package/src/core/encryption/pureWebcryptoEncryption.ts +339 -0
  79. package/src/core/errors.spec.ts +130 -0
  80. package/src/core/session/CookieSessionStorage.spec.ts +136 -0
  81. package/src/core/session/CookieSessionStorage.ts +40 -7
  82. package/src/core/session/SessionManager.spec.ts +357 -0
  83. package/src/core/session/SessionManager.ts +71 -72
  84. package/src/core/session/TokenManager.spec.ts +132 -0
  85. package/src/core/session/TokenManager.ts +8 -6
  86. package/src/core/session/types.ts +39 -3
  87. package/src/index.ts +20 -8
  88. package/src/utils.spec.ts +12 -0
  89. package/dist/index.cjs +0 -924
  90. package/dist/index.cjs.map +0 -1
  91. package/dist/index.d.cts +0 -406
  92. package/src/HttpClient.ts +0 -160
  93. package/src/adapters/chrome/ChromeExtensionStorage.ts +0 -34
  94. package/src/core/UserManagement.ts +0 -8
  95. package/src/core/client/WorkOSLite.ts +0 -284
  96. package/src/core/iron.ts +0 -103
@@ -0,0 +1,49 @@
1
+ import { once } from '../utils.js';
2
+ import { ConfigurationProvider } from './config/ConfigurationProvider.js';
3
+ const getConfigurationInstance = once(() => new ConfigurationProvider());
4
+ export function configure(configOrSource, source) {
5
+ getConfigurationInstance().configure(configOrSource, source);
6
+ }
7
+ /**
8
+ * Get a configuration value by key.
9
+ * This function will first check environment variables, then programmatically provided config,
10
+ * and finally fall back to defaults for optional settings.
11
+ * If a required setting is missing, an error will be thrown.
12
+ * @param key The configuration key
13
+ * @returns The configuration value
14
+ */
15
+ export function getConfig(key) {
16
+ return getConfigurationInstance().getValue(key);
17
+ }
18
+ export function getConfigurationProvider() {
19
+ return getConfigurationInstance();
20
+ }
21
+ export function getFullConfig() {
22
+ return getConfigurationInstance().getConfig();
23
+ }
24
+ /**
25
+ * Validates that all required configuration values are present and meet requirements.
26
+ * Collects all validation errors before throwing to provide comprehensive feedback.
27
+ *
28
+ * This is useful to call early in your application lifecycle to fail fast with
29
+ * helpful error messages showing all missing/invalid configuration at once.
30
+ *
31
+ * @throws {Error} If any required configuration is missing or invalid
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * import { validateConfig } from '@workos/authkit-session';
36
+ *
37
+ * // Validate configuration on startup
38
+ * try {
39
+ * validateConfig();
40
+ * } catch (error) {
41
+ * console.error(error.message); // Shows all missing config at once
42
+ * process.exit(1);
43
+ * }
44
+ * ```
45
+ */
46
+ export function validateConfig() {
47
+ return getConfigurationInstance().validate();
48
+ }
49
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/core/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAG1E,MAAM,wBAAwB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,qBAAqB,EAAE,CAAC,CAAC;AAqCzE,MAAM,UAAU,SAAS,CACvB,cAAoD,EACpD,MAAoB;IAEpB,wBAAwB,EAAE,CAAC,SAAS,CAAC,cAAc,EAAE,MAAM,CAAC,CAAC;AAC/D,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CACvB,GAAM;IAEN,OAAO,wBAAwB,EAAE,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,wBAAwB;IACtC,OAAO,wBAAwB,EAAE,CAAC;AACpC,CAAC;AAED,MAAM,UAAU,aAAa;IAC3B,OAAO,wBAAwB,EAAE,CAAC,SAAS,EAAE,CAAC;AAChD,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,cAAc;IAC5B,OAAO,wBAAwB,EAAE,CAAC,QAAQ,EAAE,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,64 @@
1
+ import type { WorkOS } from '@workos-inc/node';
2
+ import type { AuthKitConfig } from './config/types.js';
3
+ import type { AuthUrlOptions, BaseTokenClaims, CustomClaims, SessionEncryption, SessionStorage } from './session/types.js';
4
+ export declare const createAuthKitFactory: <TRequest, TResponse>(options: {
5
+ sessionStorageFactory: (config: AuthKitConfig) => SessionStorage<TRequest, TResponse>;
6
+ sessionEncryptionFactory?: (confg: AuthKitConfig) => SessionEncryption;
7
+ clientFactory?: (config: AuthKitConfig) => WorkOS;
8
+ }) => {
9
+ withAuth: (request: any) => Promise<import("./session/types.js").AuthResult<CustomClaims>>;
10
+ getAuthorizationUrl: (args_0?: import("./session/types.js").GetAuthorizationUrlOptions | undefined) => Promise<string>;
11
+ refreshSession: (session: import("./session/types.js").Session, organizationId?: string | undefined) => Promise<{
12
+ user: import("@workos-inc/node").User;
13
+ sessionId: string;
14
+ organizationId: string | undefined;
15
+ role: string | undefined;
16
+ roles: string[] | undefined;
17
+ permissions: string[] | undefined;
18
+ entitlements: string[] | undefined;
19
+ impersonator: import("@workos-inc/node").Impersonator | undefined;
20
+ accessToken: string;
21
+ claims: BaseTokenClaims & CustomClaims;
22
+ sessionData: string;
23
+ session: import("./session/types.js").Session;
24
+ }>;
25
+ saveSession: (response: TResponse | undefined, sessionData: string, options?: unknown) => Promise<{
26
+ response?: TResponse | undefined;
27
+ headers?: import("./session/types.js").HeadersBag;
28
+ }>;
29
+ getSignInUrl: (options?: AuthUrlOptions) => Promise<string>;
30
+ getSignUpUrl: (options?: AuthUrlOptions) => Promise<string>;
31
+ getWorkOS: () => WorkOS;
32
+ getSession: (request: TRequest) => Promise<import("./session/types.js").Session | null>;
33
+ terminateSession: (session: import("./session/types.js").Session, response: any, options?: {
34
+ returnTo?: string;
35
+ } | undefined) => Promise<{
36
+ response?: TResponse | undefined;
37
+ headers?: import("./session/types.js").HeadersBag;
38
+ logoutUrl: string;
39
+ }>;
40
+ signOut: (request: TRequest, response: TResponse, options?: {
41
+ returnTo?: string;
42
+ }) => Promise<TResponse | {
43
+ response?: TResponse | undefined;
44
+ headers?: import("./session/types.js").HeadersBag;
45
+ } | undefined>;
46
+ switchToOrganization: (request: any, response: any, organizationId: string) => Promise<{
47
+ response?: TResponse | undefined;
48
+ headers?: import("./session/types.js").HeadersBag;
49
+ authResult: import("./session/types.js").AuthResult<CustomClaims>;
50
+ }>;
51
+ handleCallback: (_request: TRequest, response: TResponse, options: {
52
+ code: string;
53
+ state?: string;
54
+ }) => Promise<{
55
+ response: {
56
+ response?: TResponse | undefined;
57
+ headers?: import("./session/types.js").HeadersBag;
58
+ };
59
+ returnPathname: string;
60
+ authResponse: import("@workos-inc/node").AuthenticationResponse;
61
+ }>;
62
+ getTokenClaims: <TCustomClaims = CustomClaims>(request: TRequest, accessToken?: string) => Promise<Partial<BaseTokenClaims & TCustomClaims>>;
63
+ };
64
+ //# sourceMappingURL=createAuthKitFactory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createAuthKitFactory.d.ts","sourceRoot":"","sources":["../../src/core/createAuthKitFactory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAQ/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAIvD,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EACf,YAAY,EACZ,iBAAiB,EACjB,cAAc,EACf,MAAM,oBAAoB,CAAC;AAE5B,eAAO,MAAM,oBAAoB,GAC/B,QAAQ,EACR,SAAS;2BAEc,CACrB,MAAM,EAAE,aAAa,KAClB,cAAc,CAAC,QAAQ,EAAE,SAAS,CAAC;+BACb,CAAC,KAAK,EAAE,aAAa,KAAK,iBAAiB;oBACtD,CAAC,MAAM,EAAE,aAAa,KAAK,MAAM;;;;;;;;;;;;;;;;;;;;;;6BA2CtB,cAAc;6BAMd,cAAc;;0BAUjB,QAAQ;;;;;;;;uBAQnB,QAAQ,YACP,SAAS,YACT;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE;;;;;;;;;+BAuCrB,QAAQ,YACR,SAAS,WACV;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE;;;;;;;;qBAoCpB,aAAa,0BACzB,QAAQ,gBACH,MAAM,KACnB,OAAO,CAAC,OAAO,CAAC,eAAe,GAAG,aAAa,CAAC,CAAC;CAiBtD,CAAC"}
@@ -0,0 +1,95 @@
1
+ import { once } from '../utils.js';
2
+ import { getWorkOS } from './client/workos.js';
3
+ import { getConfig, getConfigurationProvider, getFullConfig, } from './config.js';
4
+ import sessionEncryption from './encryption/ironWebcryptoEncryption.js';
5
+ import { SessionManager } from './session/SessionManager.js';
6
+ import TokenManager from './session/TokenManager.js';
7
+ export const createAuthKitFactory = once(function createAuthKit(options) {
8
+ const { sessionStorageFactory, clientFactory = () => getWorkOS(), sessionEncryptionFactory = () => sessionEncryption, } = options;
9
+ const getTokenManager = once(() => new TokenManager(getConfig('clientId'), clientFactory(getFullConfig())));
10
+ const getSessionManager = once(() => new SessionManager(getConfigurationProvider(), sessionStorageFactory(getFullConfig()), getTokenManager(), clientFactory(getFullConfig()), sessionEncryptionFactory(getFullConfig())));
11
+ return {
12
+ withAuth: (...args) => getSessionManager().withAuth(...args),
13
+ getAuthorizationUrl: (...args) => getSessionManager().getAuthorizationUrl(...args),
14
+ refreshSession: (...args) => getSessionManager().refreshSession(...args),
15
+ saveSession: (...args) => sessionStorageFactory(getFullConfig()).saveSession(...args),
16
+ getSignInUrl: (options) => getSessionManager().getAuthorizationUrl({
17
+ ...options,
18
+ screenHint: 'sign-in',
19
+ }),
20
+ getSignUpUrl: (options) => getSessionManager().getAuthorizationUrl({
21
+ ...options,
22
+ screenHint: 'sign-up',
23
+ }),
24
+ // Expose getWorkOS for direct client access
25
+ getWorkOS: () => clientFactory(getFullConfig()),
26
+ // Expose SessionManager's getSession directly
27
+ getSession: (request) => getSessionManager().getSession(request),
28
+ // terminateSession uses the standard SessionManager signature
29
+ terminateSession: (...args) => getSessionManager().terminateSession(...args),
30
+ signOut: async (request, response, options) => {
31
+ // Get the current session
32
+ const authResult = await getSessionManager().withAuth(request);
33
+ if (!authResult.user ||
34
+ !authResult.accessToken ||
35
+ !authResult.refreshToken) {
36
+ // No session to terminate, just clear cookies
37
+ return sessionStorageFactory(getFullConfig()).clearSession(response);
38
+ }
39
+ // Create session object for termination
40
+ const session = {
41
+ accessToken: authResult.accessToken,
42
+ refreshToken: authResult.refreshToken,
43
+ user: authResult.user,
44
+ impersonator: authResult.impersonator,
45
+ };
46
+ // Terminate the session (this clears cookies and returns logout URL)
47
+ const result = await getSessionManager().terminateSession(session, response, options);
48
+ return result.response;
49
+ },
50
+ switchToOrganization: (...args) => getSessionManager().switchToOrganization(...args),
51
+ handleCallback: async (_request, response, options) => {
52
+ const config = getFullConfig();
53
+ const clientId = config.clientId;
54
+ const client = clientFactory(config);
55
+ // Authenticate with the OAuth code
56
+ const authResponse = await client.userManagement.authenticateWithCode({
57
+ code: options.code,
58
+ clientId,
59
+ });
60
+ // Create session using SessionManager
61
+ const updatedResponse = await getSessionManager().createSession(authResponse, response);
62
+ // Decode state if provided
63
+ let returnPathname = '/';
64
+ if (options.state) {
65
+ try {
66
+ const decoded = JSON.parse(atob(options.state));
67
+ returnPathname = decoded.returnPathname || '/';
68
+ }
69
+ catch {
70
+ // Invalid state, use default
71
+ }
72
+ }
73
+ return {
74
+ response: updatedResponse,
75
+ returnPathname,
76
+ authResponse,
77
+ };
78
+ },
79
+ getTokenClaims: async (request, accessToken) => {
80
+ const tokenToUse = accessToken ||
81
+ (await getSessionManager().withAuth(request))
82
+ .accessToken;
83
+ if (!tokenToUse) {
84
+ return {};
85
+ }
86
+ try {
87
+ return getTokenManager().parseTokenClaims(tokenToUse);
88
+ }
89
+ catch {
90
+ return {};
91
+ }
92
+ },
93
+ };
94
+ });
95
+ //# sourceMappingURL=createAuthKitF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gBACjD,CAAC;gBAAC,MAAM,CAAC;oBACP,6BAA6B;gBAC/B,CAAC;YACH,CAAC;YAED,OAAO;gBACL,QAAQ,EAAE,eAAe;gBACzB,cAAc;gBACd,YAAY;aACb,CAAC;QACJ,CAAC;QAED,cAAc,EAAE,KAAK,EACnB,OAAiB,EACjB,WAAoB,EAC+B,EAAE;YACrD,MAAM,UAAU,GACd,WAAW;gBACX,CAAC,MAAM,iBAAiB,EAAE,CAAC,QAAQ,CAAgB,OAAO,CAAC,CAAC;qBACzD,WAAW,CAAC;YAEjB,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,OAAO,EAAE,CAAC;YACZ,CAAC;YAED,IAAI,CAAC;gBACH,OAAO,eAAe,EAAE,CAAC,gBAAgB,CAAgB,UAAU,CAAC,CAAC;YACvE,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,EAAE,CAAC;YACZ,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC,CAAC,CAAC"}
@@ -0,0 +1,16 @@
1
+ declare const _default: {
2
+ sealData: (data: unknown, { password, ttl, }: {
3
+ password: string | {
4
+ [x: string]: string;
5
+ };
6
+ ttl?: number;
7
+ }) => Promise<string>;
8
+ unsealData: <T>(seal: string, { password, ttl, }: {
9
+ password: string | {
10
+ [x: string]: string;
11
+ };
12
+ ttl?: number;
13
+ }) => Promise<T>;
14
+ };
15
+ export default _default;
16
+ //# sourceMappingURL=ironSessionEncryption.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ironSessionEncryption.d.ts","sourceRoot":"","sources":["../../../src/core/encryption/ironSessionEncryption.ts"],"names":[],"mappings":";;;;;WAMi1G,CAAC;;;;;;WAAuK,CAAC;;;AAJ1/G,wBAGE"}
@@ -0,0 +1,6 @@
1
+ import { sealData, unsealData } from 'iron-session';
2
+ export default {
3
+ sealData,
4
+ unsealData,
5
+ };
6
+ //# sourceMappingURL=ironSessionEncryption.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ironSessionEncryption.js","sourceRoot":"","sources":["../../../src/core/encryption/ironSessionEncryption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAEpD,eAAe;IACb,QAAQ;IACR,UAAU;CACX,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { SessionEncryption as SessionEncryptionInterface } from '../session/types.js';
2
+ /**
3
+ * A compatible implementation that works with iron-session
4
+ */
5
+ export declare class SessionEncryption implements SessionEncryptionInterface {
6
+ private readonly versionDelimiter;
7
+ private readonly currentMajorVersion;
8
+ private parseSeal;
9
+ sealData(data: unknown, { password, ttl }: {
10
+ password: string;
11
+ ttl?: number | undefined;
12
+ }): Promise<string>;
13
+ unsealData<T = unknown>(encryptedData: string, { password }: {
14
+ password: string;
15
+ }): Promise<T>;
16
+ }
17
+ declare const ironWebcryptoEncryption: SessionEncryption;
18
+ export default ironWebcryptoEncryption;
19
+ //# sourceMappingURL=ironWebcryptoEncryption.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ironWebcryptoEncryption.d.ts","sourceRoot":"","sources":["../../../src/core/encryption/ironWebcryptoEncryption.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,IAAI,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAY3F;;GAEG;AACH,qBAAa,iBAAkB,YAAW,0BAA0B;IAClE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAO;IACxC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAK;IAGzC,OAAO,CAAC,SAAS;IAaX,QAAQ,CACZ,IAAI,EAAE,OAAO,EACb,EAAE,QAAQ,EAAE,GAAO,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE;IAgCjE,UAAU,CAAC,CAAC,GAAG,OAAO,EAC1B,aAAa,EAAE,MAAM,EACrB,EAAE,QAAQ,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GACjC,OAAO,CAAC,CAAC,CAAC;CAoCd;AAED,QAAA,MAAM,uBAAuB,mBAA0B,CAAC;AAExD,eAAe,uBAAuB,CAAC"}
@@ -0,0 +1,84 @@
1
+ import { seal as sealData, unseal as unsealData } from 'iron-webcrypto';
2
+ // Cast globalThis.crypto to iron-webcrypto's expected type
3
+ // Runtime-safe: DOM Crypto is fully compatible with iron-webcrypto's _Crypto
4
+ // Type-level workaround: iron-webcrypto's _Crypto expects Uint8Array<ArrayBuffer>
5
+ // while DOM Crypto provides Uint8Array<ArrayBufferLike>
6
+ const ironCrypto = globalThis.crypto;
7
+ /**
8
+ * A compatible implementation that works with iron-session
9
+ */
10
+ export class SessionEncryption {
11
+ versionDelimiter = '~';
12
+ currentMajorVersion = 2;
13
+ // Parse an iron-session seal to extract the version
14
+ parseSeal(seal) {
15
+ const [sealWithoutVersion = '', tokenVersionAsString] = seal.split(this.versionDelimiter);
16
+ const tokenVersion = tokenVersionAsString == null ? null : parseInt(tokenVersionAsString, 10);
17
+ return { sealWithoutVersion, tokenVersion };
18
+ }
19
+ // Encrypt data in a way that's compatible with iron-session
20
+ async sealData(data, { password, ttl = 0 }) {
21
+ // Format password as iron-session expects
22
+ const passwordObj = {
23
+ id: '1',
24
+ secret: password,
25
+ };
26
+ // Seal the data using iron-webcrypto with properly formatted password
27
+ const seal = await sealData(ironCrypto, data, passwordObj, {
28
+ encryption: {
29
+ saltBits: 256,
30
+ algorithm: 'aes-256-cbc',
31
+ iterations: 1,
32
+ minPasswordlength: 32,
33
+ },
34
+ integrity: {
35
+ saltBits: 256,
36
+ algorithm: 'sha256',
37
+ iterations: 1,
38
+ minPasswordlength: 32,
39
+ },
40
+ ttl: ttl * 1000, // Convert seconds to milliseconds
41
+ timestampSkewSec: 60,
42
+ localtimeOffsetMsec: 0,
43
+ });
44
+ // Add the version delimiter exactly like iron-session does
45
+ return `${seal}${this.versionDelimiter}${this.currentMajorVersion}`;
46
+ }
47
+ // Decrypt data from iron-session with HMAC verification
48
+ async unsealData(encryptedData, { password }) {
49
+ // First, parse the seal to extract the version
50
+ const { sealWithoutVersion, tokenVersion } = this.parseSeal(encryptedData);
51
+ // Format password as a map like iron-session expects
52
+ const passwordMap = { 1: password };
53
+ // Use iron-webcrypto's unseal function
54
+ const data = await unsealData(ironCrypto, sealWithoutVersion, passwordMap, {
55
+ encryption: {
56
+ saltBits: 256,
57
+ algorithm: 'aes-256-cbc',
58
+ iterations: 1,
59
+ minPasswordlength: 32,
60
+ },
61
+ integrity: {
62
+ saltBits: 256,
63
+ algorithm: 'sha256',
64
+ iterations: 1,
65
+ minPasswordlength: 32,
66
+ },
67
+ ttl: 0,
68
+ timestampSkewSec: 60,
69
+ localtimeOffsetMsec: 0,
70
+ });
71
+ // Check the token version if needed
72
+ if (tokenVersion === 2) {
73
+ return data;
74
+ }
75
+ else if (tokenVersion !== null) {
76
+ // For older token versions, extract the persistent property
77
+ return { ...data.persistent };
78
+ }
79
+ return data;
80
+ }
81
+ }
82
+ const ironWebcryptoEncryption = new SessionEncryption();
83
+ export default ironWebcryptoEncryption;
84
+ //# sourceMappingURL=ironWebcryptoEncryption.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ironWebcryptoEncryption.js","sourceRoot":"","sources":["../../../src/core/encryption/ironWebcryptoEncryption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAOxE,2DAA2D;AAC3D,6EAA6E;AAC7E,kFAAkF;AAClF,wDAAwD;AACxD,MAAM,UAAU,GAAe,UAAU,CAAC,MAA+B,CAAC;AAE1E;;GAEG;AACH,MAAM,OAAO,iBAAiB;IACX,gBAAgB,GAAG,GAAG,CAAC;IACvB,mBAAmB,GAAG,CAAC,CAAC;IAEzC,oDAAoD;IAC5C,SAAS,CAAC,IAAY;QAI5B,MAAM,CAAC,kBAA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AAS,CAAC,aAAa,CAAC,CAAC;QAE3E,qDAAqD;QACrD,MAAM,WAAW,GAAG,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC;QAEpC,uCAAuC;QACvC,MAAM,IAAI,GAAG,MAAM,UAAU,CAAC,UAAU,EAAE,kBAAkB,EAAE,WAAW,EAAE;YACzE,UAAU,EAAE;gBACV,QAAQ,EAAE,GAAG;gBACb,SAAS,EAAE,aAAa;gBACxB,UAAU,EAAE,CAAC;gBACb,iBAAiB,EAAE,EAAE;aACtB;YACD,SAAS,EAAE;gBACT,QAAQ,EAAE,GAAG;gBACb,SAAS,EAAE,QAAQ;gBACnB,UAAU,EAAE,CAAC;gBACb,iBAAiB,EAAE,EAAE;aACtB;YACD,GAAG,EAAE,CAAC;YACN,gBAAgB,EAAE,EAAE;YACpB,mBAAmB,EAAE,CAAC;SACvB,CAAC,CAAC;QAEH,oCAAoC;QACpC,IAAI,YAAY,KAAK,CAAC,EAAE,CAAC;YACvB,OAAO,IAAS,CAAC;QACnB,CAAC;aAAM,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;YACjC,4DAA4D;YAC5D,OAAO,EAAE,GAAI,IAAY,CAAC,UAAU,EAAO,CAAC;QAC9C,CAAC;QAED,OAAO,IAAS,CAAC;IACnB,CAAC;CACF;AAED,MAAM,uBAAuB,GAAG,IAAI,iBAAiB,EAAE,CAAC;AAExD,eAAe,uBAAuB,CAAC"}
@@ -0,0 +1,29 @@
1
+ import type { SessionEncryption as SessionEncryptionInterface } from '../session/types.js';
2
+ /**
3
+ * Pure WebCrypto implementation compatible with iron-session
4
+ * Based on reverse-engineered iron-session format (Fe26.2)
5
+ */
6
+ export declare class PureWebcryptoEncryption implements SessionEncryptionInterface {
7
+ private readonly versionDelimiter;
8
+ private readonly currentMajorVersion;
9
+ private parseSeal;
10
+ private base64urlToBytes;
11
+ private bytesToBase64url;
12
+ private bytesToHex;
13
+ private generateRandomBytes;
14
+ private deriveKeyBits;
15
+ private parseLegacyFormat;
16
+ private decryptLegacyData;
17
+ private encryptLegacyData;
18
+ private signSealedData;
19
+ sealData(data: unknown, { password, ttl }: {
20
+ password: string;
21
+ ttl?: number | undefined;
22
+ }): Promise<string>;
23
+ unsealData<T = unknown>(encryptedData: string, { password }: {
24
+ password: string;
25
+ }): Promise<T>;
26
+ }
27
+ declare const pureWebcryptoEncryption: PureWebcryptoEncryption;
28
+ export default pureWebcryptoEncryption;
29
+ //# sourceMappingURL=pureWebcryptoEncryption.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pureWebcryptoEncryption.d.ts","sourceRoot":"","sources":["../../../src/core/encryption/pureWebcryptoEncryption.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,IAAI,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAE3F;;;GAGG;AACH,qBAAa,uBAAwB,YAAW,0BAA0B;IACxE,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAO;IACxC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAK;IAGzC,OAAO,CAAC,SAAS;IAajB,OAAO,CAAC,gBAAgB;IAYxB,OAAO,CAAC,gBAAgB;IAKxB,OAAO,CAAC,UAAU;IAMlB,OAAO,CAAC,mBAAmB;YAKb,aAAa;IAiC3B,OAAO,CAAC,iBAAiB;YAmCX,iBAAiB;YAwCjB,iBAAiB;YAqCjB,cAAc;IAsCtB,QAAQ,CACZ,IAAI,EAAE,OAAO,EACb,EAAE,QAAQ,EAAE,GAAO,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,GACpE,OAAO,CAAC,MAAM,CAAC;IAmDZ,UAAU,CAAC,CAAC,GAAG,OAAO,EAC1B,aAAa,EAAE,MAAM,EACrB,EAAE,QAAQ,EAAE,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GACjC,OAAO,CAAC,CAAC,CAAC;CA0Cd;AAED,QAAA,MAAM,uBAAuB,yBAAgC,CAAC;AAE9D,eAAe,uBAAuB,CAAC"}
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Pure WebCrypto implementation compatible with iron-session
3
+ * Based on reverse-engineered iron-session format (Fe26.2)
4
+ */
5
+ export class PureWebcryptoEncryption {
6
+ versionDelimiter = '~';
7
+ currentMajorVersion = 2;
8
+ // Parse version from sealed data
9
+ parseSeal(seal) {
10
+ const [sealWithoutVersion = '', tokenVersionAsString] = seal.split(this.versionDelimiter);
11
+ const tokenVersion = tokenVersionAsString == null ? null : parseInt(tokenVersionAsString, 10);
12
+ return { sealWithoutVersion, tokenVersion };
13
+ }
14
+ // Base64url utilities
15
+ base64urlToBytes(base64url) {
16
+ const base64 = base64url.replace(/-/g, '+').replace(/_/g, '/') +
17
+ '='.repeat((4 - (base64url.length % 4)) % 4);
18
+ const binaryString = atob(base64);
19
+ const bytes = new Uint8Array(binaryString.length);
20
+ for (let i = 0; i < binaryString.length; i++) {
21
+ bytes[i] = binaryString.charCodeAt(i);
22
+ }
23
+ return bytes;
24
+ }
25
+ bytesToBase64url(bytes) {
26
+ const base64 = btoa(String.fromCharCode.apply(null, Array.from(bytes)));
27
+ return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
28
+ }
29
+ bytesToHex(bytes) {
30
+ return Array.from(bytes)
31
+ .map(b => b.toString(16).padStart(2, '0'))
32
+ .join('');
33
+ }
34
+ generateRandomBytes(length) {
35
+ return globalThis.crypto.getRandomValues(new Uint8Array(length));
36
+ }
37
+ // Key derivation using PBKDF2 (iron-session compatible)
38
+ async deriveKeyBits(options) {
39
+ const keyMaterial = await globalThis.crypto.subtle.importKey('raw', new TextEncoder().encode(options.password), 'PBKDF2', false, ['deriveBits']);
40
+ const saltBytes = typeof options.salt === 'string'
41
+ ? new TextEncoder().encode(options.salt)
42
+ : options.salt;
43
+ return globalThis.crypto.subtle.deriveBits({
44
+ name: 'PBKDF2',
45
+ salt: saltBytes,
46
+ iterations: options.iterations,
47
+ hash: options.hashAlgorithm,
48
+ }, keyMaterial, options.keyLength);
49
+ }
50
+ // Parse iron-session legacy format (Fe26.2)
51
+ parseLegacyFormat(sealWithoutVersion) {
52
+ const parts = sealWithoutVersion.split('*');
53
+ if (parts.length < 6 || !parts[0]?.startsWith('Fe26.2')) {
54
+ throw new Error('Invalid legacy sealed data format');
55
+ }
56
+ const [, , encryptionSalt, encryptionIv, encryptedB64, expiration, hmacSalt, signature,] = parts;
57
+ return {
58
+ encryptionSalt: encryptionSalt,
59
+ encryptionIv: encryptionIv,
60
+ encryptedB64: encryptedB64,
61
+ expiration,
62
+ hmacSalt,
63
+ signature,
64
+ };
65
+ }
66
+ // Decrypt legacy data using AES-CBC
67
+ async decryptLegacyData(encryptedB64, encryptionIv, encryptionSalt, password) {
68
+ const encryptedBuffer = this.base64urlToBytes(encryptedB64);
69
+ const ivBuffer = this.base64urlToBytes(encryptionIv);
70
+ // Derive decryption key using iron-session parameters
71
+ const keyBits = await this.deriveKeyBits({
72
+ password,
73
+ salt: encryptionSalt,
74
+ iterations: 1,
75
+ hashAlgorithm: 'SHA-1',
76
+ keyLength: 256,
77
+ });
78
+ const encryptionKey = await globalThis.crypto.subtle.importKey('raw', keyBits, { name: 'AES-CBC' }, false, ['decrypt']);
79
+ // Decrypt using AES-256-CBC
80
+ const decryptedBuffer = await globalThis.crypto.subtle.decrypt({
81
+ name: 'AES-CBC',
82
+ iv: ivBuffer,
83
+ }, encryptionKey, encryptedBuffer);
84
+ return new TextDecoder().decode(decryptedBuffer);
85
+ }
86
+ // Encrypt data using AES-CBC
87
+ async encryptLegacyData(jsonString, encryptionIv, encryptionSalt, password) {
88
+ // Derive encryption key using iron-session parameters
89
+ const keyBits = await this.deriveKeyBits({
90
+ password,
91
+ salt: encryptionSalt,
92
+ iterations: 1,
93
+ hashAlgorithm: 'SHA-1',
94
+ keyLength: 256,
95
+ });
96
+ const encryptionKey = await globalThis.crypto.subtle.importKey('raw', keyBits, { name: 'AES-CBC' }, false, ['encrypt']);
97
+ // Encrypt the JSON string
98
+ const encryptedBuffer = await globalThis.crypto.subtle.encrypt({
99
+ name: 'AES-CBC',
100
+ iv: encryptionIv,
101
+ }, encryptionKey, new TextEncoder().encode(jsonString));
102
+ return this.bytesToBase64url(new Uint8Array(encryptedBuffer));
103
+ }
104
+ // Create HMAC signature for iron-session format
105
+ async signSealedData(encryptionSaltHex, encryptionIvB64, encryptedB64, hmacSaltHex, expiration, password) {
106
+ // Create the unsigned data for HMAC
107
+ const unsignedData = `Fe26.2*1*${encryptionSaltHex}*${encryptionIvB64}*${encryptedB64}*${expiration}`;
108
+ // Derive HMAC key
109
+ const hmacKeyBits = await this.deriveKeyBits({
110
+ password,
111
+ salt: hmacSaltHex,
112
+ iterations: 1,
113
+ hashAlgorithm: 'SHA-1',
114
+ keyLength: 256,
115
+ });
116
+ const hmacKey = await globalThis.crypto.subtle.importKey('raw', hmacKeyBits, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
117
+ // Calculate HMAC signature
118
+ const signature = await globalThis.crypto.subtle.sign('HMAC', hmacKey, new TextEncoder().encode(unsignedData));
119
+ return this.bytesToBase64url(new Uint8Array(signature));
120
+ }
121
+ async sealData(data, { password, ttl = 0 }) {
122
+ if (password.length < 32) {
123
+ throw new Error('Password must be at least 32 characters long');
124
+ }
125
+ const jsonString = JSON.stringify(data);
126
+ // Generate encryption components (matching iron-session)
127
+ const encryptionSalt = this.generateRandomBytes(32);
128
+ const hmacSalt = this.generateRandomBytes(32);
129
+ const encryptionIv = this.generateRandomBytes(16);
130
+ // Convert salts to hex (iron-session format)
131
+ const encryptionSaltHex = this.bytesToHex(encryptionSalt);
132
+ const hmacSaltHex = this.bytesToHex(hmacSalt);
133
+ // Encrypt the session data
134
+ const encryptedB64 = await this.encryptLegacyData(jsonString, encryptionIv, encryptionSaltHex, password);
135
+ const encryptionIvB64 = this.bytesToBase64url(encryptionIv);
136
+ // Calculate expiration (iron-session uses milliseconds)
137
+ const expiration = ttl > 0
138
+ ? Date.now() + ttl * 1000
139
+ : ttl < 0
140
+ ? Date.now() + ttl * 1000
141
+ : Date.now() + 24 * 60 * 60 * 1000;
142
+ // Sign the sealed data
143
+ const signatureB64 = await this.signSealedData(encryptionSaltHex, encryptionIvB64, encryptedB64, hmacSaltHex, expiration, password);
144
+ // Build iron-session format string (Fe26.2)
145
+ const sealed = `Fe26.2*1*${encryptionSaltHex}*${encryptionIvB64}*${encryptedB64}*${expiration}*${hmacSaltHex}*${signatureB64}`;
146
+ // Add version suffix for compatibility
147
+ return `${sealed}${this.versionDelimiter}${this.currentMajorVersion}`;
148
+ }
149
+ async unsealData(encryptedData, { password }) {
150
+ if (password.length < 32) {
151
+ throw new Error('Password must be at least 32 characters long');
152
+ }
153
+ // Parse version
154
+ const { sealWithoutVersion, tokenVersion } = this.parseSeal(encryptedData);
155
+ // Parse iron-session legacy format
156
+ const { encryptionSalt, encryptionIv, encryptedB64, expiration } = this.parseLegacyFormat(sealWithoutVersion);
157
+ // Check expiration if present
158
+ if (expiration) {
159
+ const expirationTime = parseInt(expiration, 10);
160
+ if (Date.now() > expirationTime) {
161
+ throw new Error('Sealed data has expired');
162
+ }
163
+ }
164
+ // Decrypt the data
165
+ const decryptedText = await this.decryptLegacyData(encryptedB64, encryptionIv, encryptionSalt, password);
166
+ // Parse the JSON data
167
+ const sessionData = JSON.parse(decryptedText);
168
+ // Handle version-specific extraction (matching iron-session behavior)
169
+ if (tokenVersion === 2) {
170
+ return sessionData;
171
+ }
172
+ else if (tokenVersion !== null && tokenVersion !== 2) {
173
+ // For older token versions, extract the persistent property
174
+ return { ...sessionData.persistent };
175
+ }
176
+ // No version info - return as-is
177
+ return sessionData;
178
+ }
179
+ }
180
+ const pureWebcryptoEncryption = new PureWebcryptoEncryption();
181
+ export default pureWebcryptoEncryption;
182
+ //# sourceMappingURL=pureWebcryptoEncryption.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pureWebcryptoEncryption.js","sourceRo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B;QAChB,MAAM,EAAE,kBAAkB,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;QAE3E,mCAAmC;QACnC,MAAM,EAAE,cAAc,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,GAC9D,IAAI,CAAC,iBAAiB,CAAC,kBAAkB,CAAC,CAAC;QAE7C,8BAA8B;QAC9B,IAAI,UAAU,EAAE,CAAC;YACf,MAAM,cAAc,GAAG,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;YAChD,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,cAAc,EAAE,CAAC;gBAChC,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;YAC7C,CAAC;QACH,CAAC;QAED,mBAAmB;QACnB,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,iBAAiB,CAChD,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,QAAQ,CACT,CAAC;QAEF,sBAAsB;QACtB,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;QAE9C,sEAAsE;QACtE,IAAI,YAAY,KAAK,CAAC,EAAE,CAAC;YACvB,OAAO,WAAgB,CAAC;QAC1B,CAAC;aAAM,IAAI,YAAY,KAAK,IAAI,IAAI,YAAY,KAAK,CAAC,EAAE,CAAC;YACvD,4DAA4D;YAC5D,OAAO,EAAE,GAAI,WAAmB,CAAC,UAAU,EAAO,CAAC;QACrD,CAAC;QAED,iCAAiC;QACjC,OAAO,WAAgB,CAAC;IAC1B,CAAC;CACF;AAED,MAAM,uBAAuB,GAAG,IAAI,uBAAuB,EAAE,CAAC;AAE9D,eAAe,uBAAuB,CAAC"}
@@ -0,0 +1,14 @@
1
+ export declare class AuthKitError extends Error {
2
+ data?: Record<string, any>;
3
+ constructor(message: string, cause?: unknown, data?: Record<string, any>);
4
+ }
5
+ export declare class SessionEncryptionError extends AuthKitError {
6
+ constructor(message: string, cause?: unknown);
7
+ }
8
+ export declare class TokenValidationError extends AuthKitError {
9
+ constructor(message: string, cause?: unknown);
10
+ }
11
+ export declare class TokenRefreshError extends AuthKitError {
12
+ constructor(message: string, cause?: unknown);
13
+ }
14
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/core/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,YAAa,SAAQ,KAAK;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;gBAEf,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;CAMzE;AAED,qBAAa,sBAAuB,SAAQ,YAAY;gBAC1C,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAI7C;AAED,qBAAa,oBAAqB,SAAQ,YAAY;gBACxC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAI7C;AAED,qBAAa,iBAAkB,SAAQ,YAAY;gBACrC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAI7C"}