-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rename get-token and try-login commands to test login and test token #100
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37119f8
rename get-token and try-login commands to test-login and test-token
morganelle 0412e8c
update comments
morganelle d9719aa
create a higher-level test command and add login-box and token sub co…
morganelle 7088519
fix comments
morganelle 30981fb
update login checking logic
morganelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,179 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/auth0/auth0-cli/internal/ansi" | ||
"github.com/auth0/auth0-cli/internal/auth/authutil" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func testCmd(cli *cli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "test", | ||
Short: "Try your universal login box or get a token", | ||
} | ||
|
||
cmd.SetUsageTemplate(resourceUsageTemplate()) | ||
cmd.AddCommand(testTokenCmd(cli)) | ||
cmd.AddCommand(testLoginCmd(cli)) | ||
|
||
return cmd | ||
} | ||
|
||
func testLoginCmd(cli *cli) *cobra.Command { | ||
var clientID string | ||
var connectionName string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "login", | ||
Short: "Try out your universal login box", | ||
Long: `auth0 test login | ||
Launch a browser to try out your universal login box for the given client. | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var userInfo *authutil.UserInfo | ||
|
||
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 | ||
} | ||
|
||
if proceed := runLoginFlowPreflightChecks(cli, client); !proceed { | ||
return nil | ||
} | ||
|
||
tokenResponse, err := runLoginFlow( | ||
cli, | ||
tenant, | ||
client, | ||
connectionName, | ||
"", // audience is only supported for the test token command | ||
"login", // force a login page when using the test login command | ||
cliLoginTestingScopes, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := ansi.Spinner("Fetching user metadata", func() error { | ||
// Use the access token to fetch user information from the /userinfo | ||
// endpoint. | ||
userInfo, err = authutil.FetchUserInfo(tenant.Domain, tokenResponse.AccessToken) | ||
return err | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprint(cli.renderer.MessageWriter, "\n") | ||
cli.renderer.TryLogin(userInfo, tokenResponse) | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.SetUsageTemplate(resourceUsageTemplate()) | ||
cmd.Flags().StringVarP(&clientID, "client-id", "c", "", "Client Id for which to test login.") | ||
cmd.Flags().StringVarP(&connectionName, "connection", "", "", "Connection to test during login.") | ||
return cmd | ||
} | ||
|
||
func testTokenCmd(cli *cli) *cobra.Command { | ||
var clientID string | ||
var audience string | ||
var scopes []string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "token", | ||
Short: "Fetch a token for the given client and API", | ||
Long: `auth0 test 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 | ||
} | ||
|
||
appType := client.GetAppType() | ||
|
||
cli.renderer.Infof("Domain: " + tenant.Domain) | ||
cli.renderer.Infof("ClientID: " + clientID) | ||
cli.renderer.Infof("Type: " + appType + "\n") | ||
|
||
// 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. | ||
if appType == "non_interactive" { | ||
tokenResponse, err := runClientCredentialsFlow(cli, client, clientID, audience, tenant) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprint(cli.renderer.MessageWriter, "\n") | ||
cli.renderer.GetToken(client, tokenResponse) | ||
return nil | ||
} | ||
|
||
if proceed := runLoginFlowPreflightChecks(cli, client); !proceed { | ||
return nil | ||
} | ||
|
||
tokenResponse, err := runLoginFlow( | ||
cli, | ||
tenant, | ||
client, | ||
"", // specifying a connection is only supported for the test login command | ||
audience, | ||
"", // We don't want to force a prompt for the test token command | ||
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 was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good catch! 👍