Files
shared/packages/node-backend/test/auth.test.js
T
Gabriel20xx da97fd4fb1
Lint / Lint (Node/Svelte) (push) Successful in 24s
Lint / Lint (Go) (push) Successful in 28s
Lint / Lint (Python) (push) Failing after 14s
Tests / Test (Node/Svelte) (push) Successful in 18s
Tests / Test (Go) (push) Successful in 25s
Tests / Test (Python) (push) Successful in 12s
feat(auth): implement TOTP setup and management
- Added TOTP support for two-factor authentication in the auth module.
- Introduced new types and interfaces for TOTP setup and token actions in the web UI.
- Created backend logic for generating and verifying TOTP secrets and tokens.
- Implemented user management features including role assignments and user CRUD operations.
- Enhanced UI components for managing users and TOTP settings.
- Added styles for optional labels and authentication reset links.
- Implemented reset code generation and validation for admin accounts.
2026-08-05 14:57:01 +02:00

56 lines
1.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
ResetManager,
createSessionToken,
hashPassword,
hashToken,
verifyPassword,
} from '../src/auth.js';
import { totpProvisioningUri, verifyTotp } from '../src/totp.js';
test('reset codes are printed, case-insensitive, expiring, and one-time', () => {
const messages = [];
const manager = new ResetManager({ log: (message) => messages.push(message) });
const code = manager.request();
assert.equal(code.length, 8);
assert.equal(messages.length, 1);
assert.match(messages[0], new RegExp(`ADMIN RESET CODE: ${code}`));
assert.equal(manager.confirm(` ${code.toLowerCase()} `), true);
assert.equal(manager.confirm(code), false);
});
test('expired reset codes are rejected', () => {
const manager = new ResetManager({ ttlMs: -1, log: () => {} });
const code = manager.request();
assert.equal(manager.confirm(code), false);
});
test('password hashes verify and reject a different password', () => {
const hash = hashPassword('correct horse battery staple');
assert.equal(verifyPassword('correct horse battery staple', hash), true);
assert.equal(verifyPassword('wrong password', hash), false);
});
test('TOTP verifies RFC 6238 SHA-1 vectors', () => {
const secret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ';
const vectors = [
[59, '287082'],
[1111111109, '081804'],
[1111111111, '050471'],
[1234567890, '005924'],
[2000000000, '279037'],
];
for (const [seconds, code] of vectors) {
assert.equal(verifyTotp(secret, code, seconds * 1000), true);
}
assert.match(totpProvisioningUri('Example App', 'admin@example.com', secret), /^otpauth:\/\/totp\//);
});
test('session token hashes are not the raw token', () => {
const { token, tokenHash } = createSessionToken();
assert.notEqual(token, tokenHash);
assert.equal(hashToken(token), tokenHash);
});