totpChecker.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. import crypto from 'crypto';
  11. const RFC_4648 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  12. export class TotpChecker {
  13. static ValidateTotp(_totpSecret, _code) {
  14. return __awaiter(this, void 0, void 0, function* () {
  15. return true;
  16. });
  17. }
  18. static EncodeBase32(input) {
  19. let secret = [];
  20. for (let i of input)
  21. secret.push(RFC_4648[i % RFC_4648.length]);
  22. return secret.join("");
  23. }
  24. static GenerateCode(optionsOrIssuer) {
  25. let options = typeof optionsOrIssuer === "string" ? { issuer: optionsOrIssuer } : optionsOrIssuer;
  26. options.digits = options.digits || 6;
  27. options.period = options.period || 30;
  28. options.algorithm = options.algorithm || "SHA-1";
  29. options.label = encodeURIComponent(options.label || options.issuer);
  30. options.secretLength = options.secretLength || 13;
  31. const secretStr = TotpChecker.EncodeBase32(crypto.randomBytes(options.secretLength));
  32. return {
  33. url: `otpauth://totp/${options.issuer}?issuer=${options.issuer}&secret=${secretStr}&digits=${options.digits}&period=${options.period}&algorithm=${options.algorithm}`,
  34. secret: secretStr
  35. };
  36. }
  37. }
  38. //# sourceMappingURL=totpChecker.js.map