Skip to content

Commit

Permalink
Merge branch 'DXCDT-267-consolidate-add-tenants-into-login-cmd' into …
Browse files Browse the repository at this point in the history
…DXCDT-267-more-reliable-access-token-regen
  • Loading branch information
willvedd committed Dec 7, 2022
2 parents e84586a + 5e4d316 commit fe4dfdc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions docs/auth0_login.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ auth0 login [flags]
```
auth0 login
auth0 login --as-machine
auth0 login --as-machine --domain <TENANT_DOMAIN> --client-id <CLIENT_ID> --client-secret <CLIENT_SECRET>
auth0 login --domain <tenant-domain> --client-id <client-id> --client-secret <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)
Expand Down
54 changes: 32 additions & 22 deletions internal/cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,35 @@ 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",
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,
AlwaysPrompt: true,
}

loginClientSecret = Flag{
Name: "Client Secret",
LongForm: "client-secret",
Help: "Client Secret of the application.",
IsRequired: true,
AlwaysPrompt: true,
}
)

type LoginInputs struct {
LoginAsMachine bool
Domain string
ClientID string
ClientSecret string
Domain string
ClientID string
ClientSecret string
}

func loginCmd(cli *cli) *cobra.Command {
Expand All @@ -44,13 +54,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 <TENANT_DOMAIN> --client-id <CLIENT_ID> --client-secret <CLIENT_SECRET>
auth0 login --domain <tenant-domain> --client-id <client-id> --client-secret <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 {
Expand All @@ -75,14 +84,15 @@ 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, "")
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)
})

Expand Down Expand Up @@ -183,11 +193,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
}

Expand Down
28 changes: 5 additions & 23 deletions internal/cli/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit fe4dfdc

Please sign in to comment.