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-601: Supporting multiple identifiers for blocks list and unblock commands #931

Merged
merged 18 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
8 changes: 5 additions & 3 deletions docs/auth0_users_blocks_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ has_toc: false
---
# auth0 users blocks list

List brute-force protection blocks for a given user.
List brute-force protection blocks for a given user by user ID, username, phone number or email.

## Usage
```
Expand All @@ -15,8 +15,10 @@ auth0 users blocks list [flags]
## Examples

```
auth0 users blocks list <user-id>
auth0 users blocks list <user-id> --json
auth0 users blocks list <user-id|username|email|phone-number>
auth0 users blocks list <user-id|username|email|phone-number> --json
auth0 users blocks unblock "auth0|61b5b6e90783fa19f7c57dad
auth0 users blocks unblock "[email protected]
```


Expand Down
7 changes: 5 additions & 2 deletions docs/auth0_users_blocks_unblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ has_toc: false
---
# auth0 users blocks unblock

Remove brute-force protection blocks for a given user.
Remove brute-force protection blocks for a given user by user ID, username, phone number or email.

## Usage
```
Expand All @@ -15,7 +15,10 @@ auth0 users blocks unblock [flags]
## Examples

```
auth0 users blocks unblock <user-id>
auth0 users blocks unblock <user-id|username|email|phone-number>
auth0 users blocks unblock "auth0|61b5b6e90783fa19f7c57dad
auth0 users blocks unblock "[email protected]"

```


