Skip to content

Commit 3f99e82

Browse files
committed
Add clear text password authentication
1 parent dec4081 commit 3f99e82

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package server
22

33
import (
4+
"bytes"
45
"crypto/rand"
56
"crypto/rsa"
67
"crypto/sha1"
@@ -28,6 +29,9 @@ func (c *Conn) compareAuthData(authPluginName string, clientAuthData []byte) err
2829
}
2930

3031
switch authPluginName {
32+
case mysql.AUTH_CLEAR_PASSWORD:
33+
return c.compareClearPasswordAuthData(clientAuthData, c.credential)
34+
3135
case mysql.AUTH_NATIVE_PASSWORD:
3236
return c.compareNativePasswordAuthData(clientAuthData, c.credential)
3337

@@ -102,6 +106,16 @@ func scrambleValidation(cached, nonce, scramble []byte) bool {
102106
return subtle.ConstantTimeCompare(m, cached) == 1
103107
}
104108

109+
func (c *Conn) compareClearPasswordAuthData(clientAuthData []byte, credential Credential) error {
110+
clearText := bytes.TrimRight(clientAuthData, "\x00")
111+
112+
if bytes.Equal([]byte(credential.Password), clearText) {
113+
return nil
114+
}
115+
116+
return errAccessDenied(credential)
117+
}
118+
105119
func (c *Conn) compareNativePasswordAuthData(clientAuthData []byte, credential Credential) error {
106120
password, err := mysql.DecodePasswordHex(c.credential.Password)
107121
if err != nil {

server/server_conf.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ func NewDefaultServer() *Server {
6363
//
6464
// NOTES:
6565
// You can control the authentication methods and TLS settings here.
66-
// For auth method, you can specify one of the supported methods 'mysql_native_password', 'caching_sha2_password', and 'sha256_password'.
66+
// For auth method, you can specify one of the supported methods 'mysql_clear_password', mysql_native_password', 'caching_sha2_password', and 'sha256_password'.
6767
// The specified auth method will be enforced by the server in the connection phase. That means, client will be asked to switch auth method
6868
// if the supplied auth method is different from the server default.
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.
71+
// WARNING:
72+
// Be carefull using mysql_clear_password since it will transmit the password in clear text
7173
func NewServer(serverVersion string, collationId uint8, defaultAuthMethod string, pubKey []byte, tlsConfig *tls.Config) *Server {
7274
if !isAuthMethodSupported(defaultAuthMethod) {
7375
panic(fmt.Sprintf("server authentication method '%s' is not supported", defaultAuthMethod))
@@ -95,7 +97,7 @@ func NewServer(serverVersion string, collationId uint8, defaultAuthMethod string
9597
}
9698

9799
func isAuthMethodSupported(authMethod string) bool {
98-
return authMethod == mysql.AUTH_NATIVE_PASSWORD || authMethod == mysql.AUTH_CACHING_SHA2_PASSWORD || authMethod == mysql.AUTH_SHA256_PASSWORD
100+
return authMethod == mysql.AUTH_CLEAR_PASSWORD || authMethod == mysql.AUTH_NATIVE_PASSWORD || authMethod == mysql.AUTH_CACHING_SHA2_PASSWORD || authMethod == mysql.AUTH_SHA256_PASSWORD
99101
}
100102

101103
func (s *Server) InvalidateCache(username string, host string) {

server/server_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func prepareServerConf() []*Server {
5454
NewServer("8.0.12", mysql.DEFAULT_COLLATION_ID, mysql.AUTH_CACHING_SHA2_PASSWORD, test_keys.PubPem, tlsConf),
5555
// test auth switch: server permits MYSQL_NATIVE_PASSWORD only but sent different method CACHING_SHA2_PASSWORD in handshake response
5656
NewServer("8.0.12", mysql.DEFAULT_COLLATION_ID, mysql.AUTH_CACHING_SHA2_PASSWORD, test_keys.PubPem, tlsConf),
57+
58+
NewServer("8.0.12", mysql.DEFAULT_COLLATION_ID, mysql.AUTH_CLEAR_PASSWORD, test_keys.PubPem, tlsConf),
5759
}
5860
return servers
5961
}
@@ -115,7 +117,8 @@ func (s *serverTestSuite) SetupSuite() {
115117

116118
time.Sleep(20 * time.Millisecond)
117119

118-
s.db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?tls=%s", *testUser, *testPassword, addr, *testDB, s.tlsPara))
120+
// We allow clear text password to test MYSQL_CLEAR_PASSWORD
121+
s.db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?tls=%s&allowCleartextPasswords=1", *testUser, *testPassword, addr, *testDB, s.tlsPara))
119122
require.NoError(s.T(), err)
120123

121124
s.db.SetMaxIdleConns(4)

0 commit comments

Comments
 (0)