From 13a26d8896f10c2c349591c54c32550f4d66b8d1 Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Thu, 5 Sep 2024 12:10:38 -0300 Subject: [PATCH 1/3] feat: create TOTP --- totp.go | 14 ++++++++++++++ user/api.go | 9 +++++++-- user/client.go | 12 ++++++++++++ user/client_test.go | 22 +++++++++++++++++++++- 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 totp.go diff --git a/totp.go b/totp.go new file mode 100644 index 0000000..a2c2cc5 --- /dev/null +++ b/totp.go @@ -0,0 +1,14 @@ +package clerk + +// TOTP describes a TOTP (Time-based One-Time Password) 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..bdb469e 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 sets or replaces 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..e82bcde 100644 --- a/user/client.go +++ b/user/client.go @@ -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") +} From c2a30d10ea5c14c1025e437d7197d775d293a50c Mon Sep 17 00:00:00 2001 From: nicolas lopes <57234795+NicolasLopes7@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:23:09 -0300 Subject: [PATCH 2/3] Update totp.go Co-authored-by: Laura Beatris <48022589+LauraBeatris@users.noreply.github.com> --- totp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/totp.go b/totp.go index a2c2cc5..e545c69 100644 --- a/totp.go +++ b/totp.go @@ -1,6 +1,6 @@ package clerk -// TOTP describes a TOTP (Time-based One-Time Password) for a user. +// Describes a Time-based One-time Password (TOTP) for a user. type TOTP struct { APIResource Object string `json:"object"` From 5548cc3627c559f138581078390db4a96d4a319e Mon Sep 17 00:00:00 2001 From: Nicolas Lopes Date: Thu, 5 Sep 2024 13:25:19 -0300 Subject: [PATCH 3/3] go gen --- user/api.go | 2 +- user/client.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/user/api.go b/user/api.go index bdb469e..6a2781c 100644 --- a/user/api.go +++ b/user/api.go @@ -28,7 +28,7 @@ func UpdateProfileImage(ctx context.Context, id string, params *UpdateProfileIma return getClient().UpdateProfileImage(ctx, id, params) } -// DeleteProfileImage sets or replaces the user'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) } diff --git a/user/client.go b/user/client.go index e82bcde..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 {