Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/auth0/auth0-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Christenson II committed Nov 16, 2023
2 parents 7dbf8ea + ff7566f commit 1fd7dc2
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 25 deletions.
12 changes: 8 additions & 4 deletions docs/auth0_users_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ auth0 users search [flags]
## Flags

```
--json Output in json format.
-n, --number int Number of users, that match the search criteria, to retrieve. Minimum 1, maximum 1000. If limit is hit, refine the search query. (default 50)
-q, --query string Query in Lucene query syntax. See https://auth0.com/docs/users/user-search/user-search-query-syntax for more details.
-s, --sort string Field to sort by. Use 'field:order' where 'order' is '1' for ascending and '-1' for descending. e.g. 'created_at:1'.
--json Output in json format.
-n, --number int Number of users, that match the search criteria, to retrieve. Minimum 1, maximum 1000. If limit is hit, refine the search query. (default 50)
-q, --query email:"user123@*.com" OR (user_id:"user-id-123" AND name:"Bob") Search query in Lucene query syntax.
For example: email:"user123@*.com" OR (user_id:"user-id-123" AND name:"Bob")
For more info: https://auth0.com/docs/users/user-search/user-search-query-syntax.
-s, --sort string Field to sort by. Use 'field:order' where 'order' is '1' for ascending and '-1' for descending. e.g. 'created_at:1'.
```


