From daecde7b3ae79791b9ba4a92c58d737580961d8f Mon Sep 17 00:00:00 2001 From: nicolas lopes <57234795+NicolasLopes7@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:38:06 -0300 Subject: [PATCH] feat: create TOTP (#322) * feat: create TOTP * Update totp.go Co-authored-by: Laura Beatris <48022589+LauraBeatris@users.noreply.github.com> * go gen --------- Co-authored-by: Laura Beatris <48022589+LauraBeatris@users.noreply.github.com> --- totp.go | 14 ++++++++++++++ user/api.go | 9 +++++++-- user/client.go | 14 +++++++++++++- user/client_test.go | 22 +++++++++++++++++++++- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 totp.go diff --git a/totp.go b/totp.go new file mode 100644 index 0000000..e545c69 --- /dev/null +++ b/totp.go @@ -0,0 +1,14 @@ +package clerk + +// Describes a Time-based One-time Password (TOTP) for a user. +type TOTP struct { + APIResource + Object string `json:"object"` + ID string `json:"id"` + Secret *string `json:"secret"` + URI *string `json:"uri" ` + Verified bool `json:"verified"` + BackupCodes []string `json:"backup_codes"` + CreatedAt int64 `json:"created_at"` + UpdatedAt int64 `json:"updated_at"` +} diff --git a/user/api.go b/user/api.go index 5f10c6d..6a2781c 100644 --- a/user/api.go +++ b/user/api.go @@ -23,12 +23,12 @@ func Update(ctx context.Context, id string, params *UpdateParams) (*clerk.User, return getClient().Update(ctx, id, params) } -// UpdateProfileImage sets or replaces the users's profile image. +// UpdateProfileImage sets or replaces the user's profile image. func UpdateProfileImage(ctx context.Context, id string, params *UpdateProfileImageParams) (*clerk.User, error) { return getClient().UpdateProfileImage(ctx, id, params) } -// DeleteProfileImage removes the users's profile image. +// DeleteProfileImage deletes the user's profile image. func DeleteProfileImage(ctx context.Context, id string) (*clerk.User, error) { return getClient().DeleteProfileImage(ctx, id) } @@ -100,6 +100,11 @@ func DeleteWeb3Wallet(ctx context.Context, userID, identificationID string) (*cl return getClient().DeleteWeb3Wallet(ctx, userID, identificationID) } +// CreateTOTP creates a TOTP (Time-based One-Time Password) for the user. +func CreateTOTP(ctx context.Context, userID string) (*clerk.TOTP, error) { + return getClient().CreateTOTP(ctx, userID) +} + func getClient() *Client { return &Client{ Backend: clerk.GetBackend(), diff --git a/user/client.go b/user/client.go index 031a9be..06b4736 100644 --- a/user/client.go +++ b/user/client.go @@ -155,7 +155,7 @@ func (c *Client) UpdateProfileImage(ctx context.Context, id string, params *Upda return resource, err } -// DeleteProfileImage sets or replaces the user's profile image. +// DeleteProfileImage deletes the user's profile image. func (c *Client) DeleteProfileImage(ctx context.Context, id string) (*clerk.User, error) { path, err := clerk.JoinPath(path, id, "/profile_image") if err != nil { @@ -452,3 +452,15 @@ func (c *Client) DeleteWeb3Wallet(ctx context.Context, userID, identificationID err = c.Backend.Call(ctx, req, resource) return resource, err } + +// CreateTOTP creates a TOTP (Time-based One-Time Password) for the user. +func (c *Client) CreateTOTP(ctx context.Context, userID string) (*clerk.TOTP, error) { + path, err := clerk.JoinPath(path, userID, "/totp") + if err != nil { + return nil, err + } + req := clerk.NewAPIRequest(http.MethodPost, path) + resource := &clerk.TOTP{} + err = c.Backend.Call(ctx, req, resource) + return resource, err +} diff --git a/user/client_test.go b/user/client_test.go index 5ef97ca..ca1da56 100644 --- a/user/client_test.go +++ b/user/client_test.go @@ -441,7 +441,6 @@ func TestUserClientDeletePasskey(t *testing.T) { require.NoError(t, err) require.Equal(t, passkeyIdentificationID, passkey.ID) } - func TestUserClientDeleteWeb3Wallet(t *testing.T) { t.Parallel() userID := "user_123" @@ -460,3 +459,24 @@ func TestUserClientDeleteWeb3Wallet(t *testing.T) { require.NoError(t, err) require.Equal(t, web3WalletIdentificationID, web3Wallet.ID) } + +func TestUserClientCreateTOTP(t *testing.T) { + t.Parallel() + userID := "user_123" + config := &clerk.ClientConfig{} + config.HTTPClient = &http.Client{ + Transport: &clerktest.RoundTripper{ + T: t, + Method: http.MethodPost, + Out: json.RawMessage(`{"backup_codes":[],"created_at":1725548779338,"id":"totp_id","object":"totp","secret":"secret","updated_at":1725548779338,"uri":"otpauth://totp/","verified":false}`), + Path: fmt.Sprintf("/v1/users/%s/totp", userID), + }, + } + client := NewClient(config) + totp, err := client.CreateTOTP(context.Background(), userID) + require.NoError(t, err) + require.NotNil(t, totp.ID) + require.NotNil(t, totp.Secret) + require.NotNil(t, totp.URI) + require.Equal(t, totp.Object, "totp") +}