Expand Down
39 changes: 39 additions & 0 deletions internal/auth0/mock/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/auth0/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ type UserAPI interface {
// Blocks retrieves a list of blocked IP addresses of a particular user.
Blocks(ctx context.Context, id string, opts ...management.RequestOption) ([]*management.UserBlock, error)

// BlocksByIdentifier retrieves a list of blocked IP addresses of a particular user using any of the user identifiers: username, phone number or email.
BlocksByIdentifier(ctx context.Context, identifier string, opts ...management.RequestOption) ([]*management.UserBlock, error)

// Unblock a user that was blocked due to an excessive amount of incorrectly
// provided credentials.
Unblock(ctx context.Context, id string, opts ...management.RequestOption) error

// UnblockByIdentifier a user that was blocked due to an excessive amount of incorrectly provided credentials using any of the user identifiers: username, phone number or email.
UnblockByIdentifier(ctx context.Context, identifier string, opts ...management.RequestOption) error

// Create a new user.
Create(ctx context.Context, u *management.User, opts ...management.RequestOption) (err error)

Expand Down
52 changes: 34 additions & 18 deletions internal/cli/users_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"net/http"

"github.com/auth0/go-auth0/management"
"github.com/spf13/cobra"
Expand All @@ -25,32 +26,39 @@ func userBlocksCmd(cli *cli) *cobra.Command {

func listUserBlocksCmd(cli *cli) *cobra.Command {
var inputs struct {
userID string
userIdentifier string
}

cmd := &cobra.Command{
Use: "list",
Args: cobra.MaximumNArgs(1),
Short: "List brute-force protection blocks for a given user",
Long: "List brute-force protection blocks for a given user.",
Example: ` auth0 users blocks list <user-id>
auth0 users blocks list <user-id> --json`,
Long: "List brute-force protection blocks for a given user by user ID, username, phone number or email.",
Example: ` auth0 users blocks list <user-id|username|email|phone-number>
auth0 users blocks list <user-id|username|email|phone-number> --json
auth0 users blocks unblock "auth0|61b5b6e90783fa19f7c57dad
auth0 users blocks unblock "[email protected]`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if err := userID.Ask(cmd, &inputs.userID); err != nil {
if err := userID.Ask(cmd, &inputs.userIdentifier); err != nil {
return err
}
} else {
inputs.userID = args[0]
inputs.userIdentifier = args[0]
}

var userBlocks []*management.UserBlock
err := ansi.Waiting(func() (err error) {
userBlocks, err = cli.api.User.Blocks(cmd.Context(), inputs.userID)
userBlocks, err = cli.api.User.Blocks(cmd.Context(), inputs.userIdentifier)
if mErr, ok := err.(management.Error); ok && mErr.Status() != http.StatusBadRequest {
return nil
m3talsmith marked this conversation as resolved.
Show resolved Hide resolved
}

userBlocks, err = cli.api.User.BlocksByIdentifier(cmd.Context(), inputs.userIdentifier)
return err
m3talsmith marked this conversation as resolved.
Show resolved Hide resolved
})
if err != nil {
return fmt.Errorf("failed to list user blocks for user with ID %s: %w", inputs.userID, err)
return fmt.Errorf("failed to list user blocks for user with ID %s: %w", inputs.userIdentifier, err)
}

cli.renderer.UserBlocksList(userBlocks)
Expand All @@ -65,29 +73,37 @@ func listUserBlocksCmd(cli *cli) *cobra.Command {

func deleteUserBlocksCmd(cli *cli) *cobra.Command {
var inputs struct {
userID string
userIdentifier string
}

cmd := &cobra.Command{
Use: "unblock",
Args: cobra.MaximumNArgs(1),
Short: "Remove brute-force protection blocks for a given user",
Long: "Remove brute-force protection blocks for a given user.",
Example: ` auth0 users blocks unblock <user-id>`,
Use: "unblock",
Args: cobra.MaximumNArgs(1),
Short: "Remove brute-force protection blocks for a given user",
Long: "Remove brute-force protection blocks for a given user by user ID, username, phone number or email.",
Example: ` auth0 users blocks unblock <user-id|username|email|phone-number>
auth0 users blocks unblock "auth0|61b5b6e90783fa19f7c57dad
auth0 users blocks unblock "[email protected]"
`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if err := userID.Ask(cmd, &inputs.userID); err != nil {
if err := userID.Ask(cmd, &inputs.userIdentifier); err != nil {
return err
}
} else {
inputs.userID = args[0]
inputs.userIdentifier = args[0]
}

err := ansi.Spinner("Unblocking user...", func() error {
return cli.api.User.Unblock(cmd.Context(), inputs.userID)
err := cli.api.User.Unblock(cmd.Context(), inputs.userIdentifier)
m3talsmith marked this conversation as resolved.
Show resolved Hide resolved
if mErr, ok := err.(management.Error); ok && mErr.Status() != http.StatusBadRequest {
return nil
}

return cli.api.User.UnblockByIdentifier(cmd.Context(), inputs.userIdentifier)
})
if err != nil {
return fmt.Errorf("failed to unblock user with ID %s: %w", inputs.userID, err)
return fmt.Errorf("failed to unblock user with ID %s: %w", inputs.userIdentifier, err)
}

return nil
Expand Down
25 changes: 20 additions & 5 deletions test/integration/users-test-cases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,42 @@ tests:
command: auth0 users roles rm $(./test/integration/scripts/get-user-id.sh) -r $(./test/integration/scripts/get-role-id.sh)
exit-code: 0

015 - users blocks list:
015 - users blocks list by email:
command: auth0 users blocks list "[email protected]"
exit-code: 0
stderr:
contains:
- No user blocks available.

016 - users blocks list by user ID:
command: auth0 users blocks list $(./test/integration/scripts/get-user-id.sh)
exit-code: 0
stderr:
contains:
- No user blocks available.

016 - users blocks list (json):
017 - users blocks list (json):
command: auth0 users blocks list $(./test/integration/scripts/get-user-id.sh) --json
exit-code: 0
stdout:
exactly: "[]"

017 - open user dashboard page:
018 - users unblock by user email:
command: auth0 users blocks unblock "[email protected]"
exit-code: 0

019 - users unblock by user ID:
command: auth0 users blocks unblock $(./test/integration/scripts/get-user-id.sh)
exit-code: 0

020 - open user dashboard page:
command: auth0 users open $(./test/integration/scripts/get-user-id.sh) --no-input
exit-code: 0
stderr:
contains:
- "Open the following URL in a browser: https://manage.auth0.com/dashboard/"

018 - users import:
021 - users import:
command: auth0 users import -c "Username-Password-Authentication" --users "[]" --email-results=false --no-input
exit-code: 0
stderr:
Expand All @@ -133,7 +148,7 @@ tests:
- "successfully started"
- "to get the status of the job"

019 - users import with piped data:
022 - users import with piped data:
command: echo "[]" | auth0 users import -c "Username-Password-Authentication" --email-results=false --no-input
exit-code: 0
stderr:
Expand Down
Loading