> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Simple Auth `SimpleAuth` 类通过基础的令牌到用户映射提供基于令牌的身份验证。它包含在 `@mastra/core/server` 中,适用于开发、测试和基础 API 密钥身份验证场景。 ## 使用场景 - 本地开发和测试 - 简单的 API 密钥身份验证 - 集成完整身份 Provider 之前的原型设计 - 使用静态令牌的内部服务 ## 安装 `SimpleAuth` 包含在 `@mastra/core` 中,无需安装其他包。 ```typescript import { SimpleAuth } from '@mastra/core/server' ``` ## 用法示例 ```typescript import { Mastra } from '@mastra/core' import { SimpleAuth } from '@mastra/core/server' // Define your user type type User = { id: string name: string role: 'admin' | 'user' } export const mastra = new Mastra({ server: { auth: new SimpleAuth({ tokens: { 'sk-admin-token-123': { id: 'user-1', name: 'Admin User', role: 'admin', }, 'sk-user-token-456': { id: 'user-2', name: 'Regular User', role: 'user', }, }, }), }, }) ``` ## 配置选项 | 选项 | 类型 | 必填 | 描述 | | --------------- | ---------------------------- | -- | ------------------- | | `tokens` | `Record` | 是 | 令牌到用户对象的映射 | | `headers` | `string \| string[]` | 否 | 要检查令牌的其他标头 | | `name` | `string` | 否 | 用于日志记录的 Provider 名称 | | `authorizeUser` | `(user, request) => boolean` | 否 | 自定义授权函数 | | `protected` | `(RegExp \| string)[]` | 否 | 需要身份验证的路径 | | `public` | `(RegExp \| string)[]` | 否 | 绕过身份验证的路径 | ### 默认标头 SimpleAuth 默认检查以下标头: - `Authorization`(带或不带 `Bearer` 前缀) - `X-Playground-Access` 使用 `headers` 选项添加自定义标头: ```typescript new SimpleAuth({ tokens: {/* ... */}, headers: ['X-API-Key', 'X-Custom-Auth'], }) ``` ## 发出经过身份验证的请求 在 `Authorization` 标头中包含令牌: ```bash curl -X POST http://localhost:4111/api/agents/myAgent/generate \ -H "Content-Type: application/json" \ -H "Authorization: Bearer sk-admin-token-123" \ -d '{"messages": "Hello"}' ``` 也可以不使用 `Bearer` 前缀: ```bash curl -X POST http://localhost:4111/api/agents/myAgent/generate \ -H "Content-Type: application/json" \ -H "Authorization: sk-admin-token-123" \ -d '{"messages": "Hello"}' ``` ## 自定义授权 添加基于角色或自定义的授权逻辑: ```typescript new SimpleAuth({ tokens: { 'sk-admin-token': { id: '1', name: 'Admin', role: 'admin' }, 'sk-user-token': { id: '2', name: 'User', role: 'user' }, }, authorizeUser: (user, request) => { // Only admins can access /admin routes if (request.url.includes('/admin')) { return user.role === 'admin' } return true }, }) ``` ## 环境变量 对于类似生产环境的设置,请从环境变量加载令牌: ```typescript const tokens: Record = {} // Load from environment const adminToken = process.env.ADMIN_API_KEY if (adminToken) { tokens[adminToken] = { id: 'admin', name: 'Admin', role: 'admin' } } const userToken = process.env.USER_API_KEY if (userToken) { tokens[userToken] = { id: 'user', name: 'User', role: 'user' } } export const mastra = new Mastra({ server: { auth: new SimpleAuth({ tokens }), }, }) ``` ## 与 `MastraClient` 配合使用 使用令牌配置客户端: ```typescript import { MastraClient } from '@mastra/client-js' const client = new MastraClient({ baseUrl: 'http://localhost:4111', headers: { Authorization: 'Bearer sk-admin-token-123', }, }) const agent = client.getAgent('myAgent') const response = await agent.generate('Hello') ``` ## 限制 SimpleAuth 以简单易用为设计目标,并非为生产环境安全而设计: - 令牌存储在内存中 - 不支持令牌过期或刷新 - 不进行加密验证 - 所有令牌都必须在启动时已知 对于生产应用,请考虑使用 [JWT](https://mastra.zisheng.pro/docs/server/auth/jwt)、[Clerk](https://mastra.zisheng.pro/docs/server/auth/clerk)、[Auth0](https://mastra.zisheng.pro/docs/server/auth/auth0) 或其他身份 Provider。