Skip to content

Commit d57d00a

Browse files
committed
refactor: Add AUTH command to send_command based on Redis version check if no exceptions occur.
1 parent e365726 commit d57d00a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

redis/connection.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,6 @@ def on_connect(self):
356356
)
357357
auth_args = cred_provider.get_credentials()
358358

359-
auth_command_response = False
360-
361359
# try to send HELLO command (for Redis 6.0 and above)
362360
try:
363361
# if resp version is specified and we have auth args,
@@ -379,8 +377,6 @@ def on_connect(self):
379377
except Exception as e:
380378
if str(e) == "Invalid RESP version":
381379
raise ConnectionError("Invalid RESP version")
382-
elif str(e) == "Invalid Username or Password":
383-
raise AuthenticationError("Invalid Username or Password")
384380
# fall back to AUTH command (for Redis versions less than 6.0)
385381
else:
386382
self.send_command('MULTI')
@@ -391,6 +387,15 @@ def on_connect(self):
391387
if auth_args:
392388
self.send_command("AUTH", *auth_args, check_health=False)
393389
auth_command_response = True
390+
# avoid checking health here -- PING will fail if we try
391+
# to check the health prior to the AUTH
392+
if auth_args:
393+
# check if only password is provided and RESP version < 6
394+
if not self.username and self.password and self.protocol in [2, "2"]:
395+
self.send_command("AUTH", self.password, check_health=False)
396+
else:
397+
self.send_command("AUTH", *auth_args, check_health=False)
398+
394399

395400
# if a client_name is given, set it
396401
if self.client_name:

0 commit comments

Comments
 (0)