77
88/**
99 * @internal
10- * @link https://dev.mysql.com/doc/internals/en/connection-phase-packets .html#packet-Protocol::HandshakeResponse
10+ * @link https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_connection_phase_packets_protocol_handshake_response .html#sect_protocol_connection_phase_packets_protocol_handshake_response41
1111 */
1212class AuthenticateCommand extends AbstractCommand
1313{
@@ -73,8 +73,19 @@ public function getId()
7373 return 0 ;
7474 }
7575
76- public function authenticatePacket ($ scramble , Buffer $ buffer )
76+ /**
77+ * @param string $scramble
78+ * @param ?string $authPlugin
79+ * @param Buffer $buffer
80+ * @return string
81+ * @throws \UnexpectedValueException for unsupported authentication plugin
82+ */
83+ public function authenticatePacket ($ scramble , $ authPlugin , Buffer $ buffer )
7784 {
85+ if ($ authPlugin !== null && $ authPlugin !== 'mysql_native_password ' ) {
86+ throw new \UnexpectedValueException ('Unknown authentication plugin " ' . addslashes ($ authPlugin ) . '" requested by server ' );
87+ }
88+
7889 $ clientFlags = Constants::CLIENT_LONG_PASSWORD |
7990 Constants::CLIENT_LONG_FLAG |
8091 Constants::CLIENT_LOCAL_FILES |
@@ -84,20 +95,28 @@ public function authenticatePacket($scramble, Buffer $buffer)
8495 Constants::CLIENT_SECURE_CONNECTION |
8596 Constants::CLIENT_CONNECT_WITH_DB ;
8697
98+ if ($ authPlugin !== null ) {
99+ $ clientFlags |= Constants::CLIENT_PLUGIN_AUTH ;
100+ }
101+
87102 return pack ('VVc ' , $ clientFlags , $ this ->maxPacketSize , $ this ->charsetNumber )
88103 . "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
89104 . $ this ->user . "\x00"
90- . $ this ->getAuthToken ($ scramble , $ this ->passwd , $ buffer )
91- . $ this ->dbname . "\x00" ;
105+ . $ buffer ->buildStringLen ($ this ->authMysqlNativePassword ($ scramble ))
106+ . $ this ->dbname . "\x00"
107+ . ($ authPlugin !== null ? $ authPlugin . "\0" : '' );
92108 }
93109
94- public function getAuthToken ($ scramble , $ password , Buffer $ buffer )
110+ /**
111+ * @param string $scramble
112+ * @return string
113+ */
114+ private function authMysqlNativePassword ($ scramble )
95115 {
96- if ($ password === '' ) {
97- return "\x00" ;
116+ if ($ this -> passwd === '' ) {
117+ return '' ;
98118 }
99- $ token = \sha1 ($ scramble . \sha1 ($ hash1 = \sha1 ($ password , true ), true ), true ) ^ $ hash1 ;
100119
101- return $ buffer -> buildStringLen ( $ token ) ;
120+ return \sha1 ( $ scramble . \sha1 ( $ hash1 = \sha1 ( $ this -> passwd , true ), true ), true ) ^ $ hash1 ;
102121 }
103122}
0 commit comments