2023-06-02 00:20:49 -03:00
|
|
|
import { IAccountCreation } from "@/src/types/customTypes";
|
|
|
|
import { IDatabaseAccount } from "@/src/types/loginTypes";
|
2023-05-23 20:42:06 -04:00
|
|
|
import crypto from "crypto";
|
2023-06-14 02:26:19 +02:00
|
|
|
import { isString, parseEmail, parseString } from "../general";
|
2023-05-19 15:22:48 -03:00
|
|
|
|
|
|
|
const getWhirlpoolHash = (rawPassword: string): string => {
|
2023-05-23 20:42:06 -04:00
|
|
|
const whirlpool = crypto.createHash("whirlpool");
|
|
|
|
const data = whirlpool.update(rawPassword, "utf8");
|
|
|
|
const hash = data.digest("hex");
|
|
|
|
return hash;
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
const parsePassword = (passwordCandidate: unknown): string => {
|
2023-05-23 20:42:06 -04:00
|
|
|
// a different function could be called that checks whether the password has a certain shape
|
|
|
|
if (!isString(passwordCandidate)) {
|
|
|
|
throw new Error("incorrect password format");
|
|
|
|
}
|
|
|
|
return passwordCandidate;
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
const toAccountCreation = (accountCreation: unknown): IAccountCreation => {
|
2023-05-23 20:42:06 -04:00
|
|
|
if (!accountCreation || typeof accountCreation !== "object") {
|
|
|
|
throw new Error("incorrect or missing account creation data");
|
|
|
|
}
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2023-05-23 20:42:06 -04:00
|
|
|
if (
|
|
|
|
"email" in accountCreation &&
|
|
|
|
"password" in accountCreation &&
|
|
|
|
"DisplayName" in accountCreation &&
|
|
|
|
"CountryCode" in accountCreation
|
|
|
|
) {
|
|
|
|
const rawPassword = parsePassword(accountCreation.password);
|
|
|
|
return {
|
|
|
|
email: parseEmail(accountCreation.email),
|
|
|
|
password: getWhirlpoolHash(rawPassword),
|
|
|
|
CountryCode: parseString(accountCreation.CountryCode),
|
|
|
|
DisplayName: parseString(accountCreation.DisplayName)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
throw new Error("incorrect account creation data: incorrect properties");
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
const toDatabaseAccount = (createAccount: IAccountCreation): IDatabaseAccount => {
|
2023-05-23 20:42:06 -04:00
|
|
|
return {
|
|
|
|
...createAccount,
|
|
|
|
ClientType: "",
|
|
|
|
ConsentNeeded: false,
|
|
|
|
CrossPlatformAllowed: true,
|
|
|
|
ForceLogoutVersion: 0,
|
2024-05-28 13:45:06 +02:00
|
|
|
TrackedSettings: [],
|
|
|
|
Nonce: 0
|
2023-05-23 20:42:06 -04:00
|
|
|
} satisfies IDatabaseAccount;
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
export { toDatabaseAccount, toAccountCreation as toCreateAccount };
|