Bladeren bron

Got native auth working!!!!

Josh Brickner 7 jaren geleden
bovenliggende
commit
b37bf880b3
4 gewijzigde bestanden met toevoegingen van 41 en 23 verwijderingen
  1. 16 8
      binlog/authentication.go
  2. 18 13
      binlog/connection.go
  3. 6 1
      binlog/handshake.go
  4. 1 1
      config.json

+ 16 - 8
binlog/authentication.go

@@ -7,7 +7,7 @@ import (
 	"fmt"
 )
 
-type AuthResponse struct {
+type AuthResponsePacket struct {
 	PacketLength   uint64
 	SequenceID     uint64
 	Status         uint64
@@ -15,8 +15,8 @@ type AuthResponse struct {
 	AuthPluginData *bytes.Buffer
 }
 
-func (c *Conn) decodeAuthResponsePacket() (*AuthResponse, error) {
-	packet := AuthResponse{}
+func (c *Conn) decodeAuthResponsePacket() (*AuthResponsePacket, error) {
+	packet := AuthResponsePacket{}
 
 	packet.PacketLength = c.getInt(TypeFixedInt, 3)
 	packet.SequenceID = c.getInt(TypeFixedInt, 1)
@@ -32,23 +32,31 @@ func (c *Conn) decodeAuthResponsePacket() (*AuthResponse, error) {
 	return &packet, err
 }
 
-func (c *Conn) writeAuthSwitchPacket() {
+func (c *Conn) writeAuthSwitchPacket(ap *AuthResponsePacket) error {
+	salt := ap.AuthPluginData.Bytes()
+	password := []byte(c.HandshakeResponse.AuthResponse)
+	c.authenticate(salt, password)
 
+	if c.Flush() != nil {
+		return c.Flush()
+	}
+
+	return nil
 }
 
-func (c *Conn) authenticate(hr *HandshakeResponse) {
+func (c *Conn) authenticate(salt []byte, password []byte) {
 	var ar []byte
-	salt := append(c.Handshake.AuthPluginDataPart1.Bytes(), c.Handshake.AuthPluginDataPart2.Bytes()...)
-	password := []byte(hr.AuthResponse)
-	fmt.Println(hr.AuthResponse)
 
+	salt = salt[:20] // trim null byte from end.
 	switch c.Handshake.AuthPluginName {
 	case "mysql_native_password":
 		ar = c.nativeSha1Auth(salt, password)
 	case "caching_sha2_password":
+		fmt.Println(len(salt))
 		ar = c.cachingSha2Auth(salt, password)
 	}
 
+	hr := c.HandshakeResponse
 	hr.AuthResponseLength = uint64(len(ar))
 	if hr.ClientFlag.PluginAuthLenEncClientData {
 		c.putInt(TypeLenEncInt, hr.AuthResponseLength, 0)

+ 18 - 13
binlog/connection.go

@@ -68,14 +68,15 @@ func newBinlogConfig(dsn string) (*Config, error) {
 }
 
 type Conn struct {
-	Config     *Config
-	tcpConn    *net.TCPConn
-	Handshake  *Handshake
-	buffer     *bufio.ReadWriter
-	scanner    *bufio.Scanner
-	err        error
-	sequenceId uint64
-	writeBuf   *bytes.Buffer
+	Config            *Config
+	tcpConn           *net.TCPConn
+	Handshake         *Handshake
+	HandshakeResponse *HandshakeResponse
+	buffer            *bufio.ReadWriter
+	scanner           *bufio.Scanner
+	err               error
+	sequenceId        uint64
+	writeBuf          *bytes.Buffer
 }
 
 func newBinlogConn(config *Config) Conn {
@@ -131,12 +132,16 @@ func (d Driver) Open(dsn string) (driver.Conn, error) {
 		return nil, err
 	}
 
-	packet, err := c.decodeAuthResponsePacket()
-	if err != nil {
-		return nil, err
-	}
+	// _, err = c.decodeAuthResponsePacket()
+	// if err != nil {
+	// 	return nil, err
+	// }
+
+	// 	err = c.writeAuthSwitchPacket(packet)
+	// 	if err != nil {
+	// 		return nil, err
+	// 	}
 
-	fmt.Printf("%+v\n", packet)
 	return c, err
 }
 

+ 6 - 1
binlog/handshake.go

@@ -128,6 +128,7 @@ func (c *Conn) decodeHandshakePacket() error {
 
 func (c *Conn) writeHandshakeResponse() error {
 	hr := c.NewHandshakeResponse()
+	c.HandshakeResponse = hr
 	cf := c.structToBitmask(hr.ClientFlag)
 	c.putBytes(cf)
 	c.putInt(TypeFixedInt, hr.MaxPacketSize, 4)
@@ -136,7 +137,9 @@ func (c *Conn) writeHandshakeResponse() error {
 	c.putString(TypeNullTerminatedString, hr.Username)
 
 	// Perform authentication
-	c.authenticate(hr)
+	salt := append(c.Handshake.AuthPluginDataPart1.Bytes(), c.Handshake.AuthPluginDataPart2.Bytes()...)
+	password := []byte(hr.AuthResponse)
+	c.authenticate(salt, password)
 
 	// Write database name
 	if hr.ClientFlag.ConnectWithDB {
@@ -154,6 +157,8 @@ func (c *Conn) writeHandshakeResponse() error {
 	// Write auth plugin
 	if hr.ClientFlag.PluginAuth {
 		c.putString(t, hr.ClientPluginName)
+
+		c.putNullBytes(1)
 	}
 
 	fmt.Printf("%+v\n", hr)

+ 1 - 1
config.json

@@ -1,6 +1,6 @@
 {
   "host": "127.0.0.1",
-  "port": 3317,
+  "port": 3306,
   "user": "root",
   "password": "root",
   "database": "information_schema",