Skip to content
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

DXCDT-583: Convert scopes into a drop down in interactive mode in test token cmd #907

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/auth0_test_login.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ auth0 test login [flags]
## Flags

```
-a, --audience string The unique identifier of the target API you want to access.
-a, --audience string The unique identifier of the target API you want to access. For Machine to Machine and Regular Web Applications, only the enabled APIs will be shown within the interactive prompt.
-c, --connection-name string The connection name to test during login.
-d, --domain string One of your custom domains.
--force Skip confirmation.
Expand Down
2 changes: 1 addition & 1 deletion docs/auth0_test_token.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ auth0 test token [flags]
## Flags

```
-a, --audience string The unique identifier of the target API you want to access.
-a, --audience string The unique identifier of the target API you want to access. For Machine to Machine and Regular Web Applications, only the enabled APIs will be shown within the interactive prompt.
--force Skip confirmation.
--json Output in json format.
-s, --scopes strings The list of scopes you want to use.
Expand Down
104 changes: 98 additions & 6 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"

"github.com/AlecAivazis/survey/v2"
"github.com/auth0/go-auth0/management"
"github.com/spf13/cobra"
"golang.org/x/net/context"
Expand Down Expand Up @@ -36,7 +37,7 @@ var (
Name: "Audience",
LongForm: "audience",
ShortForm: "a",
Help: "The unique identifier of the target API you want to access.",
Help: "The unique identifier of the target API you want to access. For Machine to Machine and Regular Web Applications, only the enabled APIs will be shown within the interactive prompt.",
}

testAudienceRequired = Flag{
Expand Down Expand Up @@ -193,18 +194,20 @@ func testTokenCmd(cli *cli) *cobra.Command {
return err
}

if err := testAudience.Ask(cmd, &inputs.Audience, nil); err != nil {
if err := testAudienceRequired.Pick(
cmd,
&inputs.Audience,
cli.audiencePickerOptions(client),
); err != nil {
return err
}

appType := client.GetAppType()

cli.renderer.Infof("Domain : " + ansi.Blue(cli.tenant))
cli.renderer.Infof("Client ID : " + ansi.Bold(client.GetClientID()))
cli.renderer.Infof("Type : " + display.ApplyColorToFriendlyAppType(display.FriendlyAppType(appType)))
cli.renderer.Infof("Type : " + display.ApplyColorToFriendlyAppType(display.FriendlyAppType(client.GetAppType())))
cli.renderer.Newline()

if appType == appTypeNonInteractive {
if client.GetAppType() == appTypeNonInteractive {
tokenResponse, err := runClientCredentialsFlow(cmd.Context(), cli, client, inputs.Audience, cli.tenant)
if err != nil {
return fmt.Errorf(
Expand All @@ -219,6 +222,12 @@ func testTokenCmd(cli *cli) *cobra.Command {
return nil
}

if len(inputs.Scopes) == 0 {
if err := cli.pickTokenScopes(cmd.Context(), &inputs); err != nil {
return err
}
}

if proceed := runLoginFlowPreflightChecks(cli, client); !proceed {
return nil
}
Expand Down Expand Up @@ -342,6 +351,89 @@ func (c *cli) appPickerWithCreateOption(ctx context.Context) (pickerOptions, err
return enhancedOptions, nil
}

func (c *cli) audiencePickerOptions(client *management.Client) func(ctx context.Context) (pickerOptions, error) {
return func(ctx context.Context) (pickerOptions, error) {
var opts pickerOptions

switch client.GetAppType() {
case "regular_web", "non_interactive":
clientGrants, err := c.api.ClientGrant.List(
ctx,
management.PerPage(100),
management.Parameter("client_id", client.GetClientID()),
)
if err != nil {
return nil, err
}

if len(clientGrants.ClientGrants) == 0 {
return nil, fmt.Errorf(
"the %s application is not authorized to request access tokens for any APIs.\n\n"+
"Run: 'auth0 apps open %s' to open the dashboard and authorize the application.",
ansi.Bold(client.GetName()),
client.GetClientID(),
)
}

for _, grant := range clientGrants.ClientGrants {
resourceServer, err := c.api.ResourceServer.Read(ctx, grant.GetAudience())
if err != nil {
return nil, err
}

label := fmt.Sprintf(
"%s %s",
resourceServer.GetName(),
ansi.Faint(fmt.Sprintf("(%s)", resourceServer.GetIdentifier())),
)

opts = append(opts, pickerOption{
label: label,
value: resourceServer.GetIdentifier(),
})
}
default:
resourceServerList, err := c.api.ResourceServer.List(ctx, management.PerPage(100))
if err != nil {
return nil, err
}

for _, resourceServer := range resourceServerList.ResourceServers {
label := fmt.Sprintf(
"%s %s",
resourceServer.GetName(),
ansi.Faint(fmt.Sprintf("(%s)", resourceServer.GetIdentifier())),
)
opts = append(opts, pickerOption{
label: label,
value: resourceServer.GetIdentifier(),
})
}
}

return opts, nil
}
}

func (c *cli) pickTokenScopes(ctx context.Context, inputs *testCmdInputs) error {
resourceServer, err := c.api.ResourceServer.Read(ctx, inputs.Audience)
if err != nil {
return err
}

var scopes []string
for _, scope := range resourceServer.GetScopes() {
scopes = append(scopes, scope.GetValue())
}

scopesPrompt := &survey.MultiSelect{
Message: "Scopes",
Options: scopes,
}

return survey.AskOne(scopesPrompt, &inputs.Scopes)
}

func checkClientIsAuthorizedForAPI(ctx context.Context, cli *cli, client *management.Client, audience string) error {
var list *management.ClientGrantList
if err := ansi.Waiting(func() (err error) {
Expand Down
Loading