Skip to content

Commit cb19923

Browse files
authored
Merge branch 'dev' into jd-event-listener
2 parents 3bf51a9 + 37edd08 commit cb19923

File tree

10 files changed

+652
-626
lines changed

10 files changed

+652
-626
lines changed

auth/auth.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strings"
2424

2525
"firebase.google.com/go/internal"
26-
"google.golang.org/api/identitytoolkit/v3"
2726
)
2827

2928
const (
@@ -41,12 +40,10 @@ var reservedClaims = []string{
4140
// Client facilitates generating custom JWT tokens for Firebase clients, and verifying ID tokens issued
4241
// by Firebase backend services.
4342
type Client struct {
44-
is *identitytoolkit.Service
4543
userManagementClient
4644
idTokenVerifier *tokenVerifier
4745
cookieVerifier *tokenVerifier
4846
signer cryptoSigner
49-
version string
5047
clock internal.Clock
5148
}
5249

@@ -90,11 +87,6 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
9087
return nil, err
9188
}
9289

93-
is, err := identitytoolkit.New(hc.Client)
94-
if err != nil {
95-
return nil, err
96-
}
97-
9890
idTokenVerifier, err := newIDTokenVerifier(ctx, conf.ProjectID)
9991
if err != nil {
10092
return nil, err
@@ -113,11 +105,9 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
113105
version: version,
114106
httpClient: hc,
115107
},
116-
is: is,
117108
idTokenVerifier: idTokenVerifier,
118109
cookieVerifier: cookieVerifier,
119110
signer: signer,
120-
version: version, // This can be removed when userManagementClient implements all user mgt APIs.
121111
clock: internal.SystemClock,
122112
}, nil
123113
}

auth/export_users.go

Lines changed: 34 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ package auth
1717
import (
1818
"context"
1919
"encoding/json"
20+
"fmt"
21+
"net/http"
22+
"net/url"
23+
"strconv"
2024

21-
identitytoolkit "google.golang.org/api/identitytoolkit/v3"
2225
"google.golang.org/api/iterator"
2326
)
2427

