Skip to content

Commit

Permalink
Convert audience into a drop down in interactive mode in test token cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Nov 10, 2023
1 parent ff1bd0a commit ca57c3c
Showing 1 changed file with 71 additions and 5 deletions.
76 changes: 71 additions & 5 deletions internal/cli/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,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 Down Expand Up @@ -342,6 +344,70 @@ 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 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

0 comments on commit ca57c3c

Please sign in to comment.