-
Notifications
You must be signed in to change notification settings - Fork 59
/
user.go
770 lines (641 loc) · 30 KB
/
user.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
package management
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"strconv"
"time"
)
// User represents an Auth0 user resource.
//
// See: https://auth0.com/docs/users
type User struct {
// The users' identifier.
ID *string `json:"user_id,omitempty"`
// The connection the user belongs to.
Connection *string `json:"connection,omitempty"`
// The users' email.
Email *string `json:"email,omitempty"`
// The users' name.
Name *string `json:"name,omitempty"`
// The users' given name.
GivenName *string `json:"given_name,omitempty"`
// The users' family name.
FamilyName *string `json:"family_name,omitempty"`
// The users' username. Only valid if the connection requires a username.
Username *string `json:"username,omitempty"`
// The users' nickname.
Nickname *string `json:"nickname,omitempty"`
// The screen name, handle, or alias that this user identifies themselves with.
ScreenName *string `json:"screen_name,omitempty"`
// The user-defined UTF-8 string describing their account.
Description *string `json:"description,omitempty"`
// The user-defined location for this account’s profile.
Location *string `json:"location,omitempty"`
// The users' password (mandatory for non SMS connections)
Password *string `json:"password,omitempty"`
// The users' phone number (following the E.164 recommendation).
// Only valid for users to be added to SMS connections.
PhoneNumber *string `json:"phone_number,omitempty"`
// The time the user was created.
CreatedAt *time.Time `json:"created_at,omitempty"`
// The last time the user was updated.
UpdatedAt *time.Time `json:"updated_at,omitempty"`
// The last time the user has logged in.
LastLogin *time.Time `json:"last_login,omitempty"`
// The last time the user had their password reset.
// Only available for Database connection users.
LastPasswordReset *time.Time `json:"last_password_reset,omitempty"`
// UserMetadata holds data that the user has read/write access to.
// For example color_preference, blog_url, etc.
UserMetadata *map[string]interface{} `json:"user_metadata,omitempty"`
// Identities is a list of user identities for when accounts are linked.
Identities []*UserIdentity `json:"identities,omitempty"`
// True if the user's email is verified, false otherwise. If it is true then
// the user will not receive a verification email, unless verify_email: true
// was specified.
EmailVerified *bool `json:"-"`
// If true, the user will receive a verification email after creation, even
// if created with email_verified set to true. If false, the user will not
// receive a verification email, even if created with email_verified set to
// false. If unspecified, defaults to the behavior determined by the value
// of email_verified.
VerifyEmail *bool `json:"verify_email,omitempty"`
// True if the user's phone number is verified, false otherwise. When the
// user is added to an SMS connection, they will not receive a verification
// SMS if this is true.
PhoneVerified *bool `json:"phone_verified,omitempty"`
// AppMetadata holds data that the user has read-only access to.
// For example roles, permissions, vip, etc.
// NOTE: Roles added to AppMetadata are not integrated with Auth0 Role-Based Access Control (RBAC).
// For RBAC, see the functions User.Roles, User.AssignRoles, and User.RemoveRoles.
AppMetadata *map[string]interface{} `json:"app_metadata,omitempty"`
// The user's picture url.
Picture *string `json:"picture,omitempty"`
// A URL provided by the user in association with their profile.
URL *string `json:"url,omitempty"`
// True if the user is blocked from the application, false if the user is enabled.
Blocked *bool `json:"blocked,omitempty"`
// Last IP address from which this user logged in. Read only, cannot be modified.
LastIP *string `json:"last_ip,omitempty"`
// Total number of logins this user has performed. Read only, cannot be modified.
LoginsCount *int64 `json:"logins_count,omitempty"`
// List of multi-factor authentication providers with which this user has enrolled.
Multifactor *[]string `json:"multifactor,omitempty"`
// Auth0 client ID. Only valid when updating email address.
ClientID *string `json:"client_id,omitempty"`
}
// UnmarshalJSON is a custom deserializer for the User type.
//
// We have to use a custom one due to possible inconsistencies in value types.
func (u *User) UnmarshalJSON(b []byte) error {
type user User
type userAlias struct {
*user
RawEmailVerified interface{} `json:"email_verified,omitempty"`
}
alias := &userAlias{(*user)(u), nil}
err := json.Unmarshal(b, alias)
if err != nil {
return err
}
if alias.RawEmailVerified != nil {
var emailVerified bool
switch rawEmailVerified := alias.RawEmailVerified.(type) {
case bool:
emailVerified = rawEmailVerified
case string:
emailVerified, err = strconv.ParseBool(rawEmailVerified)
if err != nil {
return err
}
default:
panic(reflect.TypeOf(rawEmailVerified))
}
alias.EmailVerified = &emailVerified
}
return nil
}
// MarshalJSON is a custom serializer for the User type.
func (u *User) MarshalJSON() ([]byte, error) {
type user User
type userAlias struct {
*user
RawEmailVerified interface{} `json:"email_verified,omitempty"`
}
alias := &userAlias{user: (*user)(u)}
if u.EmailVerified != nil {
alias.RawEmailVerified = u.EmailVerified
}
return json.Marshal(alias)
}
// UserIdentityLink contains the data needed for linking an identity to a given user.
type UserIdentityLink struct {
// Connection id of the secondary user account being linked when more than one auth0 database provider exists.
ConnectionID *string `json:"connection_id,omitempty"`
// Secondary account user id.
UserID *string `json:"user_id,omitempty"`
// Identity provider of the secondary user account being linked.
Provider *string `json:"provider,omitempty"`
// LinkWith requires the authenticated primary account's JWT in the Authorization header.
// Must be a JWT for the secondary account being linked. If sending this parameter,
// provider, user_id, and connection_id must not be sent.
LinkWith *string `json:"link_with,omitempty"`
}
// UserIdentity holds values that validate a User's identity.
type UserIdentity struct {
Connection *string `json:"connection,omitempty"`
UserID *string `json:"-"`
Provider *string `json:"provider,omitempty"`
IsSocial *bool `json:"isSocial,omitempty"`
AccessToken *string `json:"access_token,omitempty"`
AccessTokenSecret *string `json:"access_token_secret,omitempty"`
RefreshToken *string `json:"refresh_token,omitempty"`
ProfileData *map[string]interface{} `json:"profileData,omitempty"`
}
// UnmarshalJSON is a custom deserializer for the UserIdentity type.
//
// We have to use a custom one due to a bug in the Auth0 Management API which
// might return a number for `user_id` instead of a string.
//
// See https://community.auth0.com/t/users-user-id-returns-inconsistent-type-for-identities-user-id/39236
func (i *UserIdentity) UnmarshalJSON(b []byte) error {
type userIdentity UserIdentity
type userIdentityAlias struct {
*userIdentity
RawUserID interface{} `json:"user_id,omitempty"`
}
alias := &userIdentityAlias{(*userIdentity)(i), nil}
err := json.Unmarshal(b, alias)
if err != nil {
return err
}
if alias.RawUserID != nil {
var id string
switch rawID := alias.RawUserID.(type) {
case string:
id = rawID
case float64:
id = strconv.Itoa(int(rawID))
default:
panic(reflect.TypeOf(rawID))
}
alias.UserID = &id
}
return nil
}
// MarshalJSON is a custom serializer for the UserIdentity type.
func (i *UserIdentity) MarshalJSON() ([]byte, error) {
type userIdentity UserIdentity
type userIdentityAlias struct {
*userIdentity
RawUserID interface{} `json:"user_id,omitempty"`
}
alias := &userIdentityAlias{userIdentity: (*userIdentity)(i)}
if i.UserID != nil {
alias.RawUserID = i.UserID
}
return json.Marshal(alias)
}
type userBlock struct {
BlockedFor []*UserBlock `json:"blocked_for,omitempty"`
}
// UserBlock keeps track of a blocked IP for the login identifier associated with a User.
type UserBlock struct {
Identifier *string `json:"identifier,omitempty"`
IP *string `json:"ip,omitempty"`
}
// UserRecoveryCode represents a User's multi-factor authentication recovery code.
type UserRecoveryCode struct {
RecoveryCode *string `json:"recovery_code,omitempty"`
}
// UserEnrollment contains information about the Guardian enrollments for the user.
type UserEnrollment struct {
// Authentication method for this enrollment. Can be `authentication`, `guardian`, or `sms`.
AuthMethod *string `json:"auth_method,omitempty"`
// Start date and time of this enrollment.
EnrolledAt *time.Time `json:"enrolled_at,omitempty"`
// ID of this enrollment.
ID *string `json:"id,omitempty"`
// Device identifier (usually phone identifier) of this enrollment.
Identifier *string `json:"identifier,omitempty"`
// Last authentication date and time of this enrollment.
LastAuth *time.Time `json:"last_auth,omitempty"`
// Name of enrollment (usually phone number).
Name *string `json:"name,omitempty"`
// Phone number for this enrollment.
PhoneNumber *string `json:"phone_number,omitempty"`
// Status of this enrollment. Can be `pending` or `confirmed`.
Status *string `json:"status,omitempty"`
// Type of enrollment.
Type *string `json:"type,omitempty"`
}
// UserList is an envelope struct which is used when calling List() or Search() methods.
//
// It holds metadata such as the total result count, starting offset and limit.
type UserList struct {
List
Users []*User `json:"users"`
}
// AuthenticationMethod belonging to a user.
//
// See: https://auth0.com/docs/secure/multi-factor-authentication/manage-mfa-auth0-apis/manage-authentication-methods-with-management-api
type AuthenticationMethod struct {
// The ID of the authentication method (auto generated).
ID *string `json:"id,omitempty"`
// The type of the authentication method. Should be one of "phone", "email", "totp", "webauthn-roaming", or "passkey".
Type *string `json:"type,omitempty"`
// The authentication method status.
Confirmed *bool `json:"confirmed,omitempty"`
// A human-readable label to identify the authentication method.
Name *string `json:"name,omitempty"`
// The ID of a linked authentication method. Linked authentication methods will be deleted together.
LinkID *string `json:"link_id,omitempty"`
// Applies to phone authentication methods only. The destination phone number used to send verification codes via text and voice.
PhoneNumber *string `json:"phone_number,omitempty"`
// Applies to email authentication method only. The email address used to send verification messages.
Email *string `json:"email,omitempty"`
// Applies to webauthn authentication methods only. The ID of the generated credential.
KeyID *string `json:"key_id,omitempty"`
// Applies to webauthn authentication methods only. The public key.
PublicKey *string `json:"public_key,omitempty"`
// Authenticator creation date.
CreatedAt *time.Time `json:"created_at,omitempty"`
// Enrollment date.
EnrolledAt *time.Time `json:"enrolled_at,omitempty"`
// Last authentication.
LastAuthedAt *time.Time `json:"last_auth_at,omitempty"`
// Base32 encoded secret for TOTP generation.
TOTPSecret *string `json:"totp_secret,omitempty"`
// The authentication method preferred for phone authenticators.
PreferredAuthenticationMethod *string `json:"preferred_authentication_method,omitempty"`
// Applies to email webauthn authenticators only. The relying party identifier.
RelyingPartyIdentifier *string `json:"relying_party_identifier,omitempty"`
AuthenticationMethods *[]AuthenticationMethodReference `json:"authentication_methods,omitempty"`
// Applies to passkeys only. The kind of device the credential is stored on as defined by backup eligibility.
// "single_device" credentials cannot be backed up and synced to another device,
// "multi_device" credentials can be backed up if enabled by the end-user.
CredentialDeviceType *string `json:"credential_device_type,omitempty"`
// Applies to passkeys only. Whether the credential was backed up.
CredentialBackedUp *bool `json:"credential_backed_up,omitempty"`
// Applies to passkeys only. The ID of the user identity linked with the authentication method.
IdentityUserID *string `json:"identity_user_id,omitempty"`
// Applies to passkeys only. The user-agent of the browser used to create the passkey.
UserAgent *string `json:"user_agent,omitempty"`
}
// AuthenticationMethodReference used within the AuthenticationMethod.
type AuthenticationMethodReference struct {
// The ID of the authentication method (auto generated).
ID *string `json:"id,omitempty"`
// The type of the authentication method.
Type *string `json:"type,omitempty"`
}
// AuthenticationMethodList is an envelope struct which is used when calling GetAuthenticationMethods().
//
// It holds metadata such as the total result count, starting offset and limit.
type AuthenticationMethodList struct {
List
Authenticators []*AuthenticationMethod `json:"authenticators,omitempty"`
}
// RefreshTokenList represents a list of user refresh tokens.
type RefreshTokenList struct {
List
Tokens []*RefreshToken `json:"tokens,omitempty"`
}
// RefreshToken represents a refresh token for a user.
type RefreshToken struct {
ID *string `json:"id,omitempty"`
UserID *string `json:"user_id,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
IdleExpiresAt *time.Time `json:"idle_expires_at,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
ClientID *string `json:"client_id,omitempty"`
SessionID *string `json:"session_id,omitempty"`
Rotating *bool `json:"rotating,omitempty"`
ResourceServer []*RefreshTokenResourceServer `json:"resource_servers,omitempty"`
}
// RefreshTokenResourceServer represents the resource server associated with a refresh token.
type RefreshTokenResourceServer struct {
Audience *string `json:"audience,omitempty"`
Scopes *string `json:"scopes,omitempty"`
}
// UserManager manages Auth0 User resources.
type UserManager manager
// newUserManager returns a new instance of a user manager.
// Create a new user. It works only for database and passwordless connections.
//
// The samples on the right show you every attribute that could be used. The
// attribute connection is always mandatory but depending on the type of
// connection you are using there could be others too. For instance, database
// connections require `email` and `password`.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/post_users
func (m *UserManager) Create(ctx context.Context, u *User, opts ...RequestOption) error {
return m.management.Request(ctx, "POST", m.management.URI("users"), u, opts...)
}
// Read user details for a given user_id.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id
func (m *UserManager) Read(ctx context.Context, id string, opts ...RequestOption) (u *User, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users", id), &u, opts...)
return
}
// Update user.
//
// The following attributes can be updated at the root level:
//
// - `app_metadata`
// - `blocked`
// - `email`
// - `email_verified`
// - `family_name`
// - `given_name`
// - `name`
// - `nickname`
// - `password`
// - `phone_number`
// - `phone_verified`
// - `picture`
// - `username`
// - `user_metadata`
// - `verify_email`
//
// See: https://auth0.com/docs/api/management/v2#!/Users/patch_users_by_id
func (m *UserManager) Update(ctx context.Context, id string, u *User, opts ...RequestOption) (err error) {
return m.management.Request(ctx, "PATCH", m.management.URI("users", id), u, opts...)
}
// Delete a single user based on its id.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/delete_users_by_id
func (m *UserManager) Delete(ctx context.Context, id string, opts ...RequestOption) (err error) {
return m.management.Request(ctx, "DELETE", m.management.URI("users", id), nil, opts...)
}
// List users. This method forces the `include_totals` option.
//
// For information on how to paginate using this function see https://pkg.go.dev/github.com/auth0/go-auth0/management#hdr-Page_Based_Pagination
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_users
func (m *UserManager) List(ctx context.Context, opts ...RequestOption) (ul *UserList, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users"), &ul, applyListDefaults(opts))
return
}
// Search is an alias for List.
func (m *UserManager) Search(ctx context.Context, opts ...RequestOption) (ul *UserList, err error) {
return m.List(ctx, opts...)
}
// ListByEmail retrieves all users matching a given email.
//
// If Auth0 is the identify provider (idP), the email address associated with a
// user is saved in lower case, regardless of how you initially provided it.
// For example, if you register a user as [email protected], Auth0 saves the
// user's email as [email protected].
//
// In cases where Auth0 is not the idP, the `email` is stored based on the rules
// of idP, so make sure the search is made using the correct capitalization.
//
// When using this endpoint, make sure that you are searching for users via
// email addresses using the correct case.
//
// See: https://auth0.com/docs/api/management/v2#!/Users_By_Email/get_users_by_email
func (m *UserManager) ListByEmail(ctx context.Context, email string, opts ...RequestOption) (us []*User, err error) {
opts = append(opts, Parameter("email", email))
err = m.management.Request(ctx, "GET", m.management.URI("users-by-email"), &us, opts...)
return
}
// Roles lists roles associated with a user.
//
// For information on how to paginate using this function see https://pkg.go.dev/github.com/auth0/go-auth0/management#hdr-Page_Based_Pagination
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_user_roles
func (m *UserManager) Roles(ctx context.Context, id string, opts ...RequestOption) (r *RoleList, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users", id, "roles"), &r, applyListDefaults(opts))
return
}
// AssignRoles assigns roles to a user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/post_user_roles
func (m *UserManager) AssignRoles(ctx context.Context, id string, roles []*Role, opts ...RequestOption) error {
r := make(map[string][]*string)
r["roles"] = make([]*string, len(roles))
for i, role := range roles {
r["roles"][i] = role.ID
}
return m.management.Request(ctx, "POST", m.management.URI("users", id, "roles"), &r, opts...)
}
// RemoveRoles removes any roles associated to a user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/delete_user_roles
func (m *UserManager) RemoveRoles(ctx context.Context, id string, roles []*Role, opts ...RequestOption) error {
r := make(map[string][]*string)
r["roles"] = make([]*string, len(roles))
for i, role := range roles {
r["roles"][i] = role.ID
}
return m.management.Request(ctx, "DELETE", m.management.URI("users", id, "roles"), &r, opts...)
}
// Permissions lists the permissions associated to the user.
//
// For information on how to paginate using this function see https://pkg.go.dev/github.com/auth0/go-auth0/management#hdr-Page_Based_Pagination
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_permissions
func (m *UserManager) Permissions(ctx context.Context, id string, opts ...RequestOption) (p *PermissionList, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users", id, "permissions"), &p, applyListDefaults(opts))
return
}
// AssignPermissions assigns permissions to the user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/post_permissions
func (m *UserManager) AssignPermissions(ctx context.Context, id string, permissions []*Permission, opts ...RequestOption) error {
p := make(map[string][]*Permission)
p["permissions"] = permissions
return m.management.Request(ctx, "POST", m.management.URI("users", id, "permissions"), &p, opts...)
}
// RemovePermissions removes any permissions associated to a user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/delete_permissions
func (m *UserManager) RemovePermissions(ctx context.Context, id string, permissions []*Permission, opts ...RequestOption) error {
p := make(map[string][]*Permission)
p["permissions"] = permissions
return m.management.Request(ctx, "DELETE", m.management.URI("users", id, "permissions"), &p, opts...)
}
// Blocks retrieves a list of blocked IP addresses of a particular user using the
// user ID.
//
// See: https://auth0.com/docs/api/management/v2#!/User_Blocks/get_user_blocks_by_id
func (m *UserManager) Blocks(ctx context.Context, id string, opts ...RequestOption) ([]*UserBlock, error) {
b := new(userBlock)
err := m.management.Request(ctx, "GET", m.management.URI("user-blocks", id), &b, opts...)
return b.BlockedFor, err
}
// BlocksByIdentifier retrieves a list of blocked IP addresses of a particular
// user using any of the user identifiers: username, phone number or email.
//
// See: https://auth0.com/docs/api/management/v2#!/User_Blocks/get_user_blocks
func (m *UserManager) BlocksByIdentifier(ctx context.Context, identifier string, opts ...RequestOption) ([]*UserBlock, error) {
b := new(userBlock)
opts = append(opts, Parameter("identifier", identifier))
err := m.management.Request(ctx, "GET", m.management.URI("user-blocks"), &b, opts...)
return b.BlockedFor, err
}
// Unblock a user that was blocked due to an excessive amount of incorrectly
// provided credentials using the user ID.
//
// Note: This endpoint does not unblock users that were blocked by admins.
//
// See: https://auth0.com/docs/api/management/v2#!/User_Blocks/delete_user_blocks_by_id
func (m *UserManager) Unblock(ctx context.Context, id string, opts ...RequestOption) error {
return m.management.Request(ctx, "DELETE", m.management.URI("user-blocks", id), nil, opts...)
}
// UnblockByIdentifier a user that was blocked due to an excessive amount of incorrectly
// provided credentials using any of the user identifiers: username, phone number or email.
//
// Note: This endpoint does not unblock users that were blocked by admins.
//
// See: https://auth0.com/docs/api/management/v2#!/User_Blocks/delete_user_blocks
func (m *UserManager) UnblockByIdentifier(ctx context.Context, identifier string, opts ...RequestOption) error {
opts = append(opts, Parameter("identifier", identifier))
return m.management.Request(ctx, "DELETE", m.management.URI("user-blocks"), nil, opts...)
}
// Enrollments retrieves all Guardian enrollments for a user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_enrollments
func (m *UserManager) Enrollments(ctx context.Context, id string, opts ...RequestOption) (enrolls []*UserEnrollment, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users", id, "enrollments"), &enrolls, opts...)
return
}
// RegenerateRecoveryCode removes the current multi-factor authentication recovery code and generate a new one.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/post_recovery_code_regeneration
func (m *UserManager) RegenerateRecoveryCode(ctx context.Context, id string, opts ...RequestOption) (*UserRecoveryCode, error) {
r := new(UserRecoveryCode)
err := m.management.Request(ctx, "POST", m.management.URI("users", id, "recovery-code-regeneration"), &r, opts...)
return r, err
}
// InvalidateRememberBrowser invalidates all remembered browsers across all authentication factors for a user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/post_invalidate_remember_browser
func (m *UserManager) InvalidateRememberBrowser(ctx context.Context, id string, opts ...RequestOption) error {
uri := m.management.URI(
"users",
id,
"multifactor",
"actions",
"invalidate-remember-browser",
)
err := m.management.Request(ctx, "POST", uri, nil, opts...)
return err
}
// Link links two user accounts together forming a primary and secondary relationship.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/post_identities
func (m *UserManager) Link(ctx context.Context, id string, il *UserIdentityLink, opts ...RequestOption) (uIDs []UserIdentity, err error) {
request, err := m.management.NewRequest(ctx, "POST", m.management.URI("users", id, "identities"), il, opts...)
if err != nil {
return uIDs, err
}
response, err := m.management.Do(request)
if err != nil {
return uIDs, err
}
defer response.Body.Close()
if response.StatusCode >= http.StatusBadRequest {
return uIDs, newError(response)
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return uIDs, fmt.Errorf("failed to read the response body: %w", err)
}
if len(responseBody) > 0 {
if err = json.Unmarshal(responseBody, &uIDs); err != nil {
return uIDs, fmt.Errorf("failed to unmarshal response payload: %w", err)
}
}
return uIDs, nil
}
// Unlink unlinks an identity from a user making it a separate account again.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/delete_user_identity_by_user_id
func (m *UserManager) Unlink(ctx context.Context, id, provider, userID string, opts ...RequestOption) (uIDs []UserIdentity, err error) {
err = m.management.Request(ctx, "DELETE", m.management.URI("users", id, "identities", provider, userID), &uIDs, opts...)
return
}
// Organizations lists user's organizations.
//
// For information on how to paginate using this function see https://pkg.go.dev/github.com/auth0/go-auth0/management#hdr-Page_Based_Pagination
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_organizations
func (m *UserManager) Organizations(ctx context.Context, id string, opts ...RequestOption) (p *OrganizationList, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users", id, "organizations"), &p, applyListDefaults(opts))
return
}
// ListAuthenticationMethods retrieves a list of authentication methods.
//
// For information on how to paginate using this function see https://pkg.go.dev/github.com/auth0/go-auth0/management#hdr-Page_Based_Pagination
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_authentication_methods
func (m *UserManager) ListAuthenticationMethods(ctx context.Context, userID string, opts ...RequestOption) (a *AuthenticationMethodList, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users", userID, "authentication-methods"), &a, applyListDefaults(opts))
return
}
// GetAuthenticationMethodByID gets a specific authentication method for a user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get_authentication_methods_by_authentication_method_id
func (m *UserManager) GetAuthenticationMethodByID(ctx context.Context, userID string, id string, opts ...RequestOption) (a *AuthenticationMethod, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users", userID, "authentication-methods", id), &a, opts...)
return
}
// CreateAuthenticationMethod creates an authentication method for a user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/post_authentication_methods
func (m *UserManager) CreateAuthenticationMethod(ctx context.Context, userID string, a *AuthenticationMethod, opts ...RequestOption) (err error) {
err = m.management.Request(ctx, "POST", m.management.URI("users", userID, "authentication-methods"), &a, opts...)
return
}
// UpdateAllAuthenticationMethods updates all authentication methods by replacing them with the given ones.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/put_authentication_methods
func (m *UserManager) UpdateAllAuthenticationMethods(ctx context.Context, userID string, a *[]AuthenticationMethod, opts ...RequestOption) (err error) {
err = m.management.Request(ctx, "PUT", m.management.URI("users", userID, "authentication-methods"), &a, opts...)
return
}
// UpdateAuthenticationMethod updates an authentication method by ID.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/patch_authentication_methods_by_authentication_method_id
func (m *UserManager) UpdateAuthenticationMethod(ctx context.Context, userID string, id string, a *AuthenticationMethod, opts ...RequestOption) (err error) {
err = m.management.Request(ctx, "PATCH", m.management.URI("users", userID, "authentication-methods", id), &a, opts...)
return
}
// DeleteAuthenticationMethod deletes an authentication method by ID.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/delete_authentication_methods_by_authentication_method_id
func (m *UserManager) DeleteAuthenticationMethod(ctx context.Context, userID string, id string, opts ...RequestOption) (err error) {
err = m.management.Request(ctx, "DELETE", m.management.URI("users", userID, "authentication-methods", id), nil, opts...)
return
}
// DeleteAllAuthenticationMethods deletes all authentication methods for the given user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/delete_authentication_methods
func (m *UserManager) DeleteAllAuthenticationMethods(ctx context.Context, userID string, opts ...RequestOption) (err error) {
err = m.management.Request(ctx, "DELETE", m.management.URI("users", userID, "authentication-methods"), nil, opts...)
return
}
// ListRefreshTokens retrieves details for a user's refresh tokens.
//
// It allows pagination using the provided options. For more information on pagination, refer to:
// https://pkg.go.dev/github.com/auth0/go-auth0/management#hdr-Page_Based_Pagination
//
// See: https://auth0.com/docs/api/management/v2#!/Users/get-refresh-tokens-for-user
func (m *UserManager) ListRefreshTokens(ctx context.Context, userID string, opts ...RequestOption) (r *RefreshTokenList, err error) {
err = m.management.Request(ctx, "GET", m.management.URI("users", userID, "refresh-tokens"), &r, applyListDefaults(opts))
return
}
// DeleteRefreshTokens deletes all refresh tokens for a user.
//
// See: https://auth0.com/docs/api/management/v2#!/Users/delete-refresh-tokens-for-user
func (m *UserManager) DeleteRefreshTokens(ctx context.Context, userID string, opts ...RequestOption) (err error) {
err = m.management.Request(ctx, "DELETE", m.management.URI("users", userID, "refresh-tokens"), nil, opts...)
return
}