-
Notifications
You must be signed in to change notification settings - Fork 1k
Implement AuthenticationHandler for custom auth mechanisms
#1072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lance6716
merged 8 commits into
go-mysql-org:master
from
formalco:ramnes/credentials-passwords
Dec 20, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e54e7f0
Implement `AuthenticationHandler` for custom auth mechanisms
ramnes 20510c1
Fix xor
ramnes 4fa7140
Fall through when the cached password doesn't match
ramnes 17b4569
Run golangci-lint
ramnes 4bdd83d
Leverage `slices.Contains`
ramnes 32edb24
Unexport `Credential` methods and add comment
ramnes 8a6203f
Add a comment on empty passwords
ramnes 5f1f7c6
Add `AUTH_CLEAR_METHOD` to `isAuthMethodSupported`
ramnes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 server | ||
|
|
||
| import ( | ||
| "slices" | ||
| "sync" | ||
|
|
||
| "github.com/go-mysql-org/go-mysql/mysql" | ||
| "github.com/pingcap/errors" | ||
| "github.com/pingcap/tidb/pkg/parser/auth" | ||
| ) | ||
|
|
||
| // AuthenticationHandler provides user credentials and authentication lifecycle hooks. | ||
| // | ||
| // # Important Note | ||
| // | ||
| // if the password in a third-party auth handler could be updated at runtime, we have to invalidate the caching | ||
| // for 'caching_sha2_password' by calling 'func (s *Server)InvalidateCache(string, string)'. | ||
| type AuthenticationHandler interface { | ||
| // GetCredential returns the user credential (supports multiple valid passwords per user). | ||
| // Implementations must be safe for concurrent use. | ||
| GetCredential(username string) (credential Credential, found bool, err error) | ||
|
|
||
| // OnAuthSuccess is called after successful authentication, before the OK packet. | ||
| // Return an error to reject the connection (error will be sent to client instead of OK). | ||
| // Return nil to proceed with sending the OK packet. | ||
| OnAuthSuccess(conn *Conn) error | ||
|
|
||
| // OnAuthFailure is called after authentication fails, before the error packet. | ||
| // This is informational only - the connection will be closed regardless. | ||
| OnAuthFailure(conn *Conn, err error) | ||
| } | ||
|
|
||
| func NewInMemoryAuthenticationHandler(defaultAuthMethod ...string) *InMemoryAuthenticationHandler { | ||
| d := mysql.AUTH_CACHING_SHA2_PASSWORD | ||
| if len(defaultAuthMethod) > 0 { | ||
| d = defaultAuthMethod[0] | ||
| } | ||
| return &InMemoryAuthenticationHandler{ | ||
| userPool: sync.Map{}, | ||
| defaultAuthMethod: d, | ||
| } | ||
| } | ||
|
|
||
| // Credential holds authentication settings for a user. | ||
| // Passwords contains all valid raw passwords for the user. They are hashed on demand during comparison. | ||
| // If empty password authentication is allowed, Passwords must contain an empty string (e.g., []string{""}) | ||
| // rather than being a zero-length slice. A zero-length slice means no valid passwords are configured. | ||
| type Credential struct { | ||
| Passwords []string | ||
| AuthPluginName string | ||
| } | ||
|
|
||
| // hashPassword computes the password hash for a given password using the credential's auth plugin. | ||
| func (c Credential) hashPassword(password string) (string, error) { | ||
| if password == "" { | ||
| return "", nil | ||
| } | ||
|
|
||
| switch c.AuthPluginName { | ||
| case mysql.AUTH_NATIVE_PASSWORD: | ||
| return mysql.EncodePasswordHex(mysql.NativePasswordHash([]byte(password))), nil | ||
|
|
||
| case mysql.AUTH_CACHING_SHA2_PASSWORD: | ||
| return auth.NewHashPassword(password, mysql.AUTH_CACHING_SHA2_PASSWORD), nil | ||
|
|
||
| case mysql.AUTH_SHA256_PASSWORD: | ||
| return mysql.NewSha256PasswordHash(password) | ||
|
|
||
| case mysql.AUTH_CLEAR_PASSWORD: | ||
| return password, nil | ||
|
|
||
| default: | ||
| return "", errors.Errorf("unknown authentication plugin name '%s'", c.AuthPluginName) | ||
| } | ||
| } | ||
|
|
||
| // hasEmptyPassword returns true if any password in the credential is empty. | ||
| func (c Credential) hasEmptyPassword() bool { | ||
| return slices.Contains(c.Passwords, "") | ||
| } | ||
|
|
||
| // InMemoryAuthenticationHandler implements AuthenticationHandler with in-memory credential storage. | ||
| type InMemoryAuthenticationHandler struct { | ||
| userPool sync.Map // username -> Credential | ||
| defaultAuthMethod string | ||
| } | ||
|
|
||
| func (h *InMemoryAuthenticationHandler) CheckUsername(username string) (found bool, err error) { | ||
| _, ok := h.userPool.Load(username) | ||
| return ok, nil | ||
| } | ||
|
|
||
| func (h *InMemoryAuthenticationHandler) GetCredential(username string) (credential Credential, found bool, err error) { | ||
| v, ok := h.userPool.Load(username) | ||
ramnes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if !ok { | ||
| return Credential{}, false, nil | ||
| } | ||
| c, valid := v.(Credential) | ||
| if !valid { | ||
| return Credential{}, true, errors.Errorf("invalid credential") | ||
| } | ||
| return c, true, nil | ||
| } | ||
|
|
||
| func (h *InMemoryAuthenticationHandler) AddUser(username, password string, optionalAuthPluginName ...string) error { | ||
| authPluginName := h.defaultAuthMethod | ||
| if len(optionalAuthPluginName) > 0 { | ||
| authPluginName = optionalAuthPluginName[0] | ||
| } | ||
|
|
||
| if !isAuthMethodSupported(authPluginName) { | ||
lance6716 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return errors.Errorf("unknown authentication plugin name '%s'", authPluginName) | ||
| } | ||
|
|
||
| h.userPool.Store(username, Credential{ | ||
| Passwords: []string{password}, | ||
| AuthPluginName: authPluginName, | ||
| }) | ||
| return nil | ||
| } | ||
|
|
||
| func (h *InMemoryAuthenticationHandler) OnAuthSuccess(conn *Conn) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (h *InMemoryAuthenticationHandler) OnAuthFailure(conn *Conn, err error) { | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.