diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index f2f8da2d8..931d6f3d8 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -311,10 +311,17 @@ func quickstartsTypeFor(v string) string { } } +// promptDefaultURLs checks whether the application is SPA or WebApp and +// whether the app has already default quickstart url to allowed url lists. +// If not, it prompts the user to add the default url and updates the application +// if they accept. func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, qsType string) error { if !strings.EqualFold(qsType, QSSpa) && !strings.EqualFold(qsType, QSWebApp) { return nil } + if containsStr(client.Callbacks, _defaultURL) || containsStr(client.AllowedLogoutURLs, _defaultURL) { + return nil + } a := &management.Client{ Callbacks: client.Callbacks, @@ -322,20 +329,12 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, AllowedLogoutURLs: client.AllowedLogoutURLs, } shouldUpdate := false - if confirmed := prompt.Confirm(fmt.Sprintf("Do you want to add this URL to the list of allowed callback URLs: %s", _defaultURL)); confirmed { + if confirmed := prompt.Confirm(formatURLPrompt(qsType)); confirmed { a.Callbacks = append(a.Callbacks, _defaultURL) - shouldUpdate = true - } - if confirmed := prompt.Confirm(fmt.Sprintf("Do you want to add this URL to the list of allowed logout URLs: %s", _defaultURL)); confirmed { a.AllowedLogoutURLs = append(a.AllowedLogoutURLs, _defaultURL) + a.WebOrigins = append(a.WebOrigins, _defaultURL) shouldUpdate = true } - if strings.EqualFold(qsType, QSSpa) { - if confirmed := prompt.Confirm(fmt.Sprintf("Do you want to add this URL to the list of allowed web origins: %s", _defaultURL)); confirmed { - a.WebOrigins = append(a.WebOrigins, _defaultURL) - shouldUpdate = true - } - } if shouldUpdate { err := ansi.Spinner("Updating application", func() error { return cli.api.Client.Update(client.GetClientID(), a) @@ -347,3 +346,16 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, } return nil } + +// formatURLPrompt creates the correct prompt based on app type for +// asking the user if they would like to add default urls. +func formatURLPrompt(qsType string) string { + var p strings.Builder + p.WriteString("\nQuickstarts use localhost, do you want to add %s to the list of allowed callback URLs") + if strings.EqualFold(qsType, QSSpa) { + p.WriteString(", logout URLs, and web origins?") + } else { + p.WriteString(" and logout URLs?") + } + return fmt.Sprintf(p.String(), _defaultURL) +} diff --git a/internal/cli/utils_shared.go b/internal/cli/utils_shared.go index 4abbf4634..e34a529f0 100644 --- a/internal/cli/utils_shared.go +++ b/internal/cli/utils_shared.go @@ -260,3 +260,13 @@ func generateState(size int) (string, error) { return base64.RawURLEncoding.EncodeToString(b), nil } + +// check if slice contains a string +func containsStr(s []interface{}, u string) bool { + for _, a := range s { + if a == u { + return true + } + } + return false +}