@mastra/koa 1.3.7-alpha.0 → 1.4.0-alpha.1

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,23 @@
1
1
  # @mastra/koa
2
2
 
3
+ ## 1.4.0-alpha.1
4
+
5
+ ### Minor Changes
6
+
7
+ - Added adapter auth middleware helpers for raw framework routes. ([#14458](https://github.com/mastra-ai/mastra/pull/14458))
8
+
9
+ Use `createAuthMiddleware({ mastra })` when you mount routes directly on a Hono, Express, Fastify, or Koa app and still want Mastra auth to run. Set `requiresAuth: false` when you need to reuse the same helper chain on a public route.
10
+
11
+ ```ts
12
+ app.get('/custom/protected', createAuthMiddleware({ mastra }), handler);
13
+ ```
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [[`7dbd611`](https://github.com/mastra-ai/mastra/commit/7dbd611a85cb1e0c0a1581c57564268cb183d86e), [`8a738d8`](https://github.com/mastra-ai/mastra/commit/8a738d844772e14be381d54b22fd5d548ee4af42), [`41aee84`](https://github.com/mastra-ai/mastra/commit/41aee84561ceebe28bad1ecba8702d92838f67f0)]:
18
+ - @mastra/core@1.16.0-alpha.1
19
+ - @mastra/server@1.16.0-alpha.1
20
+
3
21
  ## 1.3.7-alpha.0
4
22
 
5
23
  ### Patch Changes
@@ -0,0 +1,8 @@
1
+ import type { Mastra } from '@mastra/core/mastra';
2
+ import type { Middleware } from 'koa';
3
+ export interface KoaAuthMiddlewareOptions {
4
+ mastra: Mastra;
5
+ requiresAuth?: boolean;
6
+ }
7
+ export declare function createAuthMiddleware({ mastra, requiresAuth }: KoaAuthMiddlewareOptions): Middleware;
8
+ //# sourceMappingURL=auth-middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-middleware.d.ts","sourceRoot":"","sources":["../src/auth-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGlD,OAAO,KAAK,EAAW,UAAU,EAAQ,MAAM,KAAK,CAAC;AAErD,MAAM,WAAW,wBAAwB;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAuBD,wBAAgB,oBAAoB,CAAC,EAAE,MAAM,EAAE,YAAmB,EAAE,EAAE,wBAAwB,GAAG,UAAU,CAgD1G"}
package/dist/index.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  var busboy = require('@fastify/busboy');
4
4
  var auth = require('@mastra/server/auth');
5
5
  var serverAdapter = require('@mastra/server/server-adapter');
6
+ var requestContext = require('@mastra/core/request-context');
6
7
 
7
8
  // src/index.ts
8
9
  // @__NO_SIDE_EFFECTS__
@@ -213,6 +214,66 @@ var initializer2 = (inst, issues) => {
213
214
  });
214
215
  };
215
216
  var ZodError = $constructor("ZodError", initializer2);
217
+ function toWebRequest(ctx) {
218
+ const protocol = ctx.protocol || "http";
219
+ const host = ctx.host || "localhost";
220
+ const url = `${protocol}://${host}${ctx.url}`;
221
+ const headers = new Headers();
222
+ for (const [key, value] of Object.entries(ctx.headers)) {
223
+ if (!value) continue;
224
+ if (Array.isArray(value)) {
225
+ value.forEach((v) => headers.append(key, v));
226
+ } else {
227
+ headers.set(key, value);
228
+ }
229
+ }
230
+ return new globalThis.Request(url, {
231
+ method: ctx.method,
232
+ headers
233
+ });
234
+ }
235
+ function createAuthMiddleware({ mastra, requiresAuth = true }) {
236
+ return async (ctx, next) => {
237
+ if (!requiresAuth) {
238
+ await next();
239
+ return;
240
+ }
241
+ const authConfig = mastra.getServer()?.auth;
242
+ if (!authConfig) {
243
+ await next();
244
+ return;
245
+ }
246
+ ctx.state.requestContext ??= new requestContext.RequestContext();
247
+ ctx.state.mastra ??= mastra;
248
+ const path = String(ctx.path || "/");
249
+ const method = String(ctx.method || "GET");
250
+ const customRouteAuthConfig = new Map(ctx.state.customRouteAuthConfig ?? []);
251
+ customRouteAuthConfig.set(`${method}:${path}`, true);
252
+ const authHeader = ctx.headers.authorization;
253
+ let token = authHeader ? authHeader.replace("Bearer ", "") : null;
254
+ if (!token) {
255
+ token = ctx.query.apiKey || null;
256
+ }
257
+ const result = await auth.coreAuthMiddleware({
258
+ path,
259
+ method,
260
+ getHeader: (name) => ctx.headers[name.toLowerCase()],
261
+ mastra,
262
+ authConfig,
263
+ customRouteAuthConfig,
264
+ requestContext: ctx.state.requestContext,
265
+ rawRequest: toWebRequest(ctx),
266
+ token,
267
+ buildAuthorizeContext: () => toWebRequest(ctx)
268
+ });
269
+ if (result.action === "next") {
270
+ await next();
271
+ return;
272
+ }
273
+ ctx.status = result.status;
274
+ ctx.body = result.body;
275
+ };
276
+ }
216
277
 
217
278
  // src/index.ts
218
279
  var _hasPermissionPromise;
@@ -227,7 +288,7 @@ function loadHasPermission() {
227
288
  }
228
289
  return _hasPermissionPromise;
229
290
  }
230
- function toWebRequest(ctx) {
291
+ function toWebRequest2(ctx) {
231
292
  const protocol = ctx.protocol || "http";
232
293
  const host = ctx.host || "localhost";
233
294
  const url = `${protocol}://${host}${ctx.url}`;
@@ -608,8 +669,8 @@ var MastraServer = class extends serverAdapter.MastraServer {
608
669
  getHeader: (name) => ctx.headers[name.toLowerCase()],
609
670
  getQuery: (name) => ctx.query[name],
610
671
  requestContext: ctx.state.requestContext,
611
- request: toWebRequest(ctx),
612
- buildAuthorizeContext: () => toWebRequest(ctx)
672
+ request: toWebRequest2(ctx),
673
+ buildAuthorizeContext: () => toWebRequest2(ctx)
613
674
  });
614
675
  if (authError) {
615
676
  ctx.status = authError.status;
@@ -775,8 +836,8 @@ var MastraServer = class extends serverAdapter.MastraServer {
775
836
  getHeader: (name) => ctx.headers[name.toLowerCase()],
776
837
  getQuery: (name) => ctx.query[name],
777
838
  requestContext: ctx.state.requestContext,
778
- request: toWebRequest(ctx),
779
- buildAuthorizeContext: () => toWebRequest(ctx)
839
+ request: toWebRequest2(ctx),
840
+ buildAuthorizeContext: () => toWebRequest2(ctx)
780
841
  });
781
842
  if (authError) {
782
843
  ctx.status = authError.status;
@@ -865,5 +926,6 @@ var MastraServer = class extends serverAdapter.MastraServer {
865
926
  };
866
927
 
867
928
  exports.MastraServer = MastraServer;
929
+ exports.createAuthMiddleware = createAuthMiddleware;
868
930
  //# sourceMappingURL=index.cjs.map
869
931
  //# sourceMappingURL=index.cjs.map