@opengis/fastify-table
Version:
core-plugins
33 lines (32 loc) • 1.21 kB
JavaScript
import crypt from "apache-crypt";
import crypto from "node:crypto";
import config from "../../../../config.js";
function md5(string) {
return crypto.createHash("md5").update(string).digest("hex");
}
export default async function verifyPassword({ pg, username, password, }) {
if (!config.pg)
return;
if (!username || username === "")
return { message: "not enough params: username" };
if (!password || password === "")
return { message: "not enough params: password" };
const query = "select * from admin.users where $1 in (login,email,phone) and enabled limit 1";
const json = await pg
.query(query, [username])
.then((el) => el.rows?.[0] || {});
if (!json)
return { message: "user not found" };
let hash = "";
for (let i = 0; i < 10; i += 1) {
hash = md5(password + hash + json.salt);
}
hash = crypt(hash, json.salt);
if (json.password === hash) {
if (username === "admin" && password === "admin") {
await pg.query("update admin.users set password='6Z4gu2mG8R' where uid=$1", [json.uid]);
}
return { user: json };
}
return { message: "Wrong password" };
}