-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add get-token command for fetching user-facing tokens for an API
- Loading branch information
Paddy Carey
committed
Jan 27, 2021
1 parent
bac0b29
commit 0064b56
Showing
4 changed files
with
239 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func getTokenCmd(cli *cli) *cobra.Command { | ||
var clientID string | ||
var audience string | ||
var scopes []string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "get-token", | ||
Short: "fetch a token for the given client and API.", | ||
Long: `$ auth0 get-token | ||
Fetch an access token for the given client and API. | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
tenant, err := cli.getTenant() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// use the client ID as passed in by the user, or default to the | ||
// "CLI Login Testing" client if none passed. This client is only | ||
// used for testing login from the CLI and will be created if it | ||
// does not exist. | ||
if clientID == "" { | ||
client, err := getOrCreateCLITesterClient(cli.api.Client) | ||
if err != nil { | ||
return err | ||
} | ||
clientID = client.GetClientID() | ||
} | ||
|
||
client, err := cli.api.Client.Read(clientID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: We can check here if the client is an m2m client, and if so | ||
// initiate the client credentials flow instead to fetch a token, | ||
// avoiding the browser and HTTP server shenanigans altogether. | ||
|
||
abort, needsLocalCallbackURL := runLoginFlowPreflightChecks(cli, client) | ||
if abort { | ||
return nil | ||
} | ||
|
||
tokenResponse, err := runLoginFlow( | ||
cli, | ||
tenant, | ||
client, | ||
"", // specifying a connection is only supported for try-login | ||
needsLocalCallbackURL, | ||
audience, | ||
scopes, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprint(cli.renderer.MessageWriter, "\n") | ||
cli.renderer.GetToken(client, tokenResponse) | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.SetUsageTemplate(resourceUsageTemplate()) | ||
cmd.Flags().StringVarP(&clientID, "client-id", "c", "", "Client ID for which to fetch a token.") | ||
cmd.Flags().StringVarP(&audience, "audience", "a", "", "The unique identifier of the target API you want to access.") | ||
cmd.Flags().StringSliceVarP(&scopes, "scope", "s", []string{}, "Client ID for which to test login.") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package display | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/auth0/auth0-cli/internal/ansi" | ||
"github.com/auth0/auth0-cli/internal/auth" | ||
"github.com/auth0/auth0-cli/internal/auth0" | ||
"gopkg.in/auth0.v5/management" | ||
) | ||
|
||
func (r *Renderer) GetToken(c *management.Client, t *auth.TokenResponse) { | ||
r.Heading(ansi.Bold(auth0.StringValue(c.Name)), "tokens\n") | ||
|
||
switch r.Format { | ||
case OutputFormatJSON: | ||
b, err := json.MarshalIndent(t, "", " ") | ||
if err != nil { | ||
r.Errorf("couldn't marshal results as JSON: %v", err) | ||
return | ||
} | ||
fmt.Fprint(r.ResultWriter, string(b)) | ||
default: | ||
rows := make([][]string, 0) | ||
|
||
if isNotZero(t.AccessToken) { | ||
rows = append(rows, []string{ansi.Faint("AccessToken"), t.AccessToken}) | ||
} | ||
if isNotZero(t.RefreshToken) { | ||
rows = append(rows, []string{ansi.Faint("RefreshToken"), 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}) | ||
} | ||
if isNotZero(t.ExpiresIn) { | ||
rows = append(rows, []string{ansi.Faint("ExpiresIn"), strconv.FormatInt(t.ExpiresIn, 10)}) | ||
} | ||
|
||
tableHeader := []string{"Field", "Value"} | ||
writeTable(r.ResultWriter, tableHeader, rows, nil) | ||
} | ||
} |