UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

169 lines (164 loc) • 5.16 kB
import { InstrumentClass } from '../chunk-DSUKMFZY.js'; import { MastraError } from '../chunk-PZUZNPFM.js'; import { MastraBase } from '../chunk-VQASQG5D.js'; import { __decoratorStart, __decorateElement, __runInitializers } from '../chunk-3HXBPDKN.js'; // src/server/auth.ts var _MastraAuthProvider_decorators, _init, _a; _MastraAuthProvider_decorators = [InstrumentClass({ prefix: "auth", excludeMethods: ["__setTools", "__setLogger", "__setTelemetry", "#log"] })]; var MastraAuthProvider = class extends (_a = MastraBase) { protected; public; constructor(options) { super({ component: "AUTH", name: options?.name }); if (options?.authorizeUser) { this.authorizeUser = options.authorizeUser.bind(this); } this.protected = options?.protected; this.public = options?.public; } registerOptions(opts) { if (opts?.authorizeUser) { this.authorizeUser = opts.authorizeUser.bind(this); } if (opts?.protected) { this.protected = opts.protected; } if (opts?.public) { this.public = opts.public; } } }; MastraAuthProvider = /*@__PURE__*/(_ => { _init = __decoratorStart(_a); MastraAuthProvider = __decorateElement(_init, 0, "MastraAuthProvider", _MastraAuthProvider_decorators, MastraAuthProvider); __runInitializers(_init, 1, MastraAuthProvider); // src/server/composite-auth.ts return MastraAuthProvider; })(); // src/server/composite-auth.ts var CompositeAuth = class extends MastraAuthProvider { providers; constructor(providers) { super(); this.providers = providers; } async authenticateToken(token, request) { for (const provider of this.providers) { try { const user = await provider.authenticateToken(token, request); if (user) { return user; } } catch {} } return null; } async authorizeUser(user, request) { for (const provider of this.providers) { const authorized = await provider.authorizeUser(user, request); if (authorized) { return true; } } return false; } }; // src/server/simple-auth.ts var DEFAULT_HEADERS = ["Authorization", "X-Playground-Access"]; var SimpleAuth = class extends MastraAuthProvider { tokens; headers; users; constructor(options) { super(options); this.tokens = options.tokens; this.users = Object.values(this.tokens); this.headers = [...DEFAULT_HEADERS].concat(options.headers || []); } async authenticateToken(token, request) { const requestTokens = this.getTokensFromHeaders(token, request); for (const requestToken of requestTokens) { const tokenToUser = this.tokens[requestToken]; if (tokenToUser) { return tokenToUser; } } return null; } async authorizeUser(user, _request) { return this.users.includes(user); } stripBearerPrefix(token) { return token.startsWith("Bearer ") ? token.slice(7) : token; } getTokensFromHeaders(token, request) { const tokens = [token]; for (const headerName of this.headers) { const headerValue = request.header(headerName); if (headerValue) { tokens.push(this.stripBearerPrefix(headerValue)); } } return tokens; } }; // src/server/index.ts function validateOptions(path, options) { const opts = options; if (opts.method === void 0) { throw new MastraError({ id: "MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS", text: `Invalid options for route "${path}", missing "method" property`, domain: "MASTRA_SERVER" /* MASTRA_SERVER */, category: "USER" /* USER */ }); } if (opts.handler === void 0 && opts.createHandler === void 0) { throw new MastraError({ id: "MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS", text: `Invalid options for route "${path}", you must define a "handler" or "createHandler" property`, domain: "MASTRA_SERVER" /* MASTRA_SERVER */, category: "USER" /* USER */ }); } if (opts.handler !== void 0 && opts.createHandler !== void 0) { throw new MastraError({ id: "MASTRA_SERVER_API_INVALID_ROUTE_OPTIONS", text: `Invalid options for route "${path}", you can only define one of the following properties: "handler" or "createHandler"`, domain: "MASTRA_SERVER" /* MASTRA_SERVER */, category: "USER" /* USER */ }); } } function registerApiRoute(path, options) { if (path.startsWith("/api/")) { throw new MastraError({ id: "MASTRA_SERVER_API_PATH_RESERVED", text: `Path must not start with "/api", it's reserved for internal API routes`, domain: "MASTRA_SERVER" /* MASTRA_SERVER */, category: "USER" /* USER */ }); } validateOptions(path, options); return { path, method: options.method, handler: options.handler, createHandler: options.createHandler, openapi: options.openapi, middleware: options.middleware, requiresAuth: options.requiresAuth }; } function defineAuth(config) { return config; } export { CompositeAuth, MastraAuthProvider, SimpleAuth, defineAuth, registerApiRoute }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map