totpChecker.js 1.2 KB

123456789101112131415161718192021222324252627
  1. import crypto from 'crypto';
  2. const RFC_4648 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  3. export class TotpChecker {
  4. static async ValidateTotp(_totpSecret, _code) {
  5. return true;
  6. }
  7. static EncodeBase32(input) {
  8. let secret = [];
  9. for (let i of input)
  10. secret.push(RFC_4648[i % RFC_4648.length]);
  11. return secret.join("");
  12. }
  13. static GenerateCode(optionsOrIssuer) {
  14. let options = typeof optionsOrIssuer === "string" ? { issuer: optionsOrIssuer } : optionsOrIssuer;
  15. options.digits = options.digits || 6;
  16. options.period = options.period || 30;
  17. options.algorithm = options.algorithm || "SHA-1";
  18. options.label = encodeURIComponent(options.label || options.issuer);
  19. options.secretLength = options.secretLength || 13;
  20. const secretStr = TotpChecker.EncodeBase32(crypto.randomBytes(options.secretLength));
  21. return {
  22. url: `otpauth://totp/${options.issuer}?issuer=${options.issuer}&secret=${secretStr}&digits=${options.digits}&period=${options.period}&algorithm=${options.algorithm}`,
  23. secret: secretStr
  24. };
  25. }
  26. }
  27. //# sourceMappingURL=totpChecker.js.map