From 26484516cc8e296d585d177c60b8c621abb9e505 Mon Sep 17 00:00:00 2001 From: Animesh Kumar Date: Thu, 21 Nov 2024 18:05:41 +0530 Subject: [PATCH] Add support for mysql_clear_password --- .../mysql/binlog/network/Authenticator.java | 18 +++++++++-------- .../AuthenticateClearPasswordCommand.java | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/github/shyiko/mysql/binlog/network/protocol/command/AuthenticateClearPasswordCommand.java diff --git a/src/main/java/com/github/shyiko/mysql/binlog/network/Authenticator.java b/src/main/java/com/github/shyiko/mysql/binlog/network/Authenticator.java index 0faf012d..4f5f40bd 100644 --- a/src/main/java/com/github/shyiko/mysql/binlog/network/Authenticator.java +++ b/src/main/java/com/github/shyiko/mysql/binlog/network/Authenticator.java @@ -5,13 +5,7 @@ import com.github.shyiko.mysql.binlog.network.protocol.ErrorPacket; import com.github.shyiko.mysql.binlog.network.protocol.GreetingPacket; import com.github.shyiko.mysql.binlog.network.protocol.PacketChannel; -import com.github.shyiko.mysql.binlog.network.protocol.command.AuthenticateNativePasswordCommand; -import com.github.shyiko.mysql.binlog.network.protocol.command.AuthenticateSHA2Command; -import com.github.shyiko.mysql.binlog.network.protocol.command.AuthenticateSHA2RSAPasswordCommand; -import com.github.shyiko.mysql.binlog.network.protocol.command.AuthenticateSecurityPasswordCommand; -import com.github.shyiko.mysql.binlog.network.protocol.command.ByteArrayCommand; -import com.github.shyiko.mysql.binlog.network.protocol.command.Command; -import com.github.shyiko.mysql.binlog.network.protocol.command.SSLRequestCommand; +import com.github.shyiko.mysql.binlog.network.protocol.command.*; import java.io.IOException; import java.util.Arrays; @@ -21,7 +15,8 @@ public class Authenticator { private enum AuthMethod { NATIVE, - CACHING_SHA2 + CACHING_SHA2, + CLEAR_PASSWORD }; private final GreetingPacket greetingPacket; @@ -35,6 +30,7 @@ private enum AuthMethod { private final String SHA2_PASSWORD = "caching_sha2_password"; private final String MYSQL_NATIVE = "mysql_native_password"; + private final String MYSQL_CLEAR_PASSWORD = "mysql_clear_password"; private AuthMethod authMethod = AuthMethod.NATIVE; @@ -170,6 +166,12 @@ private void switchAuthentication(byte[] authenticationResult) throws IOExceptio this.scramble = buffer.readZeroTerminatedString(); Command authCommand = new AuthenticateSHA2Command(scramble, password); channel.write(authCommand); + } else if (MYSQL_CLEAR_PASSWORD.equals(authName)) { + authMethod = AuthMethod.CLEAR_PASSWORD; + + Command swithCommand = new AuthenticateClearPasswordCommand(password); + channel.write(swithCommand); + } else { throw new AuthenticationException("unsupported authentication method: " + authName); } diff --git a/src/main/java/com/github/shyiko/mysql/binlog/network/protocol/command/AuthenticateClearPasswordCommand.java b/src/main/java/com/github/shyiko/mysql/binlog/network/protocol/command/AuthenticateClearPasswordCommand.java new file mode 100644 index 00000000..08db22d4 --- /dev/null +++ b/src/main/java/com/github/shyiko/mysql/binlog/network/protocol/command/AuthenticateClearPasswordCommand.java @@ -0,0 +1,20 @@ +package com.github.shyiko.mysql.binlog.network.protocol.command; + +import com.github.shyiko.mysql.binlog.io.ByteArrayOutputStream; + +import java.io.IOException; + +public class AuthenticateClearPasswordCommand implements Command { + private String password; + + public AuthenticateClearPasswordCommand(String password) { + this.password = password; + } + + @Override + public byte[] toByteArray() throws IOException { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + buffer.writeZeroTerminatedString(password); + return buffer.toByteArray(); + } +}