Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: offer to add default urls for quickstart #155

Merged
merged 12 commits into from
Mar 16, 2021
75 changes: 70 additions & 5 deletions internal/cli/quickstarts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path"
"regexp"
"strings"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/prompt"
Expand All @@ -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"`
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
as-herzog marked this conversation as resolved.
Show resolved Hide resolved
return err
}
return nil
},
}
Expand Down Expand Up @@ -283,14 +299,63 @@ 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"
}
}

// promptDefaultURLs checks whether the application is SPA or WebApp and
// 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 {
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,
WebOrigins: client.WebOrigins,
AllowedLogoutURLs: client.AllowedLogoutURLs,
}

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)
}

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
}

// 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)
}
10 changes: 10 additions & 0 deletions internal/cli/utils_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}