Skip to content

Commit

Permalink
Merge pull request #108 from auth0/cli-34-authn-status
Browse files Browse the repository at this point in the history
CLI-34: include authn status on the help text of the root command
  • Loading branch information
rene00 authored Mar 1, 2021
2 parents 6c5177d + bac7cba commit ac00134
Show file tree
Hide file tree
Showing 273 changed files with 77,087 additions and 1,515 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/* linguist-generated=true
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/fatih/color v1.10.0 // indirect
github.com/golang/mock v1.4.4
github.com/google/go-cmp v0.5.4
github.com/lestrrat-go/jwx v1.1.3
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mattn/go-isatty v0.0.12
github.com/mattn/go-runewidth v0.0.10 // indirect
Expand All @@ -17,10 +18,10 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.1
github.com/tidwall/pretty v1.0.2
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221
gopkg.in/auth0.v5 v5.8.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.8
Expand Down
49 changes: 41 additions & 8 deletions go.sum

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/display"
"github.com/lestrrat-go/jwx/jwt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/auth0.v5/management"
Expand Down Expand Up @@ -81,7 +82,23 @@ func (c *cli) isLoggedIn() bool {
// No need to check errors for initializing context.
_ = c.init()

return c.tenant != ""
if c.tenant == "" {
return false
}

// Parse the access token for the tenant.
t, err := jwt.ParseString(c.config.Tenants[c.tenant].AccessToken)
if err != nil {
return false
}

// Check if token is valid.
if err = jwt.Validate(t, jwt.WithIssuer("https://auth0.auth0.com/")); err != nil {
return false
}

return true

}

// setup will try to initialize the config context, as well as figure out if
Expand Down
45 changes: 45 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package cli

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/auth0/auth0-cli/internal/display"
"github.com/google/go-cmp/cmp"
"github.com/olekukonko/tablewriter"
"github.com/stretchr/testify/assert"
)

func TestIsExpired(t *testing.T) {
Expand Down Expand Up @@ -66,3 +71,43 @@ func expectTable(t testing.TB, got string, header []string, data [][]string) {
t.Fatal(cmp.Diff(want, got))
}
}

func TestIsLoggedIn(t *testing.T) {
tests := []struct {
defaultTenant string
tenants map[string]tenant
want bool
desc string
}{
{"", map[string]tenant{}, false, "no tenants"},
{"t0", map[string]tenant{}, false, "tenant is set but no tenants map"},
{"t0", map[string]tenant{"t0": tenant{}}, false, "tenants map set but invalid token"},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
tmpFile, err := ioutil.TempFile(os.TempDir(), "isLoggedIn-")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpFile.Name())

type Config struct {
DefaultTenant string `json:"default_tenant"`
Tenants map[string]tenant `json:"tenants"`
}

b, err := json.Marshal(&Config{test.defaultTenant, test.tenants})
if err != nil {
t.Fatal(err)
}

if err = ioutil.WriteFile(tmpFile.Name(), b, 0400); err != nil {
t.Fatal(err)
}

c := cli{renderer: display.NewRenderer(), path: tmpFile.Name()}
assert.Equal(t, test.want, c.isLoggedIn())
})
}
}
4 changes: 2 additions & 2 deletions internal/cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/auth0/auth0-cli/internal/ansi"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

//
Expand Down Expand Up @@ -157,7 +157,7 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
func getTerminalWidth() int {
var width int

width, _, err := terminal.GetSize(0)
width, _, err := term.GetSize(0)
if err != nil {
width = 80
}
Expand Down
17 changes: 17 additions & 0 deletions vendor/github.com/decred/dcrd/dcrec/secp256k1/v3/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions vendor/github.com/decred/dcrd/dcrec/secp256k1/v3/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Loading

0 comments on commit ac00134

Please sign in to comment.