add updateOTPKey wrapper to authenticator
[squeep-authentication-module] / lib / authenticator.js
index 7390db5d8803c8271e0cbc32c254e3198d057df0..dc56e9e0180d161310b724d28775c1782e157a73 100644 (file)
@@ -33,7 +33,7 @@ class Authenticator {
    * @property {(DBContextExec) => Promise<any>} context
    * @property {(dbCtx: any, identifier: String) => Promise<AuthInfo> } authenticationGet
    * @property {(dbCtx: any, identifier: String) => Promise<void>} authenticationSuccess
-   * @property {(dbCtx: any, identifier: String, credential: String, otpKey: String=) => Promise<void>} authenticationInsertIdentifier
+   * @property {(dbCtx: any, identifier: String, credential: String, otpKey: String=) => Promise<void>} authenticationUpsert
    * @property {(dbCtx: any, identifier: String, otpKey: String) => Promise<void>} authenticationUpdateOTPKey
    * @property {(dbCtx: any, identifier: String, credential: AuthInfo) => Promise<void>} authenticationUpdateCredential
    */
@@ -117,7 +117,7 @@ class Authenticator {
     const _scope = _fileScope('createIdentifier');
     try {
       const secureCredential = await this._secureCredential(credential);
-      await this.db.authenticationInsertIdentifier(dbCtx, identifier, secureCredential, otpKey);
+      await this.db.authenticationUpsert(dbCtx, identifier, secureCredential, otpKey);
     } catch (e) {
       this.logger.error(_scope, 'failed', { error: e, identifier });
       throw e;
@@ -319,6 +319,23 @@ class Authenticator {
   }
 
 
+  /**
+   * Update the authentication database with a new otp key.
+   * @param {*} dbCtx db context
+   * @param {string} identifier identifier
+   * @param {string=} otpKey otp key
+   */
+  async updateOTPKey(dbCtx, identifier, otpKey) {
+    const _scope = _fileScope('updateOTPKey');
+    try {
+      await this.db.authenticationUpdateOTPKey(dbCtx, identifier, otpKey);
+      this.logger.info(_scope, 'otp key updated');
+    } catch (e) {
+      this.logger.error(_scope, 'failed', { error: e, identifier });
+    }
+  }
+
+
   /**
    * Check for valid Basic auth, updates ctx with identifier if valid.
    * @param {String} credentials