Skip to content

Commit 77177c7

Browse files
Merge dev into master
2 parents cef91ac + 7f59540 commit 77177c7

File tree

14 files changed

+459
-76
lines changed

14 files changed

+459
-76
lines changed
File renamed without changes.
File renamed without changes.

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ required to ensure that exported user records contain the password hashes of the
137137
3. Click 'ADD ANOTHER ROLE' and choose 'Firebase Authentication Admin'.
138138
4. Click 'SAVE'.
139139

140+
Some of the integration tests require an
141+
[Identity Platform](https://cloud.google.com/identity-platform/) project with multi-tenancy
142+
[enabled](https://cloud.google.com/identity-platform/docs/multi-tenancy-quickstart#enabling_multi-tenancy).
143+
An existing Firebase project can be upgraded to an Identity Platform project without losing any
144+
functionality via the
145+
[Identity Platform Marketplace Page](https://console.cloud.google.com/customer-identity). Note that
146+
charges may be incurred for active users beyond the Identity Platform free tier.
147+
140148
Now you can invoke the test suite as follows:
141149

142150
```bash

auth/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
138138
// - If the SDK was initialized with service account credentials, uses the private key present in
139139
// the credentials to sign tokens locally.
140140
// - If a service account email was specified during initialization (via firebase.Config struct),
141-
// calls the IAM service with that email to sign tokens remotely. See
142-
// https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts/signBlob.
141+
// calls the IAMCredentials service with that email to sign tokens remotely. See
142+
// https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/signBlob.
143143
// - If the code is deployed in the Google App Engine standard environment, uses the App Identity
144144
// service to sign tokens. See https://cloud.google.com/appengine/docs/standard/go/reference#SignBytes.
145145
// - If the code is deployed in a different GCP-managed environment (e.g. Google Compute Engine),

auth/hash/hash.go

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ import (
2525
"firebase.google.com/go/v4/internal"
2626
)
2727

28+
// InputOrderType specifies the order in which users' passwords/salts are hashed
29+
type InputOrderType int
30+
31+
// Available InputOrderType values
32+
const (
33+
InputOrderUnspecified InputOrderType = iota
34+
InputOrderSaltFirst
35+
InputOrderPasswordFirst
36+
)
37+
2838
// Bcrypt represents the BCRYPT hash algorithm.
2939
//
3040
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_bcrypt_hashed_passwords
@@ -96,12 +106,13 @@ func (s Scrypt) Config() (internal.HashConfig, error) {
96106
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
97107
// for more details. Key is required.
98108
type HMACMD5 struct {
99-
Key []byte
109+
Key []byte
110+
InputOrder InputOrderType
100111
}
101112

102113
// Config returns the validated hash configuration.
103114
func (h HMACMD5) Config() (internal.HashConfig, error) {
104-
return hmacConfig("HMAC_MD5", h.Key)
115+
return hmacConfig("HMAC_MD5", h.Key, h.InputOrder)
105116
}
106117

107118
// HMACSHA1 represents the HMAC SHA512 hash algorithm.
@@ -110,12 +121,13 @@ func (h HMACMD5) Config() (internal.HashConfig, error) {
110121
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
111122
// for more details.
112123
type HMACSHA1 struct {
113-
Key []byte
124+
Key []byte
125+
InputOrder InputOrderType
114126
}
115127

116128
// Config returns the validated hash configuration.
117129
func (h HMACSHA1) Config() (internal.HashConfig, error) {
118-
return hmacConfig("HMAC_SHA1", h.Key)
130+
return hmacConfig("HMAC_SHA1", h.Key, h.InputOrder)
119131
}
120132

121133
// HMACSHA256 represents the HMAC SHA512 hash algorithm.
@@ -124,12 +136,13 @@ func (h HMACSHA1) Config() (internal.HashConfig, error) {
124136
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
125137
// for more details.
126138
type HMACSHA256 struct {
127-
Key []byte
139+
Key []byte
140+
InputOrder InputOrderType
128141
}
129142

130143
// Config returns the validated hash configuration.
131144
func (h HMACSHA256) Config() (internal.HashConfig, error) {
132-
return hmacConfig("HMAC_SHA256", h.Key)
145+
return hmacConfig("HMAC_SHA256", h.Key, h.InputOrder)
133146
}
134147

135148
// HMACSHA512 represents the HMAC SHA512 hash algorithm.
@@ -138,12 +151,13 @@ func (h HMACSHA256) Config() (internal.HashConfig, error) {
138151
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
139152
// for more details.
140153
type HMACSHA512 struct {
141-
Key []byte
154+
Key []byte
155+
InputOrder InputOrderType
142156
}
143157

144158
// Config returns the validated hash configuration.
145159
func (h HMACSHA512) Config() (internal.HashConfig, error) {
146-
return hmacConfig("HMAC_SHA512", h.Key)
160+
return hmacConfig("HMAC_SHA512", h.Key, h.InputOrder)
147161
}
148162

149163
// MD5 represents the MD5 hash algorithm.
@@ -152,12 +166,13 @@ func (h HMACSHA512) Config() (internal.HashConfig, error) {
152166
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
153167
// for more details.
154168
type MD5 struct {
155-
Rounds int
169+
Rounds int
170+
InputOrder InputOrderType
156171
}
157172

158173
// Config returns the validated hash configuration.
159174
func (h MD5) Config() (internal.HashConfig, error) {
160-
return basicConfig("MD5", h.Rounds)
175+
return basicConfig("MD5", h.Rounds, h.InputOrder)
161176
}
162177

163178
// PBKDF2SHA256 represents the PBKDF2SHA256 hash algorithm.
@@ -171,7 +186,7 @@ type PBKDF2SHA256 struct {
171186

172187
// Config returns the validated hash configuration.
173188
func (h PBKDF2SHA256) Config() (internal.HashConfig, error) {
174-
return basicConfig("PBKDF2_SHA256", h.Rounds)
189+
return basicConfig("PBKDF2_SHA256", h.Rounds, InputOrderUnspecified)
175190
}
176191

177192
// PBKDFSHA1 represents the PBKDFSHA1 hash algorithm.
@@ -185,7 +200,7 @@ type PBKDFSHA1 struct {
185200

186201
// Config returns the validated hash configuration.
187202
func (h PBKDFSHA1) Config() (internal.HashConfig, error) {
188-
return basicConfig("PBKDF_SHA1", h.Rounds)
203+
return basicConfig("PBKDF_SHA1", h.Rounds, InputOrderUnspecified)
189204
}
190205

191206
// SHA1 represents the SHA1 hash algorithm.
@@ -194,12 +209,13 @@ func (h PBKDFSHA1) Config() (internal.HashConfig, error) {
194209
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
195210
// for more details.
196211
type SHA1 struct {
197-
Rounds int
212+
Rounds int
213+
InputOrder InputOrderType
198214
}
199215

200216
// Config returns the validated hash configuration.
201217
func (h SHA1) Config() (internal.HashConfig, error) {
202-
return basicConfig("SHA1", h.Rounds)
218+
return basicConfig("SHA1", h.Rounds, h.InputOrder)
203219
}
204220

205221
// SHA256 represents the SHA256 hash algorithm.
@@ -208,12 +224,13 @@ func (h SHA1) Config() (internal.HashConfig, error) {
208224
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
209225
// for more details.
210226
type SHA256 struct {
211-
Rounds int
227+
Rounds int
228+
InputOrder InputOrderType
212229
}
213230

214231
// Config returns the validated hash configuration.
215232
func (h SHA256) Config() (internal.HashConfig, error) {
216-
return basicConfig("SHA256", h.Rounds)
233+
return basicConfig("SHA256", h.Rounds, h.InputOrder)
217234
}
218235

219236
// SHA512 represents the SHA512 hash algorithm.
@@ -222,25 +239,32 @@ func (h SHA256) Config() (internal.HashConfig, error) {
222239
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
223240
// for more details.
224241
type SHA512 struct {
225-
Rounds int
242+
Rounds int
243+
InputOrder InputOrderType
226244
}
227245

228246
// Config returns the validated hash configuration.
229247
func (h SHA512) Config() (internal.HashConfig, error) {
230-
return basicConfig("SHA512", h.Rounds)
248+
return basicConfig("SHA512", h.Rounds, h.InputOrder)
231249
}
232250

233-
func hmacConfig(name string, key []byte) (internal.HashConfig, error) {
251+
func hmacConfig(name string, key []byte, order InputOrderType) (internal.HashConfig, error) {
234252
if len(key) == 0 {
235253
return nil, errors.New("signer key not specified")
236254
}
237-
return internal.HashConfig{
255+
conf := internal.HashConfig{
238256
"hashAlgorithm": name,
239257
"signerKey": base64.RawURLEncoding.EncodeToString(key),
240-
}, nil
258+
}
259+
if order == InputOrderSaltFirst {
260+
conf["passwordHashOrder"] = "SALT_AND_PASSWORD"
261+
} else if order == InputOrderPasswordFirst {
262+
conf["passwordHashOrder"] = "PASSWORD_AND_SALT"
263+
}
264+
return conf, nil
241265
}
242266

243-
func basicConfig(name string, rounds int) (internal.HashConfig, error) {
267+
func basicConfig(name string, rounds int, order InputOrderType) (internal.HashConfig, error) {
244268
minRounds := 0
245269
maxRounds := 120000
246270
switch name {
@@ -253,8 +277,15 @@ func basicConfig(name string, rounds int) (internal.HashConfig, error) {
253277
if rounds < minRounds || maxRounds < rounds {
254278
return nil, fmt.Errorf("rounds must be between %d and %d", minRounds, maxRounds)
255279
}
256-
return internal.HashConfig{
280+
281+
conf := internal.HashConfig{
257282
"hashAlgorithm": name,
258283
"rounds": rounds,
259-
}, nil
284+
}
285+
if order == InputOrderSaltFirst {
286+
conf["passwordHashOrder"] = "SALT_AND_PASSWORD"
287+
} else if order == InputOrderPasswordFirst {
288+
conf["passwordHashOrder"] = "PASSWORD_AND_SALT"
289+
}
290+
return conf, nil
260291
}

0 commit comments

Comments
 (0)