2025-03-25 23:14:25 +01:00
|
|
|
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() {
|
2025-03-25 23:14:25 +01:00
|
|
|
if (config.MultiFactorAuthentication.totpEnabled && config.MultiFactorAuthentication.totpSecret === "") {
|
|
|
|
throw new MFAError("TOTP secret is not set!");
|
2024-09-07 11:41:54 -07:00
|
|
|
}
|
2025-03-25 23:14:25 +01:00
|
|
|
return config.MultiFactorAuthentication.totpEnabled;
|
2024-09-07 11:41:54 -07:00
|
|
|
}
|
|
|
|
|
2024-09-07 10:21:41 -07:00
|
|
|
function getTotpSecret() {
|
2025-03-25 23:14:25 +01:00
|
|
|
return config.MultiFactorAuthentication.totpSecret;
|
2024-09-07 10:21:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkForTotSecret() {
|
2025-03-25 23:14:25 +01:00
|
|
|
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) {
|
2025-03-25 23:14:25 +01:00
|
|
|
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,
|
2025-03-25 23:14:25 +01:00
|
|
|
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,
|
2025-03-25 23:14:25 +01:00
|
|
|
getTotpSecret,
|
|
|
|
checkForTotSecret,
|
2024-09-07 10:21:41 -07:00
|
|
|
validateTOTP
|
|
|
|
};
|