Wrong password when importing user into Firebase (password_hash using SHA256)

时间:2018-12-03 12:51:43

标签: python firebase firebase-authentication firebase-admin

We are migrating users into Firebase Auth from an external database using password hashed with SHA256.

We currently try from firebase_admin import auth to import the users. We are able to import an example user, but not able to login with the user (wrong password).

Here are the minimal code snippets to reproduce the issue.

  1. We use a user as an example
  2. We use sha256 to hash password
  3. Run import_users_to_firebase()
  4. Try to login with the example user -> invalid password

`

def hash_password(raw_password):
    import base64
    import hashlib
    algo = hashlib.sha256()
    algo.update(raw_password)
    return base64.b64encode(algo.digest())

def create_mock_user_data():
    email = 'test@example.com'
    password = 'test@example.com'
    password_hash = hash_password(password)
    print 'password_hash: {}'.format(password_hash)
    return email, password_hash


def import_users_to_firebase():
    mock_email, mock_password_hash = create_mock_user_data()
    users = [
        auth.ImportUserRecord(
            uid='someuid',
            display_name='Test example',
            email=mock_email,
            email_verified=False,
            password_hash=b'{}'.format(mock_password_hash),
        ),
        # users list can contain up to 1000 records
    ]
    hash_alg = auth.UserImportHash.sha256(rounds=0)
    result = auth.import_users(users, hash_alg=hash_alg)

    print('Successfully imported {0} users. Failed to import {1} users.'.format(
    result.success_count, result.failure_count))

    for err in result.errors:
        print('Failed to import {0} due to {1}'.format(
            users[err.index].uid, err.reason))

`

Am I missing something or misunderstanding something here?

Is there a way that I can check the password_hash is imported correctly? Or is there a way that I can config which hashing algorithm Firebase is using so that the imported password would match the example user login?

1 个答案:

答案 0 :(得分:1)

非常感谢Firebase支持团队,我的问题得以解决:) 诀窍是不要在hash_password()中进行base64编码,也不要在以后对其进行解码,因此它是原始字节序列。那是

auth.ImportUserRecord(
    uid='someuid',
    display_name='Test example',
    email=mock_email,
    email_verified=False,
    password_hash=mock_password_hash, # <-- mock_password_hash is not base64 encoded
)