Verify authenticator codes against RFC 6238 test vectors
Validate / validate (push) Successful in 3m31s
Validate / validate (push) Successful in 3m31s
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user