Skip to content

Commit f4f55b1

Browse files
committed
fix auth switch
1 parent 1540102 commit f4f55b1

File tree

3 files changed

+37
-52
lines changed

3 files changed

+37
-52
lines changed

const.go

-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ const (
167167
)
168168

169169
const (
170-
cachingSha2PasswordOK = 0
171170
cachingSha2PasswordRequestPublicKey = 2
172171
cachingSha2PasswordFastAuthSuccess = 3
173172
cachingSha2PasswordPerformFullAuthentication = 4

driver.go

+34-31
Original file line numberDiff line numberDiff line change
@@ -154,44 +154,47 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
154154
}
155155

156156
func handleAuthResult(mc *mysqlConn, oldCipher []byte, pluginName string) error {
157-
158-
// handle caching_sha2_password
159-
if pluginName == "caching_sha2_password" {
160-
auth, err := mc.readCachingSha2PasswordAuthResult()
161-
if err != nil {
162-
return err
163-
}
164-
165-
switch auth {
166-
case cachingSha2PasswordOK:
167-
return nil // auth successful
168-
169-
case cachingSha2PasswordFastAuthSuccess:
170-
// continue to read result packet...
171-
172-
case cachingSha2PasswordPerformFullAuthentication:
173-
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
174-
if err = mc.writeClearAuthPacket(); err != nil {
175-
return err
157+
// Read Result Packet
158+
cipher, err := mc.readResultOK()
159+
if err == nil {
160+
// handle caching_sha2_password
161+
// https://insidemysql.com/preparing-your-community-connector-for-mysql-8-part-2-sha256/
162+
if pluginName == "caching_sha2_password" {
163+
if len(cipher) == 1 {
164+
switch cipher[0] {
165+
case cachingSha2PasswordFastAuthSuccess:
166+
cipher, err = mc.readResultOK()
167+
if err == nil {
168+
return nil // auth successful
169+
}
170+
171+
case cachingSha2PasswordPerformFullAuthentication:
172+
if mc.cfg.tls != nil || mc.cfg.Net == "unix" {
173+
if err = mc.writeClearAuthPacket(); err != nil {
174+
return err
175+
}
176+
} else {
177+
if err = mc.writePublicKeyAuthPacket(oldCipher); err != nil {
178+
return err
179+
}
180+
}
181+
cipher, err = mc.readResultOK()
182+
if err == nil {
183+
return nil // auth successful
184+
}
185+
186+
default:
187+
return ErrMalformPkt
176188
}
177189
} else {
178-
if err = mc.writePublicKeyAuthPacket(oldCipher); err != nil {
179-
return err
180-
}
190+
return ErrMalformPkt
181191
}
182-
// continue to read result packet...
183192

184-
default:
185-
return ErrMalformPkt
193+
} else {
194+
return nil // auth successful
186195
}
187196
}
188197

189-
// Read Result Packet
190-
cipher, err := mc.readResultOK()
191-
if err == nil {
192-
return nil // auth successful
193-
}
194-
195198
if mc.cfg == nil {
196199
return err // auth failed and retry not possible
197200
}

packets.go

+3-20
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,9 @@ func (mc *mysqlConn) readResultOK() ([]byte, error) {
581581
case iOK:
582582
return nil, mc.handleOkPacket(data)
583583

584+
case iAuthMoreData:
585+
return data[1:], nil
586+
584587
case iEOF:
585588
return readAuthSwitch(data)
586589

@@ -589,26 +592,6 @@ func (mc *mysqlConn) readResultOK() ([]byte, error) {
589592
}
590593
}
591594

592-
// https://insidemysql.com/preparing-your-community-connector-for-mysql-8-part-2-sha256/
593-
func (mc *mysqlConn) readCachingSha2PasswordAuthResult() (int, error) {
594-
data, err := mc.readPacket()
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)
609-
}
610-
}
611-
612595
// Result Set Header Packet
613596
// http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::Resultset
614597
func (mc *mysqlConn) readResultSetHeaderPacket() (int, error) {

0 commit comments

Comments
 (0)