Notes/src/services/totp.ts

40 lines
1.0 KiB
TypeScript
Raw Normal View History

import { Totp } from 'time2fa';
import config from './config.js';
import MFAError from '../errors/mfa_error.js';
2024-09-07 10:21:41 -07:00
2024-09-07 11:41:54 -07:00
function isTotpEnabled() {
if (config.MultiFactorAuthentication.totpEnabled && config.MultiFactorAuthentication.totpSecret === "") {
throw new MFAError("TOTP secret is not set!");
2024-09-07 11:41:54 -07:00
}
return config.MultiFactorAuthentication.totpEnabled;
2024-09-07 11:41:54 -07:00
}
2024-09-07 10:21:41 -07:00
function getTotpSecret() {
return config.MultiFactorAuthentication.totpSecret;
2024-09-07 10:21:41 -07:00
}
function checkForTotSecret() {
return config.MultiFactorAuthentication.totpSecret === "" ? false : true;
2024-09-07 10:21:41 -07:00
}
2025-03-26 00:42:19 +01:00
function validateTOTP(submittedPasscode: string) {
if (config.MultiFactorAuthentication.totpSecret === "") return false;
2024-09-07 10:21:41 -07:00
try {
const valid = Totp.validate({
2025-03-26 00:42:19 +01:00
passcode: submittedPasscode,
secret: config.MultiFactorAuthentication.totpSecret.trim()
2024-09-07 10:21:41 -07:00
});
return valid;
} catch (e) {
return false;
}
}
export default {
2024-09-07 11:41:54 -07:00
isTotpEnabled,
getTotpSecret,
checkForTotSecret,
2024-09-07 10:21:41 -07:00
validateTOTP
};