Firebase-从其他帐户导入的用户无法登录

时间:2018-07-16 15:22:44

标签: python-3.x firebase firebase-authentication

要求: 将用户从Firebase帐户导出到其他帐户

我如何导出: 使用终端和Firebase CLI,并使用以下脚本 firebase auth:export export.json --format=json --project <valid project id>

这给了我一个所有用户的json文件。

要导入的代码

    import firebase_admin
from firebase_admin import auth
from firebase_admin import credentials
import base64

cred = credentials.Certificate("service.json")
firebase_admin.initialize_app(cred)

def import_with_scrypt():
    users = [
        auth.ImportUserRecord(
            uid='KmlKsGPEDiWtGMkwYyA4VlhLAQ43',
            email='testme@test.com',
            password_hash=b'base64 key',
            password_salt=b'base64 key'
        ),
    ]

    # All the parameters below can be obtained from the Firebase Console's "Users"
    # section. Base64 encoded parameters must be decoded into raw bytes.
    hash_alg = auth.UserImportHash.scrypt(
        key=base64.b64decode('base64 key'),
        salt_separator=base64.b64decode("base64 key"),
        rounds=8,
        memory_cost=14
    )
    try:
        result = auth.import_users(users, hash_alg=hash_alg)
        for err in result.errors:
            print('Failed to import user:', err.reason)
    except auth.AuthError as error:
        print('Error importing users:', error)

import_with_scrypt()

问题: 这将用户成功导入到新的Firebase帐户。 当我尝试使用现有用户凭据登录新帐户中的该用户帐户时,

它抛出一个错误

  

auth / wrong-password密码无效或用户没有密码。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并且能够通过使用crypto npm package来解决。请记住,我的解决方案是在NodeJS中,但是您应该能够将其转换为python。我将其创建为云函数,但是无论您如何传递数据,其概念都相同:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import * as crypto from 'crypto';
import { IAuthUser } from '../models/auth-user.model';

export const importUsers = functions.https.onRequest((req, res) => {

    let users = new Array<IAuthUser>()

    // Map user data with generated password hash/salts
    req.body.users.map(user => {
        let authUser = user as IAuthUser
        const salt = crypto.randomBytes(16)
        authUser.passwordHash = crypto.pbkdf2Sync(user.password, salt, 100000, 64, 'sha256')
        authUser.passwordSalt = salt
        users.push(authUser)
    })

    // Send to Firebase Auth
    admin.auth().importUsers(
        users,
        {
            hash: {
                algorithm: 'PBKDF2_SHA256',
                rounds: 100000
            }
        }
    ).then(function (results) {
        results.errors.forEach(function (indexedError) {
            console.error('Error importing user ' + indexedError.index);
        });
        res.send(results)
    }).catch(function (error) {
        console.error(error);
        res.status(500).send(error)
    });
});