
在 terraform 中为 auth0 单页应用(spa)启用 mfa 时,需使用 auth0 官方定义的标准化 grant type uri(`http://auth0.com/oauth/grant-type/mfa-otp`),而非简写的 `"mfa"`,否则将触发 400 错误。
Auth0 对多因素认证(MFA)授权类型的命名遵循 OAuth 扩展规范,不支持任意字符串如 "mfa" 作为 grant_types 值。Terraform 的 auth0_client 资源在调用 Auth0 Management API 创建应用时,会严格校验 grant type 格式。若传入非法值(如 "mfa"),API 将直接返回 400 Bad Request: Invalid grant types: mfa —— 这正是你遇到的错误。
✅ 正确做法是使用 Auth0 官方注册的、带命名空间的 URI:
resource "auth0_client" "auth0_ui_client" {
name = "auth0_ui_client"
app_type = "spa"
callbacks = ["http://localhost:3000/callback"]
allowed_logout_urls = ["http://localhost:3000/logout"]
allowed_origins = ["http://localhost:3000"]
grant_types = [
"authorization_code",
"implicit",
"refresh_token",
"http://auth0.com/oauth/grant-type/mfa-otp" # ✅ 唯一合法的 MFA grant type
]
}⚠️ 注意事项:
- http://auth0.com/oauth/grant-type/mfa-otp 是专用于基于一次性密码(OTP)的 MFA 流程(如 TOTP、SMS、Email)的标准 grant type;若使用 WebAuthn 或 Push 等其他 MFA 方式,仍需此值(Auth0 统一抽象为同一 grant type)。
- 仅声明该 grant type 不会自动启用 MFA 策cies —— 你还需通过 auth0_mfa 资源或 Auth0 Dashboard 配置 MFA 策略(如强制启用、信任设备等)。
- SPA 类型应用默认禁用 password 和 client_credentials 等敏感 grant type,但 mfa-otp 是显式允许的扩展类型,无需额外开启权限。
- 确保你使用的 auth0 Terraform provider 版本 ≥ 0.38.0(推荐 ≥ 0.45.0),以兼容最新 API 行为。
? 补充说明:该 URI 在 Auth0 官方文档 中明确列为“MFA Grant Type”,是唯一被 Management API 接受的有效值。手动在控制台勾选“Enable MFA”实际等价于在后台自动注入该 URI 到 grant_types 列表中 —— Terraform 必须显式声明这一标准值,无法依赖 UI 的隐式映射。
完成配置后,运行 terraform apply 即可成功创建支持 MFA 的 SPA 应用,后续可结合 auth0_rule 或 Auth0 Actions 实现 MFA 触发逻辑(例如:对特定用户组或高风险登录强制 MFA)。










