Skip to content

Commit f85dbbc

Browse files
committed
Changed username and password to properties
1 parent d9de5b8 commit f85dbbc

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

redis/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ def on_connect(self):
718718
# https://github.com/andymccurdy/redis-py/issues/1274
719719
self.send_command(
720720
"AUTH",
721-
self.credentials_provider.get_password(),
721+
self.credentials_provider.password,
722722
check_health=False,
723723
)
724724
auth_response = self.read_response()
@@ -1092,7 +1092,7 @@ def __init__(
10921092
self.db = db
10931093
self.client_name = client_name
10941094
self.credentials_provider = credentials_provider
1095-
if not self.credentials_provider and (username or password):
1095+
if (username or password) and self.credentials_provider is None:
10961096
self.credentials_provider = CredentialsProvider(username, password)
10971097
self.socket_timeout = socket_timeout
10981098
self.retry_on_timeout = retry_on_timeout

redis/credentials.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from typing import Callable, Optional
2+
3+
14
class CredentialsProvider:
25
def __init__(
36
self,
47
username: str = "",
58
password: str = "",
6-
supplier: callable = None,
9+
supplier: Optional[Callable] = None,
710
*args,
811
**kwargs,
912
):
@@ -15,27 +18,33 @@ def supplier(arg1, arg2, ...) -> (username, password)
1518
:param args: arguments to pass to the supplier function
1619
:param kwargs: keyword arguments to pass to the supplier function
1720
"""
18-
self.username = username
19-
self.password = password
21+
self._username = "" if username is None else username
22+
self._password = "" if password is None else password
2023
self.supplier = supplier
2124
self.args = args
2225
self.kwargs = kwargs
2326

2427
def get_credentials(self):
2528
if self.supplier:
2629
self.username, self.password = self.supplier(*self.args, **self.kwargs)
27-
if self.username:
28-
auth_args = (self.username, self.password or "")
29-
else:
30-
auth_args = (self.password,)
31-
return auth_args
30+
return self._username, self._password
3231

33-
def get_password(self, call_supplier: bool = True):
34-
if call_supplier and self.supplier:
32+
@property
33+
def password(self):
34+
if self.supplier and not self._password:
3535
self.username, self.password = self.supplier(*self.args, **self.kwargs)
36-
return self.password
36+
return self._password
3737

38-
def get_username(self, call_supplier: bool = True):
39-
if call_supplier and self.supplier:
38+
@password.setter
39+
def password(self, value):
40+
self._password = value
41+
42+
@property
43+
def username(self):
44+
if self.supplier and not self._username:
4045
self.username, self.password = self.supplier(*self.args, **self.kwargs)
41-
return self.username
46+
return self._username
47+
48+
@username.setter
49+
def username(self, value):
50+
self._username = value

tests/test_credentials.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import random
22
import string
33

4+
import pytest
5+
46
import redis
57
from redis import ResponseError
68
from redis.credentials import CredentialsProvider
@@ -17,7 +19,7 @@ def test_credentials_provider_without_supplier(self, r, request):
1719
r.config_set("requirepass", temp_pass)
1820
creds = creds_provider.get_credentials()
1921
assert r.auth(creds[1], creds[0]) is True
20-
assert r.auth(creds_provider.get_password()) is True
22+
assert r.auth(creds_provider.password) is True
2123

2224
# test for other users
2325
username = "redis-py-auth"
@@ -48,8 +50,12 @@ def teardown():
4850

4951
assert r2.ping() is True
5052

53+
@pytest.mark.parametrize("username", ["redis-py-auth", ""])
54+
@pytest.mark.parametrize("use_password", [True, False])
5155
@skip_if_redis_enterprise()
52-
def test_credentials_provider_with_supplier(self, r, request):
56+
def test_credentials_provider_with_supplier(
57+
self, r, request, username, use_password
58+
):
5359
import functools
5460

5561
@functools.lru_cache(maxsize=10)
@@ -59,16 +65,18 @@ def get_random_string(length):
5965
result_str = "".join(random.choice(letters) for i in range(length))
6066
return result_str
6167

62-
auth_token = get_random_string(5) + user + "_" + endpoint
68+
if use_password:
69+
auth_token = get_random_string(5) + user + "_" + endpoint
70+
else:
71+
auth_token = ""
6372
return user, auth_token
6473

65-
username = "redis-py-auth"
6674
creds_provider = CredentialsProvider(
6775
supplier=auth_supplier,
6876
user=username,
6977
endpoint="localhost",
7078
)
71-
password = creds_provider.get_password()
79+
password = creds_provider.password
7280

7381
assert r.acl_setuser(
7482
username,

0 commit comments

Comments
 (0)