-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for authentication plugins.
- Loading branch information
1 parent
2e00b5c
commit 030d761
Showing
5 changed files
with
270 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package mysql | ||
|
||
import "bytes" | ||
|
||
const mysqlClearPassword = "mysql_clear_password" | ||
const mysqlNativePassword = "mysql_native_password" | ||
const mysqlOldPassword = "mysql_old_password" | ||
const defaultAuthPluginName = mysqlNativePassword | ||
|
||
var authPluginFactories map[string]func(*Config) AuthPlugin | ||
|
||
func init() { | ||
authPluginFactories = make(map[string]func(*Config) AuthPlugin) | ||
authPluginFactories[mysqlClearPassword] = func(cfg *Config) AuthPlugin { | ||
return &clearTextPlugin{cfg} | ||
} | ||
authPluginFactories[mysqlNativePassword] = func(cfg *Config) AuthPlugin { | ||
return &nativePasswordPlugin{cfg} | ||
} | ||
authPluginFactories[mysqlOldPassword] = func(cfg *Config) AuthPlugin { | ||
return &oldPasswordPlugin{cfg} | ||
} | ||
} | ||
|
||
// RegisterAuthPlugin registers an authentication plugin to be used during | ||
// negotiation with the server. If a plugin with the given name already exists, | ||
// it will be overwritten. | ||
func RegisterAuthPlugin(name string, factory func(*Config) AuthPlugin) { | ||
authPluginFactories[name] = factory | ||
} | ||
|
||
// AuthPlugin handles authenticating a user. | ||
type AuthPlugin interface { | ||
// Next takes a server's challenge and returns | ||
// the bytes to send back or an error. | ||
Next(challenge []byte) ([]byte, error) | ||
} | ||
|
||
type clearTextPlugin struct { | ||
cfg *Config | ||
} | ||
|
||
func (p *clearTextPlugin) Next(challenge []byte) ([]byte, error) { | ||
if !p.cfg.AllowCleartextPasswords { | ||
return nil, ErrCleartextPassword | ||
} | ||
|
||
// \0-terminated | ||
return append([]byte(p.cfg.Passwd), 0), nil | ||
} | ||
|
||
type nativePasswordPlugin struct { | ||
cfg *Config | ||
} | ||
|
||
func (p *nativePasswordPlugin) Next(challenge []byte) ([]byte, error) { | ||
// NOTE: this seems to always be disabled... | ||
// if !p.cfg.AllowNativePasswords { | ||
// return nil, ErrNativePassword | ||
// } | ||
|
||
return scramblePassword(challenge, []byte(p.cfg.Passwd)), nil | ||
} | ||
|
||
type oldPasswordPlugin struct { | ||
cfg *Config | ||
} | ||
|
||
func (p *oldPasswordPlugin) Next(challenge []byte) ([]byte, error) { | ||
if !p.cfg.AllowOldPasswords { | ||
return nil, ErrOldPassword | ||
} | ||
|
||
// \0-terminated | ||
return append(scrambleOldPassword(challenge, []byte(p.cfg.Passwd)), 0), nil | ||
} | ||
|
||
func handleAuthResult(mc *mysqlConn, plugin AuthPlugin, oldCipher []byte) error { | ||
data, err := mc.readPacket() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var authData []byte | ||
|
||
// packet indicator | ||
switch data[0] { | ||
case iOK: | ||
return mc.handleOkPacket(data) | ||
|
||
case iEOF: // auth switch | ||
if len(data) > 1 { | ||
pluginEndIndex := bytes.IndexByte(data, 0x00) | ||
pluginName := string(data[1:pluginEndIndex]) | ||
if apf, ok := authPluginFactories[pluginName]; ok { | ||
plugin = apf(mc.cfg) | ||
} else { | ||
return ErrUnknownPlugin | ||
} | ||
|
||
if len(data) > pluginEndIndex+1 { | ||
authData = data[pluginEndIndex+1 : len(data)-1] | ||
} | ||
} else { | ||
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::OldAuthSwitchRequest | ||
plugin = authPluginFactories[mysqlOldPassword](mc.cfg) | ||
authData = oldCipher | ||
} | ||
case iAuthContinue: | ||
// continue packet for a plugin. | ||
authData = data[1:] // strip off the continue flag | ||
default: // Error otherwise | ||
return mc.handleErrorPacket(data) | ||
} | ||
|
||
authData, err = plugin.Next(authData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = mc.writeAuthDataPacket(authData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return handleAuthResult(mc, plugin, authData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package mysql | ||
|
||
import "testing" | ||
import "bytes" | ||
|
||
func TestAuthPlugin_Cleartext(t *testing.T) { | ||
cfg := &Config{ | ||
Passwd: "funny", | ||
} | ||
|
||
plugin := authPluginFactories[mysqlClearPassword](cfg) | ||
|
||
_, err := plugin.Next(nil) | ||
if err == nil { | ||
t.Fatalf("expected error when AllowCleartextPasswords is false") | ||
} | ||
|
||
cfg.AllowCleartextPasswords = true | ||
|
||
actual, err := plugin.Next(nil) | ||
if err != nil { | ||
t.Fatalf("expected no error but got: %s", err) | ||
} | ||
|
||
expected := append([]byte("funny"), 0) | ||
if bytes.Compare(actual, expected) != 0 { | ||
t.Fatalf("expected data to be %v, but got: %v", expected, actual) | ||
} | ||
} | ||
|
||
func TestAuthPlugin_NativePassword(t *testing.T) { | ||
cfg := &Config{ | ||
Passwd: "pass ", | ||
} | ||
|
||
plugin := authPluginFactories[mysqlNativePassword](cfg) | ||
|
||
actual, err := plugin.Next([]byte{9, 8, 7, 6, 5, 4, 3, 2}) | ||
if err != nil { | ||
t.Fatalf("expected no error but got: %s", err) | ||
} | ||
|
||
expected := []byte{195, 146, 3, 213, 111, 95, 252, 192, 97, 226, 173, 176, 91, 175, 131, 138, 89, 45, 75, 179} | ||
if bytes.Compare(actual, expected) != 0 { | ||
t.Fatalf("expected data to be %v, but got: %v", expected, actual) | ||
} | ||
} | ||
|
||
func TestAuthPlugin_OldPassword(t *testing.T) { | ||
cfg := &Config{ | ||
Passwd: "pass ", | ||
} | ||
|
||
plugin := authPluginFactories[mysqlOldPassword](cfg) | ||
|
||
_, err := plugin.Next(nil) | ||
if err == nil { | ||
t.Fatalf("expected error when AllowOldPasswords is false") | ||
} | ||
|
||
cfg.AllowOldPasswords = true | ||
|
||
actual, err := plugin.Next([]byte{9, 8, 7, 6, 5, 4, 3, 2}) | ||
if err != nil { | ||
t.Fatalf("expected no error but got: %s", err) | ||
} | ||
|
||
expected := []byte{71, 87, 92, 90, 67, 91, 66, 81, 0} | ||
if bytes.Compare(actual, expected) != 0 { | ||
t.Fatalf("expected data to be %v, but got: %v", expected, actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.