Skip to content

Commit

Permalink
auth: support custom user password
Browse files Browse the repository at this point in the history
  • Loading branch information
obaranov307 committed Dec 18, 2018
1 parent 319e088 commit 27c5978
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net"

"github.com/juju/errors"
. "github.com/siddontang/go-mysql/mysql"
. "github.com/space307/go-mysql/mysql"
)

/*
Expand Down
11 changes: 7 additions & 4 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/binary"

. "github.com/siddontang/go-mysql/mysql"
. "github.com/space307/go-mysql/mysql"
)

func (c *Conn) writeInitialHandshake() error {
Expand Down Expand Up @@ -58,7 +58,7 @@ func (c *Conn) writeInitialHandshake() error {
return c.WritePacket(data)
}

func (c *Conn) readHandshakeResponse(password string) error {
func (c *Conn) readHandshakeResponse(authenticator Authentificator) error {
data, err := c.ReadPacket()

if err != nil {
Expand All @@ -85,8 +85,9 @@ func (c *Conn) readHandshakeResponse(password string) error {
user := string(data[pos : pos+bytes.IndexByte(data[pos:], 0)])
pos += len(user) + 1

if c.user != user {
return NewDefaultError(ER_NO_SUCH_USER, user, c.RemoteAddr().String())
password, err := authenticator.UserPass(user)
if err != nil {
return err
}

//auth length and auth
Expand All @@ -100,6 +101,8 @@ func (c *Conn) readHandshakeResponse(password string) error {
return NewDefaultError(ER_ACCESS_DENIED_ERROR, c.RemoteAddr().String(), c.user, "Yes")
}

c.user = user

pos += authLen

if c.capability|CLIENT_CONNECT_WITH_DB > 0 {
Expand Down
2 changes: 1 addition & 1 deletion server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"bytes"
"fmt"

. "github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go/hack"
. "github.com/space307/go-mysql/mysql"
)

type Handler interface {
Expand Down
19 changes: 11 additions & 8 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import (
"net"
"sync/atomic"

. "github.com/siddontang/go-mysql/mysql"
"github.com/siddontang/go-mysql/packet"
"github.com/siddontang/go/sync2"
. "github.com/space307/go-mysql/mysql"
"github.com/space307/go-mysql/packet"
)

type Authentificator interface {
UserPass(string) (string, error)
}

/*
Conn acts like a MySQL server connection, you can use MySQL client to communicate with it.
*/
Expand All @@ -35,12 +39,11 @@ type Conn struct {

var baseConnID uint32 = 10000

func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, error) {
func NewConn(conn net.Conn, auth Authentificator, h Handler) (*Conn, error) {
c := new(Conn)

c.h = h

c.user = user

c.Conn = packet.NewConn(conn)

c.connectionID = atomic.AddUint32(&baseConnID, 1)
Expand All @@ -51,20 +54,20 @@ func NewConn(conn net.Conn, user string, password string, h Handler) (*Conn, err

c.closed.Set(false)

if err := c.handshake(password); err != nil {
if err := c.handshake(auth); err != nil {
c.Close()
return nil, err
}

return c, nil
}

func (c *Conn) handshake(password string) error {
func (c *Conn) handshake(auth Authentificator) error {
if err := c.writeInitialHandshake(); err != nil {
return err
}

if err := c.readHandshakeResponse(password); err != nil {
if err := c.readHandshakeResponse(auth); err != nil {
c.writeError(err)

return err
Expand Down
2 changes: 1 addition & 1 deletion server/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package server
import (
"fmt"

. "github.com/siddontang/go-mysql/mysql"
. "github.com/space307/go-mysql/mysql"
)

func (c *Conn) writeOK(r *Result) error {
Expand Down
2 changes: 1 addition & 1 deletion server/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"

"github.com/juju/errors"
. "github.com/siddontang/go-mysql/mysql"
. "github.com/space307/go-mysql/mysql"
)

var paramFieldData []byte
Expand Down

0 comments on commit 27c5978

Please sign in to comment.