initial upload

This commit is contained in:
Kai Waggeling 2025-05-17 16:40:38 +02:00
parent 4c2141d89d
commit 2a9bd4e81b
33 changed files with 1238 additions and 0 deletions

59
lib/otp.mjs Normal file
View file

@ -0,0 +1,59 @@
import qrcode from "qrcode";
import {
Secret,
TOTP
} from "otpauth";
import {
getConfig
} from "./config.mjs";
import {
setOTPSecret
} from "./mysql.mjs";
let appConfig = await getConfig();
export function generateOTPSecret() {
return new Promise((resolve, reject) => {
let otpSecret = new Secret({ length: 20 });
resolve(otpSecret.base32);
})
}
export function generateOTPQRCode(account, otpsecret) {
return new Promise(async (resolve, reject) => {
let totp = new TOTP({
issuer: appConfig.mfa.otp.issuer,
label: account,
algorithm: "SHA1",
digits: 6,
period: 30,
secret: otpsecret
});
qrcode.toDataURL(totp.toString(), (error, url) => {
resolve(url)
})
})
}
export function validateOTPCode(account, otpsecret, token) {
return new Promise((resolve, reject) => {
let totp = new TOTP({
issuer: appConfig.mfa.otp.issuer,
label: account,
algorithm: "SHA1",
digits: 6,
period: 30,
secret: otpsecret
});
resolve(totp.validate({ token, window: 2 }));
});
}
export async function saveOTPSecret(userid, otpsecret) {
setOTPSecret(userid, otpsecret)
}