ChangeUser.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var Sequence = require('./Sequence');
  2. var Util = require('util');
  3. var Packets = require('../packets');
  4. var Auth = require('../Auth');
  5. module.exports = ChangeUser;
  6. Util.inherits(ChangeUser, Sequence);
  7. function ChangeUser(options, callback) {
  8. Sequence.call(this, options, callback);
  9. this._user = options.user;
  10. this._password = options.password;
  11. this._database = options.database;
  12. this._charsetNumber = options.charsetNumber;
  13. this._currentConfig = options.currentConfig;
  14. }
  15. ChangeUser.prototype.determinePacket = function determinePacket(firstByte) {
  16. switch (firstByte) {
  17. case 0xfe: return Packets.AuthSwitchRequestPacket;
  18. case 0xff: return Packets.ErrorPacket;
  19. default: return undefined;
  20. }
  21. };
  22. ChangeUser.prototype.start = function(handshakeInitializationPacket) {
  23. var scrambleBuff = handshakeInitializationPacket.scrambleBuff();
  24. scrambleBuff = Auth.token(this._password, scrambleBuff);
  25. var packet = new Packets.ComChangeUserPacket({
  26. user : this._user,
  27. scrambleBuff : scrambleBuff,
  28. database : this._database,
  29. charsetNumber : this._charsetNumber
  30. });
  31. this._currentConfig.user = this._user;
  32. this._currentConfig.password = this._password;
  33. this._currentConfig.database = this._database;
  34. this._currentConfig.charsetNumber = this._charsetNumber;
  35. this.emit('packet', packet);
  36. };
  37. ChangeUser.prototype['AuthSwitchRequestPacket'] = function (packet) {
  38. var name = packet.authMethodName;
  39. var data = Auth.auth(name, packet.authMethodData, {
  40. password: this._password
  41. });
  42. if (data !== undefined) {
  43. this.emit('packet', new Packets.AuthSwitchResponsePacket({
  44. data: data
  45. }));
  46. } else {
  47. var err = new Error('MySQL is requesting the ' + name + ' authentication method, which is not supported.');
  48. err.code = 'UNSUPPORTED_AUTH_METHOD';
  49. err.fatal = true;
  50. this.end(err);
  51. }
  52. };
  53. ChangeUser.prototype['ErrorPacket'] = function(packet) {
  54. var err = this._packetToError(packet);
  55. err.fatal = true;
  56. this.end(err);
  57. };