MastraAuthAuth0 类
🌐 MastraAuthAuth0 Class
MastraAuthAuth0 类为 Mastra 提供使用 Auth0 的身份验证。它使用 Auth0 签发的 JWT 令牌验证传入请求,并通过 auth 选项与 Mastra 服务器集成。
🌐 The MastraAuthAuth0 class provides authentication for Mastra using Auth0. It verifies incoming requests using Auth0-issued JWT tokens and integrates with the Mastra server using the auth option.
使用示例Direct link to 使用示例
🌐 Usage example
import { Mastra } from "@mastra/core";
import { MastraAuthAuth0 } from "@mastra/auth-auth0";
export const mastra = new Mastra({
server: {
auth: new MastraAuthAuth0({
domain: process.env.AUTH0_DOMAIN,
audience: process.env.AUTH0_AUDIENCE,
}),
},
});
注意: 如果你已经设置了适当命名的环境变量(
AUTH0_DOMAIN和AUTH0_AUDIENCE),可以省略构造函数参数。在这种情况下,只需使用new MastraAuthAuth0()而无需任何参数。
构造函数参数Direct link to 构造函数参数
🌐 Constructor parameters
domain?:
audience?:
name?:
环境变量Direct link to 环境变量
🌐 Environment Variables
当没有提供构造函数选项时,会自动使用以下环境变量:
🌐 The following environment variables are automatically used when constructor options are not provided:
AUTH0_DOMAIN?:
AUTH0_AUDIENCE?:
默认授权行为Direct link to 默认授权行为
🌐 Default Authorization Behavior
默认情况下,MastraAuthAuth0 验证 Auth0 JWT 令牌并允许所有经过身份验证的用户访问:
🌐 By default, MastraAuthAuth0 validates Auth0 JWT tokens and allows access to all authenticated users:
- 令牌验证:JWT 令牌使用 Auth0 的公钥(JWKS)进行验证
- 签名验证:确保令牌由你的 Auth0 租户签署
- 过期检查:验证令牌是否未过期
- 受众验证:确认该令牌是针对你特定的 API(受众)颁发的
- 发行者验证:确保令牌是由你的 Auth0 域签发的
如果所有验证都通过,用户被视为已授权。要实现自定义授权逻辑(例如基于角色的访问控制),请提供自定义的 authorizeUser 函数。
🌐 If all validations pass, the user is considered authorized. To implement custom authorization logic (e.g., role-based access control), provide a custom authorizeUser function.
Auth0 用户类型Direct link to Auth0 用户类型
🌐 Auth0 User Type
authorizeUser 函数中使用的 Auth0User 类型对应解码后的 JWT 令牌负载,通常包括:
🌐 The Auth0User type used in the authorizeUser function corresponds to the decoded JWT token payload, which typically includes:
sub:用户的唯一标识符(主题)email:用户的电子邮件地址(如果令牌中包含)email_verified:电子邮件是否已验证name:用户的显示名称(如果有的话)picture:用户头像的 URL(如果可用)iss:令牌发行者(你的 Auth0 域名)aud:令牌受众(你的 API 标识符)iat:令牌在时间戳处发出exp:令牌过期时间戳scope:该令牌的授权范围- 在你的 Auth0 租户中配置的自定义声明和应用元数据
可用的确切属性取决于你的 Auth0 配置、请求的权限范围以及你配置的任何自定义声明。
🌐 The exact properties available depend on your Auth0 configuration, scopes requested, and any custom claims you've configured.
相关Direct link to 相关
🌐 Related