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

feat(user): add methods to delete totp and backup_code #331

Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions user/api.go

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

24 changes: 24 additions & 0 deletions user/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,27 @@ func (c *Client) CreateTOTP(ctx context.Context, userID string) (*clerk.TOTP, er
err = c.Backend.Call(ctx, req, resource)
return resource, err
}

// DeleteTOTP deletes all the TOTPs from a given user.
func (c *Client) DeleteTOTP(ctx context.Context, userID string) (*MultifactorAuthentication, error) {
path, err := clerk.JoinPath(path, userID, "/totp")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodDelete, path)
resource := &MultifactorAuthentication{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ It's intentional that this isn't the usual DeletedResource type right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, usually it is, but for these MFAs endpoints we're deleting all the records from a userID, so we are returning just the userID (I found a bit odd too, but that's how it was) https://clerk.com/docs/reference/backend-api/tag/Users#operation/DisableMFA

err = c.Backend.Call(ctx, req, resource)
return resource, err
}

// DeleteBackupCode deletes all the backup codes from a given user.
func (c *Client) DeleteBackupCode(ctx context.Context, userID string) (*MultifactorAuthentication, error) {
path, err := clerk.JoinPath(path, userID, "/backup_code")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodDelete, path)
resource := &MultifactorAuthentication{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
36 changes: 36 additions & 0 deletions user/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,39 @@ func TestUserClientCreateTOTP(t *testing.T) {
require.NotNil(t, totp.URI)
require.Equal(t, totp.Object, "totp")
}

func TestUserClientDeleteTOTP(t *testing.T) {
t.Parallel()
userID := "user_123"
config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Method: http.MethodDelete,
Out: json.RawMessage(fmt.Sprintf(`{"user_id": "%s"}`, userID)),
Path: fmt.Sprintf("/v1/users/%s/totp", userID),
},
}
client := NewClient(config)
totp, err := client.DeleteTOTP(context.Background(), userID)
require.NoError(t, err)
require.Equal(t, totp.UserID, userID)
}

func TestUserClientDeleteBackupCode(t *testing.T) {
t.Parallel()
userID := "user_123"
config := &clerk.ClientConfig{}
config.HTTPClient = &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Method: http.MethodDelete,
Out: json.RawMessage(fmt.Sprintf(`{"user_id": "%s"}`, userID)),
Path: fmt.Sprintf("/v1/users/%s/backup_code", userID),
},
}
client := NewClient(config)
totp, err := client.DeleteBackupCode(context.Background(), userID)
require.NoError(t, err)
require.Equal(t, totp.UserID, userID)
}
Loading