-
Notifications
You must be signed in to change notification settings - Fork 15
/
client.go
368 lines (332 loc) · 12.3 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Package user provides the Users API.
package user
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/clerk/clerk-sdk-go/v2"
)
//go:generate go run ../cmd/gen/main.go
const path = "/users"
// Client is used to invoke the Users API.
type Client struct {
Backend clerk.Backend
}
func NewClient(config *clerk.ClientConfig) *Client {
return &Client{
Backend: clerk.NewBackend(&config.BackendConfig),
}
}
type CreateParams struct {
clerk.APIParams
EmailAddresses *[]string `json:"email_address,omitempty"`
PhoneNumbers *[]string `json:"phone_number,omitempty"`
Web3Wallets *[]string `json:"web3_wallet,omitempty"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
FirstName *string `json:"first_name,omitempty"`
LastName *string `json:"last_name,omitempty"`
ExternalID *string `json:"external_id,omitempty"`
UnsafeMetadata *json.RawMessage `json:"unsafe_metadata,omitempty"`
PublicMetadata *json.RawMessage `json:"public_metadata,omitempty"`
PrivateMetadata *json.RawMessage `json:"private_metadata,omitempty"`
PasswordDigest *string `json:"password_digest,omitempty"`
PasswordHasher *string `json:"password_hasher,omitempty"`
SkipPasswordRequirement *bool `json:"skip_password_requirement,omitempty"`
SkipPasswordChecks *bool `json:"skip_password_checks,omitempty"`
TOTPSecret *string `json:"totp_secret,omitempty"`
BackupCodes *[]string `json:"backup_codes,omitempty"`
// Specified in RFC3339 format
CreatedAt *string `json:"created_at,omitempty"`
}
// Create creates a new user.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.User, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)
resource := &clerk.User{}
err := c.Backend.Call(ctx, req, resource)
return resource, err
}
// Get retrieves details about the user.
func (c *Client) Get(ctx context.Context, id string) (*clerk.User, error) {
path, err := clerk.JoinPath(path, id)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
resource := &clerk.User{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
type UpdateParams struct {
clerk.APIParams
FirstName *string `json:"first_name,omitempty"`
LastName *string `json:"last_name,omitempty"`
PrimaryEmailAddressID *string `json:"primary_email_address_id,omitempty"`
NotifyPrimaryEmailAddressChanged *bool `json:"notify_primary_email_address_changed,omitempty"`
PrimaryPhoneNumberID *string `json:"primary_phone_number_id,omitempty"`
PrimaryWeb3WalletID *string `json:"primary_web3_wallet_id,omitempty"`
Username *string `json:"username,omitempty"`
ProfileImageID *string `json:"profile_image_id,omitempty"`
ProfileImage *string `json:"profile_image,omitempty"`
Password *string `json:"password,omitempty"`
PasswordDigest *string `json:"password_digest,omitempty"`
PasswordHasher *string `json:"password_hasher,omitempty"`
SkipPasswordChecks *bool `json:"skip_password_checks,omitempty"`
SignOutOfOtherSessions *bool `json:"sign_out_of_other_sessions,omitempty"`
ExternalID *string `json:"external_id,omitempty"`
PublicMetadata *json.RawMessage `json:"public_metadata,omitempty"`
PrivateMetadata *json.RawMessage `json:"private_metadata,omitempty"`
UnsafeMetadata *json.RawMessage `json:"unsafe_metadata,omitempty"`
TOTPSecret *string `json:"totp_secret,omitempty"`
BackupCodes *[]string `json:"backup_codes,omitempty"`
DeleteSelfEnabled *bool `json:"delete_self_enabled,omitempty"`
CreateOrganizationEnabled *bool `json:"create_organization_enabled,omitempty"`
// Specified in RFC3339 format
CreatedAt *string `json:"created_at,omitempty"`
}
// Update updates a user.
func (c *Client) Update(ctx context.Context, id string, params *UpdateParams) (*clerk.User, error) {
path, err := clerk.JoinPath(path, id)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPatch, path)
req.SetParams(params)
resource := &clerk.User{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
type UpdateMetadataParams struct {
clerk.APIParams
PublicMetadata *json.RawMessage `json:"public_metadata,omitempty"`
PrivateMetadata *json.RawMessage `json:"private_metadata,omitempty"`
UnsafeMetadata *json.RawMessage `json:"unsafe_metadata,omitempty"`
}
// UpdateMetadata updates the user's metadata by merging the
// provided values with the existing ones.
func (c *Client) UpdateMetadata(ctx context.Context, id string, params *UpdateMetadataParams) (*clerk.User, error) {
path, err := clerk.JoinPath(path, id, "/metadata")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPatch, path)
req.SetParams(params)
resource := &clerk.User{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
// Delete deletes a user.
func (c *Client) Delete(ctx context.Context, id string) (*clerk.DeletedResource, error) {
path, err := clerk.JoinPath(path, id)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodDelete, path)
resource := &clerk.DeletedResource{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
type ListParams struct {
clerk.APIParams
clerk.ListParams
OrderBy *string `json:"order_by,omitempty"`
Query *string `json:"query,omitempty"`
EmailAddresses []string `json:"email_address,omitempty"`
ExternalIDs []string `json:"external_id,omitempty"`
PhoneNumbers []string `json:"phone_number,omitempty"`
Web3Wallets []string `json:"web3_wallet,omitempty"`
Usernames []string `json:"username,omitempty"`
UserIDs []string `json:"user_id,omitempty"`
LastActiveAtSince *int64 `json:"last_active_at_since,omitempty"`
}
// ToQuery returns url.Values from the params.
func (params *ListParams) ToQuery() url.Values {
q := params.ListParams.ToQuery()
if params.OrderBy != nil {
q.Add("order_by", *params.OrderBy)
}
if params.Query != nil {
q.Add("query", *params.Query)
}
for _, v := range params.EmailAddresses {
q.Add("email_address", v)
}
for _, v := range params.ExternalIDs {
q.Add("external_id", v)
}
for _, v := range params.PhoneNumbers {
q.Add("phone_number", v)
}
for _, v := range params.Web3Wallets {
q.Add("web3_wallet", v)
}
for _, v := range params.Usernames {
q.Add("username", v)
}
for _, v := range params.UserIDs {
q.Add("user_id", v)
}
if params.LastActiveAtSince != nil {
q.Add("last_active_at_since", strconv.FormatInt(*params.LastActiveAtSince, 10))
}
return q
}
// List returns a list of users.
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.UserList, error) {
// The Clerk API returns the results of GET /v1/users as an
// array. In order to build the final response that includes
// the total count, we need to make two API calls.
// GET /v1/users retrieves the actual results
// GET /v1/users/count retrieves the total count
// The response is then synthesized from the individual responses.
// GET /v1/users
req := clerk.NewAPIRequest(http.MethodGet, path)
req.SetParams(params)
data := &userList{}
err := c.Backend.Call(ctx, req, data)
if err != nil {
return nil, err
}
// GET /v1/users/count
totalCount, err := c.Count(ctx, params)
if err != nil {
return nil, err
}
users := []*clerk.User(*data)
return &clerk.UserList{
Users: users,
TotalCount: totalCount.TotalCount,
}, nil
}
// Count returns the total count of users satisfying the parameters.
func (c *Client) Count(ctx context.Context, params *ListParams) (*TotalCount, error) {
path, err := clerk.JoinPath(path, "/count")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
req.SetParams(params)
resource := &TotalCount{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
// Custom type needed in order to store the GET /v1/users results
// array.
type userList []*clerk.User
// Read implements the clerk.ResponseReader interface.
// The implementation is empty, meaning that we'll lose
// the raw response from the server.
func (_ *userList) Read(res *clerk.APIResponse) {
// no-op
}
// Response schema for GET /v1/users/count
type TotalCount struct {
clerk.APIResource
Object string `json:"object"`
TotalCount int64 `json:"total_count"`
}
type ListOAuthAccessTokensParams struct {
clerk.APIParams
ID string `json:"-"`
Provider string `json:"-"`
}
// ListOAuthAccessTokens retrieves a list of the user's access
// tokens for a specific OAuth provider.
func (c *Client) ListOAuthAccessTokens(ctx context.Context, params *ListOAuthAccessTokensParams) (*clerk.OAuthAccessTokenList, error) {
path, err := clerk.JoinPath(path, params.ID, "/oauth_access_tokens", fmt.Sprintf("%s?paginated=true", params.Provider))
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
req.SetParams(params)
list := &clerk.OAuthAccessTokenList{}
err = c.Backend.Call(ctx, req, list)
return list, err
}
type DeleteMFAParams struct {
clerk.APIParams
ID string `json:"-"`
}
// DeleteMFA disables a user's multi-factor authentication methods.
func (c *Client) DeleteMFA(ctx context.Context, params *DeleteMFAParams) (*MultifactorAuthentication, error) {
path, err := clerk.JoinPath(path, params.ID, "/mfa")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodDelete, path)
resource := &MultifactorAuthentication{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
type MultifactorAuthentication struct {
clerk.APIResource
UserID string `json:"user_id"`
}
// Ban marks the user as banned.
func (c *Client) Ban(ctx context.Context, id string) (*clerk.User, error) {
path, err := clerk.JoinPath(path, id, "/ban")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
resource := &clerk.User{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
// Unban removes the ban for a user.
func (c *Client) Unban(ctx context.Context, id string) (*clerk.User, error) {
path, err := clerk.JoinPath(path, id, "/unban")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
resource := &clerk.User{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
// Lock marks the user as locked.
func (c *Client) Lock(ctx context.Context, id string) (*clerk.User, error) {
path, err := clerk.JoinPath(path, id, "/lock")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
resource := &clerk.User{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
// Unlock removes the lock for a user.
func (c *Client) Unlock(ctx context.Context, id string) (*clerk.User, error) {
path, err := clerk.JoinPath(path, id, "/unlock")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
resource := &clerk.User{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
type ListOrganizationMembershipsParams struct {
clerk.APIParams
clerk.ListParams
}
// ToQuery returns url.Values from the params.
func (params *ListOrganizationMembershipsParams) ToQuery() url.Values {
return params.ListParams.ToQuery()
}
// ListOrganizationMemberships lists all the user's organization memberships.
func (c *Client) ListOrganizationMemberships(ctx context.Context, id string, params *ListOrganizationMembershipsParams) (*clerk.OrganizationMembershipList, error) {
path, err := clerk.JoinPath(path, id, "/organization_memberships")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, path)
req.SetParams(params)
list := &clerk.OrganizationMembershipList{}
err = c.Backend.Call(ctx, req, list)
return list, err
}