Skip to content
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

parser,ast: Support for authentication plugins #1236

Merged
merged 1 commit into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,21 @@ type AuthOption struct {
ByAuthString bool
AuthString string
HashString string
// TODO: support auth_plugin
AuthPlugin string
}

// Restore implements Node interface.
func (n *AuthOption) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("IDENTIFIED BY ")
ctx.WriteKeyWord("IDENTIFIED")
if n.AuthPlugin != "" {
ctx.WriteKeyWord(" WITH ")
ctx.WriteString(n.AuthPlugin)
}
if n.ByAuthString {
ctx.WriteKeyWord(" BY ")
ctx.WriteString(n.AuthString)
} else {
ctx.WriteKeyWord("PASSWORD ")
} else if n.HashString != "" {
ctx.WriteKeyWord(" AS ")
ctx.WriteString(n.HashString)
}
return nil
Expand Down Expand Up @@ -1096,11 +1101,25 @@ func (n *UserSpec) EncodedPassword() (string, bool) {

opt := n.AuthOpt
if opt.ByAuthString {
return auth.EncodePassword(opt.AuthString), true
switch opt.AuthPlugin {
case mysql.AuthCachingSha2Password:
return auth.NewSha2Password(opt.AuthString), true
default:
return auth.EncodePassword(opt.AuthString), true
}
}

// Not a legal password string.
if len(opt.HashString) != 41 || !strings.HasPrefix(opt.HashString, "*") {
switch opt.AuthPlugin {
case mysql.AuthCachingSha2Password:
if len(opt.HashString) != mysql.SHAPWDHashLen {
return "", false
}
case "", mysql.AuthNativePassword:
if len(opt.HashString) != (mysql.PWDHashLen+1) || !strings.HasPrefix(opt.HashString, "*") {
return "", false
}
default:
return "", false
}
return opt.HashString, true
Expand Down
3 changes: 3 additions & 0 deletions auth/caching_sha2.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func CheckShaPassword(pwhash []byte, password string) (bool, error) {
func NewSha2Password(pwd string) string {
salt := make([]byte, SALT_LENGTH)
rand.Read(salt)
for i := range salt {
salt[i] = salt[i] ^ 128
}

return sha256crypt(pwd, salt, 5*ITERATION_MULTIPLIER)
}
Loading