|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "github.com/go-mysql-org/go-mysql/mysql" |
| 7 | + "github.com/pingcap/errors" |
| 8 | + "github.com/pingcap/tidb/pkg/parser/auth" |
| 9 | +) |
| 10 | + |
| 11 | +// AuthenticationHandler provides user credentials and authentication lifecycle hooks. |
| 12 | +// |
| 13 | +// # Important Note |
| 14 | +// |
| 15 | +// if the password in a third-party auth handler could be updated at runtime, we have to invalidate the caching |
| 16 | +// for 'caching_sha2_password' by calling 'func (s *Server)InvalidateCache(string, string)'. |
| 17 | +type AuthenticationHandler interface { |
| 18 | + // get user credential (supports multiple valid passwords per user) |
| 19 | + GetCredential(username string) (credential Credential, found bool, err error) |
| 20 | + |
| 21 | + // OnAuthSuccess is called after successful authentication, before the OK packet. |
| 22 | + // Return an error to reject the connection (error will be sent to client instead of OK). |
| 23 | + // Return nil to proceed with sending the OK packet. |
| 24 | + OnAuthSuccess(conn *Conn) error |
| 25 | + |
| 26 | + // OnAuthFailure is called after authentication fails, before the error packet. |
| 27 | + // This is informational only - the connection will be closed regardless. |
| 28 | + OnAuthFailure(conn *Conn, err error) |
| 29 | +} |
| 30 | + |
| 31 | +func NewInMemoryAuthenticationHandler(defaultAuthMethod ...string) *InMemoryAuthenticationHandler { |
| 32 | + d := mysql.AUTH_CACHING_SHA2_PASSWORD |
| 33 | + if len(defaultAuthMethod) > 0 { |
| 34 | + d = defaultAuthMethod[0] |
| 35 | + } |
| 36 | + return &InMemoryAuthenticationHandler{ |
| 37 | + userPool: sync.Map{}, |
| 38 | + defaultAuthMethod: d, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type Credential struct { |
| 43 | + Passwords []string // raw passwords, hashed on demand during comparison |
| 44 | + AuthPluginName string |
| 45 | +} |
| 46 | + |
| 47 | +// HashPassword computes the password hash for a given password using the credential's auth plugin. |
| 48 | +func (c Credential) HashPassword(password string) (string, error) { |
| 49 | + if password == "" { |
| 50 | + return "", nil |
| 51 | + } |
| 52 | + |
| 53 | + switch c.AuthPluginName { |
| 54 | + case mysql.AUTH_NATIVE_PASSWORD: |
| 55 | + return mysql.EncodePasswordHex(mysql.NativePasswordHash([]byte(password))), nil |
| 56 | + |
| 57 | + case mysql.AUTH_CACHING_SHA2_PASSWORD: |
| 58 | + return auth.NewHashPassword(password, mysql.AUTH_CACHING_SHA2_PASSWORD), nil |
| 59 | + |
| 60 | + case mysql.AUTH_SHA256_PASSWORD: |
| 61 | + return mysql.NewSha256PasswordHash(password) |
| 62 | + |
| 63 | + case mysql.AUTH_CLEAR_PASSWORD: |
| 64 | + return password, nil |
| 65 | + |
| 66 | + default: |
| 67 | + return "", errors.Errorf("unknown authentication plugin name '%s'", c.AuthPluginName) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// HasEmptyPassword returns true if any password in the credential is empty. |
| 72 | +func (c Credential) HasEmptyPassword() bool { |
| 73 | + for _, p := range c.Passwords { |
| 74 | + if p == "" { |
| 75 | + return true |
| 76 | + } |
| 77 | + } |
| 78 | + return false |
| 79 | +} |
| 80 | + |
| 81 | +// InMemoryAuthenticationHandler implements AuthenticationHandler with in-memory credential storage. |
| 82 | +type InMemoryAuthenticationHandler struct { |
| 83 | + userPool sync.Map // username -> Credential |
| 84 | + defaultAuthMethod string |
| 85 | +} |
| 86 | + |
| 87 | +func (h *InMemoryAuthenticationHandler) CheckUsername(username string) (found bool, err error) { |
| 88 | + _, ok := h.userPool.Load(username) |
| 89 | + return ok, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (h *InMemoryAuthenticationHandler) GetCredential(username string) (credential Credential, found bool, err error) { |
| 93 | + v, ok := h.userPool.Load(username) |
| 94 | + if !ok { |
| 95 | + return Credential{}, false, nil |
| 96 | + } |
| 97 | + c, valid := v.(Credential) |
| 98 | + if !valid { |
| 99 | + return Credential{}, true, errors.Errorf("invalid credential") |
| 100 | + } |
| 101 | + return c, true, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (h *InMemoryAuthenticationHandler) AddUser(username, password string, optionalAuthPluginName ...string) error { |
| 105 | + authPluginName := h.defaultAuthMethod |
| 106 | + if len(optionalAuthPluginName) > 0 { |
| 107 | + authPluginName = optionalAuthPluginName[0] |
| 108 | + } |
| 109 | + |
| 110 | + if !isAuthMethodSupported(authPluginName) { |
| 111 | + return errors.Errorf("unknown authentication plugin name '%s'", authPluginName) |
| 112 | + } |
| 113 | + |
| 114 | + h.userPool.Store(username, Credential{ |
| 115 | + Passwords: []string{password}, |
| 116 | + AuthPluginName: authPluginName, |
| 117 | + }) |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func (h *InMemoryAuthenticationHandler) OnAuthSuccess(conn *Conn) error { |
| 122 | + return nil |
| 123 | +} |
| 124 | + |
| 125 | +func (h *InMemoryAuthenticationHandler) OnAuthFailure(conn *Conn, err error) { |
| 126 | +} |
0 commit comments