Skip to content

Commit

Permalink
DXCDT-583: Convert scopes into a drop down in interactive mode in tes…
Browse files Browse the repository at this point in the history
…t token cmd (#907)

* Convert audience into a drop down in interactive mode in test token cmd

* Conver scopes into a drop down in interactive mode in test token cmd

* Add warning for test token when scopes are given to the cmd
  • Loading branch information
sergiught authored Nov 14, 2023
1 parent 2314fe8 commit f9900b1
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 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 @@ -207,6 +208,10 @@ func testTokenCmd(cli *cli) *cobra.Command {
cli.renderer.Newline()

if client.GetAppType() == appTypeNonInteractive {
if len(inputs.Scopes) != 0 {
cli.renderer.Warnf("Passed in scopes do not apply to Machine to Machine applications.\n")
}

tokenResponse, err := runClientCredentialsFlow(cmd.Context(), cli, client, inputs.Audience, cli.tenant)
if err != nil {
return fmt.Errorf(
Expand All @@ -221,6 +226,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 @@ -408,6 +419,25 @@ func (c *cli) audiencePickerOptions(client *management.Client) func(ctx context.
}
}

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

0 comments on commit f9900b1

Please sign in to comment.