From 858858aac576a6826e249446a3479450db32b63d Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:03:02 +0200 Subject: [PATCH] DXCDT-549: Add logic to save branding data that gets sent by the web app (#872) --- internal/cli/universal_login_customize.go | 60 ++- .../cli/universal_login_customize_test.go | 344 +++++++++--------- 2 files changed, 229 insertions(+), 175 deletions(-) diff --git a/internal/cli/universal_login_customize.go b/internal/cli/universal_login_customize.go index 809386c2d..78b741287 100644 --- a/internal/cli/universal_login_customize.go +++ b/internal/cli/universal_login_customize.go @@ -33,7 +33,7 @@ type ( Template *management.BrandingUniversalLogin `json:"template"` Theme *management.BrandingTheme `json:"theme"` Tenant *tenantData `json:"tenant"` - Prompt *promptData `json:"prompt"` + Prompts []*promptData `json:"prompts"` } tenantData struct { @@ -43,9 +43,9 @@ type ( } promptData struct { - Language string `json:"language"` - Prompt string `json:"prompt"` - CustomText map[string]map[string]interface{} `json:"custom_text,omitempty"` + Language string `json:"language"` + Prompt string `json:"prompt"` + CustomText map[string]interface{} `json:"custom_text,omitempty"` } webSocketHandler struct { @@ -168,7 +168,7 @@ func fetchUniversalLoginBrandingData( EnabledLocales: tenant.GetEnabledLocales(), Domain: tenantDomain, }, - Prompt: prompt, + Prompts: []*promptData{prompt}, }, nil } @@ -269,10 +269,15 @@ func fetchPromptCustomTextWithDefaults( brandingTextTranslations := mergeBrandingTextTranslations(defaultTranslations, customTranslations) + customText := make(map[string]interface{}, 0) + for key, value := range brandingTextTranslations { + customText[key] = value + } + return &promptData{ Language: language, Prompt: promptName, - CustomText: brandingTextTranslations, + CustomText: customText, }, nil } @@ -405,7 +410,16 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { continue } case saveBrandingMessageType: - h.display.Warnf("not yet implemented") + var saveBrandingMsg universalLoginBrandingData + if err := json.Unmarshal(message.Payload, &saveBrandingMsg); err != nil { + h.display.Errorf("failed to unmarshal save branding data payload: %v", err) + continue + } + + if err := saveUniversalLoginBrandingData(r.Context(), h.api, &saveBrandingMsg); err != nil { + h.display.Errorf("failed to save branding data: %v", err) + continue + } } } } @@ -423,3 +437,35 @@ func checkOriginFunc(r *http.Request) bool { return originURL.String() == webAppURL } + +func saveUniversalLoginBrandingData(ctx context.Context, api *auth0.API, data *universalLoginBrandingData) error { + group, ctx := errgroup.WithContext(ctx) + + group.Go(func() (err error) { + return api.Branding.Update(ctx, data.Settings) + }) + + group.Go(func() (err error) { + return api.Branding.SetUniversalLogin(ctx, data.Template) + }) + + group.Go(func() (err error) { + themeID := data.Theme.GetID() + if themeID != "" { + data.Theme.ID = nil + return api.BrandingTheme.Update(ctx, themeID, data.Theme) + } + + return api.BrandingTheme.Create(ctx, data.Theme) + }) + + for _, prompt := range data.Prompts { + prompt := prompt + + group.Go(func() (err error) { + return api.Prompt.SetCustomText(ctx, prompt.Prompt, prompt.Language, prompt.CustomText) + }) + } + + return group.Wait() +} diff --git a/internal/cli/universal_login_customize_test.go b/internal/cli/universal_login_customize_test.go index 5c8fa9ce1..46e0652b6 100644 --- a/internal/cli/universal_login_customize_test.go +++ b/internal/cli/universal_login_customize_test.go @@ -194,48 +194,50 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) { EnabledLocales: []string{"en", "es"}, Domain: "tenant-example.auth0.com", }, - Prompt: &promptData{ - Language: "en", - Prompt: "login", - CustomText: map[string]map[string]interface{}{ - "login": { - "alertListTitle": "Alerts", - "auth0-users-validation": "Something went wrong, please try again later", - "authentication-failure": "We are sorry, something went wrong when attempting to login", - "buttonText": "Continue", - "custom-script-error-code": "Something went wrong, please try again later.", - "description": "Log in to ${companyName} to continue to ${clientName}.", - "editEmailText": "Edit", - "emailPlaceholder": "Email address", - "federatedConnectionButtonText": "Continue with ${connectionName}", - "footerLinkText": "Sign up", - "footerText": "Don't have an account?", - "forgotPasswordText": "Forgot password?", - "hidePasswordText": "Hide password", - "invalid-connection": "Invalid connection", - "invalid-email-format": "Email is not valid.", - "invitationDescription": "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", - "invitationTitle": "You've Been Invited!", - "ip-blocked": "We have detected suspicious login behavior and further attempts will be blocked. Please contact the administrator.", - "logoAltText": "${companyName}", - "no-db-connection": "Invalid connection", - "no-email": "Please enter an email address", - "no-password": "Password is required", - "no-username": "Username is required", - "pageTitle": "Log in | ${clientName}", - "password-breached": "We have detected a potential security issue with this account. To protect your account, we have prevented this login. Please reset your password to proceed.", - "passwordPlaceholder": "Password", - "same-user-login": "Too many login attempts for this user. Please wait, and try again later.", - "separatorText": "Or", - "showPasswordText": "Show password", - "signupActionLinkText": "${footerLinkText}", - "signupActionText": "${footerText}", - "title": "Welcome friend, glad to have you!", - "user-blocked": "Your account has been blocked after multiple consecutive login attempts.", - "usernameOnlyPlaceholder": "Username", - "usernamePlaceholder": "Username or email address", - "wrong-credentials": "Wrong username or password", - "wrong-email-credentials": "Wrong email or password", + Prompts: []*promptData{ + { + Language: "en", + Prompt: "login", + CustomText: map[string]interface{}{ + "login": map[string]interface{}{ + "alertListTitle": "Alerts", + "auth0-users-validation": "Something went wrong, please try again later", + "authentication-failure": "We are sorry, something went wrong when attempting to login", + "buttonText": "Continue", + "custom-script-error-code": "Something went wrong, please try again later.", + "description": "Log in to ${companyName} to continue to ${clientName}.", + "editEmailText": "Edit", + "emailPlaceholder": "Email address", + "federatedConnectionButtonText": "Continue with ${connectionName}", + "footerLinkText": "Sign up", + "footerText": "Don't have an account?", + "forgotPasswordText": "Forgot password?", + "hidePasswordText": "Hide password", + "invalid-connection": "Invalid connection", + "invalid-email-format": "Email is not valid.", + "invitationDescription": "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", + "invitationTitle": "You've Been Invited!", + "ip-blocked": "We have detected suspicious login behavior and further attempts will be blocked. Please contact the administrator.", + "logoAltText": "${companyName}", + "no-db-connection": "Invalid connection", + "no-email": "Please enter an email address", + "no-password": "Password is required", + "no-username": "Username is required", + "pageTitle": "Log in | ${clientName}", + "password-breached": "We have detected a potential security issue with this account. To protect your account, we have prevented this login. Please reset your password to proceed.", + "passwordPlaceholder": "Password", + "same-user-login": "Too many login attempts for this user. Please wait, and try again later.", + "separatorText": "Or", + "showPasswordText": "Show password", + "signupActionLinkText": "${footerLinkText}", + "signupActionText": "${footerText}", + "title": "Welcome friend, glad to have you!", + "user-blocked": "Your account has been blocked after multiple consecutive login attempts.", + "usernameOnlyPlaceholder": "Username", + "usernamePlaceholder": "Username or email address", + "wrong-credentials": "Wrong username or password", + "wrong-email-credentials": "Wrong email or password", + }, }, }, }, @@ -317,48 +319,50 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) { EnabledLocales: []string{"en", "es"}, Domain: "tenant-example.auth0.com", }, - Prompt: &promptData{ - Language: "en", - Prompt: "login", - CustomText: map[string]map[string]interface{}{ - "login": { - "alertListTitle": "Alerts", - "auth0-users-validation": "Something went wrong, please try again later", - "authentication-failure": "We are sorry, something went wrong when attempting to login", - "buttonText": "Continue", - "custom-script-error-code": "Something went wrong, please try again later.", - "description": "Log in to ${companyName} to continue to ${clientName}.", - "editEmailText": "Edit", - "emailPlaceholder": "Email address", - "federatedConnectionButtonText": "Continue with ${connectionName}", - "footerLinkText": "Sign up", - "footerText": "Don't have an account?", - "forgotPasswordText": "Forgot password?", - "hidePasswordText": "Hide password", - "invalid-connection": "Invalid connection", - "invalid-email-format": "Email is not valid.", - "invitationDescription": "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", - "invitationTitle": "You've Been Invited!", - "ip-blocked": "We have detected suspicious login behavior and further attempts will be blocked. Please contact the administrator.", - "logoAltText": "${companyName}", - "no-db-connection": "Invalid connection", - "no-email": "Please enter an email address", - "no-password": "Password is required", - "no-username": "Username is required", - "pageTitle": "Log in | ${clientName}", - "password-breached": "We have detected a potential security issue with this account. To protect your account, we have prevented this login. Please reset your password to proceed.", - "passwordPlaceholder": "Password", - "same-user-login": "Too many login attempts for this user. Please wait, and try again later.", - "separatorText": "Or", - "showPasswordText": "Show password", - "signupActionLinkText": "${footerLinkText}", - "signupActionText": "${footerText}", - "title": "Welcome friend, glad to have you!", - "user-blocked": "Your account has been blocked after multiple consecutive login attempts.", - "usernameOnlyPlaceholder": "Username", - "usernamePlaceholder": "Username or email address", - "wrong-credentials": "Wrong username or password", - "wrong-email-credentials": "Wrong email or password", + Prompts: []*promptData{ + { + Language: "en", + Prompt: "login", + CustomText: map[string]interface{}{ + "login": map[string]interface{}{ + "alertListTitle": "Alerts", + "auth0-users-validation": "Something went wrong, please try again later", + "authentication-failure": "We are sorry, something went wrong when attempting to login", + "buttonText": "Continue", + "custom-script-error-code": "Something went wrong, please try again later.", + "description": "Log in to ${companyName} to continue to ${clientName}.", + "editEmailText": "Edit", + "emailPlaceholder": "Email address", + "federatedConnectionButtonText": "Continue with ${connectionName}", + "footerLinkText": "Sign up", + "footerText": "Don't have an account?", + "forgotPasswordText": "Forgot password?", + "hidePasswordText": "Hide password", + "invalid-connection": "Invalid connection", + "invalid-email-format": "Email is not valid.", + "invitationDescription": "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", + "invitationTitle": "You've Been Invited!", + "ip-blocked": "We have detected suspicious login behavior and further attempts will be blocked. Please contact the administrator.", + "logoAltText": "${companyName}", + "no-db-connection": "Invalid connection", + "no-email": "Please enter an email address", + "no-password": "Password is required", + "no-username": "Username is required", + "pageTitle": "Log in | ${clientName}", + "password-breached": "We have detected a potential security issue with this account. To protect your account, we have prevented this login. Please reset your password to proceed.", + "passwordPlaceholder": "Password", + "same-user-login": "Too many login attempts for this user. Please wait, and try again later.", + "separatorText": "Or", + "showPasswordText": "Show password", + "signupActionLinkText": "${footerLinkText}", + "signupActionText": "${footerText}", + "title": "Welcome friend, glad to have you!", + "user-blocked": "Your account has been blocked after multiple consecutive login attempts.", + "usernameOnlyPlaceholder": "Username", + "usernamePlaceholder": "Username or email address", + "wrong-credentials": "Wrong username or password", + "wrong-email-credentials": "Wrong email or password", + }, }, }, }, @@ -442,48 +446,50 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) { EnabledLocales: []string{"en", "es"}, Domain: "tenant-example.auth0.com", }, - Prompt: &promptData{ - Language: "en", - Prompt: "login", - CustomText: map[string]map[string]interface{}{ - "login": { - "alertListTitle": "Alerts", - "auth0-users-validation": "Something went wrong, please try again later", - "authentication-failure": "We are sorry, something went wrong when attempting to login", - "buttonText": "Continue", - "custom-script-error-code": "Something went wrong, please try again later.", - "description": "Log in to ${companyName} to continue to ${clientName}.", - "editEmailText": "Edit", - "emailPlaceholder": "Email address", - "federatedConnectionButtonText": "Continue with ${connectionName}", - "footerLinkText": "Sign up", - "footerText": "Don't have an account?", - "forgotPasswordText": "Forgot password?", - "hidePasswordText": "Hide password", - "invalid-connection": "Invalid connection", - "invalid-email-format": "Email is not valid.", - "invitationDescription": "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", - "invitationTitle": "You've Been Invited!", - "ip-blocked": "We have detected suspicious login behavior and further attempts will be blocked. Please contact the administrator.", - "logoAltText": "${companyName}", - "no-db-connection": "Invalid connection", - "no-email": "Please enter an email address", - "no-password": "Password is required", - "no-username": "Username is required", - "pageTitle": "Log in | ${clientName}", - "password-breached": "We have detected a potential security issue with this account. To protect your account, we have prevented this login. Please reset your password to proceed.", - "passwordPlaceholder": "Password", - "same-user-login": "Too many login attempts for this user. Please wait, and try again later.", - "separatorText": "Or", - "showPasswordText": "Show password", - "signupActionLinkText": "${footerLinkText}", - "signupActionText": "${footerText}", - "title": "Welcome friend, glad to have you!", - "user-blocked": "Your account has been blocked after multiple consecutive login attempts.", - "usernameOnlyPlaceholder": "Username", - "usernamePlaceholder": "Username or email address", - "wrong-credentials": "Wrong username or password", - "wrong-email-credentials": "Wrong email or password", + Prompts: []*promptData{ + { + Language: "en", + Prompt: "login", + CustomText: map[string]interface{}{ + "login": map[string]interface{}{ + "alertListTitle": "Alerts", + "auth0-users-validation": "Something went wrong, please try again later", + "authentication-failure": "We are sorry, something went wrong when attempting to login", + "buttonText": "Continue", + "custom-script-error-code": "Something went wrong, please try again later.", + "description": "Log in to ${companyName} to continue to ${clientName}.", + "editEmailText": "Edit", + "emailPlaceholder": "Email address", + "federatedConnectionButtonText": "Continue with ${connectionName}", + "footerLinkText": "Sign up", + "footerText": "Don't have an account?", + "forgotPasswordText": "Forgot password?", + "hidePasswordText": "Hide password", + "invalid-connection": "Invalid connection", + "invalid-email-format": "Email is not valid.", + "invitationDescription": "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", + "invitationTitle": "You've Been Invited!", + "ip-blocked": "We have detected suspicious login behavior and further attempts will be blocked. Please contact the administrator.", + "logoAltText": "${companyName}", + "no-db-connection": "Invalid connection", + "no-email": "Please enter an email address", + "no-password": "Password is required", + "no-username": "Username is required", + "pageTitle": "Log in | ${clientName}", + "password-breached": "We have detected a potential security issue with this account. To protect your account, we have prevented this login. Please reset your password to proceed.", + "passwordPlaceholder": "Password", + "same-user-login": "Too many login attempts for this user. Please wait, and try again later.", + "separatorText": "Or", + "showPasswordText": "Show password", + "signupActionLinkText": "${footerLinkText}", + "signupActionText": "${footerText}", + "title": "Welcome friend, glad to have you!", + "user-blocked": "Your account has been blocked after multiple consecutive login attempts.", + "usernameOnlyPlaceholder": "Username", + "usernamePlaceholder": "Username or email address", + "wrong-credentials": "Wrong username or password", + "wrong-email-credentials": "Wrong email or password", + }, }, }, }, @@ -647,48 +653,50 @@ func TestFetchUniversalLoginBrandingData(t *testing.T) { EnabledLocales: []string{"en", "es"}, Domain: "tenant-example.auth0.com", }, - Prompt: &promptData{ - Language: "en", - Prompt: "login", - CustomText: map[string]map[string]interface{}{ - "login": { - "alertListTitle": "Alerts", - "auth0-users-validation": "Something went wrong, please try again later", - "authentication-failure": "We are sorry, something went wrong when attempting to login", - "buttonText": "Continue", - "custom-script-error-code": "Something went wrong, please try again later.", - "description": "Log in to ${companyName} to continue to ${clientName}.", - "editEmailText": "Edit", - "emailPlaceholder": "Email address", - "federatedConnectionButtonText": "Continue with ${connectionName}", - "footerLinkText": "Sign up", - "footerText": "Don't have an account?", - "forgotPasswordText": "Forgot password?", - "hidePasswordText": "Hide password", - "invalid-connection": "Invalid connection", - "invalid-email-format": "Email is not valid.", - "invitationDescription": "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", - "invitationTitle": "You've Been Invited!", - "ip-blocked": "We have detected suspicious login behavior and further attempts will be blocked. Please contact the administrator.", - "logoAltText": "${companyName}", - "no-db-connection": "Invalid connection", - "no-email": "Please enter an email address", - "no-password": "Password is required", - "no-username": "Username is required", - "pageTitle": "Log in | ${clientName}", - "password-breached": "We have detected a potential security issue with this account. To protect your account, we have prevented this login. Please reset your password to proceed.", - "passwordPlaceholder": "Password", - "same-user-login": "Too many login attempts for this user. Please wait, and try again later.", - "separatorText": "Or", - "showPasswordText": "Show password", - "signupActionLinkText": "${footerLinkText}", - "signupActionText": "${footerText}", - "title": "Welcome friend, glad to have you!", - "user-blocked": "Your account has been blocked after multiple consecutive login attempts.", - "usernameOnlyPlaceholder": "Username", - "usernamePlaceholder": "Username or email address", - "wrong-credentials": "Wrong username or password", - "wrong-email-credentials": "Wrong email or password", + Prompts: []*promptData{ + { + Language: "en", + Prompt: "login", + CustomText: map[string]interface{}{ + "login": map[string]interface{}{ + "alertListTitle": "Alerts", + "auth0-users-validation": "Something went wrong, please try again later", + "authentication-failure": "We are sorry, something went wrong when attempting to login", + "buttonText": "Continue", + "custom-script-error-code": "Something went wrong, please try again later.", + "description": "Log in to ${companyName} to continue to ${clientName}.", + "editEmailText": "Edit", + "emailPlaceholder": "Email address", + "federatedConnectionButtonText": "Continue with ${connectionName}", + "footerLinkText": "Sign up", + "footerText": "Don't have an account?", + "forgotPasswordText": "Forgot password?", + "hidePasswordText": "Hide password", + "invalid-connection": "Invalid connection", + "invalid-email-format": "Email is not valid.", + "invitationDescription": "Log in to accept ${inviterName}'s invitation to join ${companyName} on ${clientName}.", + "invitationTitle": "You've Been Invited!", + "ip-blocked": "We have detected suspicious login behavior and further attempts will be blocked. Please contact the administrator.", + "logoAltText": "${companyName}", + "no-db-connection": "Invalid connection", + "no-email": "Please enter an email address", + "no-password": "Password is required", + "no-username": "Username is required", + "pageTitle": "Log in | ${clientName}", + "password-breached": "We have detected a potential security issue with this account. To protect your account, we have prevented this login. Please reset your password to proceed.", + "passwordPlaceholder": "Password", + "same-user-login": "Too many login attempts for this user. Please wait, and try again later.", + "separatorText": "Or", + "showPasswordText": "Show password", + "signupActionLinkText": "${footerLinkText}", + "signupActionText": "${footerText}", + "title": "Welcome friend, glad to have you!", + "user-blocked": "Your account has been blocked after multiple consecutive login attempts.", + "usernameOnlyPlaceholder": "Username", + "usernamePlaceholder": "Username or email address", + "wrong-credentials": "Wrong username or password", + "wrong-email-credentials": "Wrong email or password", + }, }, }, },