sqliteAuthenticationHandler.js 1.2 KB

1234567891011121314151617181920212223242526272829
  1. import { TotpChecker } from "./totpChecker.js";
  2. export class SqliteAuthenticationHandler {
  3. passwordEncoder;
  4. getAccountInformation;
  5. constructor(getAccountInformationFunction, passwordEncoder) {
  6. this.passwordEncoder = passwordEncoder;
  7. this.getAccountInformation = getAccountInformationFunction;
  8. }
  9. async needTotp(username) {
  10. const accountInformation = await this.getAccountInformation(username);
  11. if (!accountInformation)
  12. return null;
  13. return !!accountInformation.totpSecret;
  14. }
  15. async tryLogin(username, password, totp) {
  16. const accountInformation = await this.getAccountInformation(username);
  17. if (!accountInformation)
  18. return null;
  19. password = this.passwordEncoder(password);
  20. if (accountInformation.passwordEncoded !== password ||
  21. (accountInformation.totpSecret && !totp) ||
  22. (!accountInformation.totpSecret && totp))
  23. return false;
  24. if (!accountInformation.totpSecret && !totp)
  25. return true;
  26. return TotpChecker.ValidateTotp(accountInformation.totpSecret, totp);
  27. }
  28. }
  29. //# sourceMappingURL=sqliteAuthenticationHandler.js.map