CI / Docker image (push) Successful in 12s
CI / Server (push) Successful in 25s
CI / CLI (push) Successful in 13s
Development image / Development image (push) Successful in 21s
CI / CLI (windows) (push) Successful in 57s
CI / CLI (linux-arm64, static) (push) Successful in 1m37s
Release / Prepare release (push) Successful in 2s
Release / Server (linux-x64) (push) Successful in 53s
Release / CLI (linux-arm64, static) (push) Successful in 1m32s
Release / Docker image (push) Successful in 2m48s
Release / Server (windows-x64) (push) Successful in 6m20s
Release / CLI (windows-x64) (push) Successful in 6m48s
Release / CLI (linux-x64) (push) Successful in 7m8s
Release / Publish release (push) Successful in 1s
Squash the tree into one commit, drop docs/architecture.html, reset the version line to 0.3.0, consolidate the changelog, enable Renovate automerge for non-major updates and add AGENTS.md.
53 lines
2.2 KiB
JavaScript
53 lines
2.2 KiB
JavaScript
const speakeasy = require("speakeasy");
|
|
const Account = require("../models/Account");
|
|
const Session = require("../models/Session");
|
|
const logger = require("../utils/logger");
|
|
|
|
const WINDOW = 1;
|
|
|
|
const secretFor = async account => {
|
|
if (account.totpSecret) return account.totpSecret;
|
|
const secret = speakeasy.generateSecret({ length: 20 }).base32;
|
|
await Account.update({ totpSecret: secret }, { where: { id: account.id } });
|
|
return secret;
|
|
};
|
|
|
|
const otpauthUrl = (secret, username, issuer) =>
|
|
`otpauth://totp/${encodeURIComponent(issuer)}:${encodeURIComponent(username)}` +
|
|
`?secret=${secret}&issuer=${encodeURIComponent(issuer)}`;
|
|
|
|
const verify = (secret, code) => speakeasy.totp.verify({
|
|
secret: secret || "",
|
|
encoding: "base32",
|
|
token: String(code || "").replace(/\s/g, ""),
|
|
window: WINDOW,
|
|
});
|
|
|
|
const begin = async (account, issuer) => {
|
|
if (account.totpEnabled) return { code: 409, message: "Two-factor is already on for this account" };
|
|
const secret = await secretFor(account);
|
|
return { secret, url: otpauthUrl(secret, account.username, issuer) };
|
|
};
|
|
|
|
const enable = async (account, code) => {
|
|
if (account.totpEnabled) return { code: 409, message: "Two-factor is already on for this account" };
|
|
if (!account.totpSecret) return { code: 409, message: "Start the setup first" };
|
|
if (!verify(account.totpSecret, code)) return { code: 401, message: "That code is not right. Check your clock and try the next one." };
|
|
|
|
await Account.update({ totpEnabled: true }, { where: { id: account.id } });
|
|
await Session.destroy({ where: { accountId: account.id } });
|
|
logger.info(`${account.username} turned on two-factor`);
|
|
return { message: "Two-factor is on" };
|
|
};
|
|
|
|
const disable = async (account, code) => {
|
|
if (!account.totpEnabled) return { code: 409, message: "Two-factor is already off" };
|
|
if (!verify(account.totpSecret, code)) return { code: 401, message: "That code is not right" };
|
|
|
|
await Account.update({ totpEnabled: false, totpSecret: null }, { where: { id: account.id } });
|
|
logger.info(`${account.username} turned off two-factor`);
|
|
return { message: "Two-factor is off" };
|
|
};
|
|
|
|
module.exports = { begin, enable, disable, verify };
|