Files
wekan/tests/twoFactorLoginState.test.cjs
T
Lauri Ojansivu 0eaa97230b Add native TOTP two-factor authentication via Meteor's accounts-2fa.
Issue #3058 asked for two-factor authentication on WeKan's own
username/password login, not through a third-party OAuth provider. Added
Meteor's official accounts-2fa package (MIT, part of the meteor/meteor
monorepo, same publisher as accounts-password already in use) instead of
hand-rolling TOTP.

A new "Two-Factor Authentication" section in the member menu
(client/components/users/userHeader.jade/.js) calls
Accounts.generate2faActivationQrCode to show a QR code plus a manual-entry
secret, and Accounts.enableUser2fa/disableUser2fa to finish enabling or to
turn it off; Accounts.has2faEnabled drives which of those is shown. All
TOTP secret generation and code verification is done by accounts-2fa
itself - WeKan only renders what it returns and calls its documented
functions.

On the sign-in form (config/accounts.js, client/components/main/layouts.jade),
a normal Meteor.loginWithPassword failure with error.error ===
'no-2fa-code' now shows a second "enter your 6-digit code" step instead of
a generic error, and resubmits via Meteor.loginWithPasswordAnd2faCode -
accounts-2fa's own login method for this case. The decision of which step
to show is a pure function (imports/lib/twoFactorAuthLoginDecision.js),
unit-tested in tests/twoFactorLoginState.test.cjs without a running Meteor
server. wekan-accounts-lockout already anticipated this: its
loginFailureDecision.js already treated 'no-2fa-code' as a non-countable
step so 2FA logins are never mistaken for brute-force attempts.

Scope: opt-in, per-user TOTP only, as the issue asked - no backup codes, no
SMS, no admin-enforced 2FA in this pass; accounts-2fa does not offer any of
those out of the box either.

This environment has no Meteor CLI, so `meteor add accounts-2fa` and the
login UI could not be exercised end-to-end here; the package was verified
against its published source (packages/accounts-2fa/package.js, version
3.1.0) and docs.meteor.com instead, and added to .meteor/packages/versions
by hand.

New UI strings (twoFactorAuth-*/twoFactorCode-*) were added to
en.i18n.json and translated into the ~100 locales WeKan already had
existing coverage for to translate from; the remaining locale files keep
the English source as a placeholder pending translation, reported by
node releases/translations/fill-translations.mjs --missing.

Thanks to r0bbie and xet7 !

Closes #3058,
2026-09-10 20:35:33 +03:00

92 lines
3.2 KiB
JavaScript

'use strict';
// Plain-Node unit test (no Meteor) for the sign-in form's 2FA-code-step
// decision (imports/lib/twoFactorAuthLoginDecision.js), part of GitHub issue
// #3058: native TOTP two-factor authentication via Meteor's official
// accounts-2fa package (https://docs.meteor.com/packages/accounts-2fa.html).
// accounts-2fa itself generates/verifies the TOTP codes and is not
// re-tested here - only WeKan's own decision of when to show the second
// "enter your 6-digit code" step is pure/testable without a running Meteor
// server, so that is what this suite pins.
//
// Run: node tests/twoFactorLoginState.test.cjs
const assert = require('assert');
const {
NEED_CODE,
INVALID_CODE,
decideTwoFactorLoginStep,
isTwoFactorLoginStep,
} = require('../imports/lib/twoFactorAuthLoginDecision');
let passed = 0;
function test(name, fn) {
fn();
passed += 1;
console.log(' ok -', name);
}
test('exposes the exact accounts-2fa error codes', () => {
assert.strictEqual(NEED_CODE, 'no-2fa-code');
assert.strictEqual(INVALID_CODE, 'invalid-2fa-code');
});
test('no-2fa-code on sign-in shows the code step for the first time', () => {
const step = decideTwoFactorLoginStep({ error: 'no-2fa-code' }, 'signIn');
assert.strictEqual(step, 'need-code');
assert.strictEqual(isTwoFactorLoginStep(step), true);
});
test('invalid-2fa-code on sign-in keeps the code step open with an inline error', () => {
const step = decideTwoFactorLoginStep({ error: 'invalid-2fa-code' }, 'signIn');
assert.strictEqual(step, 'retry-code');
assert.strictEqual(isTwoFactorLoginStep(step), true);
});
test('an ordinary credential failure is NOT treated as a 2FA step', () => {
const step = decideTwoFactorLoginStep(
{ error: 403, reason: 'Something went wrong. Please check your credentials.' },
'signIn',
);
assert.strictEqual(step, 'none');
assert.strictEqual(isTwoFactorLoginStep(step), false);
});
test('no error at all is never a 2FA step', () => {
assert.strictEqual(decideTwoFactorLoginStep(undefined, 'signIn'), 'none');
assert.strictEqual(decideTwoFactorLoginStep(null, 'signIn'), 'none');
});
test('the 2FA step only ever applies to the sign-in form, never sign-up/other states', () => {
assert.strictEqual(
decideTwoFactorLoginStep({ error: 'no-2fa-code' }, 'signUp'),
'none',
);
assert.strictEqual(
decideTwoFactorLoginStep({ error: 'no-2fa-code' }, 'changePwd'),
'none',
);
assert.strictEqual(
decideTwoFactorLoginStep({ error: 'no-2fa-code' }, undefined),
'none',
);
});
test('a non-object error is never a 2FA step (defensive)', () => {
assert.strictEqual(decideTwoFactorLoginStep('boom', 'signIn'), 'none');
assert.strictEqual(decideTwoFactorLoginStep(42, 'signIn'), 'none');
});
test('REGRESSION: does not fire on the shape of a plain lockout error', () => {
// GHSA-2g94-9x3m-hv37's lockout error is a Meteor.Error with a different
// code ('too-many-failed-attempts' style); must never be mistaken for the
// 2FA step, or a locked-out user would be shown a bogus code prompt.
const step = decideTwoFactorLoginStep(
{ error: 'too-many-failed-attempts', reason: 'Too many failed attempts' },
'signIn',
);
assert.strictEqual(step, 'none');
});
console.log(`\n${passed} passed`);