@mastra/koa 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,83 @@
1
1
  # @mastra/koa
2
2
 
3
+ ## 1.2.4
4
+
5
+ ### Patch Changes
6
+
7
+ - Added RBAC permission enforcement to all server adapters. When an auth provider is configured, each route's required permission is checked against the authenticated user's permissions before the handler runs. Permissions are derived automatically from route paths and HTTP methods using the convention-based system from `@mastra/server`. ([#13163](https://github.com/mastra-ai/mastra/pull/13163))
8
+
9
+ - Added HTTP request logging middleware. Enable with `apiReqLogs: true` for default settings, or pass a configuration object for fine-grained control. ([#11907](https://github.com/mastra-ai/mastra/pull/11907))
10
+
11
+ **Simple activation**
12
+
13
+ ```ts
14
+ const mastra = new Mastra({
15
+ server: { build: { apiReqLogs: true } },
16
+ });
17
+ ```
18
+
19
+ **Advanced configuration**
20
+
21
+ ```ts
22
+ const mastra = new Mastra({
23
+ server: {
24
+ build: {
25
+ apiReqLogs: {
26
+ enabled: true,
27
+ level: 'debug',
28
+ excludePaths: ['/health'],
29
+ includeHeaders: true,
30
+ includeQueryParams: true,
31
+ redactHeaders: ['authorization', 'cookie'],
32
+ },
33
+ },
34
+ },
35
+ });
36
+ ```
37
+
38
+ - Updated dependencies [[`504fc8b`](https://github.com/mastra-ai/mastra/commit/504fc8b9d0ddab717577ad3bf9c95ea4bd5377bd), [`f9c150b`](https://github.com/mastra-ai/mastra/commit/f9c150b7595ad05ad9cc9a11098e2944361e8c22), [`88de7e8`](https://github.com/mastra-ai/mastra/commit/88de7e8dfe4b7e1951a9e441bb33136e705ce24e), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`edee4b3`](https://github.com/mastra-ai/mastra/commit/edee4b37dff0af515fc7cc0e8d71ee39e6a762f0), [`3790c75`](https://github.com/mastra-ai/mastra/commit/3790c7578cc6a47d854eb12d89e6b1912867fe29), [`e7a235b`](https://github.com/mastra-ai/mastra/commit/e7a235be6472e0c870ed6c791ddb17c492dc188b), [`d51d298`](https://github.com/mastra-ai/mastra/commit/d51d298953967aab1f58ec965b644d109214f085), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d5f0d8d`](https://github.com/mastra-ai/mastra/commit/d5f0d8d6a03e515ddaa9b5da19b7e44b8357b07b), [`09c3b18`](https://github.com/mastra-ai/mastra/commit/09c3b1802ff14e243a8a8baea327440bc8cc2e32), [`b896379`](https://github.com/mastra-ai/mastra/commit/b8963791c6afa79484645fcec596a201f936b9a2), [`85c84eb`](https://github.com/mastra-ai/mastra/commit/85c84ebb78aebfcba9d209c8e152b16d7a00cb71), [`a89272a`](https://github.com/mastra-ai/mastra/commit/a89272a5d71939b9fcd284e6a6dc1dd091a6bdcf), [`ee9c8df`](https://github.com/mastra-ai/mastra/commit/ee9c8df644f19d055af5f496bf4942705f5a47b7), [`77b4a25`](https://github.com/mastra-ai/mastra/commit/77b4a254e51907f8ff3a3ba95596a18e93ae4b35), [`276246e`](https://github.com/mastra-ai/mastra/commit/276246e0b9066a1ea48bbc70df84dbe528daaf99), [`08ecfdb`](https://github.com/mastra-ai/mastra/commit/08ecfdbdad6fb8285deef86a034bdf4a6047cfca), [`d5f628c`](https://github.com/mastra-ai/mastra/commit/d5f628ca86c6f6f3ff1035d52f635df32dd81cab), [`524c0f3`](https://github.com/mastra-ai/mastra/commit/524c0f3c434c3d9d18f66338dcef383d6161b59c), [`c18a0e9`](https://github.com/mastra-ai/mastra/commit/c18a0e9cef1e4ca004b2963d35e4cfc031971eac), [`4bd21ea`](https://github.com/mastra-ai/mastra/commit/4bd21ea43d44d0a0427414fc047577f9f0aa3bec), [`115a7a4`](https://github.com/mastra-ai/mastra/commit/115a7a47db5e9896fec12ae6507501adb9ec89bf), [`22a48ae`](https://github.com/mastra-ai/mastra/commit/22a48ae2513eb54d8d79dad361fddbca97a155e8), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9311c17`](https://github.com/mastra-ai/mastra/commit/9311c17d7a0640d9c4da2e71b814dc67c57c6369), [`7edf78f`](https://github.com/mastra-ai/mastra/commit/7edf78f80422c43e84585f08ba11df0d4d0b73c5), [`1c4221c`](https://github.com/mastra-ai/mastra/commit/1c4221cf6032ec98d0e094d4ee11da3e48490d96), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d25b9ea`](https://github.com/mastra-ai/mastra/commit/d25b9eabd400167255a97b690ffbc4ee4097ded5), [`fe1ce5c`](https://github.com/mastra-ai/mastra/commit/fe1ce5c9211c03d561606fda95cbfe7df1d9a9b5), [`b03c0e0`](https://github.com/mastra-ai/mastra/commit/b03c0e0389a799523929a458b0509c9e4244d562), [`0a8366b`](https://github.com/mastra-ai/mastra/commit/0a8366b0a692fcdde56c4d526e4cf03c502ae4ac), [`85664e9`](https://github.com/mastra-ai/mastra/commit/85664e9fd857320fbc245e301f764f45f66f32a3), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`3a3a59e`](https://github.com/mastra-ai/mastra/commit/3a3a59e8ffaa6a985fe3d9a126a3f5ade11a6724), [`3108d4e`](https://github.com/mastra-ai/mastra/commit/3108d4e649c9fddbf03253a6feeb388a5fa9fa5a), [`0c33b2c`](https://github.com/mastra-ai/mastra/commit/0c33b2c9db537f815e1c59e2c898ffce2e395a79), [`191e5bd`](https://github.com/mastra-ai/mastra/commit/191e5bd29b82f5bda35243945790da7bc7b695c2), [`f77cd94`](https://github.com/mastra-ai/mastra/commit/f77cd94c44eabed490384e7d19232a865e13214c), [`e8135c7`](https://github.com/mastra-ai/mastra/commit/e8135c7e300dac5040670eec7eab896ac6092e30), [`daca48f`](https://github.com/mastra-ai/mastra/commit/daca48f0fb17b7ae0b62a2ac40cf0e491b2fd0b7), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`257d14f`](https://github.com/mastra-ai/mastra/commit/257d14faca5931f2e4186fc165b6f0b1f915deee), [`352f25d`](https://github.com/mastra-ai/mastra/commit/352f25da316b24cdd5b410fd8dddf6a8b763da2a), [`93477d0`](https://github.com/mastra-ai/mastra/commit/93477d0769b8a13ea5ed73d508d967fb23eaeed9), [`31c78b3`](https://github.com/mastra-ai/mastra/commit/31c78b3eb28f58a8017f1dcc795c33214d87feac), [`0bc0720`](https://github.com/mastra-ai/mastra/commit/0bc07201095791858087cc56f353fcd65e87ab54), [`36516ac`](https://github.com/mastra-ai/mastra/commit/36516aca1021cbeb42e74751b46a2614101f37c8), [`e947652`](https://github.com/mastra-ai/mastra/commit/e9476527fdecb4449e54570e80dfaf8466901254), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`ec248f6`](https://github.com/mastra-ai/mastra/commit/ec248f6b56e8a037c066c49b2178e2507471d988)]:
39
+ - @mastra/core@1.9.0
40
+ - @mastra/server@1.9.0
41
+
42
+ ## 1.2.4-alpha.0
43
+
44
+ ### Patch Changes
45
+
46
+ - Added RBAC permission enforcement to all server adapters. When an auth provider is configured, each route's required permission is checked against the authenticated user's permissions before the handler runs. Permissions are derived automatically from route paths and HTTP methods using the convention-based system from `@mastra/server`. ([#13163](https://github.com/mastra-ai/mastra/pull/13163))
47
+
48
+ - Added HTTP request logging middleware. Enable with `apiReqLogs: true` for default settings, or pass a configuration object for fine-grained control. ([#11907](https://github.com/mastra-ai/mastra/pull/11907))
49
+
50
+ **Simple activation**
51
+
52
+ ```ts
53
+ const mastra = new Mastra({
54
+ server: { build: { apiReqLogs: true } },
55
+ });
56
+ ```
57
+
58
+ **Advanced configuration**
59
+
60
+ ```ts
61
+ const mastra = new Mastra({
62
+ server: {
63
+ build: {
64
+ apiReqLogs: {
65
+ enabled: true,
66
+ level: 'debug',
67
+ excludePaths: ['/health'],
68
+ includeHeaders: true,
69
+ includeQueryParams: true,
70
+ redactHeaders: ['authorization', 'cookie'],
71
+ },
72
+ },
73
+ },
74
+ });
75
+ ```
76
+
77
+ - Updated dependencies [[`504fc8b`](https://github.com/mastra-ai/mastra/commit/504fc8b9d0ddab717577ad3bf9c95ea4bd5377bd), [`f9c150b`](https://github.com/mastra-ai/mastra/commit/f9c150b7595ad05ad9cc9a11098e2944361e8c22), [`88de7e8`](https://github.com/mastra-ai/mastra/commit/88de7e8dfe4b7e1951a9e441bb33136e705ce24e), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`edee4b3`](https://github.com/mastra-ai/mastra/commit/edee4b37dff0af515fc7cc0e8d71ee39e6a762f0), [`3790c75`](https://github.com/mastra-ai/mastra/commit/3790c7578cc6a47d854eb12d89e6b1912867fe29), [`e7a235b`](https://github.com/mastra-ai/mastra/commit/e7a235be6472e0c870ed6c791ddb17c492dc188b), [`d51d298`](https://github.com/mastra-ai/mastra/commit/d51d298953967aab1f58ec965b644d109214f085), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d5f0d8d`](https://github.com/mastra-ai/mastra/commit/d5f0d8d6a03e515ddaa9b5da19b7e44b8357b07b), [`09c3b18`](https://github.com/mastra-ai/mastra/commit/09c3b1802ff14e243a8a8baea327440bc8cc2e32), [`b896379`](https://github.com/mastra-ai/mastra/commit/b8963791c6afa79484645fcec596a201f936b9a2), [`85c84eb`](https://github.com/mastra-ai/mastra/commit/85c84ebb78aebfcba9d209c8e152b16d7a00cb71), [`a89272a`](https://github.com/mastra-ai/mastra/commit/a89272a5d71939b9fcd284e6a6dc1dd091a6bdcf), [`ee9c8df`](https://github.com/mastra-ai/mastra/commit/ee9c8df644f19d055af5f496bf4942705f5a47b7), [`77b4a25`](https://github.com/mastra-ai/mastra/commit/77b4a254e51907f8ff3a3ba95596a18e93ae4b35), [`276246e`](https://github.com/mastra-ai/mastra/commit/276246e0b9066a1ea48bbc70df84dbe528daaf99), [`08ecfdb`](https://github.com/mastra-ai/mastra/commit/08ecfdbdad6fb8285deef86a034bdf4a6047cfca), [`d5f628c`](https://github.com/mastra-ai/mastra/commit/d5f628ca86c6f6f3ff1035d52f635df32dd81cab), [`524c0f3`](https://github.com/mastra-ai/mastra/commit/524c0f3c434c3d9d18f66338dcef383d6161b59c), [`c18a0e9`](https://github.com/mastra-ai/mastra/commit/c18a0e9cef1e4ca004b2963d35e4cfc031971eac), [`4bd21ea`](https://github.com/mastra-ai/mastra/commit/4bd21ea43d44d0a0427414fc047577f9f0aa3bec), [`115a7a4`](https://github.com/mastra-ai/mastra/commit/115a7a47db5e9896fec12ae6507501adb9ec89bf), [`22a48ae`](https://github.com/mastra-ai/mastra/commit/22a48ae2513eb54d8d79dad361fddbca97a155e8), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9311c17`](https://github.com/mastra-ai/mastra/commit/9311c17d7a0640d9c4da2e71b814dc67c57c6369), [`7edf78f`](https://github.com/mastra-ai/mastra/commit/7edf78f80422c43e84585f08ba11df0d4d0b73c5), [`1c4221c`](https://github.com/mastra-ai/mastra/commit/1c4221cf6032ec98d0e094d4ee11da3e48490d96), [`6dbeeb9`](https://github.com/mastra-ai/mastra/commit/6dbeeb94a8b1eebb727300d1a98961f882180794), [`d25b9ea`](https://github.com/mastra-ai/mastra/commit/d25b9eabd400167255a97b690ffbc4ee4097ded5), [`fe1ce5c`](https://github.com/mastra-ai/mastra/commit/fe1ce5c9211c03d561606fda95cbfe7df1d9a9b5), [`b03c0e0`](https://github.com/mastra-ai/mastra/commit/b03c0e0389a799523929a458b0509c9e4244d562), [`0a8366b`](https://github.com/mastra-ai/mastra/commit/0a8366b0a692fcdde56c4d526e4cf03c502ae4ac), [`85664e9`](https://github.com/mastra-ai/mastra/commit/85664e9fd857320fbc245e301f764f45f66f32a3), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`3a3a59e`](https://github.com/mastra-ai/mastra/commit/3a3a59e8ffaa6a985fe3d9a126a3f5ade11a6724), [`3108d4e`](https://github.com/mastra-ai/mastra/commit/3108d4e649c9fddbf03253a6feeb388a5fa9fa5a), [`0c33b2c`](https://github.com/mastra-ai/mastra/commit/0c33b2c9db537f815e1c59e2c898ffce2e395a79), [`191e5bd`](https://github.com/mastra-ai/mastra/commit/191e5bd29b82f5bda35243945790da7bc7b695c2), [`f77cd94`](https://github.com/mastra-ai/mastra/commit/f77cd94c44eabed490384e7d19232a865e13214c), [`e8135c7`](https://github.com/mastra-ai/mastra/commit/e8135c7e300dac5040670eec7eab896ac6092e30), [`daca48f`](https://github.com/mastra-ai/mastra/commit/daca48f0fb17b7ae0b62a2ac40cf0e491b2fd0b7), [`bc79650`](https://github.com/mastra-ai/mastra/commit/bc796500c6e0334faa158a96077e3fb332274869), [`257d14f`](https://github.com/mastra-ai/mastra/commit/257d14faca5931f2e4186fc165b6f0b1f915deee), [`352f25d`](https://github.com/mastra-ai/mastra/commit/352f25da316b24cdd5b410fd8dddf6a8b763da2a), [`93477d0`](https://github.com/mastra-ai/mastra/commit/93477d0769b8a13ea5ed73d508d967fb23eaeed9), [`31c78b3`](https://github.com/mastra-ai/mastra/commit/31c78b3eb28f58a8017f1dcc795c33214d87feac), [`0bc0720`](https://github.com/mastra-ai/mastra/commit/0bc07201095791858087cc56f353fcd65e87ab54), [`36516ac`](https://github.com/mastra-ai/mastra/commit/36516aca1021cbeb42e74751b46a2614101f37c8), [`e947652`](https://github.com/mastra-ai/mastra/commit/e9476527fdecb4449e54570e80dfaf8466901254), [`3c6ef79`](https://github.com/mastra-ai/mastra/commit/3c6ef798481e00d6d22563be2de98818fd4dd5e0), [`9257d01`](https://github.com/mastra-ai/mastra/commit/9257d01d1366d81f84c582fe02b5e200cf9621f4), [`ec248f6`](https://github.com/mastra-ai/mastra/commit/ec248f6b56e8a037c066c49b2178e2507471d988)]:
78
+ - @mastra/core@1.9.0-alpha.0
79
+ - @mastra/server@1.9.0-alpha.0
80
+
3
81
  ## 1.2.3
4
82
 
5
83
  ### Patch Changes
package/LICENSE.md CHANGED
@@ -1,3 +1,18 @@
1
+ Portions of this software are licensed as follows:
2
+
3
+ - All content that resides under any directory named "ee/" within this
4
+ repository, including but not limited to:
5
+ - `packages/core/src/auth/ee/`
6
+ - `packages/server/src/server/auth/ee/`
7
+ is licensed under the license defined in `ee/LICENSE`.
8
+
9
+ - All third-party components incorporated into the Mastra Software are
10
+ licensed under the original license provided by the owner of the
11
+ applicable component.
12
+
13
+ - Content outside of the above-mentioned directories or restrictions is
14
+ available under the "Apache License 2.0" as defined below.
15
+
1
16
  # Apache License 2.0
2
17
 
3
18
  Copyright (c) 2025 Kepler Software, Inc.
package/dist/index.cjs CHANGED
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
3
  var busboy = require('@fastify/busboy');
4
+ var auth = require('@mastra/server/auth');
4
5
  var error = require('@mastra/server/handlers/error');
5
6
  var serverAdapter = require('@mastra/server/server-adapter');
6
- var auth = require('@mastra/server/auth');
7
7
 
8
8
  // src/index.ts
9
9
 
@@ -213,148 +213,39 @@ ZodError.create = (issues) => {
213
213
  const error = new ZodError(issues);
214
214
  return error;
215
215
  };
216
- var authenticationMiddleware = async (ctx, next) => {
217
- const mastra = ctx.state.mastra;
218
- const authConfig = mastra.getServer()?.auth;
219
- const customRouteAuthConfig = ctx.state.customRouteAuthConfig;
220
- if (!authConfig) {
221
- return next();
222
- }
223
- const path = String(ctx.path || "/");
224
- const method = String(ctx.method || "GET");
225
- const getHeader = (name) => ctx.headers[name.toLowerCase()];
226
- if (auth.isDevPlaygroundRequest(path, method, getHeader, authConfig, customRouteAuthConfig)) {
227
- return next();
228
- }
229
- if (!auth.isProtectedPath(path, method, authConfig, customRouteAuthConfig)) {
230
- return next();
231
- }
232
- if (auth.canAccessPublicly(path, method, authConfig)) {
233
- return next();
234
- }
235
- const authHeader = ctx.headers.authorization;
236
- let token = authHeader ? authHeader.replace("Bearer ", "") : null;
237
- const query = ctx.query;
238
- if (!token && query.apiKey) {
239
- token = query.apiKey || null;
240
- }
241
- if (!token) {
242
- ctx.status = 401;
243
- ctx.body = { error: "Authentication required" };
244
- return;
245
- }
246
- try {
247
- let user;
248
- if (typeof authConfig.authenticateToken === "function") {
249
- user = await authConfig.authenticateToken(token, ctx.request);
250
- } else {
251
- throw new Error("No token verification method configured");
252
- }
253
- if (!user) {
254
- ctx.status = 401;
255
- ctx.body = { error: "Invalid or expired token" };
256
- return;
257
- }
258
- ctx.state.requestContext.set("user", user);
259
- return next();
260
- } catch (err) {
261
- mastra.getLogger()?.error("Authentication error", {
262
- error: err instanceof Error ? { message: err.message, stack: err.stack } : err
216
+
217
+ // src/index.ts
218
+ var _hasPermissionPromise;
219
+ function loadHasPermission() {
220
+ if (!_hasPermissionPromise) {
221
+ _hasPermissionPromise = import('@mastra/core/auth/ee').then((m) => m.hasPermission).catch(() => {
222
+ console.error(
223
+ "[@mastra/koa] Auth features require @mastra/core >= 1.6.0. Please upgrade: npm install @mastra/core@latest"
224
+ );
225
+ return void 0;
263
226
  });
264
- ctx.status = 401;
265
- ctx.body = { error: "Invalid or expired token" };
266
- return;
267
- }
268
- };
269
- var authorizationMiddleware = async (ctx, next) => {
270
- const mastra = ctx.state.mastra;
271
- const authConfig = mastra.getServer()?.auth;
272
- const customRouteAuthConfig = ctx.state.customRouteAuthConfig;
273
- if (!authConfig) {
274
- return next();
275
- }
276
- const path = String(ctx.path || "/");
277
- const method = String(ctx.method || "GET");
278
- const getHeader = (name) => ctx.headers[name.toLowerCase()];
279
- if (auth.isDevPlaygroundRequest(path, method, getHeader, authConfig, customRouteAuthConfig)) {
280
- return next();
281
- }
282
- if (!auth.isProtectedPath(path, method, authConfig, customRouteAuthConfig)) {
283
- return next();
284
227
  }
285
- if (auth.canAccessPublicly(path, method, authConfig)) {
286
- return next();
287
- }
288
- const user = ctx.state.requestContext.get("user");
289
- if ("authorizeUser" in authConfig && typeof authConfig.authorizeUser === "function") {
290
- try {
291
- const isAuthorized = await authConfig.authorizeUser(user, ctx.request);
292
- if (isAuthorized) {
293
- return next();
294
- }
295
- ctx.status = 403;
296
- ctx.body = { error: "Access denied" };
297
- return;
298
- } catch (err) {
299
- mastra.getLogger()?.error("Authorization error in authorizeUser", {
300
- error: err instanceof Error ? { message: err.message, stack: err.stack } : err
301
- });
302
- ctx.status = 500;
303
- ctx.body = { error: "Authorization error" };
304
- return;
305
- }
306
- }
307
- if ("authorize" in authConfig && typeof authConfig.authorize === "function") {
308
- try {
309
- const context = {
310
- get: (key) => {
311
- if (key === "mastra") return ctx.state.mastra;
312
- if (key === "requestContext") return ctx.state.requestContext;
313
- if (key === "tools") return ctx.state.tools;
314
- if (key === "taskStore") return ctx.state.taskStore;
315
- if (key === "customRouteAuthConfig") return ctx.state.customRouteAuthConfig;
316
- return void 0;
317
- },
318
- req: ctx.request
319
- };
320
- const isAuthorized = await authConfig.authorize(path, method, user, context);
321
- if (isAuthorized) {
322
- return next();
228
+ return _hasPermissionPromise;
229
+ }
230
+ function toWebRequest(ctx) {
231
+ const protocol = ctx.protocol || "http";
232
+ const host = ctx.host || "localhost";
233
+ const url = `${protocol}://${host}${ctx.url}`;
234
+ const headers = new Headers();
235
+ for (const [key, value] of Object.entries(ctx.headers)) {
236
+ if (value) {
237
+ if (Array.isArray(value)) {
238
+ value.forEach((v) => headers.append(key, v));
239
+ } else {
240
+ headers.set(key, value);
323
241
  }
324
- ctx.status = 403;
325
- ctx.body = { error: "Access denied" };
326
- return;
327
- } catch (err) {
328
- mastra.getLogger()?.error("Authorization error in authorize", {
329
- error: err instanceof Error ? { message: err.message, stack: err.stack } : err,
330
- path,
331
- method
332
- });
333
- ctx.status = 500;
334
- ctx.body = { error: "Authorization error" };
335
- return;
336
- }
337
- }
338
- if ("rules" in authConfig && authConfig.rules && authConfig.rules.length > 0) {
339
- const isAuthorized = await auth.checkRules(authConfig.rules, path, method, user);
340
- if (isAuthorized) {
341
- return next();
342
- }
343
- ctx.status = 403;
344
- ctx.body = { error: "Access denied" };
345
- return;
346
- }
347
- if (auth.defaultAuthConfig.rules && auth.defaultAuthConfig.rules.length > 0) {
348
- const isAuthorized = await auth.checkRules(auth.defaultAuthConfig.rules, path, method, user);
349
- if (isAuthorized) {
350
- return next();
351
242
  }
352
243
  }
353
- ctx.status = 403;
354
- ctx.body = { error: "Access denied" };
355
- };
356
-
357
- // src/index.ts
244
+ return new globalThis.Request(url, {
245
+ method: ctx.method,
246
+ headers
247
+ });
248
+ }
358
249
  var MastraServer = class extends serverAdapter.MastraServer {
359
250
  async init() {
360
251
  this.registerErrorMiddleware();
@@ -612,7 +503,8 @@ var MastraServer = class extends serverAdapter.MastraServer {
612
503
  async sendResponse(route, ctx, result, prefix) {
613
504
  const resolvedPrefix = prefix ?? this.prefix ?? "";
614
505
  if (route.responseType === "json") {
615
- ctx.body = result;
506
+ ctx.type = "application/json";
507
+ ctx.body = result === null || result === void 0 ? JSON.stringify(null) : result;
616
508
  } else if (route.responseType === "stream") {
617
509
  await this.stream(route, ctx, result);
618
510
  } else if (route.responseType === "datastream-response") {
@@ -715,7 +607,9 @@ var MastraServer = class extends serverAdapter.MastraServer {
715
607
  method: String(ctx.method || "GET"),
716
608
  getHeader: (name) => ctx.headers[name.toLowerCase()],
717
609
  getQuery: (name) => ctx.query[name],
718
- requestContext: ctx.state.requestContext
610
+ requestContext: ctx.state.requestContext,
611
+ request: toWebRequest(ctx),
612
+ buildAuthorizeContext: () => toWebRequest(ctx)
719
613
  });
720
614
  if (authError) {
721
615
  ctx.status = authError.status;
@@ -802,6 +696,22 @@ var MastraServer = class extends serverAdapter.MastraServer {
802
696
  abortSignal: ctx.state.abortSignal,
803
697
  routePrefix: prefix
804
698
  };
699
+ const authConfig = this.mastra.getServer()?.auth;
700
+ if (authConfig) {
701
+ const hasPermission = await loadHasPermission();
702
+ if (hasPermission) {
703
+ const userPermissions = ctx.state.requestContext.get("userPermissions");
704
+ const permissionError = this.checkRoutePermission(route, userPermissions, hasPermission);
705
+ if (permissionError) {
706
+ ctx.status = permissionError.status;
707
+ ctx.body = {
708
+ error: permissionError.error,
709
+ message: permissionError.message
710
+ };
711
+ return;
712
+ }
713
+ }
714
+ }
805
715
  try {
806
716
  const result = await route.handler(handlerParams);
807
717
  await this.sendResponse(route, ctx, result, prefix);
@@ -846,6 +756,54 @@ var MastraServer = class extends serverAdapter.MastraServer {
846
756
  async registerCustomApiRoutes() {
847
757
  if (!await this.buildCustomRouteHandler()) return;
848
758
  this.app.use(async (ctx, next) => {
759
+ const path = String(ctx.path || "/");
760
+ const method = String(ctx.method || "GET");
761
+ if (auth.isProtectedCustomRoute(path, method, this.customRouteAuthConfig)) {
762
+ const serverRoute = {
763
+ method,
764
+ path,
765
+ responseType: "json",
766
+ handler: async () => {
767
+ }
768
+ };
769
+ const authError = await this.checkRouteAuth(serverRoute, {
770
+ path,
771
+ method,
772
+ getHeader: (name) => ctx.headers[name.toLowerCase()],
773
+ getQuery: (name) => ctx.query[name],
774
+ requestContext: ctx.state.requestContext,
775
+ request: toWebRequest(ctx),
776
+ buildAuthorizeContext: () => toWebRequest(ctx)
777
+ });
778
+ if (authError) {
779
+ ctx.status = authError.status;
780
+ ctx.body = { error: authError.error };
781
+ return;
782
+ }
783
+ const authConfig = this.mastra.getServer()?.auth;
784
+ if (authConfig) {
785
+ let hasPermission;
786
+ try {
787
+ ({ hasPermission } = await import('@mastra/core/auth/ee'));
788
+ } catch {
789
+ console.error(
790
+ "[@mastra/koa] Auth features require @mastra/core >= 1.6.0. Please upgrade: npm install @mastra/core@latest"
791
+ );
792
+ }
793
+ if (hasPermission) {
794
+ const userPermissions = ctx.state.requestContext.get("userPermissions");
795
+ const permissionError = this.checkRoutePermission(serverRoute, userPermissions, hasPermission);
796
+ if (permissionError) {
797
+ ctx.status = permissionError.status;
798
+ ctx.body = {
799
+ error: permissionError.error,
800
+ message: permissionError.message
801
+ };
802
+ return;
803
+ }
804
+ }
805
+ }
806
+ }
849
807
  const response = await this.handleCustomRouteRequest(
850
808
  `${ctx.protocol}://${ctx.host}${ctx.originalUrl || ctx.url}`,
851
809
  ctx.method,
@@ -862,12 +820,44 @@ var MastraServer = class extends serverAdapter.MastraServer {
862
820
  this.app.use(this.createContextMiddleware());
863
821
  }
864
822
  registerAuthMiddleware() {
865
- const authConfig = this.mastra.getServer()?.auth;
866
- if (!authConfig) {
823
+ }
824
+ registerHttpLoggingMiddleware() {
825
+ if (!this.httpLoggingConfig?.enabled) {
867
826
  return;
868
827
  }
869
- this.app.use(authenticationMiddleware);
870
- this.app.use(authorizationMiddleware);
828
+ this.app.use(async (ctx, next) => {
829
+ if (!this.shouldLogRequest(ctx.path)) {
830
+ return next();
831
+ }
832
+ const start = Date.now();
833
+ const method = ctx.method;
834
+ const path = ctx.path;
835
+ await next();
836
+ const duration = Date.now() - start;
837
+ const status = ctx.status;
838
+ const level = this.httpLoggingConfig?.level || "info";
839
+ const logData = {
840
+ method,
841
+ path,
842
+ status,
843
+ duration: `${duration}ms`
844
+ };
845
+ if (this.httpLoggingConfig?.includeQueryParams) {
846
+ logData.query = ctx.query;
847
+ }
848
+ if (this.httpLoggingConfig?.includeHeaders) {
849
+ const headers = { ...ctx.headers };
850
+ const redactHeaders = this.httpLoggingConfig.redactHeaders || [];
851
+ redactHeaders.forEach((h) => {
852
+ const key = h.toLowerCase();
853
+ if (headers[key] !== void 0) {
854
+ headers[key] = "[REDACTED]";
855
+ }
856
+ });
857
+ logData.headers = headers;
858
+ }
859
+ this.logger[level](`${method} ${path} ${status} ${duration}ms`, logData);
860
+ });
871
861
  }
872
862
  };
873
863