From 51291f8bc4eedf9cc84ecca345f51159a779bb44 Mon Sep 17 00:00:00 2001 From: Andy Herzog Date: Fri, 12 Mar 2021 15:27:22 -0700 Subject: [PATCH 1/7] feat: offer to add default urls for quickstart --- internal/cli/quickstarts.go | 63 ++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index 27d0f6982..f2f8da2d8 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -13,6 +13,7 @@ import ( "os" "path" "regexp" + "strings" "github.com/auth0/auth0-cli/internal/ansi" "github.com/auth0/auth0-cli/internal/prompt" @@ -32,6 +33,15 @@ var ( }() ) +// QuickStart app types and defaults +const ( + QSNative = "native" + QSSpa = "spa" + QSWebApp = "webapp" + QSBackend = "backend" + _defaultURL = "http://localhost:3000" +) + type quickstart struct { Name string `json:"name"` Samples []string `json:"samples"` @@ -112,7 +122,8 @@ func downloadQuickstart(cli *cli) *cobra.Command { q, err := getQuickstart(client.GetAppType(), selectedStack) if err != nil { - return fmt.Errorf("An unexpected error occurred with the specified stack %v: %v", selectedStack, err) } + return fmt.Errorf("An unexpected error occurred with the specified stack %v: %v", selectedStack, err) + } err = ansi.Spinner("Downloading quickstart sample", func() error { return downloadQuickStart(context.TODO(), cli, client, target, q) @@ -123,6 +134,11 @@ func downloadQuickstart(cli *cli) *cobra.Command { } cli.renderer.Infof("Quickstart sample sucessfully downloaded at %s", target) + + qsType := quickstartsTypeFor(client.GetAppType()) + if err := promptDefaultURLs(context.TODO(), cli, client, qsType); err != nil { + return err + } return nil }, } @@ -283,14 +299,51 @@ func quickstartStacksFromType(t string) ([]string, error) { func quickstartsTypeFor(v string) string { switch { case v == "native": - return "native" + return QSNative case v == "spa": - return "spa" + return QSSpa case v == "regular_web": - return "webapp" + return QSWebApp case v == "non_interactive": - return "backend" + return QSBackend default: return "generic" } } + +func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, qsType string) error { + if !strings.EqualFold(qsType, QSSpa) && !strings.EqualFold(qsType, QSWebApp) { + return nil + } + + a := &management.Client{ + Callbacks: client.Callbacks, + WebOrigins: client.WebOrigins, + 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 { + 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) + 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) + }) + if err != nil { + return err + } + cli.renderer.Infof("Application successfully updated") + } + return nil +} From 391d57e3048cc629facf146b9060ed517bfcb03c Mon Sep 17 00:00:00 2001 From: Andy Herzog Date: Mon, 15 Mar 2021 08:26:12 -0600 Subject: [PATCH 2/7] refactor: one prompt; check if url already added --- internal/cli/quickstarts.go | 32 ++++++++++++++++++++++---------- internal/cli/utils_shared.go | 10 ++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) 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 +} From 0d7cd7800c1332af628e8b9e29ab1ef3144fbc6a Mon Sep 17 00:00:00 2001 From: Andy Herzog Date: Mon, 15 Mar 2021 08:28:23 -0600 Subject: [PATCH 3/7] refactor: check web origin --- internal/cli/quickstarts.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index 931d6f3d8..dcff8d421 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -332,7 +332,9 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, if confirmed := prompt.Confirm(formatURLPrompt(qsType)); confirmed { a.Callbacks = append(a.Callbacks, _defaultURL) a.AllowedLogoutURLs = append(a.AllowedLogoutURLs, _defaultURL) - a.WebOrigins = append(a.WebOrigins, _defaultURL) + if strings.EqualFold(qsType, QSSpa) { + a.WebOrigins = append(a.WebOrigins, _defaultURL) + } shouldUpdate = true } if shouldUpdate { From ebfb8bfeee809201e7781061baa6826f1fe7c5d7 Mon Sep 17 00:00:00 2001 From: Andy Herzog Date: Mon, 15 Mar 2021 08:32:19 -0600 Subject: [PATCH 4/7] style: fix comment --- internal/cli/quickstarts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index dcff8d421..e3d81470a 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -312,7 +312,7 @@ 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. +// whether the app has already added the 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 { From 23296107c62560fb0d59aa7b1ae32d5a5d68761c Mon Sep 17 00:00:00 2001 From: Andy Herzog Date: Mon, 15 Mar 2021 08:42:05 -0600 Subject: [PATCH 5/7] style: internal consts --- internal/cli/quickstarts.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index e3d81470a..11d9c57f4 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -35,10 +35,10 @@ var ( // QuickStart app types and defaults const ( - QSNative = "native" - QSSpa = "spa" - QSWebApp = "webapp" - QSBackend = "backend" + qsNative = "native" + qsSpa = "spa" + qsWebApp = "webapp" + qsBackend = "backend" _defaultURL = "http://localhost:3000" ) @@ -299,13 +299,13 @@ func quickstartStacksFromType(t string) ([]string, error) { func quickstartsTypeFor(v string) string { switch { case v == "native": - return QSNative + return qsNative case v == "spa": - return QSSpa + return qsSpa case v == "regular_web": - return QSWebApp + return qsWebApp case v == "non_interactive": - return QSBackend + return qsBackend default: return "generic" } @@ -316,7 +316,7 @@ func quickstartsTypeFor(v string) string { // 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) { + if !strings.EqualFold(qsType, qsSpa) && !strings.EqualFold(qsType, qsWebApp) { return nil } if containsStr(client.Callbacks, _defaultURL) || containsStr(client.AllowedLogoutURLs, _defaultURL) { @@ -332,7 +332,7 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, if confirmed := prompt.Confirm(formatURLPrompt(qsType)); confirmed { a.Callbacks = append(a.Callbacks, _defaultURL) a.AllowedLogoutURLs = append(a.AllowedLogoutURLs, _defaultURL) - if strings.EqualFold(qsType, QSSpa) { + if strings.EqualFold(qsType, qsSpa) { a.WebOrigins = append(a.WebOrigins, _defaultURL) } shouldUpdate = true @@ -354,7 +354,7 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, 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) { + if strings.EqualFold(qsType, qsSpa) { p.WriteString(", logout URLs, and web origins?") } else { p.WriteString(" and logout URLs?") From 6eda141da7b5b884ad69cdbd2d44561cb616581d Mon Sep 17 00:00:00 2001 From: Andy Herzog Date: Mon, 15 Mar 2021 08:48:08 -0600 Subject: [PATCH 6/7] refactor: remove uneccessary var and if block --- internal/cli/quickstarts.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index 11d9c57f4..6fecd2b5f 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -328,16 +328,14 @@ func promptDefaultURLs(ctx context.Context, cli *cli, client *management.Client, WebOrigins: client.WebOrigins, AllowedLogoutURLs: client.AllowedLogoutURLs, } - shouldUpdate := false + if confirmed := prompt.Confirm(formatURLPrompt(qsType)); confirmed { a.Callbacks = append(a.Callbacks, _defaultURL) a.AllowedLogoutURLs = append(a.AllowedLogoutURLs, _defaultURL) if strings.EqualFold(qsType, qsSpa) { 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) }) From a5ede138eea6de58d5b4cfde8218148ba3a350a8 Mon Sep 17 00:00:00 2001 From: Andy <70534960+aherzog-auth0@users.noreply.github.com> Date: Tue, 16 Mar 2021 07:31:33 -0600 Subject: [PATCH 7/7] Update internal/cli/quickstarts.go Co-authored-by: Jorge L. Fatta --- internal/cli/quickstarts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/quickstarts.go b/internal/cli/quickstarts.go index 6fecd2b5f..56c62c353 100644 --- a/internal/cli/quickstarts.go +++ b/internal/cli/quickstarts.go @@ -136,7 +136,7 @@ func downloadQuickstart(cli *cli) *cobra.Command { cli.renderer.Infof("Quickstart sample sucessfully downloaded at %s", target) qsType := quickstartsTypeFor(client.GetAppType()) - if err := promptDefaultURLs(context.TODO(), cli, client, qsType); err != nil { + if err := promptDefaultURLs(cmd.Context(), cli, client, qsType); err != nil { return err } return nil