update dependencies and devDependencies, address lint issues
[squeep-indie-auther] / src / db / sqlite / index.js
index 453dbf8eada1b9d39d79ac2c47a6e27738c59b40..2dd75b2214f1962abdb18a541a4628bfeb4d7866 100644 (file)
@@ -21,7 +21,7 @@ const schemaVersionsSupported = {
   },
   max: {
     major: 1,
-    minor: 1,
+    minor: 2,
     patch: 0,
   },
 };
@@ -60,8 +60,8 @@ class DatabaseSQLite extends Database {
 
   /**
    * Boolean to 0/1 representation for SQLite params.
-   * @param {Boolean} bool
-   * @returns {Number}
+   * @param {boolean} bool boolean
+   * @returns {number} number
    */
   static _booleanToNumeric(bool) {
     // eslint-disable-next-line security/detect-object-injection
@@ -84,7 +84,7 @@ class DatabaseSQLite extends Database {
     let metaExists = tableExists.get();
     if (metaExists === undefined) {
       const fPath = path.join(__dirname, 'sql', 'schema', 'init.sql');
-      // eslint-disable-next-line security/detect-non-literal-fs-filename
+       
       const fSql = fs.readFileSync(fPath, { encoding: 'utf8' });
       this.db.exec(fSql);
       metaExists = tableExists.get();
@@ -139,7 +139,7 @@ class DatabaseSQLite extends Database {
       };
     };
 
-    // eslint-disable-next-line security/detect-non-literal-fs-filename
+     
     for (const f of fs.readdirSync(sqlDir)) {
       const fPath = path.join(sqlDir, f);
       const { name: fName, ext: fExt } = path.parse(f);
@@ -267,6 +267,11 @@ class DatabaseSQLite extends Database {
   }
 
 
+  static _almanacErrorThrow() {
+    throw new DBErrors.UnexpectedResult('did not update almanac');
+  }
+
+
   almanacGetAll(dbCtx) { // eslint-disable-line no-unused-vars
     const _scope = _fileScope('almanacGetAll');
     this.logger.debug(_scope, 'called');
@@ -289,7 +294,7 @@ class DatabaseSQLite extends Database {
       const epoch = common.dateToEpoch(date);
       const result = this.statement.almanacUpsert.run({ event, epoch });
       if (result.changes != 1) {
-        throw new DBErrors.UnexpectedResult('did not upsert almanac event');
+        this.constructor._almanacErrorThrow();
       }
     } catch (e) {
       this.logger.error(_scope, 'failed', { error: e, event, date });
@@ -337,17 +342,34 @@ class DatabaseSQLite extends Database {
   }
 
 
-  authenticationUpsert(dbCtx, identifier, credential) {
+  authenticationUpsert(dbCtx, identifier, credential, otpKey) {
     const _scope = _fileScope('authenticationUpsert');
     const scrubbedCredential = '*'.repeat((credential || '').length);
-    this.logger.debug(_scope, 'called', { identifier, scrubbedCredential });
+    const scrubbedOTPKey = '*'.repeat((otpKey || '').length);
+    this.logger.debug(_scope, 'called', { identifier, scrubbedCredential, scrubbedOTPKey });
 
-    let result;
     try {
-      result = this.statement.authenticationUpsert.run({ identifier, credential });
+      const result = this.statement.authenticationUpsert.run({ identifier, credential, otpKey });
       if (result.changes != 1) {
         throw new DBErrors.UnexpectedResult('did not upsert authentication');
       }
+    } catch (e) {
+      this.logger.error(_scope, 'failed', { error: e, identifier, scrubbedCredential, scrubbedOTPKey });
+      throw e;
+    }
+  }
+
+
+  authenticationUpdateCredential(dbCtx, identifier, credential) {
+    const _scope = _fileScope('authenticationUpdateCredential');
+    const scrubbedCredential = '*'.repeat((credential || '').length);
+    this.logger.debug(_scope, 'called', { identifier, scrubbedCredential });
+
+    try {
+      const result = this.statement.authenticationUpdateCredential.run({ identifier, credential });
+      if (result.changes != 1) {
+        throw new DBErrors.UnexpectedResult('did not update credential');
+      }
     } catch (e) {
       this.logger.error(_scope, 'failed', { error: e, identifier, scrubbedCredential });
       throw e;
@@ -355,6 +377,23 @@ class DatabaseSQLite extends Database {
   }
 
 
+  authenticationUpdateOTPKey(dbCtx, identifier, otpKey) {
+    const _scope = _fileScope('authenticationUpdateOTPKey');
+    const scrubbedOTPKey = '*'.repeat((otpKey || '').length);
+    this.logger.debug(_scope, 'called', { identifier, scrubbedOTPKey });
+
+    try {
+      const result = this.statement.authenticationUpdateOtpKey.run({ identifier, otpKey });
+      if (result.changes != 1) {
+        throw new DBErrors.UnexpectedResult('did not update otpKey');
+      }
+    } catch (e) {
+      this.logger.error(_scope, 'failed', { error: e, identifier, scrubbedOTPKey });
+      throw e;
+    }
+  }
+
+
   profileIdentifierInsert(dbCtx, profile, identifier) {
     const _scope = _fileScope('profileIdentifierInsert');
     this.logger.debug(_scope, 'called', { profile, identifier });
@@ -580,7 +619,7 @@ class DatabaseSQLite extends Database {
         // Update the last cleanup time
         const result = this.statement.almanacUpsert.run({ event: almanacEvent, epoch: nowEpoch });
         if (result.changes != 1) {
-          throw new DBErrors.UnexpectedResult('did not update almanac');
+          this.constructor._almanacErrorThrow();
         }
 
         this.logger.debug(_scope, 'finished', { scopesRemoved, atLeastMsSinceLast });
@@ -658,7 +697,7 @@ class DatabaseSQLite extends Database {
         // Update the last cleanup time
         const result = this.statement.almanacUpsert.run({ event: almanacEvent, epoch: nowEpoch });
         if (result.changes != 1) {
-          throw new DBErrors.UnexpectedResult('did not update almanac');
+          this.constructor._almanacErrorThrow();
         }
 
         this.logger.debug(_scope, 'finished', { tokensRemoved, codeLifespanSeconds, atLeastMsSinceLast });
@@ -786,7 +825,7 @@ class DatabaseSQLite extends Database {
       const epoch = common.dateToEpoch();
       const almanacResult = this.statement.almanacUpsert.run({ event: almanacEvent, epoch });
       if (almanacResult.changes != 1) {
-        throw new DBErrors.UnexpectedResult('did not update almanac');
+        this.constructor._almanacErrorThrow();
       }
 
     } catch (e) {