Skip to content

Commit

Permalink
feat: create TOTP (#322)
Browse files Browse the repository at this point in the history
* feat: create TOTP

* Update totp.go

Co-authored-by: Laura Beatris <[email protected]>

* go gen

---------

Co-authored-by: Laura Beatris <[email protected]>
  • Loading branch information
NicolasLopes7 and LauraBeatris authored Sep 5, 2024
1 parent 7bfc31c commit daecde7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
14 changes: 14 additions & 0 deletions totp.go
Original file line number Diff line number Diff line change
@@ -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"`
}
9 changes: 7 additions & 2 deletions user/api.go

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

14 changes: 13 additions & 1 deletion user/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
22 changes: 21 additions & 1 deletion user/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}

0 comments on commit daecde7

Please sign in to comment.