-
Notifications
You must be signed in to change notification settings - Fork 990
/
Copy pathaccountImporter.ts
339 lines (324 loc) · 10.6 KB
/
accountImporter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import * as clc from "colorette";
import { Client } from "./apiv2";
import { googleOrigin } from "./api";
import { logger } from "./logger";
import { FirebaseError } from "./error";
import * as utils from "./utils";
const apiClient = new Client({
urlPrefix: googleOrigin(),
});
// TODO: support for MFA at runtime was added in PR #3173, but this importer currently ignores `mfaInfo` and loses the data on import.
const ALLOWED_JSON_KEYS = [
"localId",
"email",
"emailVerified",
"passwordHash",
"salt",
"displayName",
"photoUrl",
"createdAt",
"lastSignedInAt",
"providerUserInfo",
"phoneNumber",
"disabled",
"customAttributes",
];
const ALLOWED_JSON_KEYS_RENAMING = {
lastSignedInAt: "lastLoginAt",
};
const ALLOWED_PROVIDER_USER_INFO_KEYS = ["providerId", "rawId", "email", "displayName", "photoUrl"];
const ALLOWED_PROVIDER_IDS = ["google.com", "facebook.com", "twitter.com", "github.com"];
function isValidBase64(str: string): boolean {
const expected = Buffer.from(str, "base64").toString("base64");
// Buffer automatically pads with '=' character,
// but input string might not have padding.
if (str.length < expected.length && !str.endsWith("=")) {
str += "=".repeat(expected.length - str.length);
}
return expected === str;
}
function toWebSafeBase64(data: string): string {
return data.replace(/\//g, "_").replace(/\+/g, "-");
}
function addProviderUserInfo(user: any, providerId: string, arr: any[]) {
if (arr[0]) {
user.providerUserInfo.push({
providerId: providerId,
rawId: arr[0],
email: arr[1],
displayName: arr[2],
photoUrl: arr[3],
});
}
}
function genUploadAccountPostBody(projectId: string, accounts: any[], hashOptions: any) {
const postBody: any = {
users: accounts.map((account) => {
if (account.passwordHash) {
account.passwordHash = toWebSafeBase64(account.passwordHash);
}
if (account.salt) {
account.salt = toWebSafeBase64(account.salt);
}
for (const [key, value] of Object.entries(ALLOWED_JSON_KEYS_RENAMING)) {
if (account[key]) {
account[value] = account[key];
delete account[key];
}
}
return account;
}),
};
if (hashOptions.hashAlgo) {
postBody.hashAlgorithm = hashOptions.hashAlgo;
}
if (hashOptions.hashKey) {
postBody.signerKey = toWebSafeBase64(hashOptions.hashKey);
}
if (hashOptions.saltSeparator) {
postBody.saltSeparator = toWebSafeBase64(hashOptions.saltSeparator);
}
if (hashOptions.rounds) {
postBody.rounds = hashOptions.rounds;
}
if (hashOptions.memCost) {
postBody.memoryCost = hashOptions.memCost;
}
if (hashOptions.cpuMemCost) {
postBody.cpuMemCost = hashOptions.cpuMemCost;
}
if (hashOptions.parallelization) {
postBody.parallelization = hashOptions.parallelization;
}
if (hashOptions.blockSize) {
postBody.blockSize = hashOptions.blockSize;
}
if (hashOptions.dkLen) {
postBody.dkLen = hashOptions.dkLen;
}
if (hashOptions.passwordHashOrder) {
postBody.passwordHashOrder = hashOptions.passwordHashOrder;
}
postBody.targetProjectId = projectId;
return postBody;
}
export function transArrayToUser(arr: any[]): any {
const user = {
localId: arr[0],
email: arr[1],
emailVerified: arr[2] === "true",
passwordHash: arr[3],
salt: arr[4],
displayName: arr[5],
photoUrl: arr[6],
createdAt: arr[23],
lastLoginAt: arr[24],
phoneNumber: arr[25],
providerUserInfo: [],
disabled: arr[26],
customAttributes: arr[27],
};
addProviderUserInfo(user, "google.com", arr.slice(7, 11));
addProviderUserInfo(user, "facebook.com", arr.slice(11, 15));
addProviderUserInfo(user, "twitter.com", arr.slice(15, 19));
addProviderUserInfo(user, "github.com", arr.slice(19, 23));
if (user.passwordHash && !isValidBase64(user.passwordHash)) {
return {
error: "Password hash should be base64 encoded.",
};
}
if (user.salt && !isValidBase64(user.salt)) {
return {
error: "Password salt should be base64 encoded.",
};
}
return user;
}
export function validateOptions(options: any): any {
const hashOptions = validateRequiredParameters(options);
if (!hashOptions.valid) {
return hashOptions;
}
const hashInputOrder = options.hashInputOrder ? options.hashInputOrder.toUpperCase() : undefined;
if (hashInputOrder) {
if (hashInputOrder !== "SALT_FIRST" && hashInputOrder !== "PASSWORD_FIRST") {
throw new FirebaseError("Unknown password hash order flag");
} else {
hashOptions["passwordHashOrder"] =
hashInputOrder === "SALT_FIRST" ? "SALT_AND_PASSWORD" : "PASSWORD_AND_SALT";
}
}
return hashOptions;
}
function validateRequiredParameters(options: any): any {
if (!options.hashAlgo) {
utils.logWarning("No hash algorithm specified. Password users cannot be imported.");
return { valid: true };
}
const hashAlgo = options.hashAlgo.toUpperCase();
let roundsNum;
switch (hashAlgo) {
case "HMAC_SHA512":
case "HMAC_SHA256":
case "HMAC_SHA1":
case "HMAC_MD5":
if (!options.hashKey || options.hashKey === "") {
throw new FirebaseError(
"Must provide hash key(base64 encoded) for hash algorithm " + options.hashAlgo,
);
}
return { hashAlgo: hashAlgo, hashKey: options.hashKey, valid: true };
case "MD5":
case "SHA1":
case "SHA256":
case "SHA512":
// MD5 is [0,8192] but SHA1, SHA256, and SHA512 are [1,8192]
roundsNum = parseInt(options.rounds, 10);
const minRounds = hashAlgo === "MD5" ? 0 : 1;
if (isNaN(roundsNum) || roundsNum < minRounds || roundsNum > 8192) {
throw new FirebaseError(
`Must provide valid rounds(${minRounds}..8192) for hash algorithm ${options.hashAlgo}`,
);
}
return { hashAlgo: hashAlgo, rounds: options.rounds, valid: true };
case "PBKDF_SHA1":
case "PBKDF2_SHA256":
roundsNum = parseInt(options.rounds, 10);
if (isNaN(roundsNum) || roundsNum < 0 || roundsNum > 120000) {
throw new FirebaseError(
"Must provide valid rounds(0..120000) for hash algorithm " + options.hashAlgo,
);
}
return { hashAlgo: hashAlgo, rounds: options.rounds, valid: true };
case "SCRYPT":
if (!options.hashKey || options.hashKey === "") {
throw new FirebaseError(
"Must provide hash key(base64 encoded) for hash algorithm " + options.hashAlgo,
);
}
roundsNum = parseInt(options.rounds, 10);
if (isNaN(roundsNum) || roundsNum <= 0 || roundsNum > 8) {
throw new FirebaseError(
"Must provide valid rounds(1..8) for hash algorithm " + options.hashAlgo,
);
}
const memCost = parseInt(options.memCost, 10);
if (isNaN(memCost) || memCost <= 0 || memCost > 14) {
throw new FirebaseError(
"Must provide valid memory cost(1..14) for hash algorithm " + options.hashAlgo,
);
}
let saltSeparator = "";
if (options.saltSeparator) {
saltSeparator = options.saltSeparator;
}
return {
hashAlgo: hashAlgo,
hashKey: options.hashKey,
saltSeparator: saltSeparator,
rounds: options.rounds,
memCost: options.memCost,
valid: true,
};
case "BCRYPT":
return { hashAlgo: hashAlgo, valid: true };
case "STANDARD_SCRYPT":
const cpuMemCost = parseInt(options.memCost, 10);
const parallelization = parseInt(options.parallelization, 10);
const blockSize = parseInt(options.blockSize, 10);
const dkLen = parseInt(options.dkLen, 10);
return {
hashAlgo: hashAlgo,
valid: true,
cpuMemCost: cpuMemCost,
parallelization: parallelization,
blockSize: blockSize,
dkLen: dkLen,
};
default:
throw new FirebaseError("Unsupported hash algorithm " + clc.bold(options.hashAlgo));
}
}
function validateProviderUserInfo(providerUserInfo: { providerId: string; error?: string }): {
error?: string;
} {
if (!ALLOWED_PROVIDER_IDS.includes(providerUserInfo.providerId)) {
return {
error: JSON.stringify(providerUserInfo, null, 2) + " has unsupported providerId",
};
}
const keydiff = Object.keys(providerUserInfo).filter(
(k) => !ALLOWED_PROVIDER_USER_INFO_KEYS.includes(k),
);
if (keydiff.length) {
return {
error:
JSON.stringify(providerUserInfo, null, 2) + " has unsupported keys: " + keydiff.join(","),
};
}
return {};
}
export function validateUserJson(userJson: any): { error?: string } {
const keydiff = Object.keys(userJson).filter((k) => !ALLOWED_JSON_KEYS.includes(k));
if (keydiff.length) {
return {
error: JSON.stringify(userJson, null, 2) + " has unsupported keys: " + keydiff.join(","),
};
}
if (userJson.providerUserInfo) {
for (let i = 0; i < userJson.providerUserInfo.length; i++) {
const res = validateProviderUserInfo(userJson.providerUserInfo[i]);
if (res.error) {
return res;
}
}
}
const badFormat = JSON.stringify(userJson, null, 2) + " has invalid data format: ";
if (userJson.passwordHash && !isValidBase64(userJson.passwordHash)) {
return {
error: badFormat + "Password hash should be base64 encoded.",
};
}
if (userJson.salt && !isValidBase64(userJson.salt)) {
return {
error: badFormat + "Password salt should be base64 encoded.",
};
}
return {};
}
async function sendRequest(projectId: string, userList: any[], hashOptions: any): Promise<void> {
logger.info("Starting importing " + userList.length + " account(s).");
const postData = genUploadAccountPostBody(projectId, userList, hashOptions);
return apiClient
.post<any, any>("/identitytoolkit/v3/relyingparty/uploadAccount", postData, {
skipLog: { body: true }, // Contains a lot of PII - don't log.
})
.then((ret) => {
if (ret.body.error) {
logger.info("Encountered problems while importing accounts. Details:");
logger.info(
ret.body.error.map((rawInfo: any) => {
return {
account: JSON.stringify(userList[parseInt(rawInfo.index, 10)], null, 2),
reason: rawInfo.message,
};
}),
);
} else {
utils.logSuccess("Imported successfully.");
}
logger.info();
});
}
export function serialImportUsers(
projectId: string,
hashOptions: any,
userListArr: any[],
index: number,
): Promise<any> {
return sendRequest(projectId, userListArr[index], hashOptions).then(() => {
if (index < userListArr.length - 1) {
return serialImportUsers(projectId, hashOptions, userListArr, index + 1);
}
});
}