Expand Down
6 changes: 5 additions & 1 deletion internal/cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func apiCmd(cli *cli) *cobra.Command {

cmd := &cobra.Command{
Use: "api <method> <url-path>",
Args: cobra.RangeArgs(1, 2),
Args: cobra.RangeArgs(0, 2),
Short: "Makes an authenticated HTTP request to the Auth0 Management API",
Long: fmt.Sprintf(
`Makes an authenticated HTTP request to the [Auth0 Management API](%s) and returns the response as JSON.
Expand Down Expand Up @@ -117,6 +117,10 @@ func apiUsageTemplate() string {

func apiCmdRun(cli *cli, inputs *apiCmdInputs) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return cmd.Help()
}

if err := inputs.fromArgs(args, cli.tenant); err != nil {
return fmt.Errorf("failed to parse command inputs: %w", err)
}
Expand Down
36 changes: 31 additions & 5 deletions internal/cli/prompts_custom_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var (
Help: "Text contents for the branding.",
IsRequired: true,
}

customTextPrompt = Argument{
Name: "Prompt",
Help: "ID of custom text prompt.",
}
)

type promptsTextInput struct {
Expand All @@ -59,18 +64,27 @@ func universalLoginPromptsTextCmd(cli *cli) *cobra.Command {
return cmd
}

func customTextPromptOptions(_ context.Context) (pickerOptions, error) {
var opts pickerOptions
for _, promptType := range customTextPromptTypes {
opts = append(opts, pickerOption{value: promptType, label: promptType})
}
return opts, nil
}

func showPromptsTextCmd(cli *cli) *cobra.Command {
var inputs promptsTextInput

cmd := &cobra.Command{
Use: "show",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Short: "Show the custom text for a prompt",
Long: "Show the custom text for a prompt.",
Example: ` auth0 universal-login prompts show <prompt>
auth0 universal-login prompts show <prompt> --language <language>
auth0 ul prompts show <prompt> -l <language>
auth0 ul prompts show signup -l es`,

RunE: showPromptsText(cli, &inputs),
}

Expand All @@ -84,7 +98,7 @@ func updatePromptsTextCmd(cli *cli) *cobra.Command {

cmd := &cobra.Command{
Use: "update",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Short: "Update the custom text for a prompt",
Long: "Update the custom text for a prompt.",
Example: ` auth0 universal-login prompts update <prompt>
Expand All @@ -100,9 +114,15 @@ func updatePromptsTextCmd(cli *cli) *cobra.Command {

func showPromptsText(cli *cli, inputs *promptsTextInput) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
inputs.Prompt = args[0]
brandingText := make(map[string]interface{})
if len(args) == 0 {
if err := customTextPrompt.Pick(cmd, &inputs.Prompt, customTextPromptOptions); err != nil {
return err
}
} else {
inputs.Prompt = args[0]
}

brandingText := make(map[string]interface{})
if err := ansi.Waiting(func() (err error) {
brandingText, err = cli.api.Prompt.CustomText(cmd.Context(), inputs.Prompt, inputs.Language)
return err
Expand All @@ -128,7 +148,13 @@ func showPromptsText(cli *cli, inputs *promptsTextInput) func(cmd *cobra.Command

func updateBrandingText(cli *cli, inputs *promptsTextInput) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
inputs.Prompt = args[0]
if len(args) == 0 {
if err := customTextPrompt.Pick(cmd, &inputs.Prompt, customTextPromptOptions); err != nil {
return err
}
} else {
inputs.Prompt = args[0]
}
inputs.Body = string(iostream.PipedInput())

brandingTextToEdit, err := fetchBrandingTextContentToEdit(cmd.Context(), cli, inputs)
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func buildRootCmd(cli *cli) *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
ansi.Initialize(cli.noColor)
prepareInteractivity(cmd)
cli.configureRenderer()

if !commandRequiresAuthentication(cmd.CommandPath()) {
return nil
Expand All @@ -105,7 +106,6 @@ func buildRootCmd(cli *cli) *cobra.Command {
return err
}

cli.configureRenderer()
return nil
},
}
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/terraform_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,17 @@ func (f *promptResourceFetcher) FetchData(_ context.Context) (importDataList, er
}, nil
}

var customTextPromptTypes = []string{"login", "login-id", "login-password", "login-email-verification", "signup", "signup-id", "signup-password", "reset-password", "consent", "mfa-push", "mfa-otp", "mfa-voice", "mfa-phone", "mfa-webauthn", "mfa-sms", "mfa-email", "mfa-recovery-code", "mfa", "status", "device-flow", "email-verification", "email-otp-challenge", "organizations", "invitation", "common"}

func (f *promptCustomTextResourceFetcherResourceFetcher) FetchData(ctx context.Context) (importDataList, error) {
tenant, err := f.api.Tenant.Read(ctx)
if err != nil {
return nil, err
}
promptTypes := []string{"login", "login-id", "login-password", "login-email-verification", "signup", "signup-id", "signup-password", "reset-password", "consent", "mfa-push", "mfa-otp", "mfa-voice", "mfa-phone", "mfa-webauthn", "mfa-sms", "mfa-email", "mfa-recovery-code", "mfa", "status", "device-flow", "email-verification", "email-otp-challenge", "organizations", "invitation", "common"}

var data importDataList
for _, language := range tenant.GetEnabledLocales() {
for _, promptType := range promptTypes {
for _, promptType := range customTextPromptTypes {
data = append(data, importDataItem{
ResourceName: "auth0_prompt_custom_text." + sanitizeResourceName(language+"_"+promptType),
ImportID: promptType + "::" + language,
Expand Down
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
2 changes: 1 addition & 1 deletion internal/cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var (
Name: "Query",
LongForm: "query",
ShortForm: "q",
Help: "Query in Lucene query syntax. See https://auth0.com/docs/users/user-search/user-search-query-syntax for more details.",
Help: "Search query in Lucene query syntax.\n\nFor example: `email:\"user123@*.com\" OR (user_id:\"user-id-123\" AND name:\"Bob\")`\n\n For more info: https://auth0.com/docs/users/user-search/user-search-query-syntax.",
IsRequired: true,
}
userSort = Flag{
Expand Down
26 changes: 18 additions & 8 deletions internal/display/tenants.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
package display

import "github.com/auth0/auth0-cli/internal/ansi"

type tenantView struct {
Name string
raw interface{}
Active bool
Name string
raw interface{}
}

func (v *tenantView) AsTableHeader() []string {
return []string{"Available tenants"}
return []string{"Active", "Tenant"}
}

func (v *tenantView) AsTableRow() []string {
return []string{v.Name}
activeText := ""
if v.Active {
activeText = ansi.Green("→")
}

return []string{
activeText,
v.Name,
}
}

func (v *tenantView) Object() interface{} {
return v.raw
}

func (r *Renderer) TenantList(data []string) {
r.Heading()

if len(data) == 0 {
r.EmptyState("tenants", "Use 'auth0 login' to add one")
return
Expand All @@ -28,8 +37,9 @@ func (r *Renderer) TenantList(data []string) {
var results []View
for _, item := range data {
results = append(results, &tenantView{
Name: item,
raw: item,
Active: item == r.Tenant,
Name: item,
raw: item,
})
}

Expand Down
12 changes: 9 additions & 3 deletions test/integration/test-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ config:
retries: 1

tests:
auth0 tenants list:
exit-code: 0

auth0 completion bash:
exit-code: 0

Expand Down Expand Up @@ -100,6 +97,15 @@ tests:
- STAGE_PRE_USER_REGISTRATION_RATE
exit-code: 0

tenants list:
command: auth0 tenants list
exit-code: 0
stdout:
contains:
- ACTIVE
- TENANT
-

tenants use:
command: auth0 tenants use $AUTH0_DOMAIN
exit-code: 0
Expand Down

0 comments on commit 1fd7dc2

Please sign in to comment.