@@ -25,6 +25,16 @@ import (
25
25
"firebase.google.com/go/v4/internal"
26
26
)
27
27
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
+
28
38
// Bcrypt represents the BCRYPT hash algorithm.
29
39
//
30
40
// 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) {
96
106
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
97
107
// for more details. Key is required.
98
108
type HMACMD5 struct {
99
- Key []byte
109
+ Key []byte
110
+ InputOrder InputOrderType
100
111
}
101
112
102
113
// Config returns the validated hash configuration.
103
114
func (h HMACMD5 ) Config () (internal.HashConfig , error ) {
104
- return hmacConfig ("HMAC_MD5" , h .Key )
115
+ return hmacConfig ("HMAC_MD5" , h .Key , h . InputOrder )
105
116
}
106
117
107
118
// HMACSHA1 represents the HMAC SHA512 hash algorithm.
@@ -110,12 +121,13 @@ func (h HMACMD5) Config() (internal.HashConfig, error) {
110
121
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
111
122
// for more details.
112
123
type HMACSHA1 struct {
113
- Key []byte
124
+ Key []byte
125
+ InputOrder InputOrderType
114
126
}
115
127
116
128
// Config returns the validated hash configuration.
117
129
func (h HMACSHA1 ) Config () (internal.HashConfig , error ) {
118
- return hmacConfig ("HMAC_SHA1" , h .Key )
130
+ return hmacConfig ("HMAC_SHA1" , h .Key , h . InputOrder )
119
131
}
120
132
121
133
// HMACSHA256 represents the HMAC SHA512 hash algorithm.
@@ -124,12 +136,13 @@ func (h HMACSHA1) Config() (internal.HashConfig, error) {
124
136
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
125
137
// for more details.
126
138
type HMACSHA256 struct {
127
- Key []byte
139
+ Key []byte
140
+ InputOrder InputOrderType
128
141
}
129
142
130
143
// Config returns the validated hash configuration.
131
144
func (h HMACSHA256 ) Config () (internal.HashConfig , error ) {
132
- return hmacConfig ("HMAC_SHA256" , h .Key )
145
+ return hmacConfig ("HMAC_SHA256" , h .Key , h . InputOrder )
133
146
}
134
147
135
148
// HMACSHA512 represents the HMAC SHA512 hash algorithm.
@@ -138,12 +151,13 @@ func (h HMACSHA256) Config() (internal.HashConfig, error) {
138
151
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_hmac_hashed_passwords
139
152
// for more details.
140
153
type HMACSHA512 struct {
141
- Key []byte
154
+ Key []byte
155
+ InputOrder InputOrderType
142
156
}
143
157
144
158
// Config returns the validated hash configuration.
145
159
func (h HMACSHA512 ) Config () (internal.HashConfig , error ) {
146
- return hmacConfig ("HMAC_SHA512" , h .Key )
160
+ return hmacConfig ("HMAC_SHA512" , h .Key , h . InputOrder )
147
161
}
148
162
149
163
// MD5 represents the MD5 hash algorithm.
@@ -152,12 +166,13 @@ func (h HMACSHA512) Config() (internal.HashConfig, error) {
152
166
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
153
167
// for more details.
154
168
type MD5 struct {
155
- Rounds int
169
+ Rounds int
170
+ InputOrder InputOrderType
156
171
}
157
172
158
173
// Config returns the validated hash configuration.
159
174
func (h MD5 ) Config () (internal.HashConfig , error ) {
160
- return basicConfig ("MD5" , h .Rounds )
175
+ return basicConfig ("MD5" , h .Rounds , h . InputOrder )
161
176
}
162
177
163
178
// PBKDF2SHA256 represents the PBKDF2SHA256 hash algorithm.
@@ -171,7 +186,7 @@ type PBKDF2SHA256 struct {
171
186
172
187
// Config returns the validated hash configuration.
173
188
func (h PBKDF2SHA256 ) Config () (internal.HashConfig , error ) {
174
- return basicConfig ("PBKDF2_SHA256" , h .Rounds )
189
+ return basicConfig ("PBKDF2_SHA256" , h .Rounds , InputOrderUnspecified )
175
190
}
176
191
177
192
// PBKDFSHA1 represents the PBKDFSHA1 hash algorithm.
@@ -185,7 +200,7 @@ type PBKDFSHA1 struct {
185
200
186
201
// Config returns the validated hash configuration.
187
202
func (h PBKDFSHA1 ) Config () (internal.HashConfig , error ) {
188
- return basicConfig ("PBKDF_SHA1" , h .Rounds )
203
+ return basicConfig ("PBKDF_SHA1" , h .Rounds , InputOrderUnspecified )
189
204
}
190
205
191
206
// SHA1 represents the SHA1 hash algorithm.
@@ -194,12 +209,13 @@ func (h PBKDFSHA1) Config() (internal.HashConfig, error) {
194
209
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
195
210
// for more details.
196
211
type SHA1 struct {
197
- Rounds int
212
+ Rounds int
213
+ InputOrder InputOrderType
198
214
}
199
215
200
216
// Config returns the validated hash configuration.
201
217
func (h SHA1 ) Config () (internal.HashConfig , error ) {
202
- return basicConfig ("SHA1" , h .Rounds )
218
+ return basicConfig ("SHA1" , h .Rounds , h . InputOrder )
203
219
}
204
220
205
221
// SHA256 represents the SHA256 hash algorithm.
@@ -208,12 +224,13 @@ func (h SHA1) Config() (internal.HashConfig, error) {
208
224
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
209
225
// for more details.
210
226
type SHA256 struct {
211
- Rounds int
227
+ Rounds int
228
+ InputOrder InputOrderType
212
229
}
213
230
214
231
// Config returns the validated hash configuration.
215
232
func (h SHA256 ) Config () (internal.HashConfig , error ) {
216
- return basicConfig ("SHA256" , h .Rounds )
233
+ return basicConfig ("SHA256" , h .Rounds , h . InputOrder )
217
234
}
218
235
219
236
// SHA512 represents the SHA512 hash algorithm.
@@ -222,25 +239,32 @@ func (h SHA256) Config() (internal.HashConfig, error) {
222
239
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
223
240
// for more details.
224
241
type SHA512 struct {
225
- Rounds int
242
+ Rounds int
243
+ InputOrder InputOrderType
226
244
}
227
245
228
246
// Config returns the validated hash configuration.
229
247
func (h SHA512 ) Config () (internal.HashConfig , error ) {
230
- return basicConfig ("SHA512" , h .Rounds )
248
+ return basicConfig ("SHA512" , h .Rounds , h . InputOrder )
231
249
}
232
250
233
- func hmacConfig (name string , key []byte ) (internal.HashConfig , error ) {
251
+ func hmacConfig (name string , key []byte , order InputOrderType ) (internal.HashConfig , error ) {
234
252
if len (key ) == 0 {
235
253
return nil , errors .New ("signer key not specified" )
236
254
}
237
- return internal.HashConfig {
255
+ conf := internal.HashConfig {
238
256
"hashAlgorithm" : name ,
239
257
"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
241
265
}
242
266
243
- func basicConfig (name string , rounds int ) (internal.HashConfig , error ) {
267
+ func basicConfig (name string , rounds int , order InputOrderType ) (internal.HashConfig , error ) {
244
268
minRounds := 0
245
269
maxRounds := 120000
246
270
switch name {
@@ -253,8 +277,15 @@ func basicConfig(name string, rounds int) (internal.HashConfig, error) {
253
277
if rounds < minRounds || maxRounds < rounds {
254
278
return nil , fmt .Errorf ("rounds must be between %d and %d" , minRounds , maxRounds )
255
279
}
256
- return internal.HashConfig {
280
+
281
+ conf := internal.HashConfig {
257
282
"hashAlgorithm" : name ,
258
283
"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
260
291
}
0 commit comments