@juspay/neurolink 9.31.2 → 9.32.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.
Files changed (161) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/auth/AuthProviderFactory.d.ts +71 -0
  3. package/dist/auth/AuthProviderFactory.js +111 -0
  4. package/dist/auth/AuthProviderRegistry.d.ts +33 -0
  5. package/dist/auth/AuthProviderRegistry.js +190 -0
  6. package/dist/auth/RequestContext.d.ts +23 -0
  7. package/dist/auth/RequestContext.js +78 -0
  8. package/dist/auth/authContext.d.ts +198 -0
  9. package/dist/auth/authContext.js +314 -0
  10. package/dist/auth/errors.d.ts +63 -0
  11. package/dist/auth/errors.js +39 -0
  12. package/dist/auth/index.d.ts +20 -8
  13. package/dist/auth/index.js +35 -7
  14. package/dist/auth/middleware/AuthMiddleware.d.ts +181 -0
  15. package/dist/auth/middleware/AuthMiddleware.js +519 -0
  16. package/dist/auth/middleware/rateLimitByUser.d.ts +282 -0
  17. package/dist/auth/middleware/rateLimitByUser.js +554 -0
  18. package/dist/auth/providers/BaseAuthProvider.d.ts +259 -0
  19. package/dist/auth/providers/BaseAuthProvider.js +723 -0
  20. package/dist/auth/providers/CognitoProvider.d.ts +61 -0
  21. package/dist/auth/providers/CognitoProvider.js +304 -0
  22. package/dist/auth/providers/KeycloakProvider.d.ts +61 -0
  23. package/dist/auth/providers/KeycloakProvider.js +393 -0
  24. package/dist/auth/providers/auth0.d.ts +59 -0
  25. package/dist/auth/providers/auth0.js +274 -0
  26. package/dist/auth/providers/betterAuth.d.ts +51 -0
  27. package/dist/auth/providers/betterAuth.js +182 -0
  28. package/dist/auth/providers/clerk.d.ts +65 -0
  29. package/dist/auth/providers/clerk.js +317 -0
  30. package/dist/auth/providers/custom.d.ts +64 -0
  31. package/dist/auth/providers/custom.js +112 -0
  32. package/dist/auth/providers/firebase.d.ts +63 -0
  33. package/dist/auth/providers/firebase.js +226 -0
  34. package/dist/auth/providers/jwt.d.ts +68 -0
  35. package/dist/auth/providers/jwt.js +212 -0
  36. package/dist/auth/providers/oauth2.d.ts +73 -0
  37. package/dist/auth/providers/oauth2.js +303 -0
  38. package/dist/auth/providers/supabase.d.ts +63 -0
  39. package/dist/auth/providers/supabase.js +259 -0
  40. package/dist/auth/providers/workos.d.ts +61 -0
  41. package/dist/auth/providers/workos.js +284 -0
  42. package/dist/auth/serverBridge.d.ts +14 -0
  43. package/dist/auth/serverBridge.js +25 -0
  44. package/dist/auth/sessionManager.d.ts +142 -0
  45. package/dist/auth/sessionManager.js +437 -0
  46. package/dist/cli/commands/authProviders.d.ts +43 -0
  47. package/dist/cli/commands/authProviders.js +399 -0
  48. package/dist/cli/factories/authCommandFactory.d.ts +23 -5
  49. package/dist/cli/factories/authCommandFactory.js +108 -5
  50. package/dist/cli/parser.js +1 -1
  51. package/dist/client/auth/AuthProviderFactory.js +111 -0
  52. package/dist/client/auth/AuthProviderRegistry.js +190 -0
  53. package/dist/client/auth/RequestContext.js +78 -0
  54. package/dist/client/auth/accountPool.js +178 -0
  55. package/dist/client/auth/authContext.js +314 -0
  56. package/dist/client/auth/errors.js +39 -0
  57. package/dist/client/auth/index.js +61 -0
  58. package/dist/client/auth/middleware/AuthMiddleware.js +519 -0
  59. package/dist/client/auth/middleware/rateLimitByUser.js +554 -0
  60. package/dist/client/auth/providers/BaseAuthProvider.js +723 -0
  61. package/dist/client/auth/providers/CognitoProvider.js +304 -0
  62. package/dist/client/auth/providers/KeycloakProvider.js +393 -0
  63. package/dist/client/auth/providers/auth0.js +274 -0
  64. package/dist/client/auth/providers/betterAuth.js +182 -0
  65. package/dist/client/auth/providers/clerk.js +317 -0
  66. package/dist/client/auth/providers/custom.js +112 -0
  67. package/dist/client/auth/providers/firebase.js +226 -0
  68. package/dist/client/auth/providers/jwt.js +212 -0
  69. package/dist/client/auth/providers/oauth2.js +303 -0
  70. package/dist/client/auth/providers/supabase.js +259 -0
  71. package/dist/client/auth/providers/workos.js +284 -0
  72. package/dist/client/auth/serverBridge.js +25 -0
  73. package/dist/client/auth/sessionManager.js +437 -0
  74. package/dist/client/core/infrastructure/baseRegistry.js +5 -1
  75. package/dist/client/index.js +25 -0
  76. package/dist/client/mcp/toolRegistry.js +11 -1
  77. package/dist/client/neurolink.js +218 -0
  78. package/dist/client/rag/ChunkerRegistry.js +2 -2
  79. package/dist/client/rag/metadata/MetadataExtractorRegistry.js +2 -2
  80. package/dist/client/rag/reranker/RerankerRegistry.js +2 -2
  81. package/dist/client/server/routes/agentRoutes.js +20 -2
  82. package/dist/client/types/authTypes.js +2 -1
  83. package/dist/core/infrastructure/baseRegistry.d.ts +3 -1
  84. package/dist/core/infrastructure/baseRegistry.js +5 -1
  85. package/dist/index.d.ts +1 -0
  86. package/dist/index.js +25 -0
  87. package/dist/lib/auth/AuthProviderFactory.d.ts +71 -0
  88. package/dist/lib/auth/AuthProviderFactory.js +112 -0
  89. package/dist/lib/auth/AuthProviderRegistry.d.ts +33 -0
  90. package/dist/lib/auth/AuthProviderRegistry.js +191 -0
  91. package/dist/lib/auth/RequestContext.d.ts +23 -0
  92. package/dist/lib/auth/RequestContext.js +79 -0
  93. package/dist/lib/auth/authContext.d.ts +198 -0
  94. package/dist/lib/auth/authContext.js +315 -0
  95. package/dist/lib/auth/errors.d.ts +63 -0
  96. package/dist/lib/auth/errors.js +40 -0
  97. package/dist/lib/auth/index.d.ts +20 -8
  98. package/dist/lib/auth/index.js +35 -7
  99. package/dist/lib/auth/middleware/AuthMiddleware.d.ts +181 -0
  100. package/dist/lib/auth/middleware/AuthMiddleware.js +520 -0
  101. package/dist/lib/auth/middleware/rateLimitByUser.d.ts +282 -0
  102. package/dist/lib/auth/middleware/rateLimitByUser.js +555 -0
  103. package/dist/lib/auth/providers/BaseAuthProvider.d.ts +259 -0
  104. package/dist/lib/auth/providers/BaseAuthProvider.js +724 -0
  105. package/dist/lib/auth/providers/CognitoProvider.d.ts +61 -0
  106. package/dist/lib/auth/providers/CognitoProvider.js +305 -0
  107. package/dist/lib/auth/providers/KeycloakProvider.d.ts +61 -0
  108. package/dist/lib/auth/providers/KeycloakProvider.js +394 -0
  109. package/dist/lib/auth/providers/auth0.d.ts +59 -0
  110. package/dist/lib/auth/providers/auth0.js +275 -0
  111. package/dist/lib/auth/providers/betterAuth.d.ts +51 -0
  112. package/dist/lib/auth/providers/betterAuth.js +183 -0
  113. package/dist/lib/auth/providers/clerk.d.ts +65 -0
  114. package/dist/lib/auth/providers/clerk.js +318 -0
  115. package/dist/lib/auth/providers/custom.d.ts +64 -0
  116. package/dist/lib/auth/providers/custom.js +113 -0
  117. package/dist/lib/auth/providers/firebase.d.ts +63 -0
  118. package/dist/lib/auth/providers/firebase.js +227 -0
  119. package/dist/lib/auth/providers/jwt.d.ts +68 -0
  120. package/dist/lib/auth/providers/jwt.js +213 -0
  121. package/dist/lib/auth/providers/oauth2.d.ts +73 -0
  122. package/dist/lib/auth/providers/oauth2.js +304 -0
  123. package/dist/lib/auth/providers/supabase.d.ts +63 -0
  124. package/dist/lib/auth/providers/supabase.js +260 -0
  125. package/dist/lib/auth/providers/workos.d.ts +61 -0
  126. package/dist/lib/auth/providers/workos.js +285 -0
  127. package/dist/lib/auth/serverBridge.d.ts +14 -0
  128. package/dist/lib/auth/serverBridge.js +26 -0
  129. package/dist/lib/auth/sessionManager.d.ts +142 -0
  130. package/dist/lib/auth/sessionManager.js +438 -0
  131. package/dist/lib/core/infrastructure/baseRegistry.d.ts +3 -1
  132. package/dist/lib/core/infrastructure/baseRegistry.js +5 -1
  133. package/dist/lib/index.d.ts +1 -0
  134. package/dist/lib/index.js +25 -0
  135. package/dist/lib/mcp/toolRegistry.js +11 -1
  136. package/dist/lib/neurolink.d.ts +42 -1
  137. package/dist/lib/neurolink.js +218 -0
  138. package/dist/lib/rag/ChunkerRegistry.js +2 -2
  139. package/dist/lib/rag/metadata/MetadataExtractorRegistry.js +2 -2
  140. package/dist/lib/rag/reranker/RerankerRegistry.js +2 -2
  141. package/dist/lib/server/routes/agentRoutes.js +20 -2
  142. package/dist/lib/types/authTypes.d.ts +937 -1
  143. package/dist/lib/types/authTypes.js +2 -1
  144. package/dist/lib/types/configTypes.d.ts +46 -0
  145. package/dist/lib/types/generateTypes.d.ts +6 -0
  146. package/dist/lib/types/index.d.ts +1 -0
  147. package/dist/lib/types/streamTypes.d.ts +6 -0
  148. package/dist/mcp/toolRegistry.js +11 -1
  149. package/dist/neurolink.d.ts +42 -1
  150. package/dist/neurolink.js +218 -0
  151. package/dist/rag/ChunkerRegistry.js +2 -2
  152. package/dist/rag/metadata/MetadataExtractorRegistry.js +2 -2
  153. package/dist/rag/reranker/RerankerRegistry.js +2 -2
  154. package/dist/server/routes/agentRoutes.js +20 -2
  155. package/dist/types/authTypes.d.ts +937 -1
  156. package/dist/types/authTypes.js +2 -1
  157. package/dist/types/configTypes.d.ts +46 -0
  158. package/dist/types/generateTypes.d.ts +6 -0
  159. package/dist/types/index.d.ts +1 -0
  160. package/dist/types/streamTypes.d.ts +6 -0
  161. package/package.json +2 -1
