UNPKG

@mastra/core

Version:

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

174 lines (168 loc) • 5.46 kB
'use strict'; var chunk5PSD3IKG_cjs = require('../chunk-5PSD3IKG.cjs'); var chunk5NTO7S5I_cjs = require('../chunk-5NTO7S5I.cjs'); var chunk6VOPKVYH_cjs = require('../chunk-6VOPKVYH.cjs'); var chunkEBVYYC2Q_cjs = require('../chunk-EBVYYC2Q.cjs'); // src/server/auth.ts var _MastraAuthProvider_decorators, _init, _a; _MastraAuthProvider_decorators = [chunk5PSD3IKG_cjs.InstrumentClass({ prefix: "auth", excludeMethods: ["__setTools", "__setLogger", "__setTelemetry", "#log"] })]; exports.MastraAuthProvider = class MastraAuthProvider extends (_a = chunk6VOPKVYH_cjs.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; } } }; exports.MastraAuthProvider = /*@__PURE__*/(_ => { _init = chunkEBVYYC2Q_cjs.__decoratorStart(_a); exports.MastraAuthProvider = chunkEBVYYC2Q_cjs.__decorateElement(_init, 0, "MastraAuthProvider", _MastraAuthProvider_decorators, exports.MastraAuthProvider); chunkEBVYYC2Q_cjs.__runInitializers(_init, 1, exports.MastraAuthProvider); // src/server/composite-auth.ts return exports.MastraAuthProvider; })(); // src/server/composite-auth.ts var CompositeAuth = class extends exports.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 exports.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 chunk5NTO7S5I_cjs.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 chunk5NTO7S5I_cjs.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 chunk5NTO7S5I_cjs.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 chunk5NTO7S5I_cjs.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; } exports.CompositeAuth = CompositeAuth; exports.SimpleAuth = SimpleAuth; exports.defineAuth = defineAuth; exports.registerApiRoute = registerApiRoute; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map