0
0

Auth0 のアプリケーションをTerraformから作る

Last updated at Posted at 2023-12-01

事前準備

Auth0にあるアプリケーションでアクセス権を付与しておく。
TerraformでAuth0のProviderを使うにはAuth0のアプリケーションの認証情報を使ってAuth0のTerraformプロバイダーを作る。
公式ドキュメントをみるとauth0が出しているものと個人が作成していたようにみえるものがあるので注意。後者のレポジトリはアーカイブされているため、使用する際にはAuth0の公式ドキュメントかを確認してください。

Terraformのファイルたち

provider.tf
terraform {
  required_version = "= 1.6.2"
  required_providers {
    auth0 = {
      source  = "auth0/auth0"
      version = ">= 1.0.0"
    }
  }
}

provider "auth0" {
  domain        = var.domain
  client_id     = var.client_id
  client_secret = var.client_secret
}

CallBack URL、OIDC Conformant、JWT Signature Algorithmの設定を忘れずに行う必要がありました。
手動でアプリケーションを作成した際:OIDC Conformantは有効化されていました。JWT Signature AlgorithmはRS256でした。
しかし、Terraformを使用してアプリケーションを作成すると、OIDC Conformantは無効、JWT Signature AlgorithmはHS256として設定されていました。そのため、これらの設定をTerraform側で明示的に指定する必要があります。

main.tf
resource "auth0_client" "application" {
  allowed_logout_urls = var.logout_urls
  app_type            = var.client_app_type
  callbacks           = [for domain in var.callback_domains : "${domain}${var.callback_path}"]
  description         = var.client_description
  jwt_configuration {
    alg                 = var.jwt_alg
    lifetime_in_seconds = var.jwt_lifetime_in_seconds
  }
  name            = "${var.client_name}-${var.pre_fix}"
  oidc_conformant = var.oidc_conformant
}

変数化したいものがあれば別途定義しておきます。

variables.tf
variable "callback_domains" {
  description = "List of callback domains"
  type        = list(string)
  default     = ["http://127.0.0.1:8080", "http://localhost:8080", "http://127.0.0.1:5000", "http://localhost:5000"]
}

variable "callback_path" {
  description = "Callback path"
  type        = string
  default     = "/callback"
}

variable "client_app_type" {
  description = "The type of the Auth0 client app"
  type        = string
  default     = "regular_web"
}

variable "client_description" {
  description = "The description of the Auth0 client"
  type        = string
  default     = "sample app"
}

variable "client_name" {
  description = "The name of the Auth0 client"
  type        = string
  default     = "sample client"
}

variable "jwt_alg" {
  description = "The algorithm used to sign the JWT"
  type        = string
  default     = "RS256"
}

variable "jwt_lifetime_in_seconds" {
  description = "The lifetime of the JWT in seconds"
  type        = number
  default     = 36000
}

variable "logout_urls" {
  description = "List of allowed logout URLs"
  type        = list(string)
  default     = ["http://127.0.0.1:8080/home", "http://localhost:8080/home", "http://127.0.0.1:5000/home", "http://localhost:5000/home"]
}

variable "oidc_conformant" {
  description = "Specify if the client is OIDC Conformant"
  type        = bool
  default     = true
}

variable "pre_fix" {
  description = "Prefix for the Auth0 client"
  type        = string
  default     = "test"
}

あとはアプリケーションを作成して認証します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0