forked from firebase/firebase-admin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_mgt.go
736 lines (653 loc) · 20.6 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
// 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/json"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"firebase.google.com/go/internal"
"google.golang.org/api/googleapi"
"google.golang.org/api/identitytoolkit/v3"
)
const (
maxLenPayloadCC = 1000
defaultProviderID = "firebase"
)
// Create a new interface
type identitytoolkitCall interface {
Header() http.Header
}
// set header
func (c *Client) setHeader(ic identitytoolkitCall) {
ic.Header().Set("X-Client-Version", c.version)
}
// UserInfo is a collection of standard profile information for a user.
type UserInfo struct {
DisplayName string
Email string
PhoneNumber string
PhotoURL string
// 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
UID string
}
// UserMetadata contains additional metadata associated with a user account.
// Timestamps are in milliseconds since epoch.
type UserMetadata struct {
CreationTimestamp int64
LastLogInTimestamp 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
}
// UserToCreate is the parameter struct for the CreateUser function.
type UserToCreate struct {
createReq *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest
uid bool
displayName bool
email bool
photoURL bool
phoneNumber bool
}
func (u *UserToCreate) request() *identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest {
if u.createReq == nil {
u.createReq = &identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest{}
}
return u.createReq
}
func (u *UserToCreate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySignupNewUserRequest, error) {
req := u.request() // creating a user without any parameters is allowed
if u.uid {
if err := validateUID(req.LocalId); err != nil {
return nil, err
}
}
if u.displayName {
if err := validateDisplayName(req.DisplayName); err != nil {
return nil, err
}
}
if u.email {
if err := validateEmail(req.Email); err != nil {
return nil, err
}
}
if u.phoneNumber {
if err := validatePhone(req.PhoneNumber); err != nil {
return nil, err
}
}
if u.photoURL {
if err := validatePhotoURL(req.PhotoUrl); err != nil {
return nil, err
}
}
if req.Password != "" {
if err := validatePassword(req.Password); err != nil {
return nil, err
}
}
return req, nil
}
// Disabled setter.
func (u *UserToCreate) Disabled(disabled bool) *UserToCreate {
req := u.request()
req.Disabled = disabled
if !disabled {
req.ForceSendFields = append(req.ForceSendFields, "Disabled")
}
return u
}
// DisplayName setter.
func (u *UserToCreate) DisplayName(name string) *UserToCreate {
u.request().DisplayName = name
u.displayName = true
return u
}
// Email setter.
func (u *UserToCreate) Email(email string) *UserToCreate {
u.request().Email = email
u.email = true
return u
}
// EmailVerified setter.
func (u *UserToCreate) EmailVerified(verified bool) *UserToCreate {
req := u.request()
req.EmailVerified = verified
if !verified {
req.ForceSendFields = append(req.ForceSendFields, "EmailVerified")
}
return u
}
// Password setter.
func (u *UserToCreate) Password(pw string) *UserToCreate {
u.request().Password = pw
return u
}
// PhoneNumber setter.
func (u *UserToCreate) PhoneNumber(phone string) *UserToCreate {
u.request().PhoneNumber = phone
u.phoneNumber = true
return u
}
// PhotoURL setter.
func (u *UserToCreate) PhotoURL(url string) *UserToCreate {
u.request().PhotoUrl = url
u.photoURL = true
return u
}
// UID setter.
func (u *UserToCreate) UID(uid string) *UserToCreate {
u.request().LocalId = uid
u.uid = true
return u
}
// UserToUpdate is the parameter struct for the UpdateUser function.
type UserToUpdate struct {
updateReq *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest
claims map[string]interface{}
displayName bool
email bool
phoneNumber bool
photoURL bool
customClaims bool
}
func (u *UserToUpdate) request() *identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest {
if u.updateReq == nil {
u.updateReq = &identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest{}
}
return u.updateReq
}
func (u *UserToUpdate) validatedRequest() (*identitytoolkit.IdentitytoolkitRelyingpartySetAccountInfoRequest, error) {
if u.updateReq == nil {
// update without any parameters is never allowed
return nil, fmt.Errorf("update parameters must not be nil or empty")
}
req := u.updateReq
if u.email {
if err := validateEmail(req.Email); err != nil {
return nil, err
}
}
if u.displayName && req.DisplayName == "" {
req.DeleteAttribute = append(req.DeleteAttribute, "DISPLAY_NAME")
}
if u.photoURL && req.PhotoUrl == "" {
req.DeleteAttribute = append(req.DeleteAttribute, "PHOTO_URL")
}
if u.phoneNumber {
if req.PhoneNumber == "" {
req.DeleteProvider = append(req.DeleteProvider, "phone")
} else if err := validatePhone(req.PhoneNumber); err != nil {
return nil, err
}
}
if u.customClaims {
cc, err := marshalCustomClaims(u.claims)
if err != nil {
return nil, err
}
req.CustomAttributes = cc
}
if req.Password != "" {
if err := validatePassword(req.Password); err != nil {
return nil, err
}
}
return req, nil
}
// CustomClaims setter.
func (u *UserToUpdate) CustomClaims(claims map[string]interface{}) *UserToUpdate {
u.request() // force initialization of the request for later use
u.claims = claims
u.customClaims = true
return u
}
// Disabled setter.
func (u *UserToUpdate) Disabled(disabled bool) *UserToUpdate {
req := u.request()
req.DisableUser = disabled
if !disabled {
req.ForceSendFields = append(req.ForceSendFields, "DisableUser")
}
return u
}
// DisplayName setter.
func (u *UserToUpdate) DisplayName(name string) *UserToUpdate {
u.request().DisplayName = name
u.displayName = true
return u
}
// Email setter.
func (u *UserToUpdate) Email(email string) *UserToUpdate {
u.request().Email = email
u.email = true
return u
}
// EmailVerified setter.
func (u *UserToUpdate) EmailVerified(verified bool) *UserToUpdate {
req := u.request()
req.EmailVerified = verified
if !verified {
req.ForceSendFields = append(req.ForceSendFields, "EmailVerified")
}
return u
}
// Password setter.
func (u *UserToUpdate) Password(pw string) *UserToUpdate {
u.request().Password = pw
return u
}
// PhoneNumber setter.
func (u *UserToUpdate) PhoneNumber(phone string) *UserToUpdate {
u.request().PhoneNumber = phone
u.phoneNumber = true
return u
}
// PhotoURL setter.
func (u *UserToUpdate) PhotoURL(url string) *UserToUpdate {
u.request().PhotoUrl = url
u.photoURL = true
return u
}
// revokeRefreshTokens revokes all refresh tokens for a user by setting the validSince property
// to the present in epoch seconds.
func (u *UserToUpdate) revokeRefreshTokens() *UserToUpdate {
u.request().ValidSince = time.Now().Unix()
return u
}
// CreateUser creates a new user with the specified properties.
func (c *Client) 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)
}
// UpdateUser updates an existing user account with the specified properties.
//
// DisplayName, PhotoURL and PhoneNumber will be set to "" to signify deleting them from the record.
func (c *Client) 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)
}
// DeleteUser deletes the user by the given UID.
func (c *Client) DeleteUser(ctx context.Context, uid string) error {
if err := validateUID(uid); err != nil {
return err
}
request := &identitytoolkit.IdentitytoolkitRelyingpartyDeleteAccountRequest{
LocalId: uid,
}
call := c.is.Relyingparty.DeleteAccount(request)
c.setHeader(call)
if _, err := call.Context(ctx).Do(); err != nil {
return handleServerError(err)
}
return nil
}
// GetUser gets the user data corresponding to the specified user ID.
func (c *Client) GetUser(ctx context.Context, uid string) (*UserRecord, error) {
if err := validateUID(uid); err != nil {
return nil, err
}
request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{
LocalId: []string{uid},
}
return c.getUser(ctx, request)
}
// GetUserByPhoneNumber gets the user data corresponding to the specified user phone number.
func (c *Client) GetUserByPhoneNumber(ctx context.Context, phone string) (*UserRecord, error) {
if err := validatePhone(phone); err != nil {
return nil, err
}
request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{
PhoneNumber: []string{phone},
}
return c.getUser(ctx, request)
}
// GetUserByEmail gets the user data corresponding to the specified email.
func (c *Client) GetUserByEmail(ctx context.Context, email string) (*UserRecord, error) {
if err := validateEmail(email); err != nil {
return nil, err
}
request := &identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest{
Email: []string{email},
}
return c.getUser(ctx, request)
}
// 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 *Client) 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 *Client) 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 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 (
emailAlreadyExists = "email-already-exists"
idTokenRevoked = "id-token-revoked"
insufficientPermission = "insufficient-permission"
phoneNumberAlreadyExists = "phone-number-already-exists"
projectNotFound = "project-not-found"
sessionCookieRevoked = "session-cookie-revoked"
uidAlreadyExists = "uid-already-exists"
unknown = "unknown-error"
userNotFound = "user-not-found"
)
// 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)
}
// 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)
}
// IsUIDAlreadyExists checks if the given error was due to a duplicate uid.
func IsUIDAlreadyExists(err error) bool {
return internal.HasErrorCode(err, uidAlreadyExists)
}
// 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": projectNotFound,
"DUPLICATE_EMAIL": emailAlreadyExists,
"DUPLICATE_LOCAL_ID": uidAlreadyExists,
"EMAIL_EXISTS": emailAlreadyExists,
"INSUFFICIENT_PERMISSION": insufficientPermission,
"PERMISSION_DENIED": insufficientPermission,
"PHONE_NUMBER_EXISTS": phoneNumberAlreadyExists,
"PROJECT_NOT_FOUND": projectNotFound,
"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
}
// End of validators
// Helper functions for retrieval and HTTP calls.
func (c *Client) createUser(ctx context.Context, user *UserToCreate) (string, error) {
if user == nil {
user = &UserToCreate{}
}
request, err := user.validatedRequest()
if err != nil {
return "", err
}
call := c.is.Relyingparty.SignupNewUser(request)
c.setHeader(call)
resp, err := call.Context(ctx).Do()
if err != nil {
return "", handleServerError(err)
}
return resp.LocalId, nil
}
func (c *Client) 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
call := c.is.Relyingparty.SetAccountInfo(request)
c.setHeader(call)
if _, err := call.Context(ctx).Do(); err != nil {
return handleServerError(err)
}
return nil
}
func (c *Client) getUser(ctx context.Context, request *identitytoolkit.IdentitytoolkitRelyingpartyGetAccountInfoRequest) (*UserRecord, error) {
call := c.is.Relyingparty.GetAccountInfo(request)
c.setHeader(call)
resp, err := call.Context(ctx).Do()
if err != nil {
return nil, handleServerError(err)
}
if len(resp.Users) == 0 {
var msg string
if len(request.LocalId) == 1 {
msg = fmt.Sprintf("cannot find user from uid: %q", request.LocalId[0])
} else if len(request.Email) == 1 {
msg = fmt.Sprintf("cannot find user from email: %q", request.Email[0])
} else {
msg = fmt.Sprintf("cannot find user from phone number: %q", request.PhoneNumber[0])
}
return nil, internal.Error(userNotFound, msg)
}
eu, err := makeExportedUser(resp.Users[0])
if err != nil {
return nil, err
}
return eu.UserRecord, nil
}
const idToolkitEndpoint = "https://identitytoolkit.googleapis.com/v1/projects"
// userManagementClient is a helper for interacting with the Identity Toolkit REST API.
//
// Currently it is only used for some user management operations (e.g. session cookie
// generation). We will gradually migrate all the user management functionality to this
// type, and remove our dependency on the google.golang.org/api/identitytoolkit/v3
// package.
type userManagementClient struct {
baseURL string
projectID string
version string
httpClient *internal.HTTPClient
}
// SessionCookie creates a new Firebase session cookie from the given ID token and expiry
// duration. The returned JWT can be set as a server-side session cookie with a custom cookie
// policy. Expiry duration must be at least 5 minutes but may not exceed 14 days.
func (c *userManagementClient) SessionCookie(
ctx context.Context,
idToken string,
expiresIn time.Duration,
) (string, error) {
if idToken == "" {
return "", errors.New("id token must not be empty")
}
if expiresIn < 5*time.Minute || expiresIn > 14*24*time.Hour {
return "", errors.New("expiry duration must be between 5 minutes and 14 days")
}
payload := map[string]interface{}{
"idToken": idToken,
"validDuration": int64(expiresIn.Seconds()),
}
resp, err := c.post(ctx, ":createSessionCookie", payload)
if err != nil {
return "", err
}
if resp.Status != http.StatusOK {
return "", handleHTTPError(resp)
}
var result struct {
SessionCookie string `json:"sessionCookie"`
}
err = json.Unmarshal(resp.Body, &result)
return result.SessionCookie, err
}
func (c *userManagementClient) post(
ctx context.Context,
path string,
payload interface{},
) (*internal.Response, error) {
req, err := c.newRequest(http.MethodPost, path)
if err != nil {
return nil, err
}
req.Body = internal.NewJSONEntity(payload)
return c.httpClient.Do(ctx, req)
}
func (c *userManagementClient) newRequest(method, path string) (*internal.Request, error) {
if c.projectID == "" {
return nil, errors.New("project id not available")
}
versionHeader := internal.WithHeader("X-Client-Version", c.version)
return &internal.Request{
Method: method,
URL: fmt.Sprintf("%s/%s%s", c.baseURL, c.projectID, path),
Opts: []internal.HTTPOption{versionHeader},
}, nil
}
func handleHTTPError(resp *internal.Response) error {
var httpErr struct {
Error struct {
Message string `json:"message"`
} `json:"error"`
}
json.Unmarshal(resp.Body, &httpErr) // ignore any json parse errors at this level
serverCode := httpErr.Error.Message
clientCode, ok := serverError[serverCode]
if !ok {
clientCode = unknown
}
return internal.Errorf(
clientCode,
"http error status: %d; body: %s",
resp.Status,
string(resp.Body))
}