index.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. System.register("totpChecker", ["crypto"], function (exports_1, context_1) {
  11. "use strict";
  12. var crypto_1, RFC_4648, TotpChecker;
  13. var __moduleName = context_1 && context_1.id;
  14. return {
  15. setters: [
  16. function (crypto_1_1) {
  17. crypto_1 = crypto_1_1;
  18. }
  19. ],
  20. execute: function () {
  21. RFC_4648 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  22. TotpChecker = class TotpChecker {
  23. static ValidateTotp(_totpSecret, _code) {
  24. return __awaiter(this, void 0, void 0, function* () {
  25. return true;
  26. });
  27. }
  28. static EncodeBase32(input) {
  29. let secret = [];
  30. for (let i of input)
  31. secret.push(RFC_4648[i % RFC_4648.length]);
  32. return secret.join("");
  33. }
  34. static GenerateCode(optionsOrIssuer) {
  35. let options = typeof optionsOrIssuer === "string" ? { issuer: optionsOrIssuer } : optionsOrIssuer;
  36. options.digits = options.digits || 6;
  37. options.period = options.period || 30;
  38. options.algorithm = options.algorithm || "SHA-1";
  39. options.label = encodeURIComponent(options.label || options.issuer);
  40. options.secretLength = options.secretLength || 13;
  41. const secretStr = TotpChecker.EncodeBase32(crypto_1.default.randomBytes(options.secretLength));
  42. return {
  43. url: `otpauth://totp/${options.issuer}?issuer=${options.issuer}&secret=${secretStr}&digits=${options.digits}&period=${options.period}&algorithm=${options.algorithm}`,
  44. secret: secretStr
  45. };
  46. }
  47. };
  48. exports_1("TotpChecker", TotpChecker);
  49. }
  50. };
  51. });
  52. System.register("yesManAuthenticationHandler", ["totpChecker"], function (exports_2, context_2) {
  53. "use strict";
  54. var totpChecker_1, YesManAuthenticationHandler;
  55. var __moduleName = context_2 && context_2.id;
  56. return {
  57. setters: [
  58. function (totpChecker_1_1) {
  59. totpChecker_1 = totpChecker_1_1;
  60. }
  61. ],
  62. execute: function () {
  63. YesManAuthenticationHandler = class YesManAuthenticationHandler {
  64. constructor(useTotp) {
  65. this.useTotp = useTotp;
  66. }
  67. tryLogin(username, password, totp) {
  68. if (!username)
  69. return Promise.resolve(null);
  70. if (!password)
  71. return Promise.resolve(false);
  72. if ((this.useTotp && !totp) || (!this.useTotp && totp))
  73. return Promise.resolve(false);
  74. if (!totp)
  75. return Promise.resolve(true);
  76. return totpChecker_1.TotpChecker.ValidateTotp(totpChecker_1.TotpChecker.EncodeBase32(Buffer.from(username)), totp);
  77. }
  78. needTotp(username) {
  79. return Promise.resolve(username ? this.useTotp : null);
  80. }
  81. };
  82. exports_2("YesManAuthenticationHandler", YesManAuthenticationHandler);
  83. }
  84. };
  85. });
  86. System.register("sqliteAuthenticationHandler", ["totpChecker"], function (exports_3, context_3) {
  87. "use strict";
  88. var totpChecker_2, SqliteAuthenticationHandler;
  89. var __moduleName = context_3 && context_3.id;
  90. return {
  91. setters: [
  92. function (totpChecker_2_1) {
  93. totpChecker_2 = totpChecker_2_1;
  94. }
  95. ],
  96. execute: function () {
  97. SqliteAuthenticationHandler = class SqliteAuthenticationHandler {
  98. constructor(getAccountInformationFunction, passwordEncoder) {
  99. this.passwordEncoder = passwordEncoder;
  100. this.getAccountInformation = getAccountInformationFunction;
  101. }
  102. needTotp(username) {
  103. return __awaiter(this, void 0, void 0, function* () {
  104. const accountInformation = yield this.getAccountInformation(username);
  105. if (!accountInformation)
  106. return null;
  107. return !!accountInformation.totpSecret;
  108. });
  109. }
  110. tryLogin(username, password, totp) {
  111. return __awaiter(this, void 0, void 0, function* () {
  112. const accountInformation = yield this.getAccountInformation(username);
  113. if (!accountInformation)
  114. return null;
  115. password = this.passwordEncoder(password);
  116. if (accountInformation.passwordEncoded !== password ||
  117. (accountInformation.totpSecret && !totp) ||
  118. (!accountInformation.totpSecret && totp))
  119. return false;
  120. if (!accountInformation.totpSecret && !totp)
  121. return true;
  122. return totpChecker_2.TotpChecker.ValidateTotp(accountInformation.totpSecret, totp);
  123. });
  124. }
  125. };
  126. exports_3("SqliteAuthenticationHandler", SqliteAuthenticationHandler);
  127. }
  128. };
  129. });
  130. System.register("index", ["yesManAuthenticationHandler", "totpChecker", "sqliteAuthenticationHandler"], function (exports_4, context_4) {
  131. "use strict";
  132. var AuthenticationLoader, yesManAuthenticationHandler_1, totpChecker_3, sqliteAuthenticationHandler_1;
  133. var __moduleName = context_4 && context_4.id;
  134. return {
  135. setters: [
  136. function (yesManAuthenticationHandler_1_1) {
  137. yesManAuthenticationHandler_1 = yesManAuthenticationHandler_1_1;
  138. },
  139. function (totpChecker_3_1) {
  140. totpChecker_3 = totpChecker_3_1;
  141. },
  142. function (sqliteAuthenticationHandler_1_1) {
  143. sqliteAuthenticationHandler_1 = sqliteAuthenticationHandler_1_1;
  144. }
  145. ],
  146. execute: function () {
  147. AuthenticationLoader = class AuthenticationLoader {
  148. constructor() {
  149. this.handlers = [];
  150. }
  151. addAuthenticationHandler(authenticationHandler) {
  152. this.handlers.push(authenticationHandler);
  153. }
  154. tryLogin(username, password, totpCode) {
  155. return __awaiter(this, void 0, void 0, function* () {
  156. for (let i of this.handlers) {
  157. const result = yield i.tryLogin(username, password, totpCode);
  158. if (result !== null)
  159. return result;
  160. }
  161. return false;
  162. });
  163. }
  164. needTotp(username) {
  165. return __awaiter(this, void 0, void 0, function* () {
  166. for (let i of this.handlers) {
  167. const result = yield i.needTotp(username);
  168. if (result !== null)
  169. return result;
  170. }
  171. return false;
  172. });
  173. }
  174. };
  175. exports_4("AuthenticationLoader", AuthenticationLoader);
  176. exports_4("YesManAuthenticationHandler", yesManAuthenticationHandler_1.YesManAuthenticationHandler);
  177. exports_4("TotpChecker", totpChecker_3.TotpChecker);
  178. exports_4("SqliteAuthenticationHandler", sqliteAuthenticationHandler_1.SqliteAuthenticationHandler);
  179. }
  180. };
  181. });
  182. //# sourceMappingURL=index.js.map