Skip to content

Commit 5c45def

Browse files
committed
Add clear text password authentication
1 parent dec4081 commit 5c45def

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

client/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
const defaultAuthPluginName = mysql.AUTH_NATIVE_PASSWORD
1616

1717
// defines the supported auth plugins
18-
var supportedAuthPlugins = []string{mysql.AUTH_NATIVE_PASSWORD, mysql.AUTH_SHA256_PASSWORD, mysql.AUTH_CACHING_SHA2_PASSWORD, mysql.AUTH_MARIADB_ED25519}
18+
var supportedAuthPlugins = []string{mysql.AUTH_CLEAR_PASSWORD, mysql.AUTH_NATIVE_PASSWORD, mysql.AUTH_SHA256_PASSWORD, mysql.AUTH_CACHING_SHA2_PASSWORD, mysql.AUTH_MARIADB_ED25519}
1919

2020
// helper function to determine what auth methods are allowed by this client
2121
func authPluginAllowed(pluginName string) bool {

server/auth.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ func (c *Conn) compareAuthData(authPluginName string, clientAuthData []byte) err
2727
return c.handleAuthSwitchResponse()
2828
}
2929

30+
if c.authenticationProvider != nil {
31+
handled, err := c.authenticationProvider.Authenticate(authPluginName, clientAuthData)
32+
if handled {
33+
return err
34+
}
35+
}
36+
3037
switch authPluginName {
3138
case mysql.AUTH_NATIVE_PASSWORD:
3239
return c.compareNativePasswordAuthData(clientAuthData, c.credential)

server/authentication_provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package server
2+
3+
type AuthenticationProvider interface{
4+
Authenticate(authPluginName string, clientAuthData []byte) (bool, error)
5+
}

server/conn.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Conn struct {
2929
credential Credential
3030
cachingSha2FullAuth bool
3131

32+
authenticationProvider AuthenticationProvider
33+
3234
h Handler
3335

3436
stmts map[uint32]*Stmt
@@ -68,8 +70,8 @@ func (s *Server) NewConn(conn net.Conn, user string, password string, h Handler)
6870
return s.NewCustomizedConn(conn, p, h)
6971
}
7072

71-
// NewCustomizedConn: create connection with customized server settings
72-
func (s *Server) NewCustomizedConn(conn net.Conn, p CredentialProvider, h Handler) (*Conn, error) {
73+
74+
func (s *Server) NewCustomizedConnWithAuth(conn net.Conn, p CredentialProvider, h Handler, a AuthenticationProvider) (*Conn, error) {
7375
var packetConn *packet.Conn
7476
if s.tlsConfig != nil {
7577
packetConn = packet.NewTLSConn(conn)
@@ -81,6 +83,7 @@ func (s *Server) NewCustomizedConn(conn net.Conn, p CredentialProvider, h Handle
8183
Conn: packetConn,
8284
serverConf: s,
8385
credentialProvider: p,
86+
authenticationProvider: a,
8487
h: h,
8588
connectionID: atomic.AddUint32(&baseConnID, 1),
8689
stmts: make(map[uint32]*Stmt),
@@ -96,6 +99,11 @@ func (s *Server) NewCustomizedConn(conn net.Conn, p CredentialProvider, h Handle
9699
return c, nil
97100
}
98101

102+
// NewCustomizedConn: create connection with customized server settings
103+
func (s *Server) NewCustomizedConn(conn net.Conn, p CredentialProvider, h Handler) (*Conn, error) {
104+
return s.NewCustomizedConnWithAuth(conn, p, h, nil)
105+
}
106+
99107
func (c *Conn) handshake() error {
100108
if err := c.writeInitialHandshake(); err != nil {
101109
return err

server/server_conf.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ func NewDefaultServer() *Server {
6969
// And for TLS support, you can specify self-signed or CA-signed certificates and decide whether the client needs to provide
7070
// a signed or unsigned certificate to provide different level of security.
7171
func NewServer(serverVersion string, collationId uint8, defaultAuthMethod string, pubKey []byte, tlsConfig *tls.Config) *Server {
72-
if !isAuthMethodSupported(defaultAuthMethod) {
72+
return NewServerWrapper(serverVersion, collationId, defaultAuthMethod, pubKey, tlsConfig, true)
73+
}
74+
75+
func NewServerNoAuthMethodCheck(serverVersion string, collationId uint8, defaultAuthMethod string, pubKey []byte, tlsConfig *tls.Config) *Server {
76+
return NewServerWrapper(serverVersion, collationId, defaultAuthMethod, pubKey, tlsConfig, false)
77+
}
78+
79+
func NewServerWrapper(serverVersion string, collationId uint8, defaultAuthMethod string, pubKey []byte, tlsConfig *tls.Config, checkAuthMethod bool) *Server {
80+
if checkAuthMethod && !isAuthMethodSupported(defaultAuthMethod) {
7381
panic(fmt.Sprintf("server authentication method '%s' is not supported", defaultAuthMethod))
7482
}
7583

0 commit comments

Comments
 (0)