Verify authenticator codes against RFC 6238 test vectors
Validate / validate (push) Successful in 3m31s

This commit is contained in:
2026-10-30 20:08:23 +08:00
parent 7368317a10
commit c3cc8eff28
3 changed files with 21 additions and 11 deletions
+12
View File
@@ -0,0 +1,12 @@
import { createHmac } from 'crypto';
export function totp(secret: string, step: number) {
let bits = ''; for (const c of secret.toUpperCase().replace(/=|\s/g, '')) {
const n = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.indexOf(c); if (n < 0) throw new Error('Invalid TOTP secret');
bits += n.toString(2).padStart(5, '0');
}
const key = Buffer.from(bits.match(/.{8}/g)!.map(b => parseInt(b, 2)));
const counter = Buffer.alloc(8); counter.writeBigUInt64BE(BigInt(step));
const hash = createHmac('sha1', key).update(counter).digest(); const offset = hash[19] & 15;
return String((hash.readUInt32BE(offset) & 0x7fffffff) % 1000000).padStart(6, '0');
}
+2 -11
View File
@@ -1,18 +1,9 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { createHmac, randomBytes, timingSafeEqual } from 'crypto';
import { randomBytes, timingSafeEqual } from 'crypto';
import { totp } from '../../../lib/totp';
import { query } from '../../../lib/db';
import { COOKIE, signSession } from '../../../lib/editor-session';
function totp(secret: string, step: number) {
let bits = ''; for (const c of secret.toUpperCase().replace(/=|\s/g, '')) {
const n = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.indexOf(c); if (n < 0) throw new Error('Invalid TOTP secret');
bits += n.toString(2).padStart(5, '0');
}
const key = Buffer.from(bits.match(/.{8}/g)!.map(b => parseInt(b, 2)));
const counter = Buffer.alloc(8); counter.writeBigUInt64BE(BigInt(step));
const hash = createHmac('sha1', key).update(counter).digest(); const offset = hash[19] & 15;
return String((hash.readUInt32BE(offset) & 0x7fffffff) % 1000000).padStart(6, '0');
}
export default async function login(req: NextApiRequest, res: NextApiResponse) {
res.setHeader('Cache-Control', 'no-store');
if (req.method !== 'POST') return res.status(405).end();
+7
View File
@@ -20,5 +20,12 @@ vm.runInNewContext(ts.transpileModule(fs.readFileSync('lib/editor-session.ts','u
assert.equal(await validSession(token),false);
context.process.env.EDITOR_SESSION_SECRET = '';
assert.equal(await validSession(token),false);
const totpContext = {exports:{},require,Buffer};
vm.runInNewContext(ts.transpileModule(fs.readFileSync('lib/totp.ts','utf8'), {compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2020}}).outputText, totpContext);
const rfcSecret = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ';
for (const [time,expected] of [[59,'287082'],[1111111109,'081804'],[1111111111,'050471'],[1234567890,'005924'],[2000000000,'279037'],[20000000000,'353130']]) {
assert.equal(totpContext.exports.totp(rfcSecret,Math.floor(time/30)),expected);
}
console.log('RFC 6238 TOTP vectors: passed');
console.log('Editor session validation: passed');
})().catch(error => { console.error(error); process.exitCode=1; });