Skip to content

Commit

Permalink
Fix logic that permits some commands to run without auth (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught authored Apr 15, 2023
1 parent 4a1f062 commit 4f88a7d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
59 changes: 24 additions & 35 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,56 +84,45 @@ func buildRootCmd(cli *cli) *cobra.Command {
ansi.Initialize(cli.noColor)
prepareInteractivity(cmd)

// If the user is trying to login, no need to go
// through setup.
if cmd.Use == "login" && cmd.Parent().Use == "auth0" {
if !commandRequiresAuthentication(cmd.CommandPath()) {
return nil
}

// We're tracking the login command in its Run method
// so we'll only add this defer if the command is not login
// We're tracking the login command in its Run method, so
// we'll only add this defer if the command is not login.
defer func() {
if cli.tracker != nil && cli.isLoggedIn() {
if cli.tracker != nil && cmd.Name() != "login" && cli.isLoggedIn() {
cli.tracker.TrackCommandRun(cmd, cli.config.InstallID)
}
}()

// If the user is trying to logout, session information
// isn't important as well.
if cmd.Use == "logout" && cmd.Parent().Use == "auth0" {
return nil
}

// Selecting tenants shouldn't really trigger a login.
if cmd.Parent().Use == "tenants" && (cmd.Use == "use" || cmd.Use == "add") {
return nil
}

// Getting the CLI completion script shouldn't trigger a login.
if cmd.Use == "completion" && cmd.Parent().Use == "auth0" {
return nil
}

// Getting help shouldn't trigger a login.
if cmd.CalledAs() == "help" && cmd.Parent().Use == "auth0" {
return nil
}

// config init shouldn't trigger a login.
if cmd.CalledAs() == "init" && cmd.Parent().Use == "config" {
return nil
}

// Initialize everything once. Later callers can then
// freely assume that config is fully primed and ready
// to go.
// Initialize everything once.
return cli.setup(cmd.Context())
},
}

return rootCmd
}

func commandRequiresAuthentication(invokedCommandName string) bool {
commandsWithNoAuthRequired := []string{
"auth0 completion",
"auth0 help",
"auth0 login",
"auth0 logout",
"auth0 tenants use",
"auth0 tenants list",
}

for _, cmd := range commandsWithNoAuthRequired {
if cmd == invokedCommandName {
return false
}
}

return true
}

func addPersistentFlags(rootCmd *cobra.Command, cli *cli) {
rootCmd.PersistentFlags().StringVar(&cli.tenant,
"tenant", cli.config.DefaultTenant, "Specific tenant to use.")
Expand Down
35 changes: 35 additions & 0 deletions internal/cli/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cli

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCommandRequiresAuthentication(t *testing.T) {
var testCases = []struct {
givenCommand string
expectedToRequireAuthentication bool
}{
{"auth0 user list", true},
{"auth0 user create", true},
{"auth0 api", true},
{"auth0 apps list", true},
{"auth0 apps create", true},
{"auth0 orgs members list", true},
{"auth0 completion", false},
{"auth0 help", false},
{"auth0 login", false},
{"auth0 logout", false},
{"auth0 tenants use", false},
{"auth0 tenants list", false},
}

for index, testCase := range testCases {
t.Run(fmt.Sprintf("TestCase #%d Command: %s", index, testCase.givenCommand), func(t *testing.T) {
actualAuth := commandRequiresAuthentication(testCase.givenCommand)
assert.Equal(t, testCase.expectedToRequireAuthentication, actualAuth)
})
}
}

0 comments on commit 4f88a7d

Please sign in to comment.