# Provision the Okta OIDC web application, custom authorization server,
# groups scope and claim, access policy, and security groups for Teleport
# Enterprise OIDC SSO.
# Protect Terraform state as a secret because it contains the client secret.

terraform {
  required_version = ">= 1.3"

  required_providers {
    okta = {
      source  = "okta/okta"
      version = ">= 4.6, < 6.0"
    }
  }
}

variable "proxy_public_addr" {
  description = "Public Teleport Proxy hostname with no scheme or path, for example teleport.example.com"
  type        = string
}

variable "app_label" {
  description = "Display name of the Okta OIDC application"
  type        = string
  default     = "Teleport Access"
}

variable "auth_server_name" {
  description = "Name and audience of the Okta custom authorization server"
  type        = string
  default     = "teleport"
}

variable "group_names" {
  description = "Okta group names to create and assign to the application"
  type        = list(string)
  default     = ["teleport-admins", "teleport-developers"]
}

provider "okta" {
  # Configure with OKTA_ORG_NAME, OKTA_BASE_URL, and OKTA_API_TOKEN env vars.
}

resource "okta_group" "groups" {
  for_each = toset(var.group_names)

  name        = each.key
  description = "${var.app_label} access group"
}

resource "okta_auth_server" "this" {
  name      = var.auth_server_name
  audiences = [var.auth_server_name]
}

resource "okta_app_oauth" "this" {
  label                      = var.app_label
  type                       = "web"
  grant_types                = ["authorization_code", "refresh_token"]
  response_types             = ["code"]
  redirect_uris              = ["https://${var.proxy_public_addr}/v1/webapi/oidc/callback"]
  post_logout_redirect_uris  = ["https://${var.proxy_public_addr}/web"]
  token_endpoint_auth_method = "client_secret_basic"
  omit_secret                = false
}

resource "okta_auth_server_scope" "groups" {
  auth_server_id = okta_auth_server.this.id
  name           = "groups"
  description    = "Return Okta group memberships in the groups claim"
}

resource "okta_auth_server_claim" "groups" {
  auth_server_id          = okta_auth_server.this.id
  name                    = "groups"
  always_include_in_token = true
  claim_type              = "IDENTITY"
  value_type              = "GROUPS"
  group_filter_type       = "REGEX"
  value                   = ".*"
}

resource "okta_auth_server_policy" "this" {
  auth_server_id   = okta_auth_server.this.id
  name             = var.auth_server_name
  description      = "Access policy for ${var.app_label}"
  priority         = 1
  client_whitelist = [okta_app_oauth.this.id]
}

resource "okta_auth_server_policy_rule" "default" {
  auth_server_id       = okta_auth_server.this.id
  policy_id            = okta_auth_server_policy.this.id
  name                 = "Default"
  priority             = 1
  grant_type_whitelist = ["authorization_code"]
  scope_whitelist      = ["openid", "profile", "email", "groups"]
  group_whitelist      = ["EVERYONE"]
}

resource "okta_app_group_assignment" "groups" {
  for_each = okta_group.groups

  app_id   = okta_app_oauth.this.id
  group_id = each.value.id
}

output "issuer_url" {
  description = "Custom authorization server Issuer URI for spec.issuer_url"
  value       = okta_auth_server.this.issuer
}

output "client_id" {
  description = "OIDC application Client ID for spec.client_id"
  value       = okta_app_oauth.this.client_id
}

output "client_secret" {
  description = "Client secret value for the Teleport OIDC connector"
  value       = okta_app_oauth.this.client_secret
  sensitive   = true
}

output "group_names" {
  description = "Okta group names created for claims_to_roles"
  value       = sort([for g in okta_group.groups : g.name])
}
