From ae2de42af4e6f8673ab1705901c42e5571700245 Mon Sep 17 00:00:00 2001 From: dengliming Date: Wed, 3 Mar 2021 01:00:14 +0800 Subject: [PATCH] Add support for MIGRATE AUTH2 option --- .../java/io/lettuce/core/MigrateArgs.java | 44 ++++++++++++++++++- .../io/lettuce/core/protocol/CommandType.java | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/lettuce/core/MigrateArgs.java b/src/main/java/io/lettuce/core/MigrateArgs.java index 0b24f86a8e..af82bc4b7b 100644 --- a/src/main/java/io/lettuce/core/MigrateArgs.java +++ b/src/main/java/io/lettuce/core/MigrateArgs.java @@ -31,6 +31,7 @@ * {@link MigrateArgs} is a mutable object and instances should be used only once to avoid shared mutable state. * * @author Mark Paluch + * @author dengliming */ public class MigrateArgs implements CompositeArgument { @@ -42,6 +43,8 @@ public class MigrateArgs implements CompositeArgument { private char[] password; + private char[] username; + /** * Builder entry points for {@link MigrateArgs}. */ @@ -119,6 +122,17 @@ public static MigrateArgs auth(CharSequence password) { return new MigrateArgs().auth(password); } + /** + * Creates new {@link MigrateArgs} with {@code AUTH2} (target authentication) enabled. + * + * @return new {@link MigrateArgs} with {@code AUTH2} (target authentication) enabled. + * @since 6.1 + * @see MigrateArgs#auth2(CharSequence, CharSequence) + */ + public static MigrateArgs auth2(CharSequence username, CharSequence password) { + return new MigrateArgs().auth2(username, password); + } + /** * Creates new {@link MigrateArgs} with {@code AUTH} (target authentication) enabled. * @@ -216,6 +230,29 @@ public MigrateArgs auth(CharSequence password) { return this; } + /** + * Set {@literal AUTH2} {@code username} and {@code password} option. + * + * @param username must not be {@code null}. + * @param password must not be {@code null}. + * @return {@code this} {@link MigrateArgs}. + * @since 6.1 + */ + public MigrateArgs auth2(CharSequence username, CharSequence password) { + + LettuceAssert.notNull(username, "UserName must not be null"); + LettuceAssert.notNull(password, "Password must not be null"); + + char[] chars = new char[username.length()]; + + for (int i = 0; i < username.length(); i++) { + chars[i] = username.charAt(i); + } + + this.username = chars; + return auth(password); + } + /** * Set {@literal AUTH} {@code password} option. * @@ -243,7 +280,12 @@ public void build(CommandArgs args) { } if (password != null) { - args.add(CommandType.AUTH).add(password); + if (username != null) { + args.add(CommandType.AUTH2).add(username); + } else { + args.add(CommandType.AUTH); + } + args.add(password); } if (keys.size() > 1) { diff --git a/src/main/java/io/lettuce/core/protocol/CommandType.java b/src/main/java/io/lettuce/core/protocol/CommandType.java index 957386a663..edf8f3c8bc 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandType.java +++ b/src/main/java/io/lettuce/core/protocol/CommandType.java @@ -30,7 +30,7 @@ public enum CommandType implements ProtocolKeyword { // Authentication - ACL, AUTH, + ACL, AUTH, AUTH2, // Connection