Firebase Admin SDK 提供 Auth.importUsers()
API,可透過提升權限將使用者大量匯入 Firebase Authentication。雖然這項功能也能使用
在 Firebase CLI 中
Admin SDK 則可讓您從外部上傳使用者
驗證系統或其他 Firebase 專案
不必建立中繼 CSV 或 JSON 檔案
使用者匯入 API 具備下列優點:
- 可使用其他密碼雜湊演算法,從外部驗證系統遷移使用者。
- 可遷移其他 Firebase 專案的使用者。
- 最佳化快速且有效率的大量匯入作業。這項作業會處理使用者,但不會檢查
uid
、email
、phoneNumber
或其他 ID 的重複情形。 - 能夠遷移現有或建立新的 OAuth 使用者 (Google、Facebook 等)。
- 能夠直接大量匯入自訂宣稱的使用者。
用量
單次 API 呼叫最多可匯入 1,000 位使用者。請注意此作業
已針對速度進行最佳化,且不會檢查uid
、email
、phoneNumber
和
與其他專屬 ID 重複匯入與
現有的 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 在這些使用者下次嘗試透過 Firebase Authentication 登入時,安全地驗證他們的身份。成功登入後,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 加密雜湊密碼匯入使用者
根據預設,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}");
}
使用標準密碼雜湊匯入使用者
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 到 8192 之間、SHA1
、SHA256
和 SHA512
為 1 到 8192 之間,PBKDF_SHA1
和 PBKDF2_SHA256
為 0 到 120000 之間)。
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 雜湊密碼的使用者記錄。請注意,這項功能目前只支援
定義及變更
為這個演算法建構雜湊選項所需的參數包括:
hashLengthBytes
:所需的雜湊長度 (以位元組為單位,以整數形式提供)hashType
:要使用的 Argon2 變體 (ARGON2_D
、ARGON2_ID
、ARGON2_I
)parallelism
:平行處理度,以整數提供。必須介於 1 至 16 之間 (含 1 和 16)iterations
:要執行的疊代次數,以整數提供。必須介於 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}");
}