forked from firebase/firebase-admin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_mgt.go
1116 lines (961 loc) · 32.2 KB
/
user_mgt.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"firebase.google.com/go/internal"
"google.golang.org/api/googleapi"
)
const (
maxLenPayloadCC = 1000
defaultProviderID = "firebase"
idToolkitV1Endpoint = "https://identitytoolkit.googleapis.com/v1"
// Maximum number of users allowed to batch get at a time.
maxGetAccountsBatchSize = 100
// Maximum number of users allowed to batch delete at a time.
maxDeleteAccountsBatchSize = 1000
)
// 'REDACTED', encoded as a base64 string.
var b64Redacted = base64.StdEncoding.EncodeToString([]byte("REDACTED"))
// UserInfo is a collection of standard profile information for a user.
type UserInfo struct {
DisplayName string `json:"displayName,omitempty"`
Email string `json:"email,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
PhotoURL string `json:"photoUrl,omitempty"`
// In the ProviderUserInfo[] ProviderID can be a short domain name (e.g. google.com),
// or the identity of an OpenID identity provider.
// In UserRecord.UserInfo it will return the constant string "firebase".
ProviderID string `json:"providerId,omitempty"`
UID string `json:"rawId,omitempty"`
}
// UserMetadata contains additional metadata associated with a user account.
// Timestamps are in milliseconds since epoch.
type UserMetadata struct {
CreationTimestamp int64
LastLogInTimestamp int64
// The time at which the user was last active (ID token refreshed), or 0 if
// the user was never active.
LastRefreshTimestamp int64
}
// UserRecord contains metadata associated with a Firebase user account.
type UserRecord struct {
*UserInfo
CustomClaims map[string]interface{}
Disabled bool
EmailVerified bool
ProviderUserInfo []*UserInfo
TokensValidAfterMillis int64 // milliseconds since epoch.
UserMetadata *UserMetadata
TenantID string
}
// UserToCreate is the parameter struct for the CreateUser function.
type UserToCreate struct {
params map[string]interface{}
}
// Disabled setter.
func (u *UserToCreate) Disabled(disabled bool) *UserToCreate {
return u.set("disabled", disabled)
}
// DisplayName setter.
func (u *UserToCreate) DisplayName(name string) *UserToCreate {
return u.set("displayName", name)
}
// Email setter.
func (u *UserToCreate) Email(email string) *UserToCreate {
return u.set("email", email)
}
// EmailVerified setter.
func (u *UserToCreate) EmailVerified(verified bool) *UserToCreate {
return u.set("emailVerified", verified)
}
// Password setter.
func (u *UserToCreate) Password(pw string) *UserToCreate {
return u.set("password", pw)
}
// PhoneNumber setter.
func (u *UserToCreate) PhoneNumber(phone string) *UserToCreate {
return u.set("phoneNumber", phone)
}
// PhotoURL setter.
func (u *UserToCreate) PhotoURL(url string) *UserToCreate {
return u.set("photoUrl", url)
}
// UID setter.
func (u *UserToCreate) UID(uid string) *UserToCreate {
return u.set("localId", uid)
}
func (u *UserToCreate) set(key string, value interface{}) *UserToCreate {
if u.params == nil {
u.params = make(map[string]interface{})
}
u.params[key] = value
return u
}
func (u *UserToCreate) validatedRequest() (map[string]interface{}, error) {
req := make(map[string]interface{})
for k, v := range u.params {
req[k] = v
}
if uid, ok := req["localId"]; ok {
if err := validateUID(uid.(string)); err != nil {
return nil, err
}
}
if name, ok := req["displayName"]; ok {
if err := validateDisplayName(name.(string)); err != nil {
return nil, err
}
}
if email, ok := req["email"]; ok {
if err := validateEmail(email.(string)); err != nil {
return nil, err
}
}
if phone, ok := req["phoneNumber"]; ok {
if err := validatePhone(phone.(string)); err != nil {
return nil, err
}
}
if url, ok := req["photoUrl"]; ok {
if err := validatePhotoURL(url.(string)); err != nil {
return nil, err
}
}
if pw, ok := req["password"]; ok {
if err := validatePassword(pw.(string)); err != nil {
return nil, err
}
}
return req, nil
}
// UserToUpdate is the parameter struct for the UpdateUser function.
type UserToUpdate struct {
params map[string]interface{}
}
// CustomClaims setter.
func (u *UserToUpdate) CustomClaims(claims map[string]interface{}) *UserToUpdate {
return u.set("customClaims", claims)
}
// Disabled setter.
func (u *UserToUpdate) Disabled(disabled bool) *UserToUpdate {
return u.set("disableUser", disabled)
}
// DisplayName setter. Set to empty string to remove the display name from the user account.
func (u *UserToUpdate) DisplayName(name string) *UserToUpdate {
return u.set("displayName", name)
}
// Email setter.
func (u *UserToUpdate) Email(email string) *UserToUpdate {
return u.set("email", email)
}
// EmailVerified setter.
func (u *UserToUpdate) EmailVerified(verified bool) *UserToUpdate {
return u.set("emailVerified", verified)
}
// Password setter.
func (u *UserToUpdate) Password(pw string) *UserToUpdate {
return u.set("password", pw)
}
// PhoneNumber setter. Set to empty string to remove the phone number and the corresponding auth provider
// from the user account.
func (u *UserToUpdate) PhoneNumber(phone string) *UserToUpdate {
return u.set("phoneNumber", phone)
}
// PhotoURL setter. Set to empty string to remove the photo URL from the user account.
func (u *UserToUpdate) PhotoURL(url string) *UserToUpdate {
return u.set("photoUrl", url)
}
// revokeRefreshTokens revokes all refresh tokens for a user by setting the validSince property
// to the present in epoch seconds.
func (u *UserToUpdate) revokeRefreshTokens() *UserToUpdate {
return u.set("validSince", strconv.FormatInt(time.Now().Unix(), 10))
}
func (u *UserToUpdate) set(key string, value interface{}) *UserToUpdate {
if u.params == nil {
u.params = make(map[string]interface{})
}
u.params[key] = value
return u
}
func (u *UserToUpdate) validatedRequest() (map[string]interface{}, error) {
if len(u.params) == 0 {
// update without any parameters is never allowed
return nil, fmt.Errorf("update parameters must not be nil or empty")
}
req := make(map[string]interface{})
for k, v := range u.params {
req[k] = v
}
if email, ok := req["email"]; ok {
if err := validateEmail(email.(string)); err != nil {
return nil, err
}
}
handleDeletion := func(key, deleteKey, deleteVal string) {
var deleteList []string
list, ok := req[deleteKey]
if ok {
deleteList = list.([]string)
}
req[deleteKey] = append(deleteList, deleteVal)
delete(req, key)
}
if name, ok := req["displayName"]; ok {
if name == "" {
handleDeletion("displayName", "deleteAttribute", "DISPLAY_NAME")
} else if err := validateDisplayName(name.(string)); err != nil {
return nil, err
}
}
if url, ok := req["photoUrl"]; ok {
if url == "" {
handleDeletion("photoUrl", "deleteAttribute", "PHOTO_URL")
} else if err := validatePhotoURL(url.(string)); err != nil {
return nil, err
}
}
if phone, ok := req["phoneNumber"]; ok {
if phone == "" {
handleDeletion("phoneNumber", "deleteProvider", "phone")
} else if err := validatePhone(phone.(string)); err != nil {
return nil, err
}
}
if claims, ok := req["customClaims"]; ok {
cc, err := marshalCustomClaims(claims.(map[string]interface{}))
if err != nil {
return nil, err
}
req["customAttributes"] = cc
delete(req, "customClaims")
}
if pw, ok := req["password"]; ok {
if err := validatePassword(pw.(string)); err != nil {
return nil, err
}
}
return req, nil
}
func marshalCustomClaims(claims map[string]interface{}) (string, error) {
for _, key := range reservedClaims {
if _, ok := claims[key]; ok {
return "", fmt.Errorf("claim %q is reserved and must not be set", key)
}
}
b, err := json.Marshal(claims)
if err != nil {
return "", fmt.Errorf("custom claims marshaling error: %v", err)
}
s := string(b)
if s == "null" {
s = "{}" // claims map has been explicitly set to nil for deletion.
}
if len(s) > maxLenPayloadCC {
return "", fmt.Errorf("serialized custom claims must not exceed %d characters", maxLenPayloadCC)
}
return s, nil
}
// Error handlers.
const (
configurationNotFound = "configuration-not-found"
emailAlreadyExists = "email-already-exists"
idTokenRevoked = "id-token-revoked"
insufficientPermission = "insufficient-permission"
invalidDynamicLinkDomain = "invalid-dynamic-link-domain"
invalidEmail = "invalid-email"
phoneNumberAlreadyExists = "phone-number-already-exists"
projectNotFound = "project-not-found"
sessionCookieRevoked = "session-cookie-revoked"
tenantIDMismatch = "tenant-id-mismatch"
tenantNotFound = "tenant-not-found"
uidAlreadyExists = "uid-already-exists"
unauthorizedContinueURI = "unauthorized-continue-uri"
unknown = "unknown-error"
userNotFound = "user-not-found"
)
// IsConfigurationNotFound checks if the given error was due to a non-existing IdP configuration.
func IsConfigurationNotFound(err error) bool {
return internal.HasErrorCode(err, configurationNotFound)
}
// IsEmailAlreadyExists checks if the given error was due to a duplicate email.
func IsEmailAlreadyExists(err error) bool {
return internal.HasErrorCode(err, emailAlreadyExists)
}
// IsIDTokenRevoked checks if the given error was due to a revoked ID token.
func IsIDTokenRevoked(err error) bool {
return internal.HasErrorCode(err, idTokenRevoked)
}
// IsInsufficientPermission checks if the given error was due to insufficient permissions.
func IsInsufficientPermission(err error) bool {
return internal.HasErrorCode(err, insufficientPermission)
}
// IsInvalidDynamicLinkDomain checks if the given error was due to an invalid dynamic link domain.
func IsInvalidDynamicLinkDomain(err error) bool {
return internal.HasErrorCode(err, invalidDynamicLinkDomain)
}
// IsInvalidEmail checks if the given error was due to an invalid email.
func IsInvalidEmail(err error) bool {
return internal.HasErrorCode(err, invalidEmail)
}
// IsPhoneNumberAlreadyExists checks if the given error was due to a duplicate phone number.
func IsPhoneNumberAlreadyExists(err error) bool {
return internal.HasErrorCode(err, phoneNumberAlreadyExists)
}
// IsProjectNotFound checks if the given error was due to a non-existing project.
func IsProjectNotFound(err error) bool {
return internal.HasErrorCode(err, projectNotFound)
}
// IsSessionCookieRevoked checks if the given error was due to a revoked session cookie.
func IsSessionCookieRevoked(err error) bool {
return internal.HasErrorCode(err, sessionCookieRevoked)
}
// IsTenantIDMismatch checks if the given error was due to a mismatched tenant ID in a JWT.
func IsTenantIDMismatch(err error) bool {
return internal.HasErrorCode(err, tenantIDMismatch)
}
// IsTenantNotFound checks if the given error was due to a non-existing tenant ID.
func IsTenantNotFound(err error) bool {
return internal.HasErrorCode(err, tenantNotFound)
}
// IsUIDAlreadyExists checks if the given error was due to a duplicate uid.
func IsUIDAlreadyExists(err error) bool {
return internal.HasErrorCode(err, uidAlreadyExists)
}
// IsUnauthorizedContinueURI checks if the given error was due to an unauthorized continue URI domain.
func IsUnauthorizedContinueURI(err error) bool {
return internal.HasErrorCode(err, unauthorizedContinueURI)
}
// IsUnknown checks if the given error was due to a unknown server error.
func IsUnknown(err error) bool {
return internal.HasErrorCode(err, unknown)
}
// IsUserNotFound checks if the given error was due to non-existing user.
func IsUserNotFound(err error) bool {
return internal.HasErrorCode(err, userNotFound)
}
var serverError = map[string]string{
"CONFIGURATION_NOT_FOUND": configurationNotFound,
"DUPLICATE_EMAIL": emailAlreadyExists,
"DUPLICATE_LOCAL_ID": uidAlreadyExists,
"EMAIL_EXISTS": emailAlreadyExists,
"INSUFFICIENT_PERMISSION": insufficientPermission,
"INVALID_DYNAMIC_LINK_DOMAIN": invalidDynamicLinkDomain,
"INVALID_EMAIL": invalidEmail,
"PERMISSION_DENIED": insufficientPermission,
"PHONE_NUMBER_EXISTS": phoneNumberAlreadyExists,
"PROJECT_NOT_FOUND": projectNotFound,
"TENANT_NOT_FOUND": tenantNotFound,
"UNAUTHORIZED_DOMAIN": unauthorizedContinueURI,
"USER_NOT_FOUND": userNotFound,
}
func handleServerError(err error) error {
gerr, ok := err.(*googleapi.Error)
if !ok {
// Not a back-end error
return err
}
serverCode := gerr.Message
clientCode, ok := serverError[serverCode]
if !ok {
clientCode = unknown
}
return internal.Error(clientCode, err.Error())
}
// Validators.
func validateDisplayName(val string) error {
if val == "" {
return fmt.Errorf("display name must be a non-empty string")
}
return nil
}
func validatePhotoURL(val string) error {
if val == "" {
return fmt.Errorf("photo url must be a non-empty string")
}
return nil
}
func validateEmail(email string) error {
if email == "" {
return fmt.Errorf("email must be a non-empty string")
}
if parts := strings.Split(email, "@"); len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return fmt.Errorf("malformed email string: %q", email)
}
return nil
}
func validatePassword(val string) error {
if len(val) < 6 {
return fmt.Errorf("password must be a string at least 6 characters long")
}
return nil
}
func validateUID(uid string) error {
if uid == "" {
return fmt.Errorf("uid must be a non-empty string")
}
if len(uid) > 128 {
return fmt.Errorf("uid string must not be longer than 128 characters")
}
return nil
}
func validatePhone(phone string) error {
if phone == "" {
return fmt.Errorf("phone number must be a non-empty string")
}
if !regexp.MustCompile(`\+.*[0-9A-Za-z]`).MatchString(phone) {
return fmt.Errorf("phone number must be a valid, E.164 compliant identifier")
}
return nil
}
func validateProvider(providerID string, providerUID string) error {
if providerID == "" {
return fmt.Errorf("providerID must be a non-empty string")
} else if providerUID == "" {
return fmt.Errorf("providerUID must be a non-empty string")
}
return nil
}
// End of validators
// GetUser gets the user data corresponding to the specified user ID.
func (c *baseClient) GetUser(ctx context.Context, uid string) (*UserRecord, error) {
return c.getUser(ctx, &userQuery{
field: "localId",
value: uid,
label: "uid",
})
}
// GetUserByEmail gets the user data corresponding to the specified email.
func (c *baseClient) GetUserByEmail(ctx context.Context, email string) (*UserRecord, error) {
if err := validateEmail(email); err != nil {
return nil, err
}
return c.getUser(ctx, &userQuery{
field: "email",
value: email,
})
}
// GetUserByPhoneNumber gets the user data corresponding to the specified user phone number.
func (c *baseClient) GetUserByPhoneNumber(ctx context.Context, phone string) (*UserRecord, error) {
if err := validatePhone(phone); err != nil {
return nil, err
}
return c.getUser(ctx, &userQuery{
field: "phoneNumber",
value: phone,
label: "phone number",
})
}
type userQuery struct {
field string
value string
label string
}
func (q *userQuery) description() string {
label := q.label
if label == "" {
label = q.field
}
return fmt.Sprintf("%s: %q", label, q.value)
}
func (q *userQuery) build() map[string]interface{} {
return map[string]interface{}{
q.field: []string{q.value},
}
}
type getAccountInfoResponse struct {
Users []*userQueryResponse `json:"users"`
}
func (c *baseClient) getUser(ctx context.Context, query *userQuery) (*UserRecord, error) {
var parsed getAccountInfoResponse
if _, err := c.post(ctx, "/accounts:lookup", query.build(), &parsed); err != nil {
return nil, err
}
if len(parsed.Users) == 0 {
return nil, internal.Errorf(userNotFound, "cannot find user from %s", query.description())
}
return parsed.Users[0].makeUserRecord()
}
// A UserIdentifier identifies a user to be looked up.
type UserIdentifier interface {
matches(ur *UserRecord) bool
populate(req *getAccountInfoRequest)
}
// A UIDIdentifier is used for looking up an account by uid.
//
// See GetUsers function.
type UIDIdentifier struct {
UID string
}
func (id UIDIdentifier) matches(ur *UserRecord) bool {
return id.UID == ur.UID
}
func (id UIDIdentifier) populate(req *getAccountInfoRequest) {
req.LocalID = append(req.LocalID, id.UID)
}
// An EmailIdentifier is used for looking up an account by email.
//
// See GetUsers function.
type EmailIdentifier struct {
Email string
}
func (id EmailIdentifier) matches(ur *UserRecord) bool {
return id.Email == ur.Email
}
func (id EmailIdentifier) populate(req *getAccountInfoRequest) {
req.Email = append(req.Email, id.Email)
}
// A PhoneIdentifier is used for looking up an account by phone number.
//
// See GetUsers function.
type PhoneIdentifier struct {
PhoneNumber string
}
func (id PhoneIdentifier) matches(ur *UserRecord) bool {
return id.PhoneNumber == ur.PhoneNumber
}
func (id PhoneIdentifier) populate(req *getAccountInfoRequest) {
req.PhoneNumber = append(req.PhoneNumber, id.PhoneNumber)
}
// A ProviderIdentifier is used for looking up an account by federated provider.
//
// See GetUsers function.
type ProviderIdentifier struct {
ProviderID string
ProviderUID string
}
func (id ProviderIdentifier) matches(ur *UserRecord) bool {
for _, userInfo := range ur.ProviderUserInfo {
if id.ProviderID == userInfo.ProviderID && id.ProviderUID == userInfo.UID {
return true
}
}
return false
}
func (id ProviderIdentifier) populate(req *getAccountInfoRequest) {
req.FederatedUserID = append(
req.FederatedUserID,
federatedUserIdentifier{ProviderID: id.ProviderID, RawID: id.ProviderUID})
}
// A GetUsersResult represents the result of the GetUsers() API.
type GetUsersResult struct {
// Set of UserRecords corresponding to the set of users that were requested.
// Only users that were found are listed here. The result set is unordered.
Users []*UserRecord
// Set of UserIdentifiers that were requested, but not found.
NotFound []UserIdentifier
}
type federatedUserIdentifier struct {
ProviderID string `json:"providerId,omitempty"`
RawID string `json:"rawId,omitempty"`
}
type getAccountInfoRequest struct {
LocalID []string `json:"localId,omitempty"`
Email []string `json:"email,omitempty"`
PhoneNumber []string `json:"phoneNumber,omitempty"`
FederatedUserID []federatedUserIdentifier `json:"federatedUserId,omitempty"`
}
func (req *getAccountInfoRequest) validate() error {
for i := range req.LocalID {
if err := validateUID(req.LocalID[i]); err != nil {
return err
}
}
for i := range req.Email {
if err := validateEmail(req.Email[i]); err != nil {
return err
}
}
for i := range req.PhoneNumber {
if err := validatePhone(req.PhoneNumber[i]); err != nil {
return err
}
}
for i := range req.FederatedUserID {
id := &req.FederatedUserID[i]
if err := validateProvider(id.ProviderID, id.RawID); err != nil {
return err
}
}
return nil
}
func isUserFound(id UserIdentifier, urs [](*UserRecord)) bool {
for i := range urs {
if id.matches(urs[i]) {
return true
}
}
return false
}
// GetUsers returns the user data corresponding to the specified identifiers.
//
// There are no ordering guarantees; in particular, the nth entry in the users
// result list is not guaranteed to correspond to the nth entry in the input
// parameters list.
//
// A maximum of 100 identifiers may be supplied. If more than 100
// identifiers are supplied, this method returns an error.
//
// Returns the corresponding user records. An error is returned instead if any
// of the identifiers are invalid or if more than 100 identifiers are
// specified.
func (c *baseClient) GetUsers(
ctx context.Context, identifiers []UserIdentifier,
) (*GetUsersResult, error) {
if len(identifiers) == 0 {
return &GetUsersResult{[](*UserRecord){}, [](UserIdentifier){}}, nil
} else if len(identifiers) > maxGetAccountsBatchSize {
return nil, fmt.Errorf(
"`identifiers` parameter must have <= %d entries", maxGetAccountsBatchSize)
}
var request getAccountInfoRequest
for i := range identifiers {
identifiers[i].populate(&request)
}
if err := request.validate(); err != nil {
return nil, err
}
var parsed getAccountInfoResponse
if _, err := c.post(ctx, "/accounts:lookup", request, &parsed); err != nil {
return nil, err
}
var userRecords [](*UserRecord)
for _, user := range parsed.Users {
userRecord, err := user.makeUserRecord()
if err != nil {
return nil, err
}
userRecords = append(userRecords, userRecord)
}
var notFound []UserIdentifier
for i := range identifiers {
if !isUserFound(identifiers[i], userRecords) {
notFound = append(notFound, identifiers[i])
}
}
return &GetUsersResult{userRecords, notFound}, nil
}
type userQueryResponse struct {
UID string `json:"localId,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Email string `json:"email,omitempty"`
PhoneNumber string `json:"phoneNumber,omitempty"`
PhotoURL string `json:"photoUrl,omitempty"`
CreationTimestamp int64 `json:"createdAt,string,omitempty"`
LastLogInTimestamp int64 `json:"lastLoginAt,string,omitempty"`
LastRefreshAt string `json:"lastRefreshAt,omitempty"`
ProviderID string `json:"providerId,omitempty"`
CustomAttributes string `json:"customAttributes,omitempty"`
Disabled bool `json:"disabled,omitempty"`
EmailVerified bool `json:"emailVerified,omitempty"`
ProviderUserInfo []*UserInfo `json:"providerUserInfo,omitempty"`
PasswordHash string `json:"passwordHash,omitempty"`
PasswordSalt string `json:"salt,omitempty"`
TenantID string `json:"tenantId,omitempty"`
ValidSinceSeconds int64 `json:"validSince,string,omitempty"`
}
func (r *userQueryResponse) makeUserRecord() (*UserRecord, error) {
exported, err := r.makeExportedUserRecord()
if err != nil {
return nil, err
}
return exported.UserRecord, nil
}
func (r *userQueryResponse) makeExportedUserRecord() (*ExportedUserRecord, error) {
var customClaims map[string]interface{}
if r.CustomAttributes != "" {
if err := json.Unmarshal([]byte(r.CustomAttributes), &customClaims); err != nil {
return nil, err
}
if len(customClaims) == 0 {
customClaims = nil
}
}
// If the password hash is redacted (probably due to missing permissions)
// then clear it out, similar to how the salt is returned. (Otherwise, it
// *looks* like a b64-encoded hash is present, which is confusing.)
hash := r.PasswordHash
if hash == b64Redacted {
hash = ""
}
var lastRefreshTimestamp int64
if r.LastRefreshAt != "" {
t, err := time.Parse(time.RFC3339, r.LastRefreshAt)
if err != nil {
return nil, err
}
lastRefreshTimestamp = t.Unix() * 1000
}
return &ExportedUserRecord{
UserRecord: &UserRecord{
UserInfo: &UserInfo{
DisplayName: r.DisplayName,
Email: r.Email,
PhoneNumber: r.PhoneNumber,
PhotoURL: r.PhotoURL,
UID: r.UID,
ProviderID: defaultProviderID,
},
CustomClaims: customClaims,
Disabled: r.Disabled,
EmailVerified: r.EmailVerified,
ProviderUserInfo: r.ProviderUserInfo,
TenantID: r.TenantID,
TokensValidAfterMillis: r.ValidSinceSeconds * 1000,
UserMetadata: &UserMetadata{
LastLogInTimestamp: r.LastLogInTimestamp,
CreationTimestamp: r.CreationTimestamp,
LastRefreshTimestamp: lastRefreshTimestamp,
},
},
PasswordHash: hash,
PasswordSalt: r.PasswordSalt,
}, nil
}
// CreateUser creates a new user with the specified properties.
func (c *baseClient) CreateUser(ctx context.Context, user *UserToCreate) (*UserRecord, error) {
uid, err := c.createUser(ctx, user)
if err != nil {
return nil, err
}
return c.GetUser(ctx, uid)
}
func (c *baseClient) createUser(ctx context.Context, user *UserToCreate) (string, error) {
if user == nil {
user = &UserToCreate{}
}
request, err := user.validatedRequest()
if err != nil {
return "", err
}
var result struct {
UID string `json:"localId"`
}
_, err = c.post(ctx, "/accounts", request, &result)
return result.UID, err
}
// UpdateUser updates an existing user account with the specified properties.
func (c *baseClient) UpdateUser(
ctx context.Context, uid string, user *UserToUpdate) (ur *UserRecord, err error) {
if err := c.updateUser(ctx, uid, user); err != nil {
return nil, err
}
return c.GetUser(ctx, uid)
}
// RevokeRefreshTokens revokes all refresh tokens issued to a user.
//
// RevokeRefreshTokens updates the user's TokensValidAfterMillis to the current UTC second.
// It is important that the server on which this is called has its clock set correctly and synchronized.
//
// While this revokes all sessions for a specified user and disables any new ID tokens for existing sessions
// from getting minted, existing ID tokens may remain active until their natural expiration (one hour).
// To verify that ID tokens are revoked, use `verifyIdTokenAndCheckRevoked(ctx, idToken)`.
func (c *baseClient) RevokeRefreshTokens(ctx context.Context, uid string) error {
return c.updateUser(ctx, uid, (&UserToUpdate{}).revokeRefreshTokens())
}
// SetCustomUserClaims sets additional claims on an existing user account.
//
// Custom claims set via this function can be used to define user roles and privilege levels.
// These claims propagate to all the devices where the user is already signed in (after token
// expiration or when token refresh is forced), and next time the user signs in. The claims
// can be accessed via the user's ID token JWT. If a reserved OIDC claim is specified (sub, iat,
// iss, etc), an error is thrown. Claims payload must also not be larger then 1000 characters
// when serialized into a JSON string.
func (c *baseClient) SetCustomUserClaims(ctx context.Context, uid string, customClaims map[string]interface{}) error {
if customClaims == nil || len(customClaims) == 0 {
customClaims = map[string]interface{}{}
}
return c.updateUser(ctx, uid, (&UserToUpdate{}).CustomClaims(customClaims))
}
func (c *baseClient) updateUser(ctx context.Context, uid string, user *UserToUpdate) error {
if err := validateUID(uid); err != nil {
return err
}
if user == nil {
return fmt.Errorf("update parameters must not be nil or empty")
}
request, err := user.validatedRequest()
if err != nil {
return err
}
request["localId"] = uid
_, err = c.post(ctx, "/accounts:update", request, nil)
return err
}
// DeleteUser deletes the user by the given UID.
func (c *baseClient) DeleteUser(ctx context.Context, uid string) error {
if err := validateUID(uid); err != nil {
return err
}
payload := map[string]interface{}{
"localId": uid,
}
_, err := c.post(ctx, "/accounts:delete", payload, nil)
return err
}
// A DeleteUsersResult represents the result of the DeleteUsers() call.
type DeleteUsersResult struct {
// The number of users that were deleted successfully (possibly zero). Users
// that did not exist prior to calling DeleteUsers() are considered to be
// successfully deleted.
SuccessCount int
// The number of users that failed to be deleted (possibly zero).
FailureCount int
// A list of DeleteUsersErrorInfo instances describing the errors that were
// encountered during the deletion. Length of this list is equal to the value
// of FailureCount.
Errors []*DeleteUsersErrorInfo
}
// DeleteUsersErrorInfo represents an error encountered while deleting a user
// account.
//
// The Index field corresponds to the index of the failed user in the uids
// array that was passed to DeleteUsers().
type DeleteUsersErrorInfo struct {
Index int `json:"index,omitEmpty"`
Reason string `json:"message,omitEmpty"`
}
// DeleteUsers deletes the users specified by the given identifiers.
//
// Deleting a non-existing user won't generate an error. (i.e. this method is
// idempotent.) Non-existing users are considered to be successfully
// deleted, and are therefore counted in the DeleteUsersResult.SuccessCount
// value.
//
// A maximum of 1000 identifiers may be supplied. If more than 1000
// identifiers are supplied, this method returns an error.
//
// This API is currently rate limited at the server to 1 QPS. If you exceed
// this, you may get a quota exceeded error. Therefore, if you want to delete
// more than 1000 users, you may need to add a delay to ensure you don't go
// over this limit.
//
// Returns the total number of successful/failed deletions, as well as the
// array of errors that correspond to the failed deletions. An error is
// returned if any of the identifiers are invalid or if more than 1000
// identifiers are specified.
func (c *baseClient) DeleteUsers(ctx context.Context, uids []string) (*DeleteUsersResult, error) {
if len(uids) == 0 {
return &DeleteUsersResult{}, nil
} else if len(uids) > maxDeleteAccountsBatchSize {
return nil, fmt.Errorf(
"`uids` parameter must have <= %d entries", maxDeleteAccountsBatchSize)
}