handshake_response.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. 'use strict';
  2. const ClientConstants = require('../constants/client.js');
  3. const CharsetToEncoding = require('../constants/charset_encodings.js');
  4. const Packet = require('../packets/packet.js');
  5. const auth41 = require('../auth_41.js');
  6. class HandshakeResponse {
  7. constructor(handshake) {
  8. this.user = handshake.user || '';
  9. this.database = handshake.database || '';
  10. this.password = handshake.password || '';
  11. this.passwordSha1 = handshake.passwordSha1;
  12. this.authPluginData1 = handshake.authPluginData1;
  13. this.authPluginData2 = handshake.authPluginData2;
  14. this.compress = handshake.compress;
  15. this.clientFlags = handshake.flags;
  16. // Accept pre-calculated authToken and authPluginName from caller
  17. // This allows the caller to optimize by using the server's preferred auth method
  18. if (
  19. handshake.authToken !== undefined &&
  20. handshake.authPluginName !== undefined
  21. ) {
  22. // Validate types to fail fast with clear errors
  23. if (!Buffer.isBuffer(handshake.authToken)) {
  24. throw new TypeError(
  25. 'HandshakeResponse authToken must be a Buffer when provided'
  26. );
  27. }
  28. if (typeof handshake.authPluginName !== 'string') {
  29. throw new TypeError(
  30. 'HandshakeResponse authPluginName must be a string when provided'
  31. );
  32. }
  33. this.authToken = handshake.authToken;
  34. this.authPluginName = handshake.authPluginName;
  35. } else {
  36. // Fallback to legacy behavior: calculate mysql_native_password token
  37. // TODO: pre-4.1 auth support
  38. let authToken;
  39. if (this.passwordSha1) {
  40. authToken = auth41.calculateTokenFromPasswordSha(
  41. this.passwordSha1,
  42. this.authPluginData1,
  43. this.authPluginData2
  44. );
  45. } else {
  46. authToken = auth41.calculateToken(
  47. this.password,
  48. this.authPluginData1,
  49. this.authPluginData2
  50. );
  51. }
  52. this.authToken = authToken;
  53. this.authPluginName = 'mysql_native_password';
  54. }
  55. this.charsetNumber = handshake.charsetNumber;
  56. this.encoding = CharsetToEncoding[handshake.charsetNumber];
  57. this.connectAttributes = handshake.connectAttributes;
  58. }
  59. serializeResponse(buffer) {
  60. const isSet = (flag) => this.clientFlags & ClientConstants[flag];
  61. const packet = new Packet(0, buffer, 0, buffer.length);
  62. packet.offset = 4;
  63. packet.writeInt32(this.clientFlags);
  64. packet.writeInt32(0); // max packet size. todo: move to config
  65. packet.writeInt8(this.charsetNumber);
  66. packet.skip(23);
  67. const encoding = this.encoding;
  68. packet.writeNullTerminatedString(this.user, encoding);
  69. let k;
  70. if (isSet('PLUGIN_AUTH_LENENC_CLIENT_DATA')) {
  71. packet.writeLengthCodedNumber(this.authToken.length);
  72. packet.writeBuffer(this.authToken);
  73. } else if (isSet('SECURE_CONNECTION')) {
  74. packet.writeInt8(this.authToken.length);
  75. packet.writeBuffer(this.authToken);
  76. } else {
  77. packet.writeBuffer(this.authToken);
  78. packet.writeInt8(0);
  79. }
  80. if (isSet('CONNECT_WITH_DB')) {
  81. packet.writeNullTerminatedString(this.database, encoding);
  82. }
  83. if (isSet('PLUGIN_AUTH')) {
  84. // Use the auth plugin name specified by the caller (optimized for server's preference)
  85. // or fall back to mysql_native_password for backward compatibility
  86. packet.writeNullTerminatedString(
  87. this.authPluginName || 'mysql_native_password',
  88. 'latin1'
  89. );
  90. }
  91. if (isSet('CONNECT_ATTRS')) {
  92. const connectAttributes = this.connectAttributes || {};
  93. const attrNames = Object.keys(connectAttributes);
  94. let keysLength = 0;
  95. for (k = 0; k < attrNames.length; ++k) {
  96. keysLength += Packet.lengthCodedStringLength(attrNames[k], encoding);
  97. keysLength += Packet.lengthCodedStringLength(
  98. connectAttributes[attrNames[k]],
  99. encoding
  100. );
  101. }
  102. packet.writeLengthCodedNumber(keysLength);
  103. for (k = 0; k < attrNames.length; ++k) {
  104. packet.writeLengthCodedString(attrNames[k], encoding);
  105. packet.writeLengthCodedString(
  106. connectAttributes[attrNames[k]],
  107. encoding
  108. );
  109. }
  110. }
  111. return packet;
  112. }
  113. toPacket() {
  114. if (typeof this.user !== 'string') {
  115. throw new Error('"user" connection config property must be a string');
  116. }
  117. if (typeof this.database !== 'string') {
  118. throw new Error('"database" connection config property must be a string');
  119. }
  120. // dry run: calculate resulting packet length
  121. const p = this.serializeResponse(Packet.MockBuffer());
  122. return this.serializeResponse(Buffer.alloc(p.offset));
  123. }
  124. static fromPacket(packet, serverFlags = 0xffffffff) {
  125. const args = {};
  126. args.clientFlags = packet.readInt32();
  127. function isSet(flag) {
  128. return args.clientFlags & serverFlags & ClientConstants[flag];
  129. }
  130. args.maxPacketSize = packet.readInt32();
  131. args.charsetNumber = packet.readInt8();
  132. const encoding = CharsetToEncoding[args.charsetNumber];
  133. args.encoding = encoding;
  134. packet.skip(23);
  135. args.user = packet.readNullTerminatedString(encoding);
  136. let authTokenLength;
  137. if (isSet('PLUGIN_AUTH_LENENC_CLIENT_DATA')) {
  138. authTokenLength = packet.readLengthCodedNumber(encoding);
  139. args.authToken = packet.readBuffer(authTokenLength);
  140. } else if (isSet('SECURE_CONNECTION')) {
  141. authTokenLength = packet.readInt8();
  142. args.authToken = packet.readBuffer(authTokenLength);
  143. } else {
  144. args.authToken = packet.readNullTerminatedString(encoding);
  145. }
  146. if (isSet('CONNECT_WITH_DB')) {
  147. args.database = packet.readNullTerminatedString(encoding);
  148. }
  149. if (isSet('PLUGIN_AUTH')) {
  150. args.authPluginName = packet.readNullTerminatedString(encoding);
  151. }
  152. if (isSet('CONNECT_ATTRS')) {
  153. const keysLength = packet.readLengthCodedNumber(encoding);
  154. const keysEnd = packet.offset + keysLength;
  155. const attrs = {};
  156. while (packet.offset < keysEnd) {
  157. attrs[packet.readLengthCodedString(encoding)] =
  158. packet.readLengthCodedString(encoding);
  159. }
  160. args.connectAttributes = attrs;
  161. }
  162. return args;
  163. }
  164. }
  165. module.exports = HandshakeResponse;