@@ -203,7 +203,7 @@ func (mc *mysqlConn) readInitPacket() ([]byte, string, error) {
203
203
}
204
204
pos += 2
205
205
206
- pluginName := ""
206
+ pluginName := "mysql_native_password "
207
207
if len (data ) > pos {
208
208
// character set [1 byte]
209
209
// status flags [2 bytes]
@@ -367,7 +367,6 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte, pluginName string) error {
367
367
pos ++
368
368
}
369
369
370
- // Assume native client during response
371
370
pos += copy (data [pos :], pluginName )
372
371
data [pos ] = 0x00
373
372
@@ -544,55 +543,70 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
544
543
* Result Packets *
545
544
******************************************************************************/
546
545
546
+ func readAuthSwitch (data []byte ) ([]byte , error ) {
547
+ if len (data ) > 1 {
548
+ pluginEndIndex := bytes .IndexByte (data , 0x00 )
549
+ plugin := string (data [1 :pluginEndIndex ])
550
+ cipher := data [pluginEndIndex + 1 :]
551
+
552
+ switch plugin {
553
+ case "mysql_old_password" :
554
+ // using old_passwords
555
+ return cipher , ErrOldPassword
556
+ case "mysql_clear_password" :
557
+ // using clear text password
558
+ return cipher , ErrCleartextPassword
559
+ case "mysql_native_password" :
560
+ // using mysql default authentication method
561
+ return cipher , ErrNativePassword
562
+ default :
563
+ return cipher , ErrUnknownPlugin
564
+ }
565
+ }
566
+
567
+ // https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::OldAuthSwitchRequest
568
+ return nil , ErrOldPassword
569
+ }
570
+
547
571
// Returns error if Packet is not an 'Result OK'-Packet
548
572
func (mc * mysqlConn ) readResultOK () ([]byte , error ) {
549
573
data , err := mc .readPacket ()
550
- if err = = nil {
551
- // packet indicator
552
- switch data [ 0 ] {
574
+ if err ! = nil {
575
+ return nil , err
576
+ }
553
577
554
- case iOK :
555
- return nil , mc . handleOkPacket ( data )
578
+ // packet indicator
579
+ switch data [ 0 ] {
556
580
557
- case iEOF :
558
- if len (data ) > 1 {
559
- pluginEndIndex := bytes .IndexByte (data , 0x00 )
560
- plugin := string (data [1 :pluginEndIndex ])
561
- cipher := data [pluginEndIndex + 1 :]
562
-
563
- switch plugin {
564
- case "mysql_old_password" :
565
- // using old_passwords
566
- return cipher , ErrOldPassword
567
- case "mysql_clear_password" :
568
- // using clear text password
569
- return cipher , ErrCleartextPassword
570
- case "mysql_native_password" :
571
- // using mysql default authentication method
572
- return cipher , ErrNativePassword
573
- default :
574
- return cipher , ErrUnknownPlugin
575
- }
576
- }
581
+ case iOK :
582
+ return nil , mc .handleOkPacket (data )
577
583
578
- // https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::OldAuthSwitchRequest
579
- return nil , ErrOldPassword
584
+ case iEOF :
585
+ return readAuthSwitch ( data )
580
586
581
- default : // Error otherwise
582
- return nil , mc .handleErrorPacket (data )
583
- }
587
+ default : // Error otherwise
588
+ return nil , mc .handleErrorPacket (data )
584
589
}
585
- return nil , err
586
590
}
587
591
592
+ // https://insidemysql.com/preparing-your-community-connector-for-mysql-8-part-2-sha256/
588
593
func (mc * mysqlConn ) readCachingSha2PasswordAuthResult () (int , error ) {
589
594
data , err := mc .readPacket ()
590
- if err == nil {
591
- if data [0 ] != 1 {
592
- return 0 , ErrMalformPkt
593
- }
595
+ if err != nil {
596
+ return 0 , err
597
+ }
598
+
599
+ // packet indicator
600
+ switch data [0 ] {
601
+ case iOK :
602
+ return 0 , nil
603
+
604
+ case iAuthMoreData :
605
+ return int (data [1 ]), nil
606
+
607
+ default : // Error otherwise
608
+ return 0 , mc .handleErrorPacket (data )
594
609
}
595
- return int (data [1 ]), err
596
610
}
597
611
598
612
// Result Set Header Packet
0 commit comments