I have recently been migrating my app to firebase, but I am facing a problem with migrating my users to Firebase Auth.
In my old backend written using NodeJS, I used the following code to generate a password hash for a user:
var crypto = require('crypto');
var sha256 = crypto.createHmac("sha256", "MY-SECRET");
var hash = sha256.update(str).digest('hex');
Following this documentation, I have tried running this command to import my users:
firebase auth:import "my_users.csv" --hash-algo "HMAC_SHA256" --hash-key "my-secret-in-base64" --project "my_project"
my_users.csv:
random_uid,[email protected],,their_hash_created_with_my_program
The command says that it imported the users successfuly, but when I try and log into an account using signInWithEmailAndPassword
, it gives me an auth/wrong-password
error.
I am new to Firebase and any help would be much appreciated.
CodePudding user response:
Updated Answer:
I modified your code a little but and tested it out locally using a made up user with an email of [email protected]
and a password of password
. Hopefully you can reproduce logging in by running this quick test and then modify it to work with your hash values. The secret I am using is MY-SECRET
.
var str = 'password'; // password used for testing
var mySecret = 'MY-SECRET'; // secret used for testing
var crypto = require('crypto');
var sha256 = crypto.createHmac("sha256", mySecret);
var hash = sha256.update(str).digest('hex');
var base64String = Buffer.from(hash, 'hex').toString('base64');
var secret64String = Buffer.from(mySecret).toString('base64');
console.log('hexhash: %s\nbase64StrHash: %s\nsecret base64: %s', hash, base64String, secret64String);
// This outputs the following:
// hexhash: 67f8b4aed3b3c353fcb1fbb7f2efaaafe15e08239eaebfdbe61e72b40a737d08
// base64StrHash: Z/i0rtOzw1P8sfu38u qr FeCCOerr/b5h5ytApzfQg=
// secret base64: TVktU0VDUkVU
I then ran the following command:
firebase auth:import "users.csv" --hash-algo "HMAC_SHA256" --hash-key "TVktU0VDUkVU" --project "mytestproject"
My csv:
0123456789,[email protected],,Z/i0rtOzw1P8sfu38u qr FeCCOerr/b5h5ytApzfQg=, , , , , , , , , , , , , , , , , , , , ,
Using these exact values as input for my csv and for my encoding worked for me. If this is not working for you, it may be something else with how you originally coded your hashing such as adding a salt or some other values to the password before hashing. I would first try to validate that the base64 strings that you are supplying are working correctly with a small test case and move forward from there. If the csv file and command I provided do not work with your project, it may be that your client is not connected to Firebase Auth (I sometimes forget to connect to Firebase on setting up a new client applicaton).
Original Answer: Reading the documentation, I think instead of digest("hex") you would want digest('base64') as the hash-key value in the csv as it needs to be a base64 encoded string.