@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
@@ -246,6 +246,10 @@ export class NeuroLink {
246
246
  conversationMemoryConfig;
247
247
  // Add orchestration property
248
248
  enableOrchestration;
249
+ // Authentication provider for secure access control
250
+ authProvider;
251
+ pendingAuthConfig;
252
+ authInitPromise;
249
253
  // HITL (Human-in-the-Loop) support
250
254
  hitlManager;
251
255
  // Accumulated cost in USD across all generate() calls on this instance
@@ -451,6 +455,10 @@ export class NeuroLink {
451
455
  this.initializeLangfuse(constructorId, constructorStartTime, constructorHrTimeStart);
452
456
  this.initializeMetricsListeners();
453
457
  this.logConstructorComplete(constructorId, constructorStartTime, constructorHrTimeStart);
458
+ // Store auth config for lazy initialization
459
+ if (config?.auth) {
460
+ this.pendingAuthConfig = config.auth;
461
+ }
454
462
  }
455
463
  /**
456
464
  * Initialize provider registry with security settings
@@ -2225,6 +2233,61 @@ Current user's request: ${currentInput}`;
2225
2233
  },
2226
2234
  };
2227
2235
  }
2236
+ // Handle per-call auth token validation
2237
+ if (options.auth?.token) {
2238
+ const { AuthError } = await import("./auth/errors.js");
2239
+ await this.ensureAuthProvider();
2240
+ if (!this.authProvider) {
2241
+ throw AuthError.create("PROVIDER_ERROR", "No auth provider configured. Set auth in constructor or via setAuthProvider() before using auth: { token }.");
2242
+ }
2243
+ let authResult;
2244
+ try {
2245
+ authResult = await withTimeout(this.authProvider.authenticateToken(options.auth.token), 5000, AuthError.create("PROVIDER_ERROR", "Auth token validation timed out after 5000ms"));
2246
+ }
2247
+ catch (err) {
2248
+ // Rethrow auth errors as-is; wrap anything else
2249
+ if (err instanceof Error &&
2250
+ "feature" in err &&
2251
+ err.feature === "Auth") {
2252
+ throw err;
2253
+ }
2254
+ throw AuthError.create("PROVIDER_ERROR", `Auth token validation failed: ${err instanceof Error ? err.message : String(err)}`);
2255
+ }
2256
+ if (!authResult.valid) {
2257
+ throw AuthError.create("INVALID_TOKEN", authResult.error || "Token validation failed");
2258
+ }
2259
+ // Fail closed: token valid but no user identity is a provider bug
2260
+ if (!authResult.user) {
2261
+ throw AuthError.create("INVALID_TOKEN", "Token validated but no user identity returned");
2262
+ }
2263
+ if (!authResult.user.id) {
2264
+ throw AuthError.create("INVALID_TOKEN", "Token validated but user identity missing required 'id' field");
2265
+ }
2266
+ // Merge validated user into context
2267
+ options.context = {
2268
+ ...(options.context || {}),
2269
+ userId: authResult.user.id,
2270
+ userEmail: authResult.user.email,
2271
+ userRoles: authResult.user.roles,
2272
+ };
2273
+ }
2274
+ // Handle pre-validated requestContext
2275
+ if (options.requestContext) {
2276
+ // When auth token was validated, token-derived identity fields
2277
+ // MUST take precedence over requestContext to prevent privilege escalation.
2278
+ const tokenDerivedFields = options.auth?.token && this.authProvider
2279
+ ? {
2280
+ userId: options.context?.userId,
2281
+ userEmail: options.context?.userEmail,
2282
+ userRoles: options.context?.userRoles,
2283
+ }
2284
+ : {};
2285
+ options.context = {
2286
+ ...(options.context || {}),
2287
+ ...options.requestContext,
2288
+ ...tokenDerivedFields,
2289
+ };
2290
+ }
2228
2291
  // Check if workflow is requested
2229
2292
  if (options.workflow || options.workflowConfig) {
2230
2293
  return await this.generateWithWorkflow(options);
@@ -3997,6 +4060,61 @@ Current user's request: ${currentInput}`;
3997
4060
  },
3998
4061
  });
3999
4062
  }
4063
+ // Handle per-call auth token validation
4064
+ if (options.auth?.token) {
4065
+ const { AuthError } = await import("./auth/errors.js");
4066
+ await this.ensureAuthProvider();
4067
+ if (!this.authProvider) {
4068
+ throw AuthError.create("PROVIDER_ERROR", "No auth provider configured. Set auth in constructor or via setAuthProvider() before using auth: { token }.");
4069
+ }
4070
+ let authResult;
4071
+ try {
4072
+ authResult = await withTimeout(this.authProvider.authenticateToken(options.auth.token), 5000, AuthError.create("PROVIDER_ERROR", "Auth token validation timed out after 5000ms"));
4073
+ }
4074
+ catch (err) {
4075
+ // Rethrow auth errors as-is; wrap anything else
4076
+ if (err instanceof Error &&
4077
+ "feature" in err &&
4078
+ err.feature === "Auth") {
4079
+ throw err;
4080
+ }
4081
+ throw AuthError.create("PROVIDER_ERROR", `Auth token validation failed: ${err instanceof Error ? err.message : String(err)}`);
4082
+ }
4083
+ if (!authResult.valid) {
4084
+ throw AuthError.create("INVALID_TOKEN", authResult.error || "Token validation failed");
4085
+ }
4086
+ // Fail closed: token valid but no user identity is a provider bug
4087
+ if (!authResult.user) {
4088
+ throw AuthError.create("INVALID_TOKEN", "Token validated but no user identity returned");
4089
+ }
4090
+ if (!authResult.user.id) {
4091
+ throw AuthError.create("INVALID_TOKEN", "Token validated but user identity missing required 'id' field");
4092
+ }
4093
+ // Merge validated user into context
4094
+ options.context = {
4095
+ ...(options.context || {}),
4096
+ userId: authResult.user.id,
4097
+ userEmail: authResult.user.email,
4098
+ userRoles: authResult.user.roles,
4099
+ };
4100
+ }
4101
+ // Handle pre-validated requestContext
4102
+ if (options.requestContext) {
4103
+ // When auth token was validated, token-derived identity fields
4104
+ // MUST take precedence over requestContext to prevent privilege escalation.
4105
+ const tokenDerivedFields = options.auth?.token && this.authProvider
4106
+ ? {
4107
+ userId: options.context?.userId,
4108
+ userEmail: options.context?.userEmail,
4109
+ userRoles: options.context?.userRoles,
4110
+ }
4111
+ : {};
4112
+ options.context = {
4113
+ ...(options.context || {}),
4114
+ ...options.requestContext,
4115
+ ...tokenDerivedFields,
4116
+ };
4117
+ }
4000
4118
  this.emitStreamStartEvents(options, startTime);
4001
4119
  // Auto-inject lifecycle middleware when callbacks are provided
4002
4120
  // (must happen before workflow early return so that path gets middleware too)
@@ -8001,6 +8119,106 @@ Current user's request: ${currentInput}`;
8001
8119
  });
8002
8120
  return budgetResult.shouldCompact;
8003
8121
  }
8122
+ // ============================================================================
8123
+ // Authentication Methods
8124
+ // ============================================================================
8125
+ /**
8126
+ * Set the authentication provider for the NeuroLink instance
8127
+ *
8128
+ * @param config - Auth provider or configuration to create one
8129
+ */
8130
+ async setAuthProvider(config) {
8131
+ // Clear any pending lazy-init promise so it does not race with this call.
8132
+ this.authInitPromise = undefined;
8133
+ // Duck-type check: direct MastraAuthProvider instance
8134
+ if ("authenticateToken" in config &&
8135
+ typeof config.authenticateToken === "function") {
8136
+ this.authProvider = config;
8137
+ logger.info(`Auth provider set: ${this.authProvider.type}`);
8138
+ }
8139
+ else if ("provider" in config) {
8140
+ this.authProvider = config.provider;
8141
+ logger.info(`Auth provider set: ${this.authProvider.type}`);
8142
+ }
8143
+ else {
8144
+ const typedConfig = config;
8145
+ const { AuthProviderFactory } = await import("./auth/AuthProviderFactory.js");
8146
+ this.authProvider = await AuthProviderFactory.createProvider(typedConfig.type, typedConfig.config);
8147
+ logger.info(`Auth provider created and set: ${typedConfig.type}`);
8148
+ }
8149
+ if (this.authProvider) {
8150
+ this.emitter.emit("auth:provider:set", {
8151
+ type: this.authProvider.type,
8152
+ timestamp: Date.now(),
8153
+ });
8154
+ }
8155
+ }
8156
+ /**
8157
+ * Get the currently configured authentication provider
8158
+ */
8159
+ getAuthProvider() {
8160
+ return this.authProvider;
8161
+ }
8162
+ /**
8163
+ * Lazily initialize the auth provider from pendingAuthConfig.
8164
+ * Called on first use (generate/stream with auth token) to avoid
8165
+ * async work in the synchronous constructor.
8166
+ */
8167
+ async ensureAuthProvider() {
8168
+ if (this.authProvider || !this.pendingAuthConfig) {
8169
+ return;
8170
+ }
8171
+ this.authInitPromise ??= (async () => {
8172
+ try {
8173
+ await this.setAuthProvider(this.pendingAuthConfig);
8174
+ this.pendingAuthConfig = undefined;
8175
+ }
8176
+ catch (err) {
8177
+ this.authInitPromise = undefined;
8178
+ throw err;
8179
+ }
8180
+ })();
8181
+ await this.authInitPromise;
8182
+ }
8183
+ /**
8184
+ * Set the current authentication context for request handling.
8185
+ *
8186
+ * Delegates to the global AuthContextHolder so that auth state is NOT
8187
+ * stored as an instance field (which would leak between concurrent requests
8188
+ * sharing the same NeuroLink singleton). Prefer `runWithAuthContext()` from
8189
+ * `authContext.ts` for proper request-scoped context via AsyncLocalStorage.
8190
+ *
8191
+ * @param context - The authenticated user context
8192
+ */
8193
+ async setAuthContext(context) {
8194
+ const { globalAuthContext } = await import("./auth/authContext.js");
8195
+ globalAuthContext.set(context);
8196
+ logger.debug("Auth context set", {
8197
+ userId: context.user.id,
8198
+ provider: context.provider,
8199
+ sessionId: context.session?.id,
8200
+ });
8201
+ }
8202
+ /**
8203
+ * Get the current authentication context.
8204
+ *
8205
+ * Checks AsyncLocalStorage first, then falls back to the global holder.
8206
+ */
8207
+ async getAuthContext() {
8208
+ const { getAuthContext: getCtx } = await import("./auth/authContext.js");
8209
+ return getCtx();
8210
+ }
8211
+ /**
8212
+ * Clear the current authentication context
8213
+ */
8214
+ async clearAuthContext() {
8215
+ const { globalAuthContext } = await import("./auth/authContext.js");
8216
+ const userId = globalAuthContext.get()?.user.id;
8217
+ globalAuthContext.clear();
8218
+ if (userId) {
8219
+ logger.debug(`Auth context cleared for user: ${userId}`);
8220
+ }
8221
+ }
8004
8222
  /**
8005
8223
  * Get the external server manager instance
8006
8224
  * Used internally by server adapters for external MCP server management
@@ -280,8 +280,8 @@ export class ChunkerRegistry extends BaseRegistry {
280
280
  * Register a chunker with aliases
281
281
  */
282
282
  registerChunker(strategy, factory, metadata) {
283
- this.register(strategy, factory, metadata);
284
- // Register aliases
283
+ this.register(strategy, factory, metadata.aliases ?? [], { metadata });
284
+ // Register aliases in local alias map for strategy resolution
285
285
  if (metadata.aliases) {
286
286
  for (const alias of metadata.aliases) {
287
287
  this.aliasMap.set(alias.toLowerCase(), strategy);
@@ -212,8 +212,8 @@ export class MetadataExtractorRegistry extends BaseRegistry {
212
212
  * Register an extractor with aliases
213
213
  */
214
214
  registerExtractor(type, factory, metadata) {
215
- this.register(type, factory, metadata);
216
- // Register aliases
215
+ this.register(type, factory, metadata.aliases, { metadata });
216
+ // Register aliases in local alias map for type resolution
217
217
  for (const alias of metadata.aliases) {
218
218
  this.aliasMap.set(alias.toLowerCase(), type);
219
219
  logger.debug(`[MetadataExtractorRegistry] Registered alias '${alias}' -> '${type}'`);
@@ -237,8 +237,8 @@ export class RerankerRegistry extends BaseRegistry {
237
237
  * Register a reranker with aliases
238
238
  */
239
239
  registerReranker(type, factory, metadata) {
240
- this.register(type, factory, metadata);
241
- // Register aliases
240
+ this.register(type, factory, metadata.aliases, { metadata });
241
+ // Register aliases in local alias map for type resolution
242
242
  for (const alias of metadata.aliases) {
243
243
  this.aliasMap.set(alias.toLowerCase(), type);
244
244
  logger.debug(`[RerankerRegistry] Registered alias '${alias}' -> '${type}'`);
@@ -36,8 +36,15 @@ export function createAgentRoutes(basePath = "/api") {
36
36
  // Note: tools should be passed as Record<string, Tool> in generate options
37
37
  // If request.tools is an array of tool names, we skip them
38
38
  context: {
39
- sessionId: request.sessionId,
40
- userId: request.userId,
39
+ // When an authenticated user context exists (set by auth middleware),
40
+ // always use its IDs to prevent caller-supplied impersonation.
41
+ sessionId: ctx.user
42
+ ? ctx.session?.id
43
+ : (ctx.session?.id ?? request.sessionId),
44
+ userId: ctx.user ? ctx.user.id : request.userId,
45
+ userEmail: ctx.user?.email,
46
+ userRoles: ctx.user?.roles,
47
+ requestId: ctx.requestId,
41
48
  },
42
49
  });
43
50
  // Map tool calls from SDK format to API format
@@ -78,6 +85,17 @@ export function createAgentRoutes(basePath = "/api") {
78
85
  systemPrompt: request.systemPrompt,
79
86
  temperature: request.temperature,
80
87
  maxTokens: request.maxTokens,
88
+ context: {
89
+ // When an authenticated user context exists (set by auth middleware),
90
+ // always use its IDs to prevent caller-supplied impersonation.
91
+ sessionId: ctx.user
92
+ ? ctx.session?.id
93
+ : (ctx.session?.id ?? request.sessionId),
94
+ userId: ctx.user ? ctx.user.id : request.userId,
95
+ userEmail: ctx.user?.email,
96
+ userRoles: ctx.user?.roles,
97
+ requestId: ctx.requestId,
98
+ },
81
99
  });
82
100
  // Create redactor (no-op if redaction is not enabled)
83
101
  const redactor = createStreamRedactor(ctx.redaction);
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Auth-related type definitions for NeuroLink
3
3
  *
4
- * Canonical location for OAuth token storage types and token refresher contracts.
4
+ * Canonical location for OAuth token storage types, token refresher contracts,
5
+ * and multi-provider authentication types.
5
6
  * All auth type imports should reference this module (or the barrel re-export
6
7
  * via src/lib/types/index.ts).
7
8
  */
@@ -10,7 +10,9 @@ export declare abstract class BaseRegistry<TItem, TMetadata = unknown> {
10
10
  protected initPromise: Promise<void> | null;
11
11
  protected abstract registerAll(): Promise<void>;
12
12
  ensureInitialized(): Promise<void>;
13
- register(id: string, factory: () => Promise<TItem>, metadata: TMetadata): void;
13
+ register(id: string, factory: () => Promise<TItem>, aliases?: string[], options?: {
14
+ metadata: TMetadata;
15
+ }): void;
14
16
  get(id: string): Promise<TItem | undefined>;
15
17
  has(id: string): boolean;
16
18
  list(): Array<{
@@ -14,8 +14,12 @@ export class BaseRegistry {
14
14
  await this.initPromise;
15
15
  this.initialized = true;
16
16
  }
17
- register(id, factory, metadata) {
17
+ register(id, factory, aliases = [], options) {
18
+ const metadata = options?.metadata ?? {};
18
19
  this.items.set(id, { factory, metadata });
20
+ for (const alias of aliases) {
21
+ this.items.set(alias.toLowerCase(), { factory, metadata });
22
+ }
19
23
  logger.debug(`Registered ${id} in registry`);
20
24
  }
21
25
  async get(id) {
package/dist/index.d.ts CHANGED
@@ -374,3 +374,4 @@ export { RAGASEvaluator } from "./evaluation/ragasEvaluator.js";
374
374
  export { RetryManager } from "./evaluation/retryManager.js";
375
375
  export { mapToEvaluationData } from "./evaluation/scoring.js";
376
376
  export type { EnhancedConversationTurn, EnhancedEvaluationContext, EvaluationConfig, EvaluationResult, GetPromptFunction, QualityErrorDetails, QueryIntentAnalysis, } from "./types/evaluationTypes.js";
377
+ export { AuthProviderFactory, createAuthProvider, AuthProviderRegistry, AuthError as AuthErrorFactory, AuthErrorCodes, BaseAuthProvider, InMemorySessionStorage, AuthProviderError, createAuthMiddleware as createAuthProviderMiddleware, createRBACMiddleware, createProtectedMiddleware, createExpressAuthMiddleware, createRequestContext, extractToken, AuthMiddlewareError, AuthMiddlewareErrorCodes, type MiddlewareHandler as AuthMiddlewareHandler, type MiddlewareResult, type NextFunction, type ExpressMiddleware, UserRateLimiter, MemoryRateLimitStorage, RedisRateLimitStorage, createRateLimitByUserMiddleware, createAuthenticatedRateLimitMiddleware, createRateLimitStorage, type RateLimitConfig as AuthRateLimitConfig, type RateLimitResult, type RateLimitMiddlewareResult, type RateLimitStorage, SessionManager, MemorySessionStorage, RedisSessionStorage, createSessionStorage, type SessionStorageInterface, AuthContextHolder, globalAuthContext, getAuthContext, getCurrentUser, getCurrentSession, isAuthenticated, hasRole, hasAnyRole, hasPermission, hasAllPermissions, requireAuth, requireRole, requirePermission, requireUser, runWithAuthContext, createAuthenticatedContext, RequestContext, NEUROLINK_RESOURCE_ID_KEY, NEUROLINK_THREAD_ID_KEY, createAuthValidatorFromProvider, } from "./auth/index.js";
package/dist/index.js CHANGED
@@ -509,3 +509,28 @@ export { PromptBuilder } from "./evaluation/prompts.js";
509
509
  export { RAGASEvaluator } from "./evaluation/ragasEvaluator.js";
510
510
  export { RetryManager } from "./evaluation/retryManager.js";
511
511
  export { mapToEvaluationData } from "./evaluation/scoring.js";
512
+ // ============================================================================
513
+ // AUTHENTICATION PROVIDERS - Multi-provider Auth Integration
514
+ // ============================================================================
515
+ // Single-sourced from ./auth/index.js. Only aliases that differ from the
516
+ // canonical export name are listed explicitly; everything else is re-exported
517
+ // as-is.
518
+ export {
519
+ // Factory & Registry
520
+ AuthProviderFactory, createAuthProvider, AuthProviderRegistry,
521
+ // Unified error factory
522
+ AuthError as AuthErrorFactory, AuthErrorCodes,
523
+ // Base Provider
524
+ BaseAuthProvider, InMemorySessionStorage, AuthProviderError,
525
+ // Auth Middleware (aliased to avoid conflict with server createAuthMiddleware)
526
+ createAuthMiddleware as createAuthProviderMiddleware, createRBACMiddleware, createProtectedMiddleware, createExpressAuthMiddleware, createRequestContext, extractToken, AuthMiddlewareError, AuthMiddlewareErrorCodes,
527
+ // Rate Limiting Middleware
528
+ UserRateLimiter, MemoryRateLimitStorage, RedisRateLimitStorage, createRateLimitByUserMiddleware, createAuthenticatedRateLimitMiddleware, createRateLimitStorage,
529
+ // Session Management
530
+ SessionManager, MemorySessionStorage, RedisSessionStorage, createSessionStorage,
531
+ // Auth Context
532
+ AuthContextHolder, globalAuthContext, getAuthContext, getCurrentUser, getCurrentSession, isAuthenticated, hasRole, hasAnyRole, hasPermission, hasAllPermissions, requireAuth, requireRole, requirePermission, requireUser, runWithAuthContext, createAuthenticatedContext,
533
+ // Request Context
534
+ RequestContext, NEUROLINK_RESOURCE_ID_KEY, NEUROLINK_THREAD_ID_KEY,
535
+ // Server Bridge
536
+ createAuthValidatorFromProvider, } from "./auth/index.js";
@@ -0,0 +1,71 @@
1
+ /**
2
+ * AuthProviderFactory - Static factory for authentication providers
3
+ *
4
+ * Matches the ProviderFactory pattern: all-static class, no BaseFactory
5
+ * extension, no singleton instance. Providers are registered by
6
+ * AuthProviderRegistry using dynamic imports to avoid circular dependencies.
7
+ */
8
+ import type { AuthProviderConfig, AuthProviderMetadata, MastraAuthProvider } from "../types/authTypes.js";
9
+ type AuthProviderConstructor = (config: AuthProviderConfig) => Promise<MastraAuthProvider>;
10
+ /**
11
+ * AuthProviderFactory - Creates authentication provider instances
12
+ *
13
+ * Pure static factory with no hardcoded imports. All providers are
14
+ * registered dynamically by AuthProviderRegistry to avoid circular
15
+ * dependencies and enable lazy loading.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Create a provider (after AuthProviderRegistry.registerAllProviders())
20
+ * const provider = await AuthProviderFactory.createProvider("auth0", {
21
+ * type: "auth0",
22
+ * domain: "your-tenant.auth0.com",
23
+ * clientId: "your-client-id",
24
+ * });
25
+ * ```
26
+ */
27
+ export declare class AuthProviderFactory {
28
+ private static readonly providers;
29
+ private static readonly aliasMap;
30
+ /**
31
+ * Register a provider with the factory
32
+ */
33
+ static registerProvider(type: string, factory: AuthProviderConstructor, aliases?: string[], metadata?: AuthProviderMetadata): void;
34
+ /**
35
+ * Create a provider instance
36
+ */
37
+ static createProvider(typeOrAlias: string, config: AuthProviderConfig): Promise<MastraAuthProvider>;
38
+ /**
39
+ * Check if a provider is registered
40
+ */
41
+ static hasProvider(typeOrAlias: string): boolean;
42
+ /**
43
+ * Get list of available provider types (excludes aliases)
44
+ */
45
+ static getAvailableProviders(): string[];
46
+ /**
47
+ * Get provider metadata
48
+ */
49
+ static getProviderMetadata(typeOrAlias: string): AuthProviderMetadata | undefined;
50
+ /**
51
+ * Get all registered providers with their metadata
52
+ */
53
+ static getAllProviderInfo(): Array<{
54
+ type: string;
55
+ aliases: string[];
56
+ metadata?: AuthProviderMetadata;
57
+ }>;
58
+ /**
59
+ * Clear all registrations (for testing)
60
+ */
61
+ static clearRegistrations(): void;
62
+ /**
63
+ * Resolve an alias to its canonical provider type
64
+ */
65
+ private static resolveType;
66
+ }
67
+ /**
68
+ * Create an auth provider using the factory
69
+ */
70
+ export declare function createAuthProvider(type: string, config: AuthProviderConfig): Promise<MastraAuthProvider>;
71
+ export {};
@@ -0,0 +1,112 @@
1
+ /**
2
+ * AuthProviderFactory - Static factory for authentication providers
3
+ *
4
+ * Matches the ProviderFactory pattern: all-static class, no BaseFactory
5
+ * extension, no singleton instance. Providers are registered by
6
+ * AuthProviderRegistry using dynamic imports to avoid circular dependencies.
7
+ */
8
+ import { logger } from "../utils/logger.js";
9
+ import { AuthError } from "./errors.js";
10
+ // =============================================================================
11
+ // FACTORY IMPLEMENTATION
12
+ // =============================================================================
13
+ /**
14
+ * AuthProviderFactory - Creates authentication provider instances
15
+ *
16
+ * Pure static factory with no hardcoded imports. All providers are
17
+ * registered dynamically by AuthProviderRegistry to avoid circular
18
+ * dependencies and enable lazy loading.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * // Create a provider (after AuthProviderRegistry.registerAllProviders())
23
+ * const provider = await AuthProviderFactory.createProvider("auth0", {
24
+ * type: "auth0",
25
+ * domain: "your-tenant.auth0.com",
26
+ * clientId: "your-client-id",
27
+ * });
28
+ * ```
29
+ */
30
+ export class AuthProviderFactory {
31
+ static providers = new Map();
32
+ static aliasMap = new Map();
33
+ /**
34
+ * Register a provider with the factory
35
+ */
36
+ static registerProvider(type, factory, aliases = [], metadata) {
37
+ AuthProviderFactory.providers.set(type, { factory, aliases, metadata });
38
+ for (const alias of aliases) {
39
+ AuthProviderFactory.aliasMap.set(alias.toLowerCase(), type);
40
+ }
41
+ logger.debug(`Registered auth provider: ${type}`);
42
+ }
43
+ /**
44
+ * Create a provider instance
45
+ */
46
+ static async createProvider(typeOrAlias, config) {
47
+ const resolvedType = AuthProviderFactory.resolveType(typeOrAlias);
48
+ const registration = AuthProviderFactory.providers.get(resolvedType);
49
+ if (!registration) {
50
+ throw AuthError.create("PROVIDER_NOT_FOUND", `Auth provider not found: ${typeOrAlias}. Available: ${AuthProviderFactory.getAvailableProviders().join(", ")}`);
51
+ }
52
+ try {
53
+ return await registration.factory(config);
54
+ }
55
+ catch (error) {
56
+ throw AuthError.create("CREATION_FAILED", `Failed to create auth provider ${typeOrAlias}: ${error instanceof Error ? error.message : String(error)}`, { cause: error instanceof Error ? error : undefined });
57
+ }
58
+ }
59
+ /**
60
+ * Check if a provider is registered
61
+ */
62
+ static hasProvider(typeOrAlias) {
63
+ return (AuthProviderFactory.providers.has(typeOrAlias) ||
64
+ AuthProviderFactory.aliasMap.has(typeOrAlias.toLowerCase()));
65
+ }
66
+ /**
67
+ * Get list of available provider types (excludes aliases)
68
+ */
69
+ static getAvailableProviders() {
70
+ return Array.from(AuthProviderFactory.providers.keys());
71
+ }
72
+ /**
73
+ * Get provider metadata
74
+ */
75
+ static getProviderMetadata(typeOrAlias) {
76
+ const resolvedType = AuthProviderFactory.resolveType(typeOrAlias);
77
+ return AuthProviderFactory.providers.get(resolvedType)?.metadata;
78
+ }
79
+ /**
80
+ * Get all registered providers with their metadata
81
+ */
82
+ static getAllProviderInfo() {
83
+ return Array.from(AuthProviderFactory.providers.entries()).map(([type, reg]) => ({
84
+ type,
85
+ aliases: reg.aliases,
86
+ metadata: reg.metadata,
87
+ }));
88
+ }
89
+ /**
90
+ * Clear all registrations (for testing)
91
+ */
92
+ static clearRegistrations() {
93
+ AuthProviderFactory.providers.clear();
94
+ AuthProviderFactory.aliasMap.clear();
95
+ }
96
+ /**
97
+ * Resolve an alias to its canonical provider type
98
+ */
99
+ static resolveType(typeOrAlias) {
100
+ return (AuthProviderFactory.aliasMap.get(typeOrAlias.toLowerCase()) || typeOrAlias);
101
+ }
102
+ }
103
+ // =============================================================================
104
+ // CONVENIENCE EXPORTS
105
+ // =============================================================================
106
+ /**
107
+ * Create an auth provider using the factory
108
+ */
109
+ export async function createAuthProvider(type, config) {
110
+ return AuthProviderFactory.createProvider(type, config);
111
+ }
112
+ //# sourceMappingURL=AuthProviderFactory.js.map
@@ -0,0 +1,33 @@
1
+ /**
2
+ * AuthProviderRegistry - Static one-shot registry for authentication providers
3
+ *
4
+ * Matches the ProviderRegistry pattern: static class with a single
5
+ * `registerAllProviders()` entry point that registers all 11 auth
6
+ * providers with AuthProviderFactory using dynamic imports.
7
+ */
8
+ /**
9
+ * AuthProviderRegistry - registers all auth providers with the factory
10
+ *
11
+ * Call `AuthProviderRegistry.registerAllProviders()` once during
12
+ * application startup. The method is idempotent and concurrency-safe.
13
+ */
14
+ export declare class AuthProviderRegistry {
15
+ private static registered;
16
+ private static registrationPromise;
17
+ /**
18
+ * Register all auth providers with the factory
19
+ */
20
+ static registerAllProviders(): Promise<void>;
21
+ /**
22
+ * Internal registration implementation
23
+ */
24
+ private static _doRegister;
25
+ /**
26
+ * Check if providers are registered
27
+ */
28
+ static isRegistered(): boolean;
29
+ /**
30
+ * Clear registrations (for testing)
31
+ */
32
+ static clearRegistrations(): void;
33
+ }