Firebase Admin SDK 提供了一个 Auth.importUsers()
API,用于以提升的权限将用户批量导入 Firebase Authentication。虽然 Firebase CLI 也提供此功能,但 Admin SDK 可让您以程序化方式上传外部身份验证系统或其他 Firebase 项目中的现有用户,而无需创建中间 CSV 或 JSON 文件。
用户导入 API 具有以下优势:
- 能够从使用不同密码哈希算法的外部身份验证系统迁移用户。
- 能够从其他 Firebase 项目迁移用户。
- 经过优化,能够快速而高效地执行批量导入操作。此操作在处理用户时不会核实
uid
、email
、phoneNumber
或其他标识符的重复情况。 - 能够迁移现有 OAuth 用户或创建新的 OAuth 用户(Google、Facebook 等)。
- 能够直接批量导入使用了自定义声明的用户。
用法
一次 API 调用最多可以导入 1000 名用户。请注意,此操作针对速度进行了优化,不会核实 uid
、email
、phoneNumber
和其他唯一标识符的重复情况。导入与现有 uid
冲突的用户将会替换这个现有用户。导入具有任何其他重复字段(例如 email
)的用户,会生成具有该相同值的另一位用户。因此,在您使用此 API 时,必须确保唯一字段不会出现重复。
Node.js
// Up to 1000 users can be imported at once.
const userImportRecords = [
{
uid: 'uid1',
email: 'user1@example.com',
passwordHash: Buffer.from('passwordHash1'),
passwordSalt: Buffer.from('salt1'),
},
{
uid: 'uid2',
email: 'user2@example.com',
passwordHash: Buffer.from('passwordHash2'),
passwordSalt: Buffer.from('salt2'),
},
//...
];
Java
// Up to 1000 users can be imported at once.
List<ImportUserRecord> users = new ArrayList<>();
users.add(ImportUserRecord.builder()
.setUid("uid1")
.setEmail("user1@example.com")
.setPasswordHash("passwordHash1".getBytes())
.setPasswordSalt("salt1".getBytes())
.build());
users.add(ImportUserRecord.builder()
.setUid("uid2")
.setEmail("user2@example.com")
.setPasswordHash("passwordHash2".getBytes())
.setPasswordSalt("salt2".getBytes())
.build());
Python
# Up to 1000 users can be imported at once.
users = [
auth.ImportUserRecord(
uid='uid1',
email='user1@example.com',
password_hash=b'password_hash_1',
password_salt=b'salt1'
),
auth.ImportUserRecord(
uid='uid2',
email='user2@example.com',
password_hash=b'password_hash_2',
password_salt=b'salt2'
),
]
Go
// Up to 1000 users can be imported at once.
var users []*auth.UserToImport
users = append(users, (&auth.UserToImport{}).
UID("uid1").
Email("user1@example.com").
PasswordHash([]byte("passwordHash1")).
PasswordSalt([]byte("salt1")))
users = append(users, (&auth.UserToImport{}).
UID("uid2").
Email("user2@example.com").
PasswordHash([]byte("passwordHash2")).
PasswordSalt([]byte("salt2")))
C#
// Up to 1000 users can be imported at once.
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "uid1",
Email = "user1@example.com",
PasswordHash = Encoding.ASCII.GetBytes("passwordHash1"),
PasswordSalt = Encoding.ASCII.GetBytes("salt1"),
},
new ImportUserRecordArgs()
{
Uid = "uid2",
Email = "user2@example.com",
PasswordHash = Encoding.ASCII.GetBytes("passwordHash2"),
PasswordSalt = Encoding.ASCII.GetBytes("salt2"),
},
};
此示例中指定了哈希选项,用户下次尝试通过 Firebase Authentication 登录时,可帮助 Firebase 安全地对这些用户进行身份验证。成功登录后,Firebase 会使用内部 Firebase 哈希算法重新对用户密码进行哈希处理。您可以在下面详细了解每种算法的必填字段。
即使发生与特定用户相关的错误,Firebase Authentication 也会尝试上传所提供的整个用户列表。此操作会返回一个结果,列举出所有成功和失败的导入。每个失败的用户导入都有对应的错误详情。
Node.js
getAuth()
.importUsers(userImportRecords, {
hash: {
algorithm: 'HMAC_SHA256',
key: Buffer.from('secretKey'),
},
})
.then((userImportResult) => {
// The number of successful imports is determined via: userImportResult.successCount.
// The number of failed imports is determined via: userImportResult.failureCount.
// To get the error details.
userImportResult.errors.forEach((indexedError) => {
// The corresponding user that failed to upload.
console.log(
'Error ' + indexedError.index,
' failed to import: ',
indexedError.error
);
});
})
.catch((error) => {
// Some unrecoverable error occurred that prevented the operation from running.
});
Java
UserImportOptions options = UserImportOptions.withHash(
HmacSha256.builder()
.setKey("secretKey".getBytes())
.build());
try {
UserImportResult result = FirebaseAuth.getInstance().importUsers(users, options);
System.out.println("Successfully imported " + result.getSuccessCount() + " users");
System.out.println("Failed to import " + result.getFailureCount() + " users");
for (ErrorInfo indexedError : result.getErrors()) {
System.out.println("Failed to import user at index: " + indexedError.getIndex()
+ " due to error: " + indexedError.getReason());
}
} catch (FirebaseAuthException e) {
// Some unrecoverable error occurred that prevented the operation from running.
}
Python
hash_alg = auth.UserImportHash.hmac_sha256(key=b'secret_key')
try:
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))
except exceptions.FirebaseError:
# Some unrecoverable error occurred that prevented the operation from running.
pass
Go
client, err := app.Auth(ctx)
if err != nil {
log.Fatalln("Error initializing Auth client", err)
}
h := hash.HMACSHA256{
Key: []byte("secretKey"),
}
result, err := client.ImportUsers(ctx, users, auth.WithHash(h))
if err != nil {
log.Fatalln("Unrecoverable error prevented the operation from running", err)
}
log.Printf("Successfully imported %d users\n", result.SuccessCount)
log.Printf("Failed to import %d users\n", result.FailureCount)
for _, e := range result.Errors {
log.Printf("Failed to import user at index: %d due to error: %s\n", e.Index, e.Reason)
}
C#
var options = new UserImportOptions()
{
Hash = new HmacSha256()
{
Key = Encoding.ASCII.GetBytes("secretKey"),
},
};
try
{
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
Console.WriteLine($"Successfully imported {result.SuccessCount} users");
Console.WriteLine($"Failed to import {result.FailureCount} users");
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user at index: {indexedError.Index}"
+ $" due to error: {indexedError.Reason}");
}
}
catch (FirebaseAuthException)
{
// Some unrecoverable error occurred that prevented the operation from running.
}
如果不需要对密码进行哈希处理(电话号码、自定义令牌用户、OAuth 用户等),则无需提供哈希选项。
导入密码经过 Firebase SCRYPT 哈希处理的用户
默认情况下,Firebase 使用 Firebase 修订版 SCRYPT 哈希算法存储密码。导入通过修订版 SCRYPT 进行哈希处理的密码有助于从另一个现有的 Firebase 项目迁移用户。为此,您需要确定原始项目的一些内部参数。
Firebase 会为每个 Firebase 项目生成唯一的密码哈希参数。 如需访问这些参数,请前往 Firebase 控制台中的用户标签页,然后从用户表列表右上角的下拉列表中选择密码哈希参数。
为此算法构建哈希选项所需的参数包括:
key
:通常以 base64 编码形式提供的签名者密钥。saltSeparator
:通常以 base64 编码形式提供的盐分隔符(可选)。rounds
:用于对密码进行哈希处理的轮次数。memoryCost
:此算法所需的内存消耗。
Node.js
getAuth()
.importUsers(
[
{
uid: 'some-uid',
email: 'user@example.com',
// Must be provided in a byte buffer.
passwordHash: Buffer.from('base64-password-hash', 'base64'),
// Must be provided in a byte buffer.
passwordSalt: Buffer.from('base64-salt', 'base64'),
},
],
{
hash: {
algorithm: 'SCRYPT',
// All the parameters below can be obtained from the Firebase Console's users section.
// Must be provided in a byte buffer.
key: Buffer.from('base64-secret', 'base64'),
saltSeparator: Buffer.from('base64SaltSeparator', 'base64'),
rounds: 8,
memoryCost: 14,
},
}
)
.then((results) => {
results.errors.forEach((indexedError) => {
console.log(`Error importing user ${indexedError.index}`);
});
})
.catch((error) => {
console.log('Error importing users :', error);
});
Java
try {
List<ImportUserRecord> users = Collections.singletonList(ImportUserRecord.builder()
.setUid("some-uid")
.setEmail("user@example.com")
.setPasswordHash(BaseEncoding.base64().decode("password-hash"))
.setPasswordSalt(BaseEncoding.base64().decode("salt"))
.build());
UserImportOptions options = UserImportOptions.withHash(
Scrypt.builder()
// All the parameters below can be obtained from the Firebase Console's "Users"
// section. Base64 encoded parameters must be decoded into raw bytes.
.setKey(BaseEncoding.base64().decode("base64-secret"))
.setSaltSeparator(BaseEncoding.base64().decode("base64-salt-separator"))
.setRounds(8)
.setMemoryCost(14)
.build());
UserImportResult result = FirebaseAuth.getInstance().importUsers(users, options);
for (ErrorInfo indexedError : result.getErrors()) {
System.out.println("Failed to import user: " + indexedError.getReason());
}
} catch (FirebaseAuthException e) {
System.out.println("Error importing users: " + e.getMessage());
}
Python
users = [
auth.ImportUserRecord(
uid='some-uid',
email='user@example.com',
password_hash=base64.urlsafe_b64decode('password_hash'),
password_salt=base64.urlsafe_b64decode('salt')
),
]
# 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_secret'),
salt_separator=base64.b64decode('base64_salt_separator'),
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 exceptions.FirebaseError as error:
print('Error importing users:', error)
Go
b64URLdecode := func(s string) []byte {
b, err := base64.URLEncoding.DecodeString(s)
if err != nil {
log.Fatalln("Failed to decode string", err)
}
return b
}
b64Stddecode := func(s string) []byte {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
log.Fatalln("Failed to decode string", err)
}
return b
}
// Users retrieved from Firebase Auth's backend need to be base64URL decoded
users := []*auth.UserToImport{
(&auth.UserToImport{}).
UID("some-uid").
Email("user@example.com").
PasswordHash(b64URLdecode("password-hash")).
PasswordSalt(b64URLdecode("salt")),
}
// All the parameters below can be obtained from the Firebase Console's "Users"
// section. Base64 encoded parameters must be decoded into raw bytes.
h := hash.Scrypt{
Key: b64Stddecode("base64-secret"),
SaltSeparator: b64Stddecode("base64-salt-separator"),
Rounds: 8,
MemoryCost: 14,
}
result, err := client.ImportUsers(ctx, users, auth.WithHash(h))
if err != nil {
log.Fatalln("Error importing users", err)
}
for _, e := range result.Errors {
log.Println("Failed to import user", e.Reason)
}
C#
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
// All the parameters below can be obtained from the Firebase Console's "Users"
// section. Base64 encoded parameters must be decoded into raw bytes.
Hash = new Scrypt()
{
Key = Encoding.ASCII.GetBytes("base64-secret"),
SaltSeparator = Encoding.ASCII.GetBytes("base64-salt-separator"),
Rounds = 8,
MemoryCost = 14,
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
导入密码经过标准 SCRYPT 哈希处理的用户
Firebase Authentication 支持标准 scrypt 算法以及上述修改后版本。对于标准 SCRYPT 算法,需要提供以下哈希参数:
memoryCost
:哈希算法的 CPU/内存消耗。parallelization
:哈希算法的并行化。blockSize
:哈希算法的块大小(通常为 8)。derivedKeyLength
:哈希算法的派生密钥长度。
Node.js
getAuth()
.importUsers(
[
{
uid: 'some-uid',
email: 'user@example.com',
// Must be provided in a byte buffer.
passwordHash: Buffer.from('password-hash'),
// Must be provided in a byte buffer.
passwordSalt: Buffer.from('salt'),
},
],
{
hash: {
algorithm: 'STANDARD_SCRYPT',
memoryCost: 1024,
parallelization: 16,
blockSize: 8,
derivedKeyLength: 64,
},
}
)
.then((results) => {
results.errors.forEach((indexedError) => {
console.log(`Error importing user ${indexedError.index}`);
});
})
.catch((error) => {
console.log('Error importing users :', error);
});
Java
try {
List<ImportUserRecord> users = Collections.singletonList(ImportUserRecord.builder()
.setUid("some-uid")
.setEmail("user@example.com")
.setPasswordHash("password-hash".getBytes())
.setPasswordSalt("salt".getBytes())
.build());
UserImportOptions options = UserImportOptions.withHash(
StandardScrypt.builder()
.setMemoryCost(1024)
.setParallelization(16)
.setBlockSize(8)
.setDerivedKeyLength(64)
.build());
UserImportResult result = FirebaseAuth.getInstance().importUsers(users, options);
for (ErrorInfo indexedError : result.getErrors()) {
System.out.println("Failed to import user: " + indexedError.getReason());
}
} catch (FirebaseAuthException e) {
System.out.println("Error importing users: " + e.getMessage());
}
Python
users = [
auth.ImportUserRecord(
uid='some-uid',
email='user@example.com',
password_hash=b'password_hash',
password_salt=b'salt'
),
]
hash_alg = auth.UserImportHash.standard_scrypt(
memory_cost=1024, parallelization=16, block_size=8, derived_key_length=64)
try:
result = auth.import_users(users, hash_alg=hash_alg)
for err in result.errors:
print('Failed to import user:', err.reason)
except exceptions.FirebaseError as error:
print('Error importing users:', error)
Go
users := []*auth.UserToImport{
(&auth.UserToImport{}).
UID("some-uid").
Email("user@example.com").
PasswordHash([]byte("password-hash")).
PasswordSalt([]byte("salt")),
}
h := hash.StandardScrypt{
MemoryCost: 1024,
Parallelization: 16,
BlockSize: 8,
DerivedKeyLength: 64,
}
result, err := client.ImportUsers(ctx, users, auth.WithHash(h))
if err != nil {
log.Fatalln("Error importing users", err)
}
for _, e := range result.Errors {
log.Println("Failed to import user", e.Reason)
}
C#
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
Hash = new StandardScrypt()
{
MemoryCost = 1024,
Parallelization = 16,
BlockSize = 8,
DerivedKeyLength = 64,
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
导入密码经过 HMAC 哈希处理的用户
HMAC 哈希算法包括:HMAC_MD5
、HMAC_SHA1
、HMAC_SHA256
和 HMAC_SHA512
。对于这些哈希算法,您必须提供哈希签名者密钥。
Node.js
getAuth()
.importUsers(
[
{
uid: 'some-uid',
email: 'user@example.com',
// Must be provided in a byte buffer.
passwordHash: Buffer.from('password-hash'),
// Must be provided in a byte buffer.
passwordSalt: Buffer.from('salt'),
},
],
{
hash: {
algorithm: 'HMAC_SHA256',
// Must be provided in a byte buffer.
key: Buffer.from('secret'),
},
}
)
.then((results) => {
results.errors.forEach((indexedError) => {
console.log(`Error importing user ${indexedError.index}`);
});
})
.catch((error) => {
console.log('Error importing users :', error);
});
Java
try {
List<ImportUserRecord> users = Collections.singletonList(ImportUserRecord.builder()
.setUid("some-uid")
.setEmail("user@example.com")
.setPasswordHash("password-hash".getBytes())
.setPasswordSalt("salt".getBytes())
.build());
UserImportOptions options = UserImportOptions.withHash(
HmacSha256.builder()
.setKey("secret".getBytes())
.build());
UserImportResult result = FirebaseAuth.getInstance().importUsers(users, options);
for (ErrorInfo indexedError : result.getErrors()) {
System.out.println("Failed to import user: " + indexedError.getReason());
}
} catch (FirebaseAuthException e) {
System.out.println("Error importing users: " + e.getMessage());
}
Python
users = [
auth.ImportUserRecord(
uid='some-uid',
email='user@example.com',
password_hash=b'password_hash',
password_salt=b'salt'
),
]
hash_alg = auth.UserImportHash.hmac_sha256(key=b'secret')
try:
result = auth.import_users(users, hash_alg=hash_alg)
for err in result.errors:
print('Failed to import user:', err.reason)
except exceptions.FirebaseError as error:
print('Error importing users:', error)
Go
users := []*auth.UserToImport{
(&auth.UserToImport{}).
UID("some-uid").
Email("user@example.com").
PasswordHash([]byte("password-hash")).
PasswordSalt([]byte("salt")),
}
h := hash.HMACSHA256{
Key: []byte("secret"),
}
result, err := client.ImportUsers(ctx, users, auth.WithHash(h))
if err != nil {
log.Fatalln("Error importing users", err)
}
for _, e := range result.Errors {
log.Println("Failed to import user", e.Reason)
}
C#
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
Hash = new HmacSha256()
{
Key = Encoding.ASCII.GetBytes("secret"),
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
导入密码经过 MD5、SHA 和 PBKDF 哈希处理的用户
MD5、SHA 和 PBKDF 哈希算法包括:MD5
、SHA1
、SHA256
、SHA512
、PBKDF_SHA1
和 PBKDF2_SHA256
。对于这些哈希算法,您必须提供用于对密码进行哈希处理的轮数(MD5
的范围介于 0 到 8,192 之间;SHA1
、SHA256
和 SHA512
的范围介于 1 到 8,192 之间;PBKDF_SHA1
和 PBKDF2_SHA256
的范围介于 0 到 120,000 之间)。
Node.js
getAuth()
.importUsers(
[
{
uid: 'some-uid',
email: 'user@example.com',
// Must be provided in a byte buffer.
passwordHash: Buffer.from('password-hash'),
// Must be provided in a byte buffer.
passwordSalt: Buffer.from('salt'),
},
],
{
hash: {
algorithm: 'PBKDF2_SHA256',
rounds: 100000,
},
}
)
.then((results) => {
results.errors.forEach((indexedError) => {
console.log(`Error importing user ${indexedError.index}`);
});
})
.catch((error) => {
console.log('Error importing users :', error);
});
Java
try {
List<ImportUserRecord> users = Collections.singletonList(ImportUserRecord.builder()
.setUid("some-uid")
.setEmail("user@example.com")
.setPasswordHash("password-hash".getBytes())
.setPasswordSalt("salt".getBytes())
.build());
UserImportOptions options = UserImportOptions.withHash(
Pbkdf2Sha256.builder()
.setRounds(100000)
.build());
UserImportResult result = FirebaseAuth.getInstance().importUsers(users, options);
for (ErrorInfo indexedError : result.getErrors()) {
System.out.println("Failed to import user: " + indexedError.getReason());
}
} catch (FirebaseAuthException e) {
System.out.println("Error importing users: " + e.getMessage());
}
Python
users = [
auth.ImportUserRecord(
uid='some-uid',
email='user@example.com',
password_hash=b'password_hash',
password_salt=b'salt'
),
]
hash_alg = auth.UserImportHash.pbkdf2_sha256(rounds=100000)
try:
result = auth.import_users(users, hash_alg=hash_alg)
for err in result.errors:
print('Failed to import user:', err.reason)
except exceptions.FirebaseError as error:
print('Error importing users:', error)
Go
users := []*auth.UserToImport{
(&auth.UserToImport{}).
UID("some-uid").
Email("user@example.com").
PasswordHash([]byte("password-hash")).
PasswordSalt([]byte("salt")),
}
h := hash.PBKDF2SHA256{
Rounds: 100000,
}
result, err := client.ImportUsers(ctx, users, auth.WithHash(h))
if err != nil {
log.Fatalln("Error importing users", err)
}
for _, e := range result.Errors {
log.Println("Failed to import user", e.Reason)
}
C#
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
Hash = new Pbkdf2Sha256()
{
Rounds = 100000,
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
导入密码经过 BCRYPT 哈希处理的用户
对于 BCRYPT 哈希密码,既不需要额外的哈希参数,也不需要每个用户的密码盐。
Node.js
getAuth()
.importUsers(
[
{
uid: 'some-uid',
email: 'user@example.com',
// Must be provided in a byte buffer.
passwordHash: Buffer.from('password-hash'),
},
],
{
hash: {
algorithm: 'BCRYPT',
},
}
)
.then((results) => {
results.errors.forEach((indexedError) => {
console.log(`Error importing user ${indexedError.index}`);
});
})
.catch((error) => {
console.log('Error importing users :', error);
});
Java
try {
List<ImportUserRecord> users = Collections.singletonList(ImportUserRecord.builder()
.setUid("some-uid")
.setEmail("user@example.com")
.setPasswordHash("password-hash".getBytes())
.setPasswordSalt("salt".getBytes())
.build());
UserImportOptions options = UserImportOptions.withHash(Bcrypt.getInstance());
UserImportResult result = FirebaseAuth.getInstance().importUsers(users, options);
for (ErrorInfo indexedError : result.getErrors()) {
System.out.println("Failed to import user: " + indexedError.getReason());
}
} catch (FirebaseAuthException e) {
System.out.println("Error importing users: " + e.getMessage());
}
Python
users = [
auth.ImportUserRecord(
uid='some-uid',
email='user@example.com',
password_hash=b'password_hash',
password_salt=b'salt'
),
]
hash_alg = auth.UserImportHash.bcrypt()
try:
result = auth.import_users(users, hash_alg=hash_alg)
for err in result.errors:
print('Failed to import user:', err.reason)
except exceptions.FirebaseError as error:
print('Error importing users:', error)
Go
users := []*auth.UserToImport{
(&auth.UserToImport{}).
UID("some-uid").
Email("user@example.com").
PasswordHash([]byte("password-hash")).
PasswordSalt([]byte("salt")),
}
h := hash.Bcrypt{}
result, err := client.ImportUsers(ctx, users, auth.WithHash(h))
if err != nil {
log.Fatalln("Error importing users", err)
}
for _, e := range result.Errors {
log.Println("Failed to import user", e.Reason)
}
C#
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
Email = "user@example.com",
PasswordHash = Encoding.ASCII.GetBytes("password-hash"),
PasswordSalt = Encoding.ASCII.GetBytes("salt"),
},
};
var options = new UserImportOptions()
{
Hash = new Bcrypt(),
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users, options);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}
导入密码经过 Argon2 哈希处理的用户
您可以通过构造一个 Argon2
哈希对象,来导入密码经过 Argon2 哈希处理的用户记录。请注意,目前只有 Admin Java SDK 支持此功能。
为此算法构建哈希选项所需的参数包括:
hashLengthBytes
:所需的哈希长度(以字节为单位),以整数形式提供hashType
:要使用的 Argon2 变体(ARGON2_D
、ARGON2_ID
、ARGON2_I
)parallelism
:并行度,以整数形式提供。必须介于 1 到 16 之间(包括 1 和 16)。iterations
:要执行的迭代次数,以整数形式提供。必须介于 1 到 16 之间(包括 1 和 16)。memoryCostKib
:此算法所需的内存消耗(以千字节为单位),必须小于 32768。version
:Argon2 算法的版本(VERSION_10
或VERSION_13
)。可选参数,如果未指定,则默认为 VERSION_13。associatedData
:其他关联的数据(以字节数组形式提供),附加到哈希值以提供一层额外的安全防护。可选参数,此数据在发送到 API 之前采用 base64 编码。
Java
try {
List<ImportUserRecord> users = Collections.singletonList(ImportUserRecord.builder()
.setUid("some-uid")
.setEmail("user@example.com")
.setPasswordHash("password-hash".getBytes())
.setPasswordSalt("salt".getBytes())
.build());
UserImportOptions options = UserImportOptions.withHash(
Argon2.builder()
.setHashLengthBytes(512)
.setHashType(Argon2HashType.ARGON2_ID)
.setParallelism(8)
.setIterations(16)
.setMemoryCostKib(2048)
.setVersion(Argon2Version.VERSION_10)
.setAssociatedData("associated-data".getBytes())
.build());
UserImportResult result = FirebaseAuth.getInstance().importUsers(users, options);
for (ErrorInfo indexedError : result.getErrors()) {
System.out.println("Failed to import user: " + indexedError.getReason());
}
} catch (FirebaseAuthException e) {
System.out.println("Error importing users: " + e.getMessage());
}
导入没有密码的用户
您可以导入没有密码的用户。对于没有密码的用户,可以将他们与使用 OAuth 提供方、自定义声明和电话号码等的用户合并在一起导入。
Node.js
getAuth()
.importUsers([
{
uid: 'some-uid',
displayName: 'John Doe',
email: 'johndoe@gmail.com',
photoURL: 'http://www.example.com/12345678/photo.png',
emailVerified: true,
phoneNumber: '+11234567890',
// Set this user as admin.
customClaims: { admin: true },
// User with Google provider.
providerData: [
{
uid: 'google-uid',
email: 'johndoe@gmail.com',
displayName: 'John Doe',
photoURL: 'http://www.example.com/12345678/photo.png',
providerId: 'google.com',
},
],
},
])
.then((results) => {
results.errors.forEach((indexedError) => {
console.log(`Error importing user ${indexedError.index}`);
});
})
.catch((error) => {
console.log('Error importing users :', error);
});
Java
try {
List<ImportUserRecord> users = Collections.singletonList(ImportUserRecord.builder()
.setUid("some-uid")
.setDisplayName("John Doe")
.setEmail("johndoe@gmail.com")
.setPhotoUrl("http://www.example.com/12345678/photo.png")
.setEmailVerified(true)
.setPhoneNumber("+11234567890")
.putCustomClaim("admin", true) // set this user as admin
.addUserProvider(UserProvider.builder() // user with Google provider
.setUid("google-uid")
.setEmail("johndoe@gmail.com")
.setDisplayName("John Doe")
.setPhotoUrl("http://www.example.com/12345678/photo.png")
.setProviderId("google.com")
.build())
.build());
UserImportResult result = FirebaseAuth.getInstance().importUsers(users);
for (ErrorInfo indexedError : result.getErrors()) {
System.out.println("Failed to import user: " + indexedError.getReason());
}
} catch (FirebaseAuthException e) {
System.out.println("Error importing users: " + e.getMessage());
}
Python
users = [
auth.ImportUserRecord(
uid='some-uid',
display_name='John Doe',
email='johndoe@gmail.com',
photo_url='http://www.example.com/12345678/photo.png',
email_verified=True,
phone_number='+11234567890',
custom_claims={'admin': True}, # set this user as admin
provider_data=[ # user with Google provider
auth.UserProvider(
uid='google-uid',
email='johndoe@gmail.com',
display_name='John Doe',
photo_url='http://www.example.com/12345678/photo.png',
provider_id='google.com'
)
],
),
]
try:
result = auth.import_users(users)
for err in result.errors:
print('Failed to import user:', err.reason)
except exceptions.FirebaseError as error:
print('Error importing users:', error)
Go
users := []*auth.UserToImport{
(&auth.UserToImport{}).
UID("some-uid").
DisplayName("John Doe").
Email("johndoe@gmail.com").
PhotoURL("http://www.example.com/12345678/photo.png").
EmailVerified(true).
PhoneNumber("+11234567890").
CustomClaims(map[string]interface{}{"admin": true}). // set this user as admin
ProviderData([]*auth.UserProvider{ // user with Google provider
{
UID: "google-uid",
Email: "johndoe@gmail.com",
DisplayName: "John Doe",
PhotoURL: "http://www.example.com/12345678/photo.png",
ProviderID: "google.com",
},
}),
}
result, err := client.ImportUsers(ctx, users)
if err != nil {
log.Fatalln("Error importing users", err)
}
for _, e := range result.Errors {
log.Println("Failed to import user", e.Reason)
}
C#
try
{
var users = new List<ImportUserRecordArgs>()
{
new ImportUserRecordArgs()
{
Uid = "some-uid",
DisplayName = "John Doe",
Email = "johndoe@gmail.com",
PhotoUrl = "http://www.example.com/12345678/photo.png",
EmailVerified = true,
PhoneNumber = "+11234567890",
CustomClaims = new Dictionary<string, object>()
{
{ "admin", true }, // set this user as admin
},
UserProviders = new List<UserProvider>
{
new UserProvider() // user with Google provider
{
Uid = "google-uid",
Email = "johndoe@gmail.com",
DisplayName = "John Doe",
PhotoUrl = "http://www.example.com/12345678/photo.png",
ProviderId = "google.com",
},
},
},
};
UserImportResult result = await FirebaseAuth.DefaultInstance.ImportUsersAsync(users);
foreach (ErrorInfo indexedError in result.Errors)
{
Console.WriteLine($"Failed to import user: {indexedError.Reason}");
}
}
catch (FirebaseAuthException e)
{
Console.WriteLine($"Error importing users: {e.Message}");
}