贝利信息

Auth0 SPA 应用启用 MFA 的 Terraform 配置指南

日期:2025-12-29 00:00 / 作者:聖光之護

在 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
  ]
}

⚠️ 注意事项:

? 补充说明:该 URI 在 Auth0 官方文档 中明确列为“MFA Grant Type”,是唯一被 Management API 接受的有效值。手动在控制台勾选“Enable MFA”实际等价于在后台自动注入该 URI 到 grant_types 列表中 —— Terraform 必须显式声明这一标准值,无法依赖 UI 的隐式映射。

完成配置后,运行 terraform apply 即可成功创建支持 MFA 的 SPA 应用,后续可结合 auth0_rule 或 Auth0 Actions 实现 MFA 触发逻辑(例如:对特定用户组或高风险登录强制 MFA)。