@mastra/fastify 1.1.7 → 1.1.8

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/fastify
2
2
 
3
+ ## 1.1.8
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.1.8-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.1.7
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
@@ -3,7 +3,6 @@
3
3
  var busboy = require('@fastify/busboy');
4
4
  var error = require('@mastra/server/handlers/error');
5
5
  var serverAdapter = require('@mastra/server/server-adapter');
6
- var auth = require('@mastra/server/auth');
7
6
 
8
7
  // src/index.ts
9
8
 
@@ -213,131 +212,39 @@ ZodError.create = (issues) => {
213
212
  const error = new ZodError(issues);
214
213
  return error;
215
214
  };
216
- var authenticationMiddleware = async (request, reply) => {
217
- const mastra = request.mastra;
218
- const authConfig = mastra.getServer()?.auth;
219
- const customRouteAuthConfig = request.customRouteAuthConfig;
220
- if (!authConfig) {
221
- return;
222
- }
223
- const path = String(request.url.split("?")[0] || "/");
224
- const method = String(request.method || "GET");
225
- const getHeader = (name) => request.headers[name.toLowerCase()];
226
- if (auth.isDevPlaygroundRequest(path, method, getHeader, authConfig, customRouteAuthConfig)) {
227
- return;
228
- }
229
- if (!auth.isProtectedPath(path, method, authConfig, customRouteAuthConfig)) {
230
- return;
231
- }
232
- if (auth.canAccessPublicly(path, method, authConfig)) {
233
- return;
234
- }
235
- const authHeader = request.headers.authorization;
236
- let token = authHeader ? authHeader.replace("Bearer ", "") : null;
237
- const query = request.query;
238
- if (!token && query.apiKey) {
239
- token = query.apiKey || null;
240
- }
241
- if (!token) {
242
- return reply.status(401).send({ error: "Authentication required" });
243
- }
244
- try {
245
- let user;
246
- if (typeof authConfig.authenticateToken === "function") {
247
- user = await authConfig.authenticateToken(token, request);
248
- } else {
249
- throw new Error("No token verification method configured");
250
- }
251
- if (!user) {
252
- return reply.status(401).send({ error: "Invalid or expired token" });
253
- }
254
- request.requestContext.set("user", user);
255
- return;
256
- } catch (err) {
257
- mastra.getLogger()?.error("Authentication error", {
258
- error: err instanceof Error ? { message: err.message, stack: err.stack } : err
215
+
216
+ // src/index.ts
217
+ var _hasPermissionPromise;
218
+ function loadHasPermission() {
219
+ if (!_hasPermissionPromise) {
220
+ _hasPermissionPromise = import('@mastra/core/auth/ee').then((m) => m.hasPermission).catch(() => {
221
+ console.error(
222
+ "[@mastra/fastify] Auth features require @mastra/core >= 1.6.0. Please upgrade: npm install @mastra/core@latest"
223
+ );
224
+ return void 0;
259
225
  });
260
- return reply.status(401).send({ error: "Invalid or expired token" });
261
- }
262
- };
263
- var authorizationMiddleware = async (request, reply) => {
264
- const mastra = request.mastra;
265
- const authConfig = mastra.getServer()?.auth;
266
- const customRouteAuthConfig = request.customRouteAuthConfig;
267
- if (!authConfig) {
268
- return;
269
- }
270
- const path = String(request.url.split("?")[0] || "/");
271
- const method = String(request.method || "GET");
272
- const getHeader = (name) => request.headers[name.toLowerCase()];
273
- if (auth.isDevPlaygroundRequest(path, method, getHeader, authConfig, customRouteAuthConfig)) {
274
- return;
275
- }
276
- if (!auth.isProtectedPath(path, method, authConfig, customRouteAuthConfig)) {
277
- return;
278
226
  }
279
- if (auth.canAccessPublicly(path, method, authConfig)) {
280
- return;
281
- }
282
- const user = request.requestContext.get("user");
283
- if ("authorizeUser" in authConfig && typeof authConfig.authorizeUser === "function") {
284
- try {
285
- const isAuthorized = await authConfig.authorizeUser(user, request);
286
- if (isAuthorized) {
287
- return;
288
- }
289
- return reply.status(403).send({ error: "Access denied" });
290
- } catch (err) {
291
- mastra.getLogger()?.error("Authorization error in authorizeUser", {
292
- error: err instanceof Error ? { message: err.message, stack: err.stack } : err
293
- });
294
- return reply.status(500).send({ error: "Authorization error" });
295
- }
296
- }
297
- if ("authorize" in authConfig && typeof authConfig.authorize === "function") {
298
- try {
299
- const context = {
300
- get: (key) => {
301
- if (key === "mastra") return request.mastra;
302
- if (key === "requestContext") return request.requestContext;
303
- if (key === "tools") return request.tools;
304
- if (key === "taskStore") return request.taskStore;
305
- if (key === "customRouteAuthConfig") return request.customRouteAuthConfig;
306
- return void 0;
307
- },
308
- req: request
309
- };
310
- const isAuthorized = await authConfig.authorize(path, method, user, context);
311
- if (isAuthorized) {
312
- return;
227
+ return _hasPermissionPromise;
228
+ }
229
+ function toWebRequest(request) {
230
+ const protocol = request.protocol || "http";
231
+ const host = request.headers.host || "localhost";
232
+ const url = `${protocol}://${host}${request.url}`;
233
+ const headers = new Headers();
234
+ for (const [key, value] of Object.entries(request.headers)) {
235
+ if (value) {
236
+ if (Array.isArray(value)) {
237
+ value.forEach((v) => headers.append(key, v));
238
+ } else {
239
+ headers.set(key, value);
313
240
  }
314
- return reply.status(403).send({ error: "Access denied" });
315
- } catch (err) {
316
- mastra.getLogger()?.error("Authorization error in authorize", {
317
- error: err instanceof Error ? { message: err.message, stack: err.stack } : err,
318
- path,
319
- method
320
- });
321
- return reply.status(500).send({ error: "Authorization error" });
322
241
  }
323
242
  }
324
- if ("rules" in authConfig && authConfig.rules && authConfig.rules.length > 0) {
325
- const isAuthorized = await auth.checkRules(authConfig.rules, path, method, user);
326
- if (isAuthorized) {
327
- return;
328
- }
329
- return reply.status(403).send({ error: "Access denied" });
330
- }
331
- if (auth.defaultAuthConfig.rules && auth.defaultAuthConfig.rules.length > 0) {
332
- const isAuthorized = await auth.checkRules(auth.defaultAuthConfig.rules, path, method, user);
333
- if (isAuthorized) {
334
- return;
335
- }
336
- }
337
- return reply.status(403).send({ error: "Access denied" });
338
- };
339
-
340
- // src/index.ts
243
+ return new globalThis.Request(url, {
244
+ method: request.method,
245
+ headers
246
+ });
247
+ }
341
248
  var MastraServer = class extends serverAdapter.MastraServer {
342
249
  createContextMiddleware() {
343
250
  return async (request, _reply) => {
@@ -611,7 +518,9 @@ var MastraServer = class extends serverAdapter.MastraServer {
611
518
  method: String(request.method || "GET"),
612
519
  getHeader: (name) => request.headers[name.toLowerCase()],
613
520
  getQuery: (name) => request.query[name],
614
- requestContext: request.requestContext
521
+ requestContext: request.requestContext,
522
+ request: toWebRequest(request),
523
+ buildAuthorizeContext: () => toWebRequest(request)
615
524
  });
616
525
  if (authError) {
617
526
  return reply.status(authError.status).send({ error: authError.error });
@@ -682,6 +591,20 @@ var MastraServer = class extends serverAdapter.MastraServer {
682
591
  abortSignal: request.abortSignal,
683
592
  routePrefix: prefix
684
593
  };
594
+ const authConfig = this.mastra.getServer()?.auth;
595
+ if (authConfig) {
596
+ const hasPermission = await loadHasPermission();
597
+ if (hasPermission) {
598
+ const userPermissions = request.requestContext.get("userPermissions");
599
+ const permissionError = this.checkRoutePermission(route, userPermissions, hasPermission);
600
+ if (permissionError) {
601
+ return reply.status(permissionError.status).send({
602
+ error: permissionError.error,
603
+ message: permissionError.message
604
+ });
605
+ }
606
+ }
607
+ }
685
608
  try {
686
609
  const result = await route.handler(handlerParams);
687
610
  await this.sendResponse(route, reply, result, request, prefix);
@@ -735,7 +658,48 @@ var MastraServer = class extends serverAdapter.MastraServer {
735
658
  if (!await this.buildCustomRouteHandler()) return;
736
659
  const routes = this.customApiRoutes ?? this.mastra.getServer()?.apiRoutes ?? [];
737
660
  for (const route of routes) {
661
+ const serverRoute = {
662
+ method: route.method,
663
+ path: route.path,
664
+ responseType: "json",
665
+ handler: async () => {
666
+ },
667
+ requiresAuth: route.requiresAuth
668
+ };
738
669
  const fastifyHandler = async (request, reply) => {
670
+ const authError = await this.checkRouteAuth(serverRoute, {
671
+ path: String(request.url.split("?")[0] || "/"),
672
+ method: String(request.method || "GET"),
673
+ getHeader: (name) => request.headers[name.toLowerCase()],
674
+ getQuery: (name) => request.query[name],
675
+ requestContext: request.requestContext,
676
+ request: toWebRequest(request),
677
+ buildAuthorizeContext: () => toWebRequest(request)
678
+ });
679
+ if (authError) {
680
+ return reply.status(authError.status).send({ error: authError.error });
681
+ }
682
+ const authConfig = this.mastra.getServer()?.auth;
683
+ if (authConfig) {
684
+ let hasPermission;
685
+ try {
686
+ ({ hasPermission } = await import('@mastra/core/auth/ee'));
687
+ } catch {
688
+ console.error(
689
+ "[@mastra/fastify] Auth features require @mastra/core >= 1.6.0. Please upgrade: npm install @mastra/core@latest"
690
+ );
691
+ }
692
+ if (hasPermission) {
693
+ const userPermissions = request.requestContext.get("userPermissions");
694
+ const permissionError = this.checkRoutePermission(serverRoute, userPermissions, hasPermission);
695
+ if (permissionError) {
696
+ return reply.status(permissionError.status).send({
697
+ error: permissionError.error,
698
+ message: permissionError.message
699
+ });
700
+ }
701
+ }
702
+ }
739
703
  const response = await this.handleCustomRouteRequest(
740
704
  `http://${request.headers.host}${request.url}`,
741
705
  request.method,
@@ -784,12 +748,46 @@ var MastraServer = class extends serverAdapter.MastraServer {
784
748
  this.app.addHook("preHandler", this.createContextMiddleware());
785
749
  }
786
750
  registerAuthMiddleware() {
787
- const authConfig = this.mastra.getServer()?.auth;
788
- if (!authConfig) {
751
+ }
752
+ registerHttpLoggingMiddleware() {
753
+ if (!this.httpLoggingConfig?.enabled) {
789
754
  return;
790
755
  }
791
- this.app.addHook("preHandler", authenticationMiddleware);
792
- this.app.addHook("preHandler", authorizationMiddleware);
756
+ this.app.addHook("onRequest", async (request, reply) => {
757
+ const urlPath = request.url.split("?")[0];
758
+ if (!this.shouldLogRequest(urlPath)) {
759
+ return;
760
+ }
761
+ const start = Date.now();
762
+ const method = request.method;
763
+ const path = urlPath;
764
+ reply.raw.once("finish", () => {
765
+ const duration = Date.now() - start;
766
+ const status = reply.statusCode;
767
+ const level = this.httpLoggingConfig?.level || "info";
768
+ const logData = {
769
+ method,
770
+ path,
771
+ status,
772
+ duration: `${duration}ms`
773
+ };
774
+ if (this.httpLoggingConfig?.includeQueryParams) {
775
+ logData.query = request.query;
776
+ }
777
+ if (this.httpLoggingConfig?.includeHeaders) {
778
+ const headers = { ...request.headers };
779
+ const redactHeaders = this.httpLoggingConfig.redactHeaders || [];
780
+ redactHeaders.forEach((h) => {
781
+ const key = h.toLowerCase();
782
+ if (headers[key] !== void 0) {
783
+ headers[key] = "[REDACTED]";
784
+ }
785
+ });
786
+ logData.headers = headers;
787
+ }
788
+ this.logger[level](`${method} ${path} ${status} ${duration}ms`, logData);
789
+ });
790
+ });
793
791
  }
794
792
  };
795
793