Skip to content

Commit

Permalink
Allow access token to be piped on test login [CLI-186] (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Jun 22, 2021
1 parent ea68099 commit 331bd86
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
4 changes: 0 additions & 4 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ auth0 test token --client-id <id> --audience <audience> --scopes <scope1,scope2>
if err != nil {
return fmt.Errorf("An unexpected error occurred while logging in to machine-to-machine client %s: %w", inputs.ClientID, err)
}

fmt.Fprint(cli.renderer.MessageWriter, "\n")
cli.renderer.GetToken(client, tokenResponse)
return nil
}
Expand All @@ -278,8 +276,6 @@ auth0 test token --client-id <id> --audience <audience> --scopes <scope1,scope2>
if err != nil {
return fmt.Errorf("An unexpected error occurred when logging in to client %s: %w", inputs.ClientID, err)
}

fmt.Fprint(cli.renderer.MessageWriter, "\n")
cli.renderer.GetToken(client, tokenResponse)
return nil
},
Expand Down
14 changes: 14 additions & 0 deletions internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/charmbracelet/glamour"
"github.com/olekukonko/tablewriter"
)
Expand Down Expand Up @@ -274,3 +275,16 @@ func boolean(v bool) string {
}
return ansi.Red("✗")
}

func isOutputPiped() bool {
fi, err := os.Stdout.Stat()
if err != nil {
panic(auth0.Error(err, "failed to get the FileInfo struct of stdout"))
}

if (fi.Mode() & os.ModeCharDevice) == 0 {
return true
}

return false
}
20 changes: 13 additions & 7 deletions internal/display/get_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"fmt"
"strconv"

"github.com/auth0/auth0-cli/internal/ansi"
"github.com/auth0/auth0-cli/internal/auth/authutil"
"github.com/auth0/auth0-cli/internal/auth0"
"gopkg.in/auth0.v5/management"
)

func (r *Renderer) GetToken(c *management.Client, t *authutil.TokenResponse) {
r.Heading(fmt.Sprintf("tokens for %s", auth0.StringValue(c.Name)))
// pass the access token to the pipe and exit
if isOutputPiped() {
fmt.Fprint(r.ResultWriter, t.AccessToken)
return
}

fmt.Fprint(r.ResultWriter, "\n")
r.Heading(fmt.Sprintf("token for %s", auth0.StringValue(c.Name)))

switch r.Format {
case OutputFormatJSON:
Expand All @@ -26,24 +32,24 @@ func (r *Renderer) GetToken(c *management.Client, t *authutil.TokenResponse) {
rows := make([][]string, 0)

if isNotZero(t.AccessToken) {
rows = append(rows, []string{ansi.Faint("AccessToken"), t.AccessToken})
rows = append(rows, []string{"ACCESS TOKEN", t.AccessToken})
}
if isNotZero(t.RefreshToken) {
rows = append(rows, []string{ansi.Faint("RefreshToken"), t.RefreshToken})
rows = append(rows, []string{"REFRESH TOKEN", t.RefreshToken})
}
// TODO: This is a long string and it messes up formatting when printed
// to the table, so need to come back to this one and fix it later.
// if isNotZero(t.IDToken) {
// rows = append(rows, []string{ansi.Faint("IDToken"), t.IDToken})
// }
if isNotZero(t.TokenType) {
rows = append(rows, []string{ansi.Faint("TokenType"), t.TokenType})
rows = append(rows, []string{"TOKEN TYPE", t.TokenType})
}
if isNotZero(t.ExpiresIn) {
rows = append(rows, []string{ansi.Faint("ExpiresIn"), strconv.FormatInt(t.ExpiresIn, 10)})
rows = append(rows, []string{"EXPIRES IN", strconv.FormatInt(t.ExpiresIn, 10)})
}

tableHeader := []string{"Field", "Value"}
tableHeader := []string{"", ""}
writeTable(r.ResultWriter, tableHeader, rows)
}
}

0 comments on commit 331bd86

Please sign in to comment.