diff --git a/user/api.go b/user/api.go index 6a2781c..7966f23 100644 --- a/user/api.go +++ b/user/api.go @@ -105,6 +105,16 @@ func CreateTOTP(ctx context.Context, userID string) (*clerk.TOTP, error) { return getClient().CreateTOTP(ctx, userID) } +// DeleteTOTP deletes all the TOTPs from a given user. +func DeleteTOTP(ctx context.Context, userID string) (*MultifactorAuthentication, error) { + return getClient().DeleteTOTP(ctx, userID) +} + +// DeleteBackupCode deletes all the backup codes from a given user. +func DeleteBackupCode(ctx context.Context, userID string) (*MultifactorAuthentication, error) { + return getClient().DeleteBackupCode(ctx, userID) +} + func getClient() *Client { return &Client{ Backend: clerk.GetBackend(), diff --git a/user/client.go b/user/client.go index 06b4736..fe78e43 100644 --- a/user/client.go +++ b/user/client.go @@ -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{} + 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 +} diff --git a/user/client_test.go b/user/client_test.go index ca1da56..fe9c3a2 100644 --- a/user/client_test.go +++ b/user/client_test.go @@ -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) +}