Google Authenticator 같은 TOTP 2FA를 직접 구현하는 방법을 알려주세요.
Node.js에서 TOTP 구현:
npm install otpauth qrcode// 시크릿 생성import { TOTP } from "otpauth";const totp = new TOTP({ issuer: "MyApp", label: "user@email.com", algorithm: "SHA1", digits: 6, period: 30 });const secret = totp.secret.base32; // DB에 저장const uri = totp.toString(); // QR 코드용 URI// QR 코드 생성import QRCode from "qrcode";const qr = await QRCode.toDataURL(uri);// 검증const isValid = totp.validate({ token: userInput, window: 1 }) !== null;
네, 반드시 구현하세요. 사용자가 폰을 잃어버리면 로그인이 불가능합니다.
// 복구 코드 생성 (8개)import crypto from "crypto";const codes = Array.from({length: 8}, () => crypto.randomBytes(4).toString("hex")); // ["a1b2c3d4", "e5f6g7h8", ...]// 해시해서 DB 저장, 사용자에게 평문 보여주기// 사용 시 해당 코드 삭제 (일회용)
댓글 3
Node.js에서 TOTP 구현:
네, 반드시 구현하세요. 사용자가 폰을 잃어버리면 로그인이 불가능합니다.