From 464d597c04b538f78bc91f2ff3f5ce1d5eed8b50 Mon Sep 17 00:00:00 2001 From: Rajat Bajaj Date: Mon, 2 Sep 2024 21:00:20 +0530 Subject: [PATCH 1/2] Fetch list of languages from CDN instead of hardcoding them --- docs/resources/prompt_custom_text.md | 2 +- internal/auth0/prompt/resource_custom_text.go | 49 +++++++++++++++++-- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/docs/resources/prompt_custom_text.md b/docs/resources/prompt_custom_text.md index 23993882..f444c923 100644 --- a/docs/resources/prompt_custom_text.md +++ b/docs/resources/prompt_custom_text.md @@ -48,7 +48,7 @@ resource "auth0_prompt_custom_text" "example" { ### Required - `body` (String) JSON containing the custom texts. You can check the options for each prompt [here](https://auth0.com/docs/customize/universal-login-pages/customize-login-text-prompts#prompt-values). -- `language` (String) Language of the custom text. Options include: `ar`, `bg`, `bs`, `ca-ES`, `cs`, `cy`, `da`, `de`, `el`, `en`, `es`, `et`, `eu-ES`, `fi`, `fr`, `fr-CA`, `fr-FR`, `gl-ES`, `he`, `hi`, `hr`, `hu`, `id`, `is`, `it`, `ja`, `ko`, `lt`, `lv`, `nb`, `nl`, `nn`, `no`, `pl`, `pt`, `pt-BR`, `pt-PT`, `ro`, `ru`, `sk`, `sl`, `sr`, `sv`, `th`, `tr`, `uk`, `vi`, `zh-CN`, `zh-TW`. +- `language` (String) Language of the custom text. Options include: `ar`, `ar-EG`, `ar-SA`, `az`, `bg`, `bs`, `ca-ES`, `cs`, `cy`, `da`, `de`, `el`, `en`, `es`, `es-AR`, `es-MX`, `et`, `eu-ES`, `fa`, `fi`, `fr`, `fr-CA`, `fr-FR`, `gl-ES`, `he`, `hi`, `hr`, `hu`, `hy`, `id`, `is`, `it`, `ja`, `ko`, `lt`, `lv`, `ms`, `nb`, `nl`, `nn`, `no`, `pl`, `pt`, `pt-BR`, `pt-PT`, `ro`, `ru`, `sk`, `sl`, `sq`, `sr`, `sv`, `th`, `tl`, `tr`, `uk`, `ur`, `vi`, `zh-CN`, `zh-HK`, `zh-TW`. - `prompt` (String) The term `prompt` is used to refer to a specific step in the login flow. Options include: `captcha`, `common`, `consent`, `custom-form`, `customized-consent`, `device-flow`, `email-otp-challenge`, `email-verification`, `invitation`, `login`, `login-email-verification`, `login-id`, `login-password`, `login-passwordless`, `logout`, `mfa`, `mfa-email`, `mfa-otp`, `mfa-phone`, `mfa-push`, `mfa-recovery-code`, `mfa-sms`, `mfa-voice`, `mfa-webauthn`, `organizations`, `passkeys`, `phone-identifier-challenge`, `phone-identifier-enrollment`, `reset-password`, `signup`, `signup-id`, `signup-password`, `status`. ### Read-Only diff --git a/internal/auth0/prompt/resource_custom_text.go b/internal/auth0/prompt/resource_custom_text.go index 9054fd8a..54454b42 100644 --- a/internal/auth0/prompt/resource_custom_text.go +++ b/internal/auth0/prompt/resource_custom_text.go @@ -3,7 +3,10 @@ package prompt import ( "context" "encoding/json" + "io" + "net/http" "strings" + "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -15,6 +18,45 @@ import ( internalSchema "github.com/auth0/terraform-provider-auth0/internal/schema" ) +const languagesURL = "https://cdn.auth0.com/ulp/react-components/development/languages/available-languages.json" + +func fetchLanguages() []string { + + fallbackAvailableLanguages := []string{ + "ar", "bg", "bs", "ca-ES", "cs", "cy", "da", "de", "el", "en", "es", "et", "eu-ES", "fi", "fr", "fr-CA", "fr-FR", "gl-ES", "he", "hi", "hr", + "hu", "id", "is", "it", "ja", "ko", "lt", "lv", "nb", "nl", "nn", "no", "pl", "pt", "pt-BR", "pt-PT", "ro", "ru", "sk", + "sl", "sr", "sv", "th", "tr", "uk", "vi", "zh-CN", "zh-TW", + } + + client := http.Client{ + Timeout: 10 * time.Second, // Set a timeout for the HTTP request + } + + resp, err := client.Get(languagesURL) + if err != nil { + return fallbackAvailableLanguages + } + defer func(Body io.ReadCloser) { + err = Body.Close() + }(resp.Body) + + if resp.StatusCode != http.StatusOK { + return fallbackAvailableLanguages + } + + var retrievedLanguages []string + decoder := json.NewDecoder(resp.Body) + if err := decoder.Decode(&retrievedLanguages); err != nil { + return fallbackAvailableLanguages + } + + if len(retrievedLanguages) == 0 { + return fallbackAvailableLanguages + } + + return retrievedLanguages +} + var ( availablePrompts = []string{ "captcha", "common", "consent", "custom-form", "customized-consent", "device-flow", "email-otp-challenge", @@ -23,11 +65,8 @@ var ( "mfa-sms", "mfa-voice", "mfa-webauthn", "organizations", "passkeys", "phone-identifier-challenge", "phone-identifier-enrollment", "reset-password", "signup", "signup-id", "signup-password", "status", } - availableLanguages = []string{ - "ar", "bg", "bs", "ca-ES", "cs", "cy", "da", "de", "el", "en", "es", "et", "eu-ES", "fi", "fr", "fr-CA", "fr-FR", "gl-ES", "he", "hi", "hr", - "hu", "id", "is", "it", "ja", "ko", "lt", "lv", "nb", "nl", "nn", "no", "pl", "pt", "pt-BR", "pt-PT", "ro", "ru", "sk", - "sl", "sr", "sv", "th", "tr", "uk", "vi", "zh-CN", "zh-TW", - } + + availableLanguages = fetchLanguages() ) // NewCustomTextResource will return a new auth0_prompt_custom_text resource. From 48e40849f3d0787ec0a1b282d10604cc1a77a315 Mon Sep 17 00:00:00 2001 From: Rajat Bajaj Date: Tue, 3 Sep 2024 12:02:52 +0530 Subject: [PATCH 2/2] Updated linting --- internal/auth0/prompt/resource_custom_text.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/auth0/prompt/resource_custom_text.go b/internal/auth0/prompt/resource_custom_text.go index 54454b42..0fda6531 100644 --- a/internal/auth0/prompt/resource_custom_text.go +++ b/internal/auth0/prompt/resource_custom_text.go @@ -21,7 +21,6 @@ import ( const languagesURL = "https://cdn.auth0.com/ulp/react-components/development/languages/available-languages.json" func fetchLanguages() []string { - fallbackAvailableLanguages := []string{ "ar", "bg", "bs", "ca-ES", "cs", "cy", "da", "de", "el", "en", "es", "et", "eu-ES", "fi", "fr", "fr-CA", "fr-FR", "gl-ES", "he", "hi", "hr", "hu", "id", "is", "it", "ja", "ko", "lt", "lv", "nb", "nl", "nn", "no", "pl", "pt", "pt-BR", "pt-PT", "ro", "ru", "sk", @@ -29,7 +28,7 @@ func fetchLanguages() []string { } client := http.Client{ - Timeout: 10 * time.Second, // Set a timeout for the HTTP request + Timeout: 10 * time.Second, // Set a timeout for the HTTP request. } resp, err := client.Get(languagesURL)