@@ -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.
98108type HMACMD5 struct {
99- Key []byte
109+ Key []byte
110+ InputOrder InputOrderType
100111}
101112
102113// Config returns the validated hash configuration.
103114func (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.
112123type HMACSHA1 struct {
113- Key []byte
124+ Key []byte
125+ InputOrder InputOrderType
114126}
115127
116128// Config returns the validated hash configuration.
117129func (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.
126138type HMACSHA256 struct {
127- Key []byte
139+ Key []byte
140+ InputOrder InputOrderType
128141}
129142
130143// Config returns the validated hash configuration.
131144func (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.
140153type HMACSHA512 struct {
141- Key []byte
154+ Key []byte
155+ InputOrder InputOrderType
142156}
143157
144158// Config returns the validated hash configuration.
145159func (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.
154168type MD5 struct {
155- Rounds int
169+ Rounds int
170+ InputOrder InputOrderType
156171}
157172
158173// Config returns the validated hash configuration.
159174func (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.
173188func (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.
187202func (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.
196211type SHA1 struct {
197- Rounds int
212+ Rounds int
213+ InputOrder InputOrderType
198214}
199215
200216// Config returns the validated hash configuration.
201217func (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.
210226type SHA256 struct {
211- Rounds int
227+ Rounds int
228+ InputOrder InputOrderType
212229}
213230
214231// Config returns the validated hash configuration.
215232func (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.
224241type SHA512 struct {
225- Rounds int
242+ Rounds int
243+ InputOrder InputOrderType
226244}
227245
228246// Config returns the validated hash configuration.
229247func (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