<commit_before>/*
 * jasstor
 * https://github.com/lkrnac/jasstor
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */
'use strict';

//Traceur is included here only to provide runtime in transpiled ES5 code
// jshint -W098
var traceur = require('gulp-traceur');
var Bluebird = require('bluebird');

var fs = Bluebird.promisifyAll(require('fs'));
var bcrypt = Bluebird.promisifyAll(require('bcrypt'));

var hashPassword = (password, callback) => {
  bcrypt.genSaltAsync(10)
  .then(salt => {
    return bcrypt.hashAsync(password, salt);
  }).then(hash => {
    callback(undefined, hash);
  }).catch(callback);
};

var readJsonFile = (storageFile, callback) => {
  fs.exists(storageFile, result => {
    if (result === true) {
      fs.readFileAsync(storageFile)
        .then(JSON.parse)
        .then(jsonData => {
          callback(undefined, jsonData);
        }).catch(callback);
    } else {
      callback(null, {});
    }
  });
};

module.exports = class Jasstor {
  constructor(storageFile) {
    this.storageFile = storageFile;
  }

  saveCredentials(user, password, role, callback) {
    readJsonFile(this.storageFile, (err, jsonData) => {
      hashPassword(password, (err, hash) => {
        jsonData[user] = {
          password: <PASSWORD>,
          role: role
        };
        var jsonDataString = JSON.stringify(jsonData);

        fs.writeFile(this.storageFile, jsonDataString, callback);
      });
    });
  }

  verify(user, password, callback) {
    readJsonFile(this.storageFile, (err, jsonData) => {
      var dataForUser = jsonData[user];
      if (dataForUser !== undefined) {
        bcrypt.compare(password, dataForUser.password, (err, result) => {
          if (result === true) {
            callback(err, dataForUser.role);
          } else {
            callback(err, undefined);
          }
        });
      } else {
        callback(err, undefined);
      }
    });
  }

  getRole(user, callback) {
    readJsonFile(this.storageFile, (err, jsonData) => {
      if (jsonData[user] !== undefined){
        callback(err, jsonData[user].role);
      } else {
        callback(err, undefined);
      }
    });
  }
};<commit_msg>hashPassword function refactored to return promise<commit_after>/*
 * jasstor
 * https://github.com/lkrnac/jasstor
 *
 * Copyright (c) 2014 <NAME>
 * Licensed under the MIT license.
 */
'use strict';

//Traceur is included here only to provide runtime in transpiled ES5 code
// jshint -W098
var traceur = require('gulp-traceur');
var Bluebird = require('bluebird');

var fs = Bluebird.promisifyAll(require('fs'));
var bcrypt = Bluebird.promisifyAll(require('bcrypt'));

var hashPassword = password => {
  return new Promise(resolve => {
    bcrypt.genSaltAsync(10)
      .then(salt => {
        return bcrypt.hashAsync(password, salt);
      }).then(resolve);
  });
};

var readJsonFile = (storageFile, callback) => {
  fs.exists(storageFile, result => {
    if (result === true) {
      fs.readFileAsync(storageFile)
        .then(JSON.parse)
        .then(jsonData => {
          callback(undefined, jsonData);
        }).catch(callback);
    } else {
      callback(null, {});
    }
  });
};

module.exports = class Jasstor {
  constructor(storageFile) {
    this.storageFile = storageFile;
  }

  saveCredentials(user, password, role, callback) {
    readJsonFile(this.storageFile, (err, jsonData) => {
      hashPassword(password)
        .then((hash) => {
          jsonData[user] = {
            password: <PASSWORD>,
            role: role
          };
          var jsonDataString = JSON.stringify(jsonData);

          fs.writeFile(this.storageFile, jsonDataString, callback);
        });
    });
  }

  verify(user, password, callback) {
    readJsonFile(this.storageFile, (err, jsonData) => {
      var dataForUser = jsonData[user];
      if (dataForUser !== undefined) {
        bcrypt.compare(password, dataForUser.password, (err, result) => {
          if (result === true) {
            callback(err, dataForUser.role);
          } else {
            callback(err, undefined);
          }
        });
      } else {
        callback(err, undefined);
      }
    });
  }

  getRole(user, callback) {
    readJsonFile(this.storageFile, (err, jsonData) => {
      if (jsonData[user] !== undefined) {
        callback(err, jsonData[user].role);
      } else {
        callback(err, undefined);
      }
    });
  }
};
{'fraction_non_alphanumeric': 0.14707217430776215, 'fraction_numerical': 0.004539264639128461, 'mean_word_length': 2.6004901960784315, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4067413', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1357, 'n_words': 277}