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
Changes from all 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
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
Loading