@@ -28,7 +31,7 @@ const maxReturnedResults = 1000
2831
//
2932
// If nextPageToken is empty, the iterator will start at the beginning.
3033
// If the nextPageToken is not empty, the iterator starts after the token.
31-
func (c *Client) Users(ctx context.Context, nextPageToken string) *UserIterator {
34+
func (c *userManagementClient) Users(ctx context.Context, nextPageToken string) *UserIterator {
3235
it := &UserIterator{
3336
ctx: ctx,
3437
client: c,
@@ -46,7 +49,7 @@ func (c *Client) Users(ctx context.Context, nextPageToken string) *UserIterator
4649
//
4750
// Also see: https://github.com/GoogleCloudPlatform/google-cloud-go/wiki/Iterator-Guidelines
4851
type UserIterator struct {
49-
client *Client
52+
client *userManagementClient
5053
ctx context.Context
5154
nextFunc func() error
5255
pageInfo *iterator.PageInfo
@@ -70,26 +73,43 @@ func (it *UserIterator) Next() (*ExportedUserRecord, error) {
7073
}
7174

7275
func (it *UserIterator) fetch(pageSize int, pageToken string) (string, error) {
73-
request := &identitytoolkit.IdentitytoolkitRelyingpartyDownloadAccountRequest{
74-
MaxResults: int64(pageSize),
75-
NextPageToken: pageToken,
76+
query := make(url.Values)
77+
query.Set("maxResults", strconv.Itoa(pageSize))
78+
if pageToken != "" {
79+
query.Set("nextPageToken", pageToken)
7680
}
77-
call := it.client.is.Relyingparty.DownloadAccount(request)
78-
it.client.setHeader(call)
79-
resp, err := call.Context(it.ctx).Do()
81+
82+
req, err := it.client.newRequest(http.MethodGet, fmt.Sprintf("/accounts:batchGet?%s", query.Encode()))
8083
if err != nil {
81-
return "", handleServerError(err)
84+
return "", err
85+
}
86+
87+
resp, err := it.client.httpClient.Do(it.ctx, req)
88+
if err != nil {
89+
return "", err
90+
}
91+
92+
if resp.Status != http.StatusOK {
93+
return "", handleHTTPError(resp)
8294
}
8395

84-
for _, u := range resp.Users {
85-
eu, err := makeExportedUser(u)
96+
var parsed struct {
97+
Users []userQueryResponse `json:"users"`
98+
NextPageToken string `json:"nextPageToken"`
99+
}
100+
if err := json.Unmarshal(resp.Body, &parsed); err != nil {
101+
return "", err
102+
}
103+
104+
for _, u := range parsed.Users {
105+
eu, err := u.makeExportedUserRecord()
86106
if err != nil {
87107
return "", err
88108
}
89109
it.users = append(it.users, eu)
90110
}
91-
it.pageInfo.Token = resp.NextPageToken
92-
return resp.NextPageToken, nil
111+
it.pageInfo.Token = parsed.NextPageToken
112+
return parsed.NextPageToken, nil
93113
}
94114

95115
// ExportedUserRecord is the returned user value used when listing all the users.
@@ -98,53 +118,3 @@ type ExportedUserRecord struct {
98118
PasswordHash string
99119
PasswordSalt string
100120
}
101-
102-
func makeExportedUser(r *identitytoolkit.UserInfo) (*ExportedUserRecord, error) {
103-
var cc map[string]interface{}
104-
if r.CustomAttributes != "" {
105-
if err := json.Unmarshal([]byte(r.CustomAttributes), &cc); err != nil {
106-
return nil, err
107-
}
108-
if len(cc) == 0 {
109-
cc = nil
110-
}
111-
}
112-
113-
var providerUserInfo []*UserInfo
114-
for _, u := range r.ProviderUserInfo {
115-
info := &UserInfo{
116-
DisplayName: u.DisplayName,
117-
Email: u.Email,
118-
PhoneNumber: u.PhoneNumber,
119-
PhotoURL: u.PhotoUrl,
120-
ProviderID: u.ProviderId,
121-
UID: u.RawId,
122-
}
123-
providerUserInfo = append(providerUserInfo, info)
124-
}
125-
126-
resp := &ExportedUserRecord{
127-
UserRecord: &UserRecord{
128-
UserInfo: &UserInfo{
129-
DisplayName: r.DisplayName,
130-
Email: r.Email,
131-
PhoneNumber: r.PhoneNumber,
132-
PhotoURL: r.PhotoUrl,
133-
ProviderID: defaultProviderID,
134-
UID: r.LocalId,
135-
},
136-
CustomClaims: cc,
137-
Disabled: r.Disabled,
138-
EmailVerified: r.EmailVerified,
139-
ProviderUserInfo: providerUserInfo,
140-
TokensValidAfterMillis: r.ValidSince * 1000,
141-
UserMetadata: &UserMetadata{
142-
LastLogInTimestamp: r.LastLoginAt,
143-
CreationTimestamp: r.CreatedAt,
144-
},
145-
},
146-
PasswordHash: r.PasswordHash,
147-
PasswordSalt: r.Salt,
148-
}
149-
return resp, nil
150-
}

auth/hash/hash.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import (
3131
type Bcrypt struct{}
3232

3333
// Config returns the validated hash configuration.
34-
func (b Bcrypt) Config() (*internal.HashConfig, error) {
35-
return &internal.HashConfig{HashAlgorithm: "BCRYPT"}, nil
34+
func (b Bcrypt) Config() (internal.HashConfig, error) {
35+
return internal.HashConfig{"hashAlgorithm": "BCRYPT"}, nil
3636
}
3737

3838
// StandardScrypt represents the standard scrypt hash algorithm.
@@ -47,14 +47,13 @@ type StandardScrypt struct {
4747
}
4848

4949
// Config returns the validated hash configuration.
50-
func (s StandardScrypt) Config() (*internal.HashConfig, error) {
51-
return &internal.HashConfig{
52-
HashAlgorithm: "STANDARD_SCRYPT",
53-
DerivedKeyLength: int64(s.DerivedKeyLength),
54-
BlockSize: int64(s.BlockSize),
55-
Parallelization: int64(s.Parallelization),
56-
MemoryCost: int64(s.MemoryCost),
57-
ForceSendFields: []string{"BlockSize", "Parallelization", "MemoryCost", "DkLen"},
50+
func (s StandardScrypt) Config() (internal.HashConfig, error) {
51+
return internal.HashConfig{
52+
"hashAlgorithm": "STANDARD_SCRYPT",
53+
"dkLen": s.DerivedKeyLength,
54+
"blockSize": s.BlockSize,
55+
"parallelization": s.Parallelization,
56+
"memoryCost": s.MemoryCost,
5857
}, nil
5958
}
6059

@@ -72,7 +71,7 @@ type Scrypt struct {
7271
}
7372

7473
// Config returns the validated hash configuration.
75-
func (s Scrypt) Config() (*internal.HashConfig, error) {
74+
func (s Scrypt) Config() (internal.HashConfig, error) {
7675
if len(s.Key) == 0 {
7776
return nil, errors.New("signer key not specified")
7877
}
@@ -82,12 +81,12 @@ func (s Scrypt) Config() (*internal.HashConfig, error) {
8281
if s.MemoryCost < 1 || s.MemoryCost > 14 {
8382
return nil, errors.New("memory cost must be between 1 and 14")
8483
}
85-
return &internal.HashConfig{
86-
HashAlgorithm: "SCRYPT",
87-
SignerKey: base64.RawURLEncoding.EncodeToString(s.Key),
88-
SaltSeparator: base64.RawURLEncoding.EncodeToString(s.SaltSeparator),
89-
Rounds: int64(s.Rounds),
90-
MemoryCost: int64(s.MemoryCost),
84+
return internal.HashConfig{
85+
"hashAlgorithm": "SCRYPT",
86+
"signerKey": base64.RawURLEncoding.EncodeToString(s.Key),
87+
"saltSeparator": base64.RawURLEncoding.EncodeToString(s.SaltSeparator),
88+
"rounds": s.Rounds,
89+
"memoryCost": s.MemoryCost,
9190
}, nil
9291
}
9392

@@ -100,7 +99,7 @@ type HMACMD5 struct {
10099
}
101100

102101
// Config returns the validated hash configuration.
103-
func (h HMACMD5) Config() (*internal.HashConfig, error) {
102+
func (h HMACMD5) Config() (internal.HashConfig, error) {
104103
return hmacConfig("HMAC_MD5", h.Key)
105104
}
106105

@@ -114,7 +113,7 @@ type HMACSHA1 struct {
114113
}
115114

116115
// Config returns the validated hash configuration.
117-
func (h HMACSHA1) Config() (*internal.HashConfig, error) {
116+
func (h HMACSHA1) Config() (internal.HashConfig, error) {
118117
return hmacConfig("HMAC_SHA1", h.Key)
119118
}
120119

@@ -128,7 +127,7 @@ type HMACSHA256 struct {
128127
}
129128

130129
// Config returns the validated hash configuration.
131-
func (h HMACSHA256) Config() (*internal.HashConfig, error) {
130+
func (h HMACSHA256) Config() (internal.HashConfig, error) {
132131
return hmacConfig("HMAC_SHA256", h.Key)
133132
}
134133

@@ -142,7 +141,7 @@ type HMACSHA512 struct {
142141
}
143142

144143
// Config returns the validated hash configuration.
145-
func (h HMACSHA512) Config() (*internal.HashConfig, error) {
144+
func (h HMACSHA512) Config() (internal.HashConfig, error) {
146145
return hmacConfig("HMAC_SHA512", h.Key)
147146
}
148147

@@ -156,7 +155,7 @@ type MD5 struct {
156155
}
157156

158157
// Config returns the validated hash configuration.
159-
func (h MD5) Config() (*internal.HashConfig, error) {
158+
func (h MD5) Config() (internal.HashConfig, error) {
160159
return basicConfig("MD5", h.Rounds)
161160
}
162161

@@ -170,7 +169,7 @@ type PBKDF2SHA256 struct {
170169
}
171170

172171
// Config returns the validated hash configuration.
173-
func (h PBKDF2SHA256) Config() (*internal.HashConfig, error) {
172+
func (h PBKDF2SHA256) Config() (internal.HashConfig, error) {
174173
return basicConfig("PBKDF2_SHA256", h.Rounds)
175174
}
176175

@@ -184,7 +183,7 @@ type PBKDFSHA1 struct {
184183
}
185184

186185
// Config returns the validated hash configuration.
187-
func (h PBKDFSHA1) Config() (*internal.HashConfig, error) {
186+
func (h PBKDFSHA1) Config() (internal.HashConfig, error) {
188187
return basicConfig("PBKDF_SHA1", h.Rounds)
189188
}
190189

@@ -198,7 +197,7 @@ type SHA1 struct {
198197
}
199198

200199
// Config returns the validated hash configuration.
201-
func (h SHA1) Config() (*internal.HashConfig, error) {
200+
func (h SHA1) Config() (internal.HashConfig, error) {
202201
return basicConfig("SHA1", h.Rounds)
203202
}
204203

@@ -212,7 +211,7 @@ type SHA256 struct {
212211
}
213212

214213
// Config returns the validated hash configuration.
215-
func (h SHA256) Config() (*internal.HashConfig, error) {
214+
func (h SHA256) Config() (internal.HashConfig, error) {
216215
return basicConfig("SHA256", h.Rounds)
217216
}
218217

@@ -226,27 +225,26 @@ type SHA512 struct {
226225
}
227226

228227
// Config returns the validated hash configuration.
229-
func (h SHA512) Config() (*internal.HashConfig, error) {
228+
func (h SHA512) Config() (internal.HashConfig, error) {
230229
return basicConfig("SHA512", h.Rounds)
231230
}
232231

233-
func hmacConfig(name string, key []byte) (*internal.HashConfig, error) {
232+
func hmacConfig(name string, key []byte) (internal.HashConfig, error) {
234233
if len(key) == 0 {
235234
return nil, errors.New("signer key not specified")
236235
}
237-
return &internal.HashConfig{
238-
HashAlgorithm: name,
239-
SignerKey: base64.RawURLEncoding.EncodeToString(key),
236+
return internal.HashConfig{
237+
"hashAlgorithm": name,
238+
"signerKey": base64.RawURLEncoding.EncodeToString(key),
240239
}, nil
241240
}
242241

243-
func basicConfig(name string, rounds int) (*internal.HashConfig, error) {
242+
func basicConfig(name string, rounds int) (internal.HashConfig, error) {
244243
if rounds < 0 || rounds > 120000 {
245244
return nil, errors.New("rounds must be between 0 and 120000")
246245
}
247-
return &internal.HashConfig{
248-
HashAlgorithm: name,
249-
Rounds: int64(rounds),
250-
ForceSendFields: []string{"Rounds"},
246+
return internal.HashConfig{
247+
"hashAlgorithm": name,
248+
"rounds": rounds,
251249
}, nil
252250
}

0 commit comments

Comments
 (0)