@@ -0,0 +1,227 @@
1
+ // src/lib/auth/providers/firebase.ts
2
+ import { BaseAuthProvider } from "./BaseAuthProvider.js";
3
+ import { AuthError } from "../errors.js";
4
+ import { logger } from "../../utils/logger.js";
5
+ import { createProxyFetch } from "../../proxy/proxyFetch.js";
6
+ import * as jose from "jose";
7
+ /**
8
+ * Firebase Authentication Provider
9
+ *
10
+ * Supports Firebase ID token validation using Google's public keys.
11
+ * Can validate tokens locally or via Firebase REST API.
12
+ *
13
+ * Features:
14
+ * - JWT validation using Google's public keys
15
+ * - Token verification via Firebase REST API
16
+ * - Custom claims extraction for roles/permissions
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const firebase = new FirebaseAuthProvider({
21
+ * type: "firebase",
22
+ * projectId: "your-project-id"
23
+ * });
24
+ *
25
+ * const result = await firebase.authenticateToken(idToken);
26
+ * if (result.valid) {
27
+ * console.log("Authenticated user:", result.user);
28
+ * }
29
+ * ```
30
+ */
31
+ export class FirebaseAuthProvider extends BaseAuthProvider {
32
+ type = "firebase";
33
+ projectId;
34
+ apiKey;
35
+ serviceAccount;
36
+ jwks = null;
37
+ constructor(config) {
38
+ super(config);
39
+ if (!config.projectId) {
40
+ throw AuthError.create("CONFIGURATION_ERROR", "Firebase projectId is required", { details: { missingFields: ["projectId"] } });
41
+ }
42
+ this.projectId = config.projectId;
43
+ this.apiKey = config.apiKey;
44
+ this.serviceAccount = config.serviceAccount;
45
+ }
46
+ /**
47
+ * Initialize JWKS for Firebase token verification
48
+ */
49
+ async initialize() {
50
+ // Firebase uses Google's secure token service public keys
51
+ const jwksUrl = new URL("https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com");
52
+ this.jwks = jose.createRemoteJWKSet(jwksUrl);
53
+ logger.debug(`Firebase provider initialized for project: ${this.projectId}`);
54
+ }
55
+ /**
56
+ * Validate Firebase ID token
57
+ */
58
+ async authenticateToken(token, _context) {
59
+ if (!this.jwks) {
60
+ await this.initialize();
61
+ }
62
+ try {
63
+ // Verify the token using Google's public keys
64
+ const { payload } = await jose.jwtVerify(token, this.jwks, {
65
+ issuer: `https://securetoken.google.com/${this.projectId}`,
66
+ audience: this.projectId,
67
+ });
68
+ // Extract user info from Firebase token claims
69
+ const user = this.payloadToUser(payload);
70
+ return {
71
+ valid: true,
72
+ payload: payload,
73
+ user,
74
+ expiresAt: payload.exp ? new Date(payload.exp * 1000) : undefined,
75
+ tokenType: "jwt",
76
+ };
77
+ }
78
+ catch (error) {
79
+ // If local validation fails and API key is available, try REST API
80
+ if (this.apiKey) {
81
+ return this.validateViaApi(token);
82
+ }
83
+ return {
84
+ valid: false,
85
+ error: error instanceof Error ? error.message : String(error),
86
+ };
87
+ }
88
+ }
89
+ /**
90
+ * Validate token via Firebase REST API
91
+ */
92
+ async validateViaApi(token) {
93
+ try {
94
+ const proxyFetch = createProxyFetch();
95
+ const response = await proxyFetch(`https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=${this.apiKey}`, {
96
+ method: "POST",
97
+ headers: {
98
+ "Content-Type": "application/json",
99
+ },
100
+ body: JSON.stringify({ idToken: token }),
101
+ signal: AbortSignal.timeout(5000),
102
+ });
103
+ if (!response.ok) {
104
+ const error = (await response.json());
105
+ return {
106
+ valid: false,
107
+ error: error.error?.message || `Firebase API returned ${response.status}`,
108
+ };
109
+ }
110
+ const data = (await response.json());
111
+ const users = data.users || [];
112
+ if (users.length === 0) {
113
+ return {
114
+ valid: false,
115
+ error: "User not found",
116
+ };
117
+ }
118
+ const userData = users[0];
119
+ const user = this.firebaseUserToAuthUser(userData);
120
+ return {
121
+ valid: true,
122
+ payload: userData,
123
+ user,
124
+ tokenType: "jwt",
125
+ };
126
+ }
127
+ catch (error) {
128
+ return {
129
+ valid: false,
130
+ error: error instanceof Error ? error.message : String(error),
131
+ };
132
+ }
133
+ }
134
+ /**
135
+ * Convert JWT payload to AuthUser
136
+ */
137
+ payloadToUser(payload) {
138
+ // Extract custom claims
139
+ const customClaims = payload;
140
+ return {
141
+ id: payload.sub,
142
+ email: payload.email,
143
+ name: payload.name,
144
+ picture: payload.picture,
145
+ emailVerified: payload.email_verified,
146
+ roles: customClaims.roles || [],
147
+ permissions: customClaims.permissions || [],
148
+ metadata: {
149
+ firebase: {
150
+ sign_in_provider: payload.firebase?.sign_in_provider ||
151
+ "unknown",
152
+ identities: payload.firebase?.identities,
153
+ },
154
+ },
155
+ };
156
+ }
157
+ /**
158
+ * Convert Firebase user data to AuthUser
159
+ */
160
+ firebaseUserToAuthUser(userData) {
161
+ let customAttributes = {};
162
+ if (userData.customAttributes) {
163
+ try {
164
+ customAttributes = JSON.parse(userData.customAttributes);
165
+ }
166
+ catch {
167
+ logger.warn("Failed to parse Firebase customAttributes, treating as empty");
168
+ }
169
+ }
170
+ return {
171
+ id: userData.localId,
172
+ email: userData.email,
173
+ name: userData.displayName,
174
+ picture: userData.photoUrl,
175
+ emailVerified: userData.emailVerified,
176
+ roles: customAttributes.roles || [],
177
+ permissions: customAttributes.permissions || [],
178
+ createdAt: userData.createdAt
179
+ ? new Date(parseInt(userData.createdAt))
180
+ : undefined,
181
+ lastLoginAt: userData.lastLoginAt
182
+ ? new Date(parseInt(userData.lastLoginAt))
183
+ : undefined,
184
+ metadata: {
185
+ providerUserInfo: userData.providerUserInfo,
186
+ },
187
+ };
188
+ }
189
+ /**
190
+ * Get user by ID via Firebase REST API
191
+ * Requires API key
192
+ */
193
+ async getUser(_userId) {
194
+ if (!this.apiKey) {
195
+ logger.warn("Firebase API key required for user lookup");
196
+ return null;
197
+ }
198
+ // Firebase REST API doesn't support direct user lookup by UID
199
+ // This would require Admin SDK or custom backend
200
+ logger.warn("Direct user lookup by ID requires Firebase Admin SDK which is not supported in browser/edge environments");
201
+ return null;
202
+ }
203
+ /**
204
+ * Health check
205
+ */
206
+ async healthCheck() {
207
+ try {
208
+ // Check if we can fetch the JWKS
209
+ const proxyFetch = createProxyFetch();
210
+ const response = await proxyFetch("https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com", { signal: AbortSignal.timeout(5000) });
211
+ return {
212
+ healthy: response.ok,
213
+ providerConnected: response.ok,
214
+ sessionStorageHealthy: true,
215
+ };
216
+ }
217
+ catch (error) {
218
+ return {
219
+ healthy: false,
220
+ providerConnected: false,
221
+ sessionStorageHealthy: true,
222
+ error: error instanceof Error ? error.message : String(error),
223
+ };
224
+ }
225
+ }
226
+ }
227
+ //# sourceMappingURL=firebase.js.map
@@ -0,0 +1,68 @@
1
+ import type { AuthHealthCheck, AuthProviderConfig, AuthRequestContext, JWTConfig, TokenValidationResult } from "../../types/authTypes.js";
2
+ import { BaseAuthProvider } from "./BaseAuthProvider.js";
3
+ /**
4
+ * Generic JWT Provider
5
+ *
6
+ * Supports validation of JWT tokens using either symmetric secrets (HS256/384/512)
7
+ * or asymmetric keys (RS256/384/512, ES256/384/512).
8
+ *
9
+ * Features:
10
+ * - Symmetric secret validation (HMAC)
11
+ * - Asymmetric key validation (RSA, ECDSA)
12
+ * - Configurable algorithms
13
+ * - Issuer and audience validation
14
+ * - Token signing (symmetric keys only)
15
+ * - Session management (inherited from BaseAuthProvider)
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Symmetric key (HMAC)
20
+ * const jwtProvider = new JWTProvider({
21
+ * type: "jwt",
22
+ * secret: "your-256-bit-secret",
23
+ * algorithms: ["HS256"],
24
+ * issuer: "your-app",
25
+ * audience: "your-api",
26
+ * });
27
+ *
28
+ * // Asymmetric key (RSA/ECDSA)
29
+ * const jwtProvider = new JWTProvider({
30
+ * type: "jwt",
31
+ * publicKey: "-----BEGIN PUBLIC KEY-----...",
32
+ * algorithms: ["RS256"],
33
+ * issuer: "your-app",
34
+ * });
35
+ *
36
+ * const result = await jwtProvider.authenticateToken(token);
37
+ * ```
38
+ */
39
+ export declare class JWTProvider extends BaseAuthProvider {
40
+ readonly type: "jwt";
41
+ private secret?;
42
+ private publicKey?;
43
+ private algorithms;
44
+ private issuer?;
45
+ private audience?;
46
+ private keyObject;
47
+ constructor(config: AuthProviderConfig & JWTConfig);
48
+ /**
49
+ * Initialize the key for verification
50
+ */
51
+ initialize(): Promise<void>;
52
+ /**
53
+ * Validate JWT token
54
+ */
55
+ authenticateToken(token: string, _context?: AuthRequestContext): Promise<TokenValidationResult>;
56
+ /**
57
+ * Create a signed JWT token
58
+ *
59
+ * Useful for issuing tokens from this provider.
60
+ */
61
+ signToken(payload: Record<string, unknown>, options?: {
62
+ expiresIn?: string;
63
+ }): Promise<string>;
64
+ /**
65
+ * Health check
66
+ */
67
+ healthCheck(): Promise<AuthHealthCheck>;
68
+ }
@@ -0,0 +1,213 @@
1
+ // src/lib/auth/providers/jwt.ts
2
+ import * as jose from "jose";
3
+ import { logger } from "../../utils/logger.js";
4
+ import { AuthError } from "../errors.js";
5
+ import { BaseAuthProvider } from "./BaseAuthProvider.js";
6
+ /**
7
+ * Generic JWT Provider
8
+ *
9
+ * Supports validation of JWT tokens using either symmetric secrets (HS256/384/512)
10
+ * or asymmetric keys (RS256/384/512, ES256/384/512).
11
+ *
12
+ * Features:
13
+ * - Symmetric secret validation (HMAC)
14
+ * - Asymmetric key validation (RSA, ECDSA)
15
+ * - Configurable algorithms
16
+ * - Issuer and audience validation
17
+ * - Token signing (symmetric keys only)
18
+ * - Session management (inherited from BaseAuthProvider)
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // Symmetric key (HMAC)
23
+ * const jwtProvider = new JWTProvider({
24
+ * type: "jwt",
25
+ * secret: "your-256-bit-secret",
26
+ * algorithms: ["HS256"],
27
+ * issuer: "your-app",
28
+ * audience: "your-api",
29
+ * });
30
+ *
31
+ * // Asymmetric key (RSA/ECDSA)
32
+ * const jwtProvider = new JWTProvider({
33
+ * type: "jwt",
34
+ * publicKey: "-----BEGIN PUBLIC KEY-----...",
35
+ * algorithms: ["RS256"],
36
+ * issuer: "your-app",
37
+ * });
38
+ *
39
+ * const result = await jwtProvider.authenticateToken(token);
40
+ * ```
41
+ */
42
+ export class JWTProvider extends BaseAuthProvider {
43
+ type = "jwt";
44
+ secret;
45
+ publicKey;
46
+ algorithms;
47
+ issuer;
48
+ audience;
49
+ keyObject = null;
50
+ constructor(config) {
51
+ super(config);
52
+ if (!config.secret && !config.publicKey) {
53
+ throw AuthError.create("CONFIGURATION_ERROR", "JWT requires either secret (for HMAC) or publicKey (for RSA/ECDSA)", {
54
+ details: { provider: "jwt", missingFields: ["secret", "publicKey"] },
55
+ });
56
+ }
57
+ this.secret = config.secret;
58
+ this.publicKey = config.publicKey;
59
+ this.algorithms =
60
+ config.algorithms ?? (config.secret ? ["HS256"] : ["RS256"]);
61
+ this.issuer = config.issuer;
62
+ this.audience = config.audience;
63
+ }
64
+ /**
65
+ * Initialize the key for verification
66
+ */
67
+ async initialize() {
68
+ try {
69
+ if (this.secret) {
70
+ // Symmetric key (HMAC)
71
+ this.keyObject = new TextEncoder().encode(this.secret);
72
+ logger.debug("JWT provider initialized with symmetric secret");
73
+ }
74
+ else if (this.publicKey) {
75
+ // Asymmetric key (RSA/ECDSA)
76
+ this.keyObject = await jose.importSPKI(this.publicKey, this.algorithms[0]);
77
+ logger.debug("JWT provider initialized with asymmetric public key");
78
+ }
79
+ }
80
+ catch (error) {
81
+ throw AuthError.create("PROVIDER_INIT_FAILED", `Failed to initialize JWT key: ${error instanceof Error ? error.message : String(error)}`, {
82
+ details: { provider: "jwt" },
83
+ cause: error instanceof Error ? error : undefined,
84
+ });
85
+ }
86
+ }
87
+ /**
88
+ * Validate JWT token
89
+ */
90
+ async authenticateToken(token, _context) {
91
+ if (!this.keyObject) {
92
+ await this.initialize();
93
+ }
94
+ try {
95
+ const verifyOptions = {};
96
+ if (this.algorithms.length > 0) {
97
+ verifyOptions.algorithms = this
98
+ .algorithms;
99
+ }
100
+ if (this.issuer) {
101
+ verifyOptions.issuer = this.issuer;
102
+ }
103
+ if (this.audience) {
104
+ verifyOptions.audience = this.audience;
105
+ }
106
+ const { payload } = await jose.jwtVerify(token, this.keyObject, verifyOptions);
107
+ // Reject tokens without a non-empty sub claim
108
+ if (!payload.sub) {
109
+ return {
110
+ valid: false,
111
+ error: "JWT is missing required 'sub' claim: cannot identify user",
112
+ };
113
+ }
114
+ // Extract user from standard JWT claims
115
+ const user = {
116
+ id: payload.sub,
117
+ email: payload.email,
118
+ name: payload.name,
119
+ picture: payload.picture,
120
+ emailVerified: payload.email_verified,
121
+ roles: payload.roles ?? [],
122
+ permissions: payload.permissions ??
123
+ payload.scope?.split(" ") ??
124
+ [],
125
+ metadata: {
126
+ iss: payload.iss,
127
+ aud: payload.aud,
128
+ jti: payload.jti,
129
+ },
130
+ };
131
+ return {
132
+ valid: true,
133
+ payload: payload,
134
+ user,
135
+ expiresAt: payload.exp ? new Date(payload.exp * 1000) : undefined,
136
+ tokenType: "jwt",
137
+ };
138
+ }
139
+ catch (error) {
140
+ const message = error instanceof Error ? error.message : String(error);
141
+ logger.warn("JWT validation failed:", message);
142
+ // Provide specific error messages
143
+ let errorDetail = message;
144
+ if (message.includes("JWTExpired")) {
145
+ errorDetail = "Token has expired";
146
+ }
147
+ else if (message.includes("signature")) {
148
+ errorDetail = "Invalid token signature";
149
+ }
150
+ else if (message.includes("audience")) {
151
+ errorDetail = "Invalid token audience";
152
+ }
153
+ else if (message.includes("issuer")) {
154
+ errorDetail = "Invalid token issuer";
155
+ }
156
+ return {
157
+ valid: false,
158
+ error: errorDetail,
159
+ };
160
+ }
161
+ }
162
+ /**
163
+ * Create a signed JWT token
164
+ *
165
+ * Useful for issuing tokens from this provider.
166
+ */
167
+ async signToken(payload, options) {
168
+ if (!this.secret) {
169
+ throw AuthError.create("CONFIGURATION_ERROR", "Token signing requires a secret (symmetric key)", { details: { provider: "jwt" } });
170
+ }
171
+ if (!this.keyObject) {
172
+ await this.initialize();
173
+ }
174
+ const jwt = new jose.SignJWT(payload)
175
+ .setProtectedHeader({ alg: this.algorithms[0] })
176
+ .setIssuedAt();
177
+ if (this.issuer) {
178
+ jwt.setIssuer(this.issuer);
179
+ }
180
+ if (this.audience) {
181
+ jwt.setAudience(this.audience);
182
+ }
183
+ if (options?.expiresIn) {
184
+ jwt.setExpirationTime(options.expiresIn);
185
+ }
186
+ return jwt.sign(this.keyObject);
187
+ }
188
+ /**
189
+ * Health check
190
+ */
191
+ async healthCheck() {
192
+ try {
193
+ // Verify the key is properly initialized
194
+ if (!this.keyObject) {
195
+ await this.initialize();
196
+ }
197
+ return {
198
+ healthy: this.keyObject !== null,
199
+ providerConnected: true,
200
+ sessionStorageHealthy: true,
201
+ };
202
+ }
203
+ catch (error) {
204
+ return {
205
+ healthy: false,
206
+ providerConnected: false,
207
+ sessionStorageHealthy: true,
208
+ error: error instanceof Error ? error.message : String(error),
209
+ };
210
+ }
211
+ }
212
+ }
213
+ //# sourceMappingURL=jwt.js.map
@@ -0,0 +1,73 @@
1
+ import type { AuthHealthCheck, AuthProviderConfig, AuthRequestContext, OAuth2Config, TokenValidationResult } from "../../types/authTypes.js";
2
+ import { BaseAuthProvider } from "./BaseAuthProvider.js";
3
+ /**
4
+ * Generic OAuth2/OIDC Provider
5
+ *
6
+ * Supports any OAuth2-compliant identity provider with configurable endpoints.
7
+ * Works with both JWKS-based JWT validation and token introspection.
8
+ *
9
+ * Features:
10
+ * - JWT validation with JWKS (if jwksUrl provided)
11
+ * - Token introspection endpoint support
12
+ * - User info endpoint integration
13
+ * - PKCE support
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const oauth2 = new OAuth2Provider({
18
+ * type: "oauth2",
19
+ * authorizationUrl: "https://idp.example.com/oauth/authorize",
20
+ * tokenUrl: "https://idp.example.com/oauth/token",
21
+ * userInfoUrl: "https://idp.example.com/userinfo",
22
+ * jwksUrl: "https://idp.example.com/.well-known/jwks.json",
23
+ * clientId: "your-client-id",
24
+ * clientSecret: "your-client-secret",
25
+ * });
26
+ *
27
+ * const result = await oauth2.authenticateToken(accessToken);
28
+ * ```
29
+ */
30
+ export declare class OAuth2Provider extends BaseAuthProvider {
31
+ readonly type: "oauth2";
32
+ private authorizationUrl;
33
+ private tokenUrl;
34
+ private userInfoUrl?;
35
+ private jwksUrl?;
36
+ private clientId;
37
+ private clientSecret?;
38
+ private scopes;
39
+ private redirectUrl?;
40
+ private usePKCE;
41
+ private jwks;
42
+ constructor(config: AuthProviderConfig & OAuth2Config);
43
+ /**
44
+ * Initialize JWKS for JWT verification (if jwksUrl is provided)
45
+ */
46
+ initialize(): Promise<void>;
47
+ /**
48
+ * Validate OAuth2 access token
49
+ *
50
+ * Uses JWKS validation if available, otherwise falls back to userinfo endpoint
51
+ */
52
+ authenticateToken(token: string, _context?: AuthRequestContext): Promise<TokenValidationResult>;
53
+ /**
54
+ * Validate token via userinfo endpoint
55
+ */
56
+ private validateViaUserInfo;
57
+ /**
58
+ * Get authorization URL for OAuth2 flow
59
+ */
60
+ getAuthorizationUrl(state: string, codeChallenge?: string): string;
61
+ /**
62
+ * Exchange authorization code for tokens
63
+ */
64
+ exchangeCode(code: string, codeVerifier?: string): Promise<{
65
+ accessToken: string;
66
+ refreshToken?: string;
67
+ idToken?: string;
68
+ }>;
69
+ /**
70
+ * Health check
71
+ */
72
+ healthCheck(): Promise<AuthHealthCheck>;
73
+ }