This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #497 from jluiz20/added_auth0_prompt_custom_text
Add auth0 prompt custom text
- Loading branch information
Showing
6 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package auth0 | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/structure" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
|
||
"gopkg.in/auth0.v5/management" | ||
) | ||
|
||
var ( | ||
availablePrompts = []string{ | ||
"login", "login-id", "login-password", "login-email-verification", "signup", "signup-id", "signup-password", | ||
"reset-password", "consent", "mfa-push", "mfa-otp", "mfa-voice", "mfa-phone", "mfa-webauthn", "mfa-sms", | ||
"mfa-email", "mfa-recovery-code", "mfa", "status", "device-flow", "email-verification", "email-otp-challenge", | ||
"organizations", "invitation", "common", | ||
} | ||
availableLanguages = []string{ | ||
"ar", "bg", "bs", "cs", "da", "de", "el", "en", "es", "et", "fi", "fr", "fr-CA", "fr-FR", "he", "hi", "hr", | ||
"hu", "id", "is", "it", "ja", "ko", "lt", "lv", "nb", "nl", "pl", "pt", "pt-BR", "pt-PT", "ro", "ru", "sk", | ||
"sl", "sr", "sv", "th", "tr", "uk", "vi", "zh-CN", "zh-TW", | ||
} | ||
errEmptyPromptCustomTextID = fmt.Errorf("ID cannot be empty") | ||
errInvalidPromptCustomTextIDFormat = fmt.Errorf("ID must be formated as prompt:language") | ||
) | ||
|
||
func newPromptCustomText() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: createPromptCustomText, | ||
Read: readPromptCustomText, | ||
Update: updatePromptCustomText, | ||
Delete: deletePromptCustomText, | ||
Importer: &schema.ResourceImporter{ | ||
State: importPromptCustomText, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"prompt": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice(availablePrompts, false), | ||
}, | ||
"language": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice(availableLanguages, false), | ||
}, | ||
"body": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringIsJSON, | ||
DiffSuppressFunc: structure.SuppressJsonDiff, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func importPromptCustomText(d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { | ||
prompt, language, err := getPromptAndLanguage(d) | ||
if err != nil { | ||
return []*schema.ResourceData{}, err | ||
} | ||
|
||
d.SetId(d.Id()) | ||
d.Set("prompt", prompt) | ||
d.Set("language", language) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
func createPromptCustomText(d *schema.ResourceData, m interface{}) error { | ||
d.SetId(d.Get("prompt").(string) + ":" + d.Get("language").(string)) | ||
return updatePromptCustomText(d, m) | ||
} | ||
|
||
func readPromptCustomText(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*management.Management) | ||
customText, err := api.Prompt.CustomText(d.Get("prompt").(string), d.Get("language").(string)) | ||
if err != nil { | ||
if mErr, ok := err.(management.Error); ok { | ||
if mErr.Status() == http.StatusNotFound { | ||
d.SetId("") | ||
return nil | ||
} | ||
} | ||
return err | ||
} | ||
|
||
body, err := marshalCustomTextBody(customText) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.Set("body", body) | ||
return nil | ||
} | ||
|
||
func updatePromptCustomText(d *schema.ResourceData, m interface{}) error { | ||
api := m.(*management.Management) | ||
prompt, language, err := getPromptAndLanguage(d) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if d.Get("body").(string) != "" { | ||
var body map[string]interface{} | ||
if err := json.Unmarshal([]byte(d.Get("body").(string)), &body); err != nil { | ||
return err | ||
} | ||
|
||
err := api.Prompt.SetCustomText(prompt, language, body) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return readPromptCustomText(d, m) | ||
} | ||
|
||
func deletePromptCustomText(d *schema.ResourceData, m interface{}) error { | ||
d.Set("body", "{}") | ||
|
||
if err := updatePromptCustomText(d, m); err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func getPromptAndLanguage(d *schema.ResourceData) (string, string, error) { | ||
rawID := d.Id() | ||
if rawID == "" { | ||
return "", "", errEmptyPromptCustomTextID | ||
} | ||
|
||
if !strings.Contains(rawID, ":") { | ||
return "", "", errInvalidPromptCustomTextIDFormat | ||
} | ||
|
||
idPair := strings.Split(rawID, ":") | ||
if len(idPair) != 2 { | ||
return "", "", errInvalidPromptCustomTextIDFormat | ||
} | ||
|
||
return idPair[0], idPair[1], nil | ||
} | ||
|
||
func marshalCustomTextBody(b map[string]interface{}) (string, error) { | ||
bodyBytes, err := json.Marshal(b) | ||
if err != nil { | ||
return "", fmt.Errorf("Failed to serialize the custom texts to JSON: %w", err) | ||
} | ||
|
||
var buffer bytes.Buffer | ||
const jsonIndentation = " " | ||
if err := json.Indent(&buffer, bodyBytes, "", jsonIndentation); err != nil { | ||
return "", fmt.Errorf("Failed to format the custom texts JSON: %w", err) | ||
} | ||
|
||
return buffer.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package auth0 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccPromptCustomText(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
Providers: map[string]terraform.ResourceProvider{ | ||
"auth0": Provider(), | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccPromptCustomTextCreate, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("auth0_prompt_custom_text.prompt_custom_text", "prompt", "login"), | ||
resource.TestCheckResourceAttr("auth0_prompt_custom_text.prompt_custom_text", "language", "en"), | ||
resource.TestCheckResourceAttr( | ||
"auth0_prompt_custom_text.prompt_custom_text", | ||
"body", | ||
"{\n \"login\": {\n \"alertListTitle\": \"Alerts\",\n \"buttonText\": \"Continue\",\n \"emailPlaceholder\": \"Email address\"\n }\n}", | ||
), | ||
), | ||
}, | ||
{ | ||
Config: testAccPromptCustomTextUpdate, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("auth0_prompt_custom_text.prompt_custom_text", "prompt", "login"), | ||
resource.TestCheckResourceAttr("auth0_prompt_custom_text.prompt_custom_text", "language", "en"), | ||
resource.TestCheckResourceAttr( | ||
"auth0_prompt_custom_text.prompt_custom_text", | ||
"body", | ||
"{\n \"login\": {\n \"alertListTitle\": \"Alerts\",\n \"buttonText\": \"Proceed\",\n \"emailPlaceholder\": \"Email Address\"\n }\n}", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccPromptCustomTextCreate = ` | ||
resource "auth0_prompt_custom_text" "prompt_custom_text" { | ||
prompt = "login" | ||
language = "en" | ||
body = jsonencode( | ||
{ | ||
"login" : { | ||
"alertListTitle" : "Alerts", | ||
"buttonText" : "Continue", | ||
"emailPlaceholder" : "Email address" | ||
} | ||
} | ||
) | ||
} | ||
` | ||
|
||
const testAccPromptCustomTextUpdate = ` | ||
resource "auth0_prompt_custom_text" "prompt_custom_text" { | ||
prompt = "login" | ||
language = "en" | ||
body = jsonencode( | ||
{ | ||
"login" : { | ||
"alertListTitle" : "Alerts", | ||
"buttonText" : "Proceed", | ||
"emailPlaceholder" : "Email Address" | ||
} | ||
} | ||
) | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
layout: "auth0" | ||
page_title: "Auth0: auth0_prompt_custom_text" | ||
description: |- | ||
With this resource, you can manage custom texts on your Auth0 prompts. | ||
--- | ||
|
||
# auth0_prompt_custom_text | ||
|
||
With this resource, you can manage custom text on your Auth0 prompts. You can read more about custom texts | ||
[here](https://auth0.com/docs/customize/universal-login-pages/customize-login-text-prompts). | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "auth0_prompt_custom_text" "example" { | ||
prompt = "login" | ||
language = "en" | ||
body = jsonencode( | ||
{ | ||
"login" : { | ||
"alertListTitle" : "Alerts", | ||
"buttonText" : "Continue", | ||
"description" : "Login to", | ||
"editEmailText" : "Edit", | ||
"emailPlaceholder" : "Email address", | ||
"federatedConnectionButtonText" : "Continue with ${connectionName}", | ||
"footerLinkText" : "Sign up", | ||
"footerText" : "Don't have an account?", | ||
"forgotPasswordText" : "Forgot password?", | ||
"invitationDescription" : "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", | ||
"invitationTitle" : "You've Been Invited!", | ||
"logoAltText" : "${companyName}", | ||
"pageTitle" : "Log in | ${clientName}", | ||
"passwordPlaceholder" : "Password", | ||
"separatorText" : "Or", | ||
"signupActionLinkText" : "${footerLinkText}", | ||
"signupActionText" : "${footerText}", | ||
"title" : "Welcome", | ||
"usernamePlaceholder" : "Username or email address" | ||
} | ||
} | ||
) | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `prompt` - (Required) The term `prompt` is used to refer to a specific step in the login flow. Options include `login`, `login-id`, `login-password`, `login-email-verification`, `signup`, `signup-id`, `signup-password`, `reset-password`, `consent`, `mfa-push`, `mfa-otp`, `mfa-voice`, `mfa-phone`, `mfa-webauthn`, `mfa-sms`, `mfa-email`, `mfa-recovery-code`, `mfa`, `status`, `device-flow`, `email-verification`, `email-otp-challenge`, `organizations`, `invitation`, `common` | ||
* `language` - (Required) Language of the custom text. Options include `ar`, `bg`, `bs`, `cs`, `da`, `de`, `el`, `en`, `es`, `et`, `fi`, `fr`, `fr-CA`, `fr-FR`, `he`, `hi`, `hr`, `hu`, `id`, `is`, `it`, `ja`, `ko`, `lt`, `lv`, `nb`, `nl`, `pl`, `pt`, `pt-BR`, `pt-PT`, `ro`, `ru`, `sk`, `sl`, `sr`, `sv`, `th`, `tr`, `uk`, `vi`, `zh-CN`, `zh-TW` | ||
* `body` - (Required) 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) | ||
|
||
## Import | ||
|
||
auth0_prompt_custom_text can be imported using the import command and specifying the prompt and language separated | ||
by *:* , e.g. | ||
|
||
```terminal | ||
terraform import auth0_prompt_custom_text.example login:en | ||
``` |