Handshake.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var Sequence = require('./Sequence');
  2. var Util = require('util');
  3. var Packets = require('../packets');
  4. var Auth = require('../Auth');
  5. module.exports = Handshake;
  6. Util.inherits(Handshake, Sequence);
  7. function Handshake(config, callback) {
  8. Sequence.call(this, callback);
  9. this._config = config;
  10. this._handshakeInitializationPacket = null;
  11. }
  12. Handshake.prototype.determinePacket = function(firstByte) {
  13. if (firstByte === 0xff) {
  14. return Packets.ErrorPacket;
  15. }
  16. if (!this._handshakeInitializationPacket) {
  17. return Packets.HandshakeInitializationPacket;
  18. }
  19. if (firstByte === 0xfe) {
  20. return Packets.UseOldPasswordPacket;
  21. }
  22. };
  23. Handshake.prototype['HandshakeInitializationPacket'] = function(packet) {
  24. this._handshakeInitializationPacket = packet;
  25. this.emit('packet', new Packets.ClientAuthenticationPacket({
  26. clientFlags : this._config.clientFlags,
  27. maxPacketSize : this._config.maxPacketSize,
  28. charsetNumber : this._config.charsetNumber,
  29. user : this._config.user,
  30. scrambleBuff : Auth.token(this._config.password, packet.scrambleBuff()),
  31. database : this._config.database,
  32. }));
  33. };
  34. Handshake.prototype['UseOldPasswordPacket'] = function(packet) {
  35. if (!this._config.insecureAuth) {
  36. var err = new Error(
  37. 'MySQL server is requesting the old and insecure pre-4.1 auth mechanism.' +
  38. 'Upgrade the user password or use the {insecureAuth: true} option.'
  39. );
  40. err.code = 'HANDSHAKE_INSECURE_AUTH';
  41. err.fatal = true;
  42. this.end(err);
  43. return;
  44. }
  45. this.emit('packet', new Packets.OldPasswordPacket({
  46. scrambleBuff : Auth.scramble323(this._handshakeInitializationPacket.scrambleBuff(), this._config.password),
  47. }));
  48. };
  49. Handshake.prototype['ErrorPacket'] = function(packet) {
  50. var err = this._packetToError(packet, true);
  51. err.fatal = true;
  52. this.end(err);
  53. };