Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #497 from jluiz20/added_auth0_prompt_custom_text
Browse files Browse the repository at this point in the history
Add auth0 prompt custom text
  • Loading branch information
sergiught committed Jan 20, 2022
2 parents 8ca0c0b + d550a43 commit b0b1c5f
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ resource/auth0_prompt:
- '**/*prompt.go'
- '**/*prompt_test.go'

resource/auth0_prompt_custom_text:
- '**/*prompt_custom_text.go'
- '**/*prompt_custom_text_test.go'

resource/auth0_resource_server:
- '**/*resource_server.go'
- '**/*resource_server_test.go'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.26.0

ENHANCEMENTS:

* **New Resource:** `auth0_prompt_custom_text` ([#497](https://github.com/alexkappa/terraform-provider-auth0/pull/497))

## 0.25.1

ENHANCEMENTS:
Expand Down
1 change: 1 addition & 0 deletions auth0/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func init() {
"auth0_rule_config": newRuleConfig(),
"auth0_hook": newHook(),
"auth0_prompt": newPrompt(),
"auth0_prompt_custom_text": newPromptCustomText(),
"auth0_email": newEmail(),
"auth0_email_template": newEmailTemplate(),
"auth0_user": newUser(),
Expand Down
167 changes: 167 additions & 0 deletions auth0/resource_auth0_prompt_custom_text.go
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
}
74 changes: 74 additions & 0 deletions auth0/resource_auth0_prompt_custom_text_test.go
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"
}
}
)
}
`
62 changes: 62 additions & 0 deletions docs/resources/prompt_custom_text.md
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
```

0 comments on commit b0b1c5f

Please sign in to comment.