@@ -23,7 +23,8 @@ ChangeUser.prototype.start = function(handshakeInitializationPacket) {
23
23
user : this . _user ,
24
24
scrambleBuff : scrambleBuff ,
25
25
database : this . _database ,
26
- charsetNumber : this . _charsetNumber
26
+ charsetNumber : this . _charsetNumber ,
27
+ pluginName : 'mysql_native_password'
27
28
} ) ;
28
29
29
30
this . _currentConfig . user = this . _user ;
@@ -34,8 +35,58 @@ ChangeUser.prototype.start = function(handshakeInitializationPacket) {
34
35
this . emit ( 'packet' , packet ) ;
35
36
} ;
36
37
38
+ ChangeUser . prototype . determinePacket = function ( firstByte ) {
39
+ if ( firstByte === 0xfe ) {
40
+ return Packets . UseOldPasswordPacket ;
41
+ }
42
+
43
+ return undefined ;
44
+ } ;
45
+
37
46
ChangeUser . prototype [ 'ErrorPacket' ] = function ( packet ) {
38
47
var err = this . _packetToError ( packet ) ;
39
48
err . fatal = true ;
40
49
this . end ( err ) ;
41
50
} ;
51
+
52
+ ChangeUser . prototype [ 'UseOldPasswordPacket' ] = function ( packet ) {
53
+ if ( ! packet || ! packet . methodName || packet . methodName === 'mysql_old_password' ) {
54
+ if ( ! this . _currentConfig . insecureAuth ) {
55
+ var err = new Error (
56
+ 'MySQL server is requesting the old and insecure pre-4.1 auth mechanism.' +
57
+ 'Upgrade the user password or use the {insecureAuth: true} option.'
58
+ ) ;
59
+
60
+ err . code = 'HANDSHAKE_INSECURE_AUTH' ;
61
+ err . fatal = true ;
62
+
63
+ this . end ( err ) ;
64
+ return ;
65
+ }
66
+
67
+ this . emit ( 'packet' , new Packets . OldPasswordPacket ( {
68
+ scrambleBuff : Auth . scramble323 ( packet . pluginData || this . _handshakeInitializationPacket . scrambleBuff ( ) , this . _currentConfig . password )
69
+ } ) ) ;
70
+ } else if ( packet . methodName === 'mysql_native_password' ) {
71
+ // "auth plugin data" is documented as "string[EOF]", but MySQL Server will send a
72
+ // null-terminated byte array for mysql_native_password; we only need to hash with
73
+ // the first 20 bytes
74
+ var challenge = packet . pluginData ;
75
+ if ( challenge . length === 21 && challenge [ 20 ] === 0 ) {
76
+ challenge = challenge . slice ( 0 , 20 ) ;
77
+ }
78
+
79
+ this . emit ( 'packet' , new Packets . AuthenticationSwitchResponsePacket ( {
80
+ scrambleBuff : Auth . token ( this . _currentConfig . password , challenge )
81
+ } ) ) ;
82
+ } else {
83
+ var err = new Error (
84
+ 'MySQL is requesting the ' + packet . methodName + ' authentication method, which is not supported.'
85
+ ) ;
86
+
87
+ err . code = 'UNSUPPORTED_AUTH_METHOD' ;
88
+ err . fatal = true ;
89
+
90
+ this . end ( err ) ;
91
+ }
92
+ } ;
0 commit comments