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Q,EAAK,GAEjB,GACF,CAiEqB,CAAa,EAAU,EAAO,GAChC,IAAuC,IAAnC,KAAK,OAAO,mBACrB,MAAM,IAAI,KAAK,YAAY,qBAE/B,IAEJ,EAQA,EAAM,UAAU,QAAU,SAAU,GAClC,OAvER,SAAkB,EAAK,GACrB,OAAO,IAAI,SAAQ,SAAU,EAAS,GACpC,OAAO,QAAQ,EAAK,GAAM,CAAC,EAAK,KAC1B,EACF,EAAO,GAEP,EAAQ,EACV,GAEJ,GACF,CA6De,CAAQ,EAAK,KAAK,IAAI,GAC/B,CACF,CACF,GACD","file":"/npm/bookshelf-bcryptjs@1.0.3/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/bookshelf-bcryptjs@1.0.3/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","'use strict'\r\n\r\nlet merge = require('lodash.merge')\r\nlet get = require('lodash.get')\r\nlet bcrypt = require('bcryptjs')\r\n\r\n// https://paragonie.com/blog/2016/02/how-safely-store-password-in-2016\r\nconst RECOMMENDED_ROUNDS = 12\r\n\r\nmodule.exports = (bookshelf, settings) => {\r\n  let BookshelfModel = bookshelf.Model\r\n\r\n  // Add default settings\r\n  settings = merge({\r\n    allowEmptyPassword: false,\r\n    rounds: RECOMMENDED_ROUNDS,\r\n    detectBcrypt: () => false,\r\n    onRehash: function () {\r\n      throw new this.constructor.BcryptRehashDetected()\r\n    }\r\n  }, settings)\r\n\r\n  /**\r\n   * Hashes a string and stores it inside the provided model\r\n   * @param {String} string A string to be hashed\r\n   * @param {String} field The field that will receive the hashed string\r\n   * @param {Object} model An instantiated bookshelf model\r\n   * @throws {BcryptRehashDetected} If it detects a rehash\r\n   * @return {Promise} A promise that resolves with the model correctly updated\r\n   */\r\n  function hashAndStore (string, field, model) {\r\n    return new Promise(function (resolve, reject) {\r\n      // Avoid rehashing a string by mistake but allow users to implement\r\n      // non throwing logic\r\n      if (settings.detectBcrypt(string) && typeof settings.onRehash === 'function') {\r\n        try {\r\n          settings.onRehash.call(model)\r\n        } catch (err) {\r\n          return reject(err)\r\n        }\r\n      }\r\n\r\n      bcrypt.hash(string, settings.rounds, (err, hash) => {\r\n        if (err) return reject(err)\r\n\r\n        // Set the field and resolves the promise\r\n        model.set(field, hash)\r\n        resolve(model)\r\n      })\r\n    })\r\n  }\r\n\r\n  /**\r\n   * Compares a string against a bcrypt hash\r\n   * @param {String} str The raw string to be compared\r\n   * @param {String} hash A bcrypt hash to match against the string\r\n   * @return {Promise} A promise that resolves to a boolean indicating if the\r\n   * hash was generated from the provided string\r\n   */\r\n  function compare (str, hash) {\r\n    return new Promise(function (resolve, reject) {\r\n      bcrypt.compare(str, hash, (err, res) => {\r\n        if (err) {\r\n          reject(err)\r\n        } else {\r\n          resolve(res)\r\n        }\r\n      })\r\n    })\r\n  }\r\n\r\n  /**\r\n   * Custom error class for throwing when this plugin detects a rehash\r\n   * @type {Error}\r\n   */\r\n  bookshelf.BcryptRehashDetected = BookshelfModel.BcryptRehashDetected = class extends Error {\r\n    constructor () {\r\n      super('Bcrypt tried to hash another bcrypt hash')\r\n      this.name = 'BcryptRehashDetected'\r\n    }\r\n  }\r\n\r\n  /**\r\n   * Custom error class for throwing when this plugin detects a null or undefined password\r\n   * @type {Error}\r\n   */\r\n  bookshelf.EmptyPasswordDetected = BookshelfModel.EmptyPasswordDetected = class extends Error {\r\n    constructor () {\r\n      super('Bcrypt cannot hash a null or undefined password')\r\n      this.name = 'EmptyPasswordDetected'\r\n    }\r\n  }\r\n\r\n  // Extends the default model class\r\n  bookshelf.Model = bookshelf.Model.extend({}, {\r\n    extended (child) {\r\n      // Check if the extended model has the bcrypt option\r\n      let field = get(child.prototype, 'bcrypt.field')\r\n\r\n      // Configure bcrypt only for enabled models\r\n      if (field) {\r\n        let initialize = child.prototype.initialize\r\n\r\n        child.prototype.initialize = function () {\r\n          // Do not override child's initialization\r\n          if (initialize) initialize.call(this)\r\n\r\n          // Hash the password when saving\r\n          this.on('saving', (model, attrs, options) => {\r\n            let field = get(this, 'bcrypt.field')\r\n\r\n            if (model.hasChanged(field) && options.bcrypt !== false) {\r\n              let password = model.get(field)\r\n\r\n              if (password !== null && typeof password !== 'undefined') {\r\n                return hashAndStore(password, field, model)\r\n              } else if (this.bcrypt.allowEmptyPassword !== true) {\r\n                throw new this.constructor.EmptyPasswordDetected()\r\n              }\r\n            }\r\n          })\r\n        }\r\n\r\n        /**\r\n         * Compares a string against a bcrypt hash stored in the current model\r\n         * @param {String} str The string to compare against the hash\r\n         * @return {Promise} A promise that resolves to a boolean indicating if\r\n         * the provided string is valid or not\r\n         */\r\n        child.prototype.compare = function (str) {\r\n          return compare(str, this.get(field))\r\n        }\r\n      }\r\n    }\r\n  })\r\n}\r\n"]}