{"size": 2578, "ext": "js", "max_stars_repo_path": "src/scripts/MVC3/change_password.js", "max_stars_repo_name": "mauricioschneider/db-connections-templates", "max_stars_repo_stars_event_min_datetime": "2019-05-29T06:34:12.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-18T07:14:24.000Z", "max_issues_repo_path": "src/scripts/MVC3/change_password.js", "max_issues_repo_name": "mauricioschneider/db-connections-templates", "max_issues_repo_issues_event_min_datetime": "2018-10-18T17:55:56.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T15:31:58.000Z", "max_forks_repo_path": "src/scripts/MVC3/change_password.js", "max_forks_repo_name": "mauricioschneider/db-connections-templates", "max_forks_repo_forks_event_min_datetime": "2018-10-04T06:13:20.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-19T18:18:13.000Z"}
{"max_stars_count": 6, "max_issues_count": 14, "max_forks_count": 16, "avg_line_length": 33.9210526316, "max_line_length": 106, "alphanum_fraction": 0.6920093095}
function changePassword(email, newPassword, callback) {
  const crypto = require('crypto');
  const sqlserver = require('tedious@1.11.0');

  const Connection = sqlserver.Connection;
  const Request = sqlserver.Request;
  const TYPES = sqlserver.TYPES;

  const connection = new Connection({
    userName: 'the username',
    password: 'the password',
    server: 'the server',
    options: {
      database: 'the db name',
      // encrypt: true for Windows Azure enable this
    }
  });

  /**
   * hashPassword
   *
   * This function creates a hashed version of the password to store in the database.
   *
   * @password  {[string]}      the password entered by the user
   * @return    {[string]}      the hashed password
   */
  function hashPassword(password, salt) {
    // the default implementation uses HMACSHA256 and since Key length is 64
    // and default salt is 16 bytes, Membership will fill the buffer repeating the salt
    const key = Buffer.concat([salt, salt, salt, salt]);
    const hmac = crypto.createHmac('sha256', key);
    hmac.update(Buffer.from(password, 'ucs2'));

    return hmac.digest('base64');
  }

  connection.on('debug', function(text) {
    // if you have connection issues, uncomment this to get more detailed info
    //console.log(text);
  }).on('errorMessage', function(text) {
    // this will show any errors when connecting to the SQL database or with the SQL statements
    console.log(JSON.stringify(text));
  });

  connection.on('connect', function(err) {
    if (err) return callback(err);

    updateMembershipUser(email, newPassword, function(err, wasUpdated) {
      if (err) return callback(err); // this will return a 500

      callback(null, wasUpdated);
    });
  });

  function updateMembershipUser(email, newPassword, callback) {
    const salt = crypto.randomBytes(16);
    const hashedPassword = hashPassword(newPassword, salt);
    const updateMembership =
      'UPDATE Memberships ' +
      'SET Password=@NewPassword, PasswordSalt=@NewSalt, LastPasswordChangedDate=GETDATE() ' +
      'WHERE Email=@Email';

    const updateMembershipQuery = new Request(updateMembership, function(membershipErr, membershipCount) {
      if (membershipErr) return callback(membershipErr);

      callback(null, membershipCount > 0);
    });

    updateMembershipQuery.addParameter('NewPassword', TYPES.VarChar, hashedPassword);
    updateMembershipQuery.addParameter('NewSalt', TYPES.VarChar, salt);
    updateMembershipQuery.addParameter('Email', TYPES.VarChar, email);

    connection.execSql(updateMembershipQuery);
  }
}
