@@ -25,7 +25,7 @@ Handshake.prototype.determinePacket = function(firstByte) {
25
25
}
26
26
27
27
if ( firstByte === 0xfe ) {
28
- return Packets . UseOldPasswordPacket ;
28
+ return Packets . AuthenticationMethodSwitchRequestPacket ;
29
29
}
30
30
31
31
return undefined ;
@@ -80,23 +80,46 @@ Handshake.prototype._sendCredentials = function() {
80
80
} ) ) ;
81
81
} ;
82
82
83
- Handshake . prototype [ 'UseOldPasswordPacket' ] = function ( ) {
84
- if ( ! this . _config . insecureAuth ) {
83
+ Handshake . prototype [ 'AuthenticationMethodSwitchRequestPacket' ] = function ( packet ) {
84
+ if ( packet . authMethodName === 'mysql_native_password' ) {
85
+ // "auth plugin data" is documented as "string[EOF]", but MySQL Server will send a
86
+ // null-terminated byte array for mysql_native_password; we only need to hash with
87
+ // the first 20 bytes
88
+ var challenge = packet . authPluginData ;
89
+ if ( challenge . length === 21 ) {
90
+ challenge = challenge . slice ( 0 , 20 ) ;
91
+ }
92
+
93
+ this . emit ( 'packet' , new Packets . AuthenticationSwitchResponsePacket ( {
94
+ scrambleBuff : Auth . token ( this . _config . password , challenge )
95
+ } ) ) ;
96
+ } else if ( packet . authMethodName === 'mysql_old_password' ) {
97
+ if ( ! this . _config . insecureAuth ) {
98
+ var err = new Error (
99
+ 'MySQL server is requesting the old and insecure pre-4.1 auth mechanism.' +
100
+ 'Upgrade the user password or use the {insecureAuth: true} option.'
101
+ ) ;
102
+
103
+ err . code = 'HANDSHAKE_INSECURE_AUTH' ;
104
+ err . fatal = true ;
105
+
106
+ this . end ( err ) ;
107
+ return ;
108
+ }
109
+
110
+ this . emit ( 'packet' , new Packets . OldPasswordPacket ( {
111
+ scrambleBuff : Auth . scramble323 ( this . _handshakeInitializationPacket . scrambleBuff ( ) , this . _config . password )
112
+ } ) ) ;
113
+ } else {
85
114
var err = new Error (
86
- 'MySQL server is requesting the old and insecure pre-4.1 auth mechanism.' +
87
- 'Upgrade the user password or use the {insecureAuth: true} option.'
115
+ 'MySQL is requesting the ' + packet . authMethodName + ' authentication method, which is not supported.'
88
116
) ;
89
117
90
- err . code = 'HANDSHAKE_INSECURE_AUTH ' ;
118
+ err . code = 'UNSUPPORTED_AUTH_METHOD ' ;
91
119
err . fatal = true ;
92
120
93
121
this . end ( err ) ;
94
- return ;
95
122
}
96
-
97
- this . emit ( 'packet' , new Packets . OldPasswordPacket ( {
98
- scrambleBuff : Auth . scramble323 ( this . _handshakeInitializationPacket . scrambleBuff ( ) , this . _config . password )
99
- } ) ) ;
100
123
} ;
101
124
102
125
Handshake . prototype [ 'ErrorPacket' ] = function ( packet ) {
0 commit comments