From 37119f8a5e894a5e5ba00564e0d71f7655a6da19 Mon Sep 17 00:00:00 2001 From: morganelle Date: Fri, 19 Feb 2021 15:38:48 -0800 Subject: [PATCH 1/5] rename get-token and try-login commands to test-login and test-token --- internal/cli/root.go | 4 ++-- internal/cli/{try_login.go => test_login.go} | 6 +++--- internal/cli/{get_token.go => test_token.go} | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) rename internal/cli/{try_login.go => test_login.go} (95%) rename internal/cli/{get_token.go => test_token.go} (96%) diff --git a/internal/cli/root.go b/internal/cli/root.go index 9a9e113de..7088385e5 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -62,10 +62,10 @@ func Execute() { rootCmd.AddCommand(quickstartCmd(cli)) rootCmd.AddCommand(clientsCmd(cli)) rootCmd.AddCommand(apisCmd(cli)) - rootCmd.AddCommand(tryLoginCmd(cli)) + rootCmd.AddCommand(testLoginCmd(cli)) rootCmd.AddCommand(logsCmd(cli)) rootCmd.AddCommand(actionsCmd(cli)) - rootCmd.AddCommand(getTokenCmd(cli)) + rootCmd.AddCommand(testTokenCmd(cli)) // keep completion at the bottom: rootCmd.AddCommand(completionCmd(cli)) diff --git a/internal/cli/try_login.go b/internal/cli/test_login.go similarity index 95% rename from internal/cli/try_login.go rename to internal/cli/test_login.go index 27bd2401a..b3356ac9a 100644 --- a/internal/cli/try_login.go +++ b/internal/cli/test_login.go @@ -8,14 +8,14 @@ import ( "github.com/spf13/cobra" ) -func tryLoginCmd(cli *cli) *cobra.Command { +func testLoginCmd(cli *cli) *cobra.Command { var clientID string var connectionName string cmd := &cobra.Command{ - Use: "try-login", + Use: "test login", Short: "Try out your universal login box", - Long: `auth0 try-login + Long: `auth0 test login Launch a browser to try out your universal login box for the given client. `, RunE: func(cmd *cobra.Command, args []string) error { diff --git a/internal/cli/get_token.go b/internal/cli/test_token.go similarity index 96% rename from internal/cli/get_token.go rename to internal/cli/test_token.go index 11ed04da5..1448d3c1e 100644 --- a/internal/cli/get_token.go +++ b/internal/cli/test_token.go @@ -6,15 +6,15 @@ import ( "github.com/spf13/cobra" ) -func getTokenCmd(cli *cli) *cobra.Command { +func testTokenCmd(cli *cli) *cobra.Command { var clientID string var audience string var scopes []string cmd := &cobra.Command{ - Use: "get-token", + Use: "test token", Short: "Fetch a token for the given client and API", - Long: `auth0 get-token + Long: `auth0 test token Fetch an access token for the given client and API. `, RunE: func(cmd *cobra.Command, args []string) error { From 0412e8c3fd2cdf2786e3c8f62f5edbaf69e90f3e Mon Sep 17 00:00:00 2001 From: morganelle Date: Fri, 19 Feb 2021 15:42:17 -0800 Subject: [PATCH 2/5] update comments --- internal/cli/test_login.go | 4 ++-- internal/cli/test_token.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/cli/test_login.go b/internal/cli/test_login.go index b3356ac9a..b7f75cfc7 100644 --- a/internal/cli/test_login.go +++ b/internal/cli/test_login.go @@ -52,8 +52,8 @@ Launch a browser to try out your universal login box for the given client. tenant, client, connectionName, - "", // audience is only supported for get-token - "login", // force a login page when using try-login + "", // audience is only supported for test token command + "login", // force a login page when using test login command cliLoginTestingScopes, ) if err != nil { diff --git a/internal/cli/test_token.go b/internal/cli/test_token.go index 1448d3c1e..45bd0ebbf 100644 --- a/internal/cli/test_token.go +++ b/internal/cli/test_token.go @@ -68,9 +68,9 @@ Fetch an access token for the given client and API. cli, tenant, client, - "", // specifying a connection is only supported for try-login + "", // specifying a connection is only supported for test login command audience, - "", // We don't want to force a prompt for get-token + "", // We don't want to force a prompt for test token command scopes, ) if err != nil { From d9719aa90a05250284e381356dbd4dd21443ef6a Mon Sep 17 00:00:00 2001 From: morganelle Date: Fri, 19 Feb 2021 17:23:56 -0800 Subject: [PATCH 3/5] create a higher-level test command and add login-box and token sub commands to it --- internal/cli/root.go | 3 +- internal/cli/{test_token.go => test.go} | 90 ++++++++++++++++++++++++- internal/cli/test_login.go | 82 ---------------------- 3 files changed, 90 insertions(+), 85 deletions(-) rename internal/cli/{test_token.go => test.go} (51%) delete mode 100644 internal/cli/test_login.go diff --git a/internal/cli/root.go b/internal/cli/root.go index 7088385e5..551fdd7f8 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -62,10 +62,9 @@ func Execute() { rootCmd.AddCommand(quickstartCmd(cli)) rootCmd.AddCommand(clientsCmd(cli)) rootCmd.AddCommand(apisCmd(cli)) - rootCmd.AddCommand(testLoginCmd(cli)) + rootCmd.AddCommand(testCmd(cli)) rootCmd.AddCommand(logsCmd(cli)) rootCmd.AddCommand(actionsCmd(cli)) - rootCmd.AddCommand(testTokenCmd(cli)) // keep completion at the bottom: rootCmd.AddCommand(completionCmd(cli)) diff --git a/internal/cli/test_token.go b/internal/cli/test.go similarity index 51% rename from internal/cli/test_token.go rename to internal/cli/test.go index 45bd0ebbf..7680e495e 100644 --- a/internal/cli/test_token.go +++ b/internal/cli/test.go @@ -3,16 +3,104 @@ package cli import ( "fmt" + "github.com/auth0/auth0-cli/internal/ansi" + "github.com/auth0/auth0-cli/internal/auth/authutil" "github.com/spf13/cobra" ) +func testCmd(cli *cli) *cobra.Command { + cmd := &cobra.Command{ + Use: "test", + Short: "Try your universal login box or get a token", + } + + cmd.SetUsageTemplate(resourceUsageTemplate()) + cmd.AddCommand(testTokenCmd(cli)) + cmd.AddCommand(testLoginCmd(cli)) + + return cmd +} + +func testLoginCmd(cli *cli) *cobra.Command { + var clientID string + var connectionName string + + cmd := &cobra.Command{ + Use: "login-box", + Short: "Try out your universal login box", + Long: `auth0 test login-box +Launch a browser to try out your universal login box for the given client. +`, + RunE: func(cmd *cobra.Command, args []string) error { + var userInfo *authutil.UserInfo + + tenant, err := cli.getTenant() + if err != nil { + return err + } + + // use the client ID as passed in by the user, or default to the + // "CLI Login Testing" client if none passed. This client is only + // used for testing login from the CLI and will be created if it + // does not exist. + if clientID == "" { + client, err := getOrCreateCLITesterClient(cli.api.Client) + if err != nil { + return err + } + clientID = client.GetClientID() + } + + client, err := cli.api.Client.Read(clientID) + if err != nil { + return err + } + + if proceed := runLoginFlowPreflightChecks(cli, client); !proceed { + return nil + } + + tokenResponse, err := runLoginFlow( + cli, + tenant, + client, + connectionName, + "", // audience is only supported for test token command + "login", // force a login page when using test login command + cliLoginTestingScopes, + ) + if err != nil { + return err + } + + if err := ansi.Spinner("Fetching user metadata", func() error { + // Use the access token to fetch user information from the /userinfo + // endpoint. + userInfo, err = authutil.FetchUserInfo(tenant.Domain, tokenResponse.AccessToken) + return err + }); err != nil { + return err + } + + fmt.Fprint(cli.renderer.MessageWriter, "\n") + cli.renderer.TryLogin(userInfo, tokenResponse) + return nil + }, + } + + cmd.SetUsageTemplate(resourceUsageTemplate()) + cmd.Flags().StringVarP(&clientID, "client-id", "c", "", "Client Id for which to test login.") + cmd.Flags().StringVarP(&connectionName, "connection", "", "", "Connection to test during login.") + return cmd +} + func testTokenCmd(cli *cli) *cobra.Command { var clientID string var audience string var scopes []string cmd := &cobra.Command{ - Use: "test token", + Use: "token", Short: "Fetch a token for the given client and API", Long: `auth0 test token Fetch an access token for the given client and API. diff --git a/internal/cli/test_login.go b/internal/cli/test_login.go deleted file mode 100644 index b7f75cfc7..000000000 --- a/internal/cli/test_login.go +++ /dev/null @@ -1,82 +0,0 @@ -package cli - -import ( - "fmt" - - "github.com/auth0/auth0-cli/internal/ansi" - "github.com/auth0/auth0-cli/internal/auth/authutil" - "github.com/spf13/cobra" -) - -func testLoginCmd(cli *cli) *cobra.Command { - var clientID string - var connectionName string - - cmd := &cobra.Command{ - Use: "test login", - Short: "Try out your universal login box", - Long: `auth0 test login -Launch a browser to try out your universal login box for the given client. -`, - RunE: func(cmd *cobra.Command, args []string) error { - var userInfo *authutil.UserInfo - - tenant, err := cli.getTenant() - if err != nil { - return err - } - - // use the client ID as passed in by the user, or default to the - // "CLI Login Testing" client if none passed. This client is only - // used for testing login from the CLI and will be created if it - // does not exist. - if clientID == "" { - client, err := getOrCreateCLITesterClient(cli.api.Client) - if err != nil { - return err - } - clientID = client.GetClientID() - } - - client, err := cli.api.Client.Read(clientID) - if err != nil { - return err - } - - if proceed := runLoginFlowPreflightChecks(cli, client); !proceed { - return nil - } - - tokenResponse, err := runLoginFlow( - cli, - tenant, - client, - connectionName, - "", // audience is only supported for test token command - "login", // force a login page when using test login command - cliLoginTestingScopes, - ) - if err != nil { - return err - } - - if err := ansi.Spinner("Fetching user metadata", func() error { - // Use the access token to fetch user information from the /userinfo - // endpoint. - userInfo, err = authutil.FetchUserInfo(tenant.Domain, tokenResponse.AccessToken) - return err - }); err != nil { - return err - } - - fmt.Fprint(cli.renderer.MessageWriter, "\n") - cli.renderer.TryLogin(userInfo, tokenResponse) - return nil - }, - } - - cmd.SetUsageTemplate(resourceUsageTemplate()) - cmd.Flags().StringVarP(&clientID, "client-id", "c", "", "Client Id for which to test login.") - cmd.Flags().StringVarP(&connectionName, "connection", "", "", "Connection to test during login.") - return cmd -} From 7088519d4eff5fcd635966268f87049be2a0ba18 Mon Sep 17 00:00:00 2001 From: morganelle Date: Fri, 19 Feb 2021 17:30:14 -0800 Subject: [PATCH 4/5] fix comments --- internal/cli/test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/cli/test.go b/internal/cli/test.go index 7680e495e..7c021a627 100644 --- a/internal/cli/test.go +++ b/internal/cli/test.go @@ -65,8 +65,8 @@ Launch a browser to try out your universal login box for the given client. tenant, client, connectionName, - "", // audience is only supported for test token command - "login", // force a login page when using test login command + "", // audience is only supported for the test token command + "login", // force a login page when using the test login command cliLoginTestingScopes, ) if err != nil { @@ -156,9 +156,9 @@ Fetch an access token for the given client and API. cli, tenant, client, - "", // specifying a connection is only supported for test login command + "", // specifying a connection is only supported for the test login command audience, - "", // We don't want to force a prompt for test token command + "", // We don't want to force a prompt for the test token command scopes, ) if err != nil { From 30981fb48cc77a9b09b7dc016065fadb876b3dfd Mon Sep 17 00:00:00 2001 From: morganelle Date: Fri, 19 Feb 2021 17:59:40 -0800 Subject: [PATCH 5/5] update login checking logic --- internal/cli/root.go | 2 +- internal/cli/test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/cli/root.go b/internal/cli/root.go index 551fdd7f8..ba913b4b9 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -29,7 +29,7 @@ func Execute() { PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // If the user is trying to login, no need to go // through setup. - if cmd.Use == "login" { + if cmd.Use == "login" && cmd.Parent().Use == "auth0" { return nil } diff --git a/internal/cli/test.go b/internal/cli/test.go index 7c021a627..53d8e425d 100644 --- a/internal/cli/test.go +++ b/internal/cli/test.go @@ -26,9 +26,9 @@ func testLoginCmd(cli *cli) *cobra.Command { var connectionName string cmd := &cobra.Command{ - Use: "login-box", + Use: "login", Short: "Try out your universal login box", - Long: `auth0 test login-box + Long: `auth0 test login Launch a browser to try out your universal login box for the given client. `, RunE: func(cmd *cobra.Command, args []string) error {