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