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: create TOTP #322

Merged
merged 3 commits into from
Sep 5, 2024
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
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")
}
Loading