diff --git a/.gitignore b/.gitignore index 344dde3f..03967d08 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ aredis.egg-info build/ dist/ .idea/ +venv/ env/ env3.6/ dump.rdb diff --git a/README.rst b/README.rst index 28832617..2e4be784 100644 --- a/README.rst +++ b/README.rst @@ -50,6 +50,8 @@ single node client async def example(): client = StrictRedis(host='127.0.0.1', port=6379, db=0) + # If not anonymous + # client = StrictRedis(host='127.0.0.1', port=6379, db=0, username='user', password='password') await client.flushdb() await client.set('foo', 1) assert await client.exists('foo') is True diff --git a/aredis/client.py b/aredis/client.py index 91973db5..dddfdc83 100644 --- a/aredis/client.py +++ b/aredis/client.py @@ -92,7 +92,7 @@ def from_url(cls, url, db=None, **kwargs): return cls(connection_pool=connection_pool) def __init__(self, host='localhost', port=6379, - db=0, password=None, stream_timeout=None, + db=0, username=None, password=None, stream_timeout=None, connect_timeout=None, connection_pool=None, unix_socket_path=None, encoding='utf-8', decode_responses=False, ssl=False, ssl_context=None, @@ -104,6 +104,7 @@ def __init__(self, host='localhost', port=6379, if not connection_pool: kwargs = { 'db': db, + 'username': username, 'password': password, 'encoding': encoding, 'stream_timeout': stream_timeout, diff --git a/aredis/connection.py b/aredis/connection.py index 6dc79050..24409d52 100755 --- a/aredis/connection.py +++ b/aredis/connection.py @@ -372,6 +372,7 @@ def __init__(self, retry_on_timeout=False, stream_timeout=None, self._stream_timeout = stream_timeout self._reader = None self._writer = None + self.username = None self.password = '' self.db = '' self.pid = os.getpid() @@ -435,7 +436,10 @@ async def on_connect(self): # if a password is specified, authenticate if self.password: - await self.send_command('AUTH', self.password) + if self.username: + await self.send_command('AUTH', self.username, self.password) + else: + await self.send_command('AUTH', self.password) if nativestr(await self.read_response()) != 'OK': raise ConnectionError('Invalid Password') @@ -515,7 +519,7 @@ def disconnect(self): self._writer = None def pack_command(self, *args): - "Pack a series of arguments into the Redis protocol" + """Pack a series of arguments into the Redis protocol.""" output = [] # the client might have included 1 or more literal arguments in # the command name, e.g., 'CONFIG GET'. The Redis server expects these @@ -569,7 +573,7 @@ def pack_commands(self, commands): class Connection(BaseConnection): description = 'Connection' - def __init__(self, host='127.0.0.1', port=6379, password=None, + def __init__(self, host='127.0.0.1', port=6379, username=None, password=None, db=0, retry_on_timeout=False, stream_timeout=None, connect_timeout=None, ssl_context=None, parser_class=DefaultParser, reader_read_size=65535, encoding='utf-8', decode_responses=False, socket_keepalive=None, @@ -580,6 +584,7 @@ def __init__(self, host='127.0.0.1', port=6379, password=None, loop=loop) self.host = host self.port = port + self.username = username self.password = password self.db = db self.ssl_context = ssl_context diff --git a/aredis/pool.py b/aredis/pool.py index 1fbb2cc7..3b0a80c3 100644 --- a/aredis/pool.py +++ b/aredis/pool.py @@ -47,9 +47,9 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): For example:: - redis://[:password]@localhost:6379/0 - rediss://[:password]@localhost:6379/0 - unix://[:password]@/path/to/socket.sock?db=0 + redis://[username:password]@localhost:6379/0 + rediss://[username:password]@localhost:6379/0 + unix://[username:password]@/path/to/socket.sock?db=0 Three URL schemes are supported: @@ -103,10 +103,12 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): url_options[name] = value[0] if decode_components: + username = unquote(url.username) if url.username else None password = unquote(url.password) if url.password else None path = unquote(url.path) if url.path else None hostname = unquote(url.hostname) if url.hostname else None else: + username = url.username password = url.password path = url.path hostname = url.hostname @@ -114,6 +116,7 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): # We only support redis:// and unix:// schemes. if url.scheme == 'unix': url_options.update({ + 'username': username, 'password': password, 'path': path, 'connection_class': UnixDomainSocketConnection, @@ -123,6 +126,7 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs): url_options.update({ 'host': hostname, 'port': int(url.port or 6379), + 'username': username, 'password': password, })