From 754880fe6d691e2003630ec814c416259d72d139 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 6 Dec 2022 15:05:34 -0500 Subject: [PATCH 1/9] Consolidating Manage configured tenants. Usage: auth0 tenants [parameters...] [flags] Available Operations: use Set the active tenant list List your tenants [ls] open Open tenant settings page in the Auth0 Dashboard Flags: -h, --help help for tenants Global Flags: --debug Enable debug mode. --json Output in json format. --no-color Disable colors. --no-input Disable interactivity. --tenant string Specific tenant to use. (default "auth0-cli-integration-tests.us.auth0.com") Use "auth0 tenants [command] --help" for more information about a command. into --- Makefile | 2 +- docs/auth0_login.md | 18 ++++++- docs/auth0_tenants.md | 1 - docs/auth0_tenants_add.md | 41 -------------- internal/cli/cli.go | 4 +- internal/cli/login.go | 92 ++++++++++++++++++++++++++++++-- internal/cli/tenants.go | 72 ------------------------- test/integration/test-cases.yaml | 33 +++++++----- 8 files changed, 125 insertions(+), 138 deletions(-) delete mode 100644 docs/auth0_tenants_add.md diff --git a/Makefile b/Makefile index e1f267425..d68f0c181 100644 --- a/Makefile +++ b/Makefile @@ -129,7 +129,7 @@ test-unit: ## Run unit tests test-integration: $(GO_BIN)/commander ## Run integration tests. To run a specific test pass the FILTER var. Usage: `make test-integration FILTER="attack protection"` ${call print, "Running integration tests"} @$(MAKE) install # ensure fresh install prior to running test - auth0 tenants add ${AUTH0_CLI_CLIENT_DOMAIN} --client-id ${AUTH0_CLI_CLIENT_ID} --client-secret ${AUTH0_CLI_CLIENT_SECRET} && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \ + auth0 login --as-machine --domain ${AUTH0_CLI_CLIENT_DOMAIN} --client-id ${AUTH0_CLI_CLIENT_ID} --client-secret ${AUTH0_CLI_CLIENT_SECRET} && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \ exit_code=$$?; \ bash ./test/integration/scripts/test-cleanup.sh; \ exit $$exit_code diff --git a/docs/auth0_login.md b/docs/auth0_login.md index 384b018bf..a2a6d0d56 100644 --- a/docs/auth0_login.md +++ b/docs/auth0_login.md @@ -7,16 +7,30 @@ Authenticate the Auth0 CLI ### Synopsis -Sign in to your Auth0 account and authorize the CLI to access the Management API. +Authenticates the Auth0 CLI either as a user using personal credentials or as a machine using client credentials (client ID/secret). ``` auth0 login [flags] ``` +### Examples + +``` + + auth0 login + auth0 login --as-machine + auth0 login --as-machine --domain --client-id --client-secret + +``` + ### Options ``` - -h, --help help for login + --as-machine Initiates authentication as a machine via client credentials (client ID, client secret) + -i, --client-id string Client ID of the application. + -s, --client-secret string Client Secret of the application. + --domain string Specifies tenant domain when authenticating via client credentials (client ID, client secret) + -h, --help help for login ``` ### Options inherited from parent commands diff --git a/docs/auth0_tenants.md b/docs/auth0_tenants.md index 56be1080a..34b51b17b 100644 --- a/docs/auth0_tenants.md +++ b/docs/auth0_tenants.md @@ -28,7 +28,6 @@ Manage configured tenants. ### SEE ALSO * [auth0](/auth0-cli/) - Supercharge your development workflow. -* [auth0 tenants add](auth0_tenants_add.md) - Add a tenant with client credentials * [auth0 tenants list](auth0_tenants_list.md) - List your tenants * [auth0 tenants open](auth0_tenants_open.md) - Open tenant settings page in the Auth0 Dashboard * [auth0 tenants use](auth0_tenants_use.md) - Set the active tenant diff --git a/docs/auth0_tenants_add.md b/docs/auth0_tenants_add.md deleted file mode 100644 index a8d7ffe68..000000000 --- a/docs/auth0_tenants_add.md +++ /dev/null @@ -1,41 +0,0 @@ ---- -layout: default ---- -## auth0 tenants add - -Add a tenant with client credentials - -### Synopsis - -Add a tenant with client credentials. - -``` -auth0 tenants add [flags] -``` - -### Examples - -``` -auth0 tenants add --client-id --client-secret -``` - -### Options - -``` - -h, --help help for add -``` - -### Options inherited from parent commands - -``` - --debug Enable debug mode. - --json Output in json format. - --no-color Disable colors. - --no-input Disable interactivity. - --tenant string Specific tenant to use. -``` - -### SEE ALSO - -* [auth0 tenants](auth0_tenants.md) - Manage configured tenants - diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 116688ecb..927d7223c 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -208,7 +208,7 @@ func (c *cli) prepareTenant(ctx context.Context) (Tenant, error) { } if t.AccessToken == "" || (scopesChanged(t) && t.authenticatedWithDeviceCodeFlow()) { - return RunLogin(ctx, c, true) + return RunLoginAsUser(ctx, c, true) } if !t.hasExpiredToken() { @@ -218,7 +218,7 @@ func (c *cli) prepareTenant(ctx context.Context) (Tenant, error) { if err := t.regenerateAccessToken(ctx, c); err != nil { // Ask and guide the user through the login process. c.renderer.Errorf("failed to renew access token, %s", err) - return RunLogin(ctx, c, true) + return RunLoginAsUser(ctx, c, true) } if err := c.addTenant(t); err != nil { diff --git a/internal/cli/login.go b/internal/cli/login.go index 4ddd967f6..fee6a734b 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -12,24 +12,68 @@ import ( "github.com/auth0/auth0-cli/internal/prompt" ) +var ( + loginAsMachine = Flag{ + Name: "Login as machine", + LongForm: "as-machine", + Help: "Initiates authentication as a machine via client credentials (client ID, client secret)", + IsRequired: false, + } + loginTenantDomain = Flag{ + Name: "Tenant Domain", + LongForm: "domain", + Help: "Specifies tenant domain when authenticating via client credentials (client ID, client secret)", + IsRequired: false, + } +) + +type LoginInputs struct { + LoginAsMachine bool + Domain string + ClientID string + ClientSecret string +} + func loginCmd(cli *cli) *cobra.Command { + var inputs LoginInputs + cmd := &cobra.Command{ Use: "login", Args: cobra.NoArgs, Short: "Authenticate the Auth0 CLI", - Long: "Sign in to your Auth0 account and authorize the CLI to access the Management API.", + Long: "Authenticates the Auth0 CLI either as a user using personal credentials or as a machine using client credentials (client ID/secret).", + Example: ` + auth0 login + auth0 login --as-machine + auth0 login --as-machine --domain --client-id --client-secret + `, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - if _, err := RunLogin(ctx, cli, false); err != nil { - return err + + shouldLoginAsMachine := inputs.LoginAsMachine || inputs.ClientID != "" || inputs.ClientSecret != "" || inputs.Domain != "" + + if shouldLoginAsMachine { + if err := RunLoginAsMachine(ctx, inputs, cli, cmd); err != nil { + return err + } + } else { + if _, err := RunLoginAsUser(ctx, cli, false); err != nil { + return err + } } + cli.renderer.Infof("Successfully authenticated to %s", inputs.Domain) cli.tracker.TrackCommandRun(cmd, cli.config.InstallID) return nil }, } + loginAsMachine.RegisterBool(cmd, &inputs.LoginAsMachine, false) + loginTenantDomain.RegisterString(cmd, &inputs.Domain, "") + tenantClientID.RegisterString(cmd, &inputs.ClientID, "") + tenantClientSecret.RegisterString(cmd, &inputs.ClientSecret, "") + cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { _ = cmd.Flags().MarkHidden("tenant") _ = cmd.Flags().MarkHidden("json") @@ -39,11 +83,11 @@ func loginCmd(cli *cli) *cobra.Command { return cmd } -// RunLogin runs the login flow guiding the user through the process +// RunLoginAsUser runs the login flow guiding the user through the process // by showing the login instructions, opening the browser. // Use `expired` to run the login from other commands setup: // this will only affect the messages. -func RunLogin(ctx context.Context, cli *cli, expired bool) (Tenant, error) { +func RunLoginAsUser(ctx context.Context, cli *cli, expired bool) (Tenant, error) { message := fmt.Sprintf( "%s\n\n%s\n\n", "✪ Welcome to the Auth0 CLI 🎊", @@ -141,3 +185,41 @@ func RunLogin(ctx context.Context, cli *cli, expired bool) (Tenant, error) { return tenant, nil } + +// RunLoginAsUser facilitates the authentication process using client credentials (client ID, client secret) +func RunLoginAsMachine(ctx context.Context, inputs LoginInputs, cli *cli, cmd *cobra.Command) error { + if err := loginTenantDomain.Ask(cmd, &inputs.Domain, nil); err != nil { + return err + } + + if err := tenantClientID.Ask(cmd, &inputs.ClientID, nil); err != nil { + return err + } + + if err := tenantClientSecret.AskPassword(cmd, &inputs.ClientSecret, nil); err != nil { + return err + } + + token, err := auth.GetAccessTokenFromClientCreds(auth.ClientCredentials{ + ClientID: inputs.ClientID, + ClientSecret: inputs.ClientSecret, + Domain: inputs.Domain, + }) + if err != nil { + return err + } + + t := Tenant{ + Domain: inputs.Domain, + AccessToken: token.AccessToken, + ExpiresAt: token.ExpiresAt, + ClientID: inputs.ClientID, + ClientSecret: inputs.ClientSecret, + } + + if err := cli.addTenant(t); err != nil { + return fmt.Errorf("unexpected error when attempting to save tenant data: %w", err) + } + + return nil +} diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index 7d178905c..405b80dc8 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -1,12 +1,10 @@ package cli import ( - "errors" "fmt" "github.com/spf13/cobra" - "github.com/auth0/auth0-cli/internal/auth" "github.com/auth0/auth0-cli/internal/prompt" ) @@ -44,7 +42,6 @@ func tenantsCmd(cli *cli) *cobra.Command { cmd.AddCommand(useTenantCmd(cli)) cmd.AddCommand(listTenantCmd(cli)) cmd.AddCommand(openTenantCmd(cli)) - cmd.AddCommand(addTenantCmd(cli)) return cmd } @@ -162,75 +159,6 @@ func openTenantCmd(cli *cli) *cobra.Command { return cmd } -func addTenantCmd(cli *cli) *cobra.Command { - var inputs struct { - Domain string - ClientID string - ClientSecret string - } - - cmd := &cobra.Command{ - Use: "add", - Args: cobra.MaximumNArgs(1), - Short: "Add a tenant with client credentials", - Long: "Add a tenant with client credentials.", - Example: "auth0 tenants add --client-id --client-secret ", - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - err := tenantDomain.Pick(cmd, &inputs.Domain, cli.tenantPickerOptions) - if err != nil { - if !errors.Is(err, errUnauthenticated) { - return err - } - - if err := tenantDomain.Ask(cmd, &inputs.Domain); err != nil { - return err - } - } - } else { - inputs.Domain = args[0] - } - - if err := tenantClientID.Ask(cmd, &inputs.ClientID, nil); err != nil { - return err - } - - if err := tenantClientSecret.Ask(cmd, &inputs.ClientSecret, nil); err != nil { - return err - } - - token, err := auth.GetAccessTokenFromClientCreds(auth.ClientCredentials{ - ClientID: inputs.ClientID, - ClientSecret: inputs.ClientSecret, - Domain: inputs.Domain, - }) - if err != nil { - return err - } - - t := Tenant{ - Domain: inputs.Domain, - AccessToken: token.AccessToken, - ExpiresAt: token.ExpiresAt, - ClientID: inputs.ClientID, - ClientSecret: inputs.ClientSecret, - } - - if err := cli.addTenant(t); err != nil { - return fmt.Errorf("unexpected error when attempting to save tenant data: %w", err) - } - - cli.renderer.Infof("Tenant added successfully: %s", t.Domain) - return nil - }, - } - - tenantClientID.RegisterString(cmd, &inputs.ClientID, "") - tenantClientSecret.RegisterString(cmd, &inputs.ClientSecret, "") - - return cmd -} - func (c *cli) tenantPickerOptions() (pickerOptions, error) { tens, err := c.listTenants() if err != nil { diff --git a/test/integration/test-cases.yaml b/test/integration/test-cases.yaml index c3b071bdc..cca704b62 100644 --- a/test/integration/test-cases.yaml +++ b/test/integration/test-cases.yaml @@ -2,6 +2,11 @@ config: inherit-env: true tests: + login as machine: + command: auth0 logout $AUTH0_CLI_CLIENT_DOMAIN; auth0 login --as-machine --client-id $AUTH0_CLI_CLIENT_ID --client-secret $AUTH0_CLI_CLIENT_SECRET --domain $AUTH0_CLI_CLIENT_DOMAIN + stderr: "Successfully authenticated to" + exit-code: 0 + auth0 apis list: exit-code: 0 @@ -37,7 +42,7 @@ tests: auth0 completion bash: exit-code: 0 - + # Test 'apps create' --type flag apps create type native and check data: command: auth0 apps create --name integration-test-app-nativeapp1 --type native --description NativeApp1 --json @@ -109,7 +114,7 @@ tests: json: token_endpoint_auth_method: client_secret_basic exit-code: 0 - + # Test 'apps create' --callbacks flag apps create type m2m callbacks: command: auth0 apps create --name integration-test-app-m2mapp3 --type m2m --description M2mApp3 --callbacks https://example.com @@ -124,7 +129,7 @@ tests: json: callbacks: "[https://example.com https://google.com]" exit-code: 0 - + # Test 'apps create' --grants flag apps create type regular grants: command: auth0 apps create --name integration-test-app-regapp5 --type regular --description RegApp4 --grants credentials,password @@ -212,7 +217,7 @@ tests: json: callbacks: "[https://example.com]" exit-code: 0 - + apps update description: command: auth0 apps update $(cat ./test/integration/identifiers/app-id) --description "A better description" --json stdout: @@ -350,10 +355,10 @@ tests: command: auth0 apis show $(cat ./test/integration/identifiers/api-id) # depends on "apis create test app" test stdout: contains: - - NAME integration-test-api-newapi - - IDENTIFIER http://integration-test-api-newapi - - SCOPES read:todos - - TOKEN LIFETIME 86400 + - NAME integration-test-api-newapi + - IDENTIFIER http://integration-test-api-newapi + - SCOPES read:todos + - TOKEN LIFETIME 86400 - ALLOW OFFLINE ACCESS ✗ exit-code: 0 @@ -403,7 +408,7 @@ tests: exit-code: 0 stdout: json: - email: "testuser@example.com" + email: "testuser@example.com" connection: "Username-Password-Authentication" users create and check output: @@ -423,7 +428,7 @@ tests: command: auth0 users show $(cat ./test/integration/identifiers/user-id) --json stdout: json: - email: "newuser@example.com" + email: "newuser@example.com" connection: "Username-Password-Authentication" exit-code: 0 @@ -456,8 +461,8 @@ tests: exit-code: 0 stdout: json: - name: integration-test-role-new1 - description: testRole + name: integration-test-role-new1 + description: testRole roles create and check output: command: auth0 roles create --name integration-test-role-new2 --description testRole2 --no-input @@ -476,7 +481,7 @@ tests: command: auth0 roles show $(cat ./test/integration/identifiers/role-id) --json stdout: json: - name: integration-test-role-newRole + name: integration-test-role-newRole description: integration-test-role exit-code: 0 @@ -521,7 +526,7 @@ tests: - NAME integration-test-rule-new2 - ENABLED ✗ - ORDER 2 - - SCRIPT function(user, context, cb) { + - SCRIPT function(user, context, cb) { exit-code: 0 # Test 'rules show' From cb84cb6c1f36b1b36486cd943545727fe57e63f5 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Tue, 6 Dec 2022 16:52:27 -0500 Subject: [PATCH 2/9] Fixing typo --- internal/cli/login.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cli/login.go b/internal/cli/login.go index fee6a734b..ca1f1cd27 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -186,7 +186,7 @@ func RunLoginAsUser(ctx context.Context, cli *cli, expired bool) (Tenant, error) return tenant, nil } -// RunLoginAsUser facilitates the authentication process using client credentials (client ID, client secret) +// RunLoginAsMachine facilitates the authentication process using client credentials (client ID, client secret) func RunLoginAsMachine(ctx context.Context, inputs LoginInputs, cli *cli, cmd *cobra.Command) error { if err := loginTenantDomain.Ask(cmd, &inputs.Domain, nil); err != nil { return err From fe0c49cb2533a4180faca34d5dbbacfd102b3794 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 7 Dec 2022 12:16:34 -0500 Subject: [PATCH 3/9] Removing --as-machine flag --- Makefile | 2 +- docs/auth0_login.md | 4 +--- internal/cli/login.go | 19 +++++-------------- test/integration/test-cases.yaml | 2 +- 4 files changed, 8 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index d68f0c181..8c703574f 100644 --- a/Makefile +++ b/Makefile @@ -129,7 +129,7 @@ test-unit: ## Run unit tests test-integration: $(GO_BIN)/commander ## Run integration tests. To run a specific test pass the FILTER var. Usage: `make test-integration FILTER="attack protection"` ${call print, "Running integration tests"} @$(MAKE) install # ensure fresh install prior to running test - auth0 login --as-machine --domain ${AUTH0_CLI_CLIENT_DOMAIN} --client-id ${AUTH0_CLI_CLIENT_ID} --client-secret ${AUTH0_CLI_CLIENT_SECRET} && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \ + auth0 login --domain ${AUTH0_CLI_CLIENT_DOMAIN} --client-id ${AUTH0_CLI_CLIENT_ID} --client-secret ${AUTH0_CLI_CLIENT_SECRET} && commander test ./test/integration/test-cases.yaml --filter "$(FILTER)"; \ exit_code=$$?; \ bash ./test/integration/scripts/test-cleanup.sh; \ exit $$exit_code diff --git a/docs/auth0_login.md b/docs/auth0_login.md index a2a6d0d56..b8d1f46c8 100644 --- a/docs/auth0_login.md +++ b/docs/auth0_login.md @@ -18,15 +18,13 @@ auth0 login [flags] ``` auth0 login - auth0 login --as-machine - auth0 login --as-machine --domain --client-id --client-secret + auth0 login --domain --client-id --client-secret ``` ### Options ``` - --as-machine Initiates authentication as a machine via client credentials (client ID, client secret) -i, --client-id string Client ID of the application. -s, --client-secret string Client Secret of the application. --domain string Specifies tenant domain when authenticating via client credentials (client ID, client secret) diff --git a/internal/cli/login.go b/internal/cli/login.go index ca1f1cd27..59d7a4a2a 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -13,12 +13,6 @@ import ( ) var ( - loginAsMachine = Flag{ - Name: "Login as machine", - LongForm: "as-machine", - Help: "Initiates authentication as a machine via client credentials (client ID, client secret)", - IsRequired: false, - } loginTenantDomain = Flag{ Name: "Tenant Domain", LongForm: "domain", @@ -28,10 +22,9 @@ var ( ) type LoginInputs struct { - LoginAsMachine bool - Domain string - ClientID string - ClientSecret string + Domain string + ClientID string + ClientSecret string } func loginCmd(cli *cli) *cobra.Command { @@ -44,13 +37,12 @@ func loginCmd(cli *cli) *cobra.Command { Long: "Authenticates the Auth0 CLI either as a user using personal credentials or as a machine using client credentials (client ID/secret).", Example: ` auth0 login - auth0 login --as-machine - auth0 login --as-machine --domain --client-id --client-secret + auth0 login --domain --client-id --client-secret `, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - shouldLoginAsMachine := inputs.LoginAsMachine || inputs.ClientID != "" || inputs.ClientSecret != "" || inputs.Domain != "" + shouldLoginAsMachine := inputs.ClientID != "" || inputs.ClientSecret != "" || inputs.Domain != "" if shouldLoginAsMachine { if err := RunLoginAsMachine(ctx, inputs, cli, cmd); err != nil { @@ -69,7 +61,6 @@ func loginCmd(cli *cli) *cobra.Command { }, } - loginAsMachine.RegisterBool(cmd, &inputs.LoginAsMachine, false) loginTenantDomain.RegisterString(cmd, &inputs.Domain, "") tenantClientID.RegisterString(cmd, &inputs.ClientID, "") tenantClientSecret.RegisterString(cmd, &inputs.ClientSecret, "") diff --git a/test/integration/test-cases.yaml b/test/integration/test-cases.yaml index cca704b62..e0bd30e39 100644 --- a/test/integration/test-cases.yaml +++ b/test/integration/test-cases.yaml @@ -3,7 +3,7 @@ config: tests: login as machine: - command: auth0 logout $AUTH0_CLI_CLIENT_DOMAIN; auth0 login --as-machine --client-id $AUTH0_CLI_CLIENT_ID --client-secret $AUTH0_CLI_CLIENT_SECRET --domain $AUTH0_CLI_CLIENT_DOMAIN + command: auth0 logout $AUTH0_CLI_CLIENT_DOMAIN; auth0 login --client-id $AUTH0_CLI_CLIENT_ID --client-secret $AUTH0_CLI_CLIENT_SECRET --domain $AUTH0_CLI_CLIENT_DOMAIN stderr: "Successfully authenticated to" exit-code: 0 From dc8a2b5f2fd305307a6d6bb7e50bd6b17f302b95 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 7 Dec 2022 12:34:50 -0500 Subject: [PATCH 4/9] Lowercasing example --- docs/auth0_login.md | 2 +- internal/cli/login.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/auth0_login.md b/docs/auth0_login.md index b8d1f46c8..0c78e2253 100644 --- a/docs/auth0_login.md +++ b/docs/auth0_login.md @@ -18,7 +18,7 @@ auth0 login [flags] ``` auth0 login - auth0 login --domain --client-id --client-secret + auth0 login --domain --client-id --client-secret ``` diff --git a/internal/cli/login.go b/internal/cli/login.go index 59d7a4a2a..818a9422f 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -37,7 +37,7 @@ func loginCmd(cli *cli) *cobra.Command { Long: "Authenticates the Auth0 CLI either as a user using personal credentials or as a machine using client credentials (client ID/secret).", Example: ` auth0 login - auth0 login --domain --client-id --client-secret + auth0 login --domain --client-id --client-secret `, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() From f422fcd7e7172aeae7257ea39cb6e3dab8816fd7 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 7 Dec 2022 12:58:31 -0500 Subject: [PATCH 5/9] Removing flag declarations from tenants file context to login --- internal/cli/login.go | 22 ++++++++++++++++++---- internal/cli/tenants.go | 28 +++++----------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/internal/cli/login.go b/internal/cli/login.go index 818a9422f..45c3062ee 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -19,6 +19,20 @@ var ( Help: "Specifies tenant domain when authenticating via client credentials (client ID, client secret)", IsRequired: false, } + + loginClientID = Flag{ + Name: "Client ID", + LongForm: "client-id", + Help: "Client ID of the application.", + IsRequired: true, + } + + loginClientSecret = Flag{ + Name: "Client Secret", + LongForm: "client-secret", + Help: "Client Secret of the application.", + IsRequired: true, + } ) type LoginInputs struct { @@ -62,8 +76,8 @@ func loginCmd(cli *cli) *cobra.Command { } loginTenantDomain.RegisterString(cmd, &inputs.Domain, "") - tenantClientID.RegisterString(cmd, &inputs.ClientID, "") - tenantClientSecret.RegisterString(cmd, &inputs.ClientSecret, "") + loginClientID.RegisterString(cmd, &inputs.ClientID, "") + loginClientSecret.RegisterString(cmd, &inputs.ClientSecret, "") cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { _ = cmd.Flags().MarkHidden("tenant") @@ -183,11 +197,11 @@ func RunLoginAsMachine(ctx context.Context, inputs LoginInputs, cli *cli, cmd *c return err } - if err := tenantClientID.Ask(cmd, &inputs.ClientID, nil); err != nil { + if err := loginClientID.Ask(cmd, &inputs.ClientID, nil); err != nil { return err } - if err := tenantClientSecret.AskPassword(cmd, &inputs.ClientSecret, nil); err != nil { + if err := loginClientSecret.AskPassword(cmd, &inputs.ClientSecret, nil); err != nil { return err } diff --git a/internal/cli/tenants.go b/internal/cli/tenants.go index 405b80dc8..c11859adc 100644 --- a/internal/cli/tenants.go +++ b/internal/cli/tenants.go @@ -8,29 +8,6 @@ import ( "github.com/auth0/auth0-cli/internal/prompt" ) -var ( - tenantDomain = Argument{ - Name: "Tenant", - Help: "Tenant to select", - } - - tenantClientID = Flag{ - Name: "Client ID", - LongForm: "client-id", - ShortForm: "i", - Help: "Client ID of the application.", - IsRequired: true, - } - - tenantClientSecret = Flag{ - Name: "Client Secret", - LongForm: "client-secret", - ShortForm: "s", - Help: "Client Secret of the application.", - IsRequired: true, - } -) - func tenantsCmd(cli *cli) *cobra.Command { cmd := &cobra.Command{ Use: "tenants", @@ -126,6 +103,11 @@ func openTenantCmd(cli *cli) *cobra.Command { Domain string } + var tenantDomain = Argument{ + Name: "Tenant", + Help: "Tenant to select", + } + cmd := &cobra.Command{ Use: "open", Args: cobra.MaximumNArgs(1), From 389eb7f485e11eade94c570e601396be5c15db19 Mon Sep 17 00:00:00 2001 From: Will Vedder Date: Wed, 7 Dec 2022 14:06:58 -0500 Subject: [PATCH 6/9] Grouping domain, client id and client secret flags together --- internal/cli/login.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/internal/cli/login.go b/internal/cli/login.go index 45c3062ee..0ca4bbda7 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -14,24 +14,27 @@ import ( var ( loginTenantDomain = Flag{ - Name: "Tenant Domain", - LongForm: "domain", - Help: "Specifies tenant domain when authenticating via client credentials (client ID, client secret)", - IsRequired: false, + Name: "Tenant Domain", + LongForm: "domain", + Help: "Specifies tenant domain when authenticating via client credentials (client ID, client secret)", + IsRequired: false, + AlwaysPrompt: true, } loginClientID = Flag{ - Name: "Client ID", - LongForm: "client-id", - Help: "Client ID of the application.", - IsRequired: true, + Name: "Client ID", + LongForm: "client-id", + Help: "Client ID of the application.", + IsRequired: true, + AlwaysPrompt: true, } loginClientSecret = Flag{ - Name: "Client Secret", - LongForm: "client-secret", - Help: "Client Secret of the application.", - IsRequired: true, + Name: "Client Secret", + LongForm: "client-secret", + Help: "Client Secret of the application.", + IsRequired: true, + AlwaysPrompt: true, } ) @@ -78,10 +81,12 @@ func loginCmd(cli *cli) *cobra.Command { loginTenantDomain.RegisterString(cmd, &inputs.Domain, "") loginClientID.RegisterString(cmd, &inputs.ClientID, "") loginClientSecret.RegisterString(cmd, &inputs.ClientSecret, "") + cmd.MarkFlagsRequiredTogether("client-id", "client-secret", "domain") cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { _ = cmd.Flags().MarkHidden("tenant") _ = cmd.Flags().MarkHidden("json") + _ = cmd.Flags().MarkHidden("no-input") cmd.Parent().HelpFunc()(cmd, args) }) From eb243cbd302cbec444048cae899d3aa5600c5051 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Fri, 9 Dec 2022 16:03:34 +0100 Subject: [PATCH 7/9] Apply minor improvements on the login cmd --- internal/cli/login.go | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/internal/cli/login.go b/internal/cli/login.go index 0ca4bbda7..23c5693bf 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -16,25 +16,25 @@ var ( loginTenantDomain = Flag{ Name: "Tenant Domain", LongForm: "domain", - Help: "Specifies tenant domain when authenticating via client credentials (client ID, client secret)", + Help: "Tenant domain of the application when authenticating via client credentials.", IsRequired: false, - AlwaysPrompt: true, + AlwaysPrompt: false, } loginClientID = Flag{ Name: "Client ID", LongForm: "client-id", - Help: "Client ID of the application.", - IsRequired: true, - AlwaysPrompt: true, + Help: "Client ID of the application when authenticating via client credentials.", + IsRequired: false, + AlwaysPrompt: false, } loginClientSecret = Flag{ Name: "Client Secret", LongForm: "client-secret", - Help: "Client Secret of the application.", - IsRequired: true, - AlwaysPrompt: true, + Help: "Client secret of the application when authenticating via client credentials.", + IsRequired: false, + AlwaysPrompt: false, } ) @@ -44,6 +44,10 @@ type LoginInputs struct { ClientSecret string } +func (i *LoginInputs) shouldLoginAsMachine() bool { + return i.ClientID != "" || i.ClientSecret != "" || i.Domain != "" +} + func loginCmd(cli *cli) *cobra.Command { var inputs LoginInputs @@ -51,17 +55,13 @@ func loginCmd(cli *cli) *cobra.Command { Use: "login", Args: cobra.NoArgs, Short: "Authenticate the Auth0 CLI", - Long: "Authenticates the Auth0 CLI either as a user using personal credentials or as a machine using client credentials (client ID/secret).", - Example: ` - auth0 login - auth0 login --domain --client-id --client-secret - `, + Long: "Authenticates the Auth0 CLI either as a user using personal credentials or as a machine using client credentials.", + Example: `auth0 login +auth0 login --domain --client-id --client-secret `, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() - shouldLoginAsMachine := inputs.ClientID != "" || inputs.ClientSecret != "" || inputs.Domain != "" - - if shouldLoginAsMachine { + if inputs.shouldLoginAsMachine() { if err := RunLoginAsMachine(ctx, inputs, cli, cmd); err != nil { return err } @@ -71,7 +71,6 @@ func loginCmd(cli *cli) *cobra.Command { } } - cli.renderer.Infof("Successfully authenticated to %s", inputs.Domain) cli.tracker.TrackCommandRun(cmd, cli.config.InstallID) return nil @@ -231,5 +230,13 @@ func RunLoginAsMachine(ctx context.Context, inputs LoginInputs, cli *cli, cmd *c return fmt.Errorf("unexpected error when attempting to save tenant data: %w", err) } + cli.renderer.Newline() + cli.renderer.Infof("Successfully logged in.") + cli.renderer.Infof("Tenant: %s", inputs.Domain) + + if err := checkInstallID(cli); err != nil { + return fmt.Errorf("failed to update the config: %w", err) + } + return nil } From 267124ff5a483e2a34a21c3415ce2db25618bca1 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Fri, 9 Dec 2022 16:04:41 +0100 Subject: [PATCH 8/9] Rebuild docs --- docs/auth0_login.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/auth0_login.md b/docs/auth0_login.md index 0c78e2253..e22cb0f2e 100644 --- a/docs/auth0_login.md +++ b/docs/auth0_login.md @@ -7,7 +7,7 @@ Authenticate the Auth0 CLI ### Synopsis -Authenticates the Auth0 CLI either as a user using personal credentials or as a machine using client credentials (client ID/secret). +Authenticates the Auth0 CLI either as a user using personal credentials or as a machine using client credentials. ``` auth0 login [flags] @@ -16,18 +16,16 @@ auth0 login [flags] ### Examples ``` - - auth0 login - auth0 login --domain --client-id --client-secret - +auth0 login +auth0 login --domain --client-id --client-secret ``` ### Options ``` - -i, --client-id string Client ID of the application. - -s, --client-secret string Client Secret of the application. - --domain string Specifies tenant domain when authenticating via client credentials (client ID, client secret) + --client-id string Client ID of the application when authenticating via client credentials. + --client-secret string Client secret of the application when authenticating via client credentials. + --domain string Tenant domain of the application when authenticating via client credentials. -h, --help help for login ``` From af53a4ede362dd665edd314a7625126c9b29f777 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea Date: Fri, 9 Dec 2022 16:11:29 +0100 Subject: [PATCH 9/9] Remove extra login test --- test/integration/test-cases.yaml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test/integration/test-cases.yaml b/test/integration/test-cases.yaml index e0bd30e39..8457936a6 100644 --- a/test/integration/test-cases.yaml +++ b/test/integration/test-cases.yaml @@ -2,11 +2,6 @@ config: inherit-env: true tests: - login as machine: - command: auth0 logout $AUTH0_CLI_CLIENT_DOMAIN; auth0 login --client-id $AUTH0_CLI_CLIENT_ID --client-secret $AUTH0_CLI_CLIENT_SECRET --domain $AUTH0_CLI_CLIENT_DOMAIN - stderr: "Successfully authenticated to" - exit-code: 0 - auth0 apis list: exit-code: 0