Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async CredentialProvider #3423

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion redis/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
SSLConnection,
UnixDomainSocketConnection,
)
from redis.asyncio.credentials import CredentialProvider
from redis.asyncio.lock import Lock
from redis.asyncio.retry import Retry
from redis.client import (
Expand All @@ -52,7 +53,6 @@
AsyncSentinelCommands,
list_or_args,
)
from redis.credentials import CredentialProvider
from redis.exceptions import (
ConnectionError,
ExecAbortError,
Expand Down
2 changes: 1 addition & 1 deletion redis/asyncio/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from redis.asyncio.client import ResponseCallbackT
from redis.asyncio.connection import Connection, DefaultParser, SSLConnection, parse_url
from redis.asyncio.credentials import CredentialProvider
from redis.asyncio.lock import Lock
from redis.asyncio.retry import Retry
from redis.backoff import default_backoff
Expand All @@ -44,7 +45,6 @@
)
from redis.commands import READ_COMMANDS, AsyncRedisClusterCommands
from redis.crc import REDIS_CLUSTER_HASH_SLOTS, key_slot
from redis.credentials import CredentialProvider
from redis.exceptions import (
AskError,
BusyLoadingError,
Expand Down
7 changes: 5 additions & 2 deletions redis/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@
else:
from async_timeout import timeout as async_timeout

from redis.asyncio.credentials import (
CredentialProvider,
UsernamePasswordCredentialProvider,
)
from redis.asyncio.retry import Retry
from redis.backoff import NoBackoff
from redis.connection import DEFAULT_RESP_VERSION
from redis.credentials import CredentialProvider, UsernamePasswordCredentialProvider
from redis.exceptions import (
AuthenticationError,
AuthenticationWrongNumberOfArgsError,
Expand Down Expand Up @@ -333,7 +336,7 @@ async def on_connect(self) -> None:
self.credential_provider
or UsernamePasswordCredentialProvider(self.username, self.password)
)
auth_args = cred_provider.get_credentials()
auth_args = await cred_provider.get_credentials()
# if resp version is specified and we have auth args,
# we need to send them via HELLO
if auth_args and self.protocol not in [2, "2"]:
Expand Down
26 changes: 26 additions & 0 deletions redis/asyncio/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Optional, Tuple, Union


class CredentialProvider:
"""
Credentials Provider.
"""

async def get_credentials(self) -> Union[Tuple[str], Tuple[str, str]]:
raise NotImplementedError("get_credentials must be implemented")


class UsernamePasswordCredentialProvider(CredentialProvider):
"""
Simple implementation of CredentialProvider that just wraps static
username and password.
"""

def __init__(self, username: Optional[str] = None, password: Optional[str] = None):
self.username = username or ""
self.password = password or ""

async def get_credentials(self):
if self.username:
return self.username, self.password
return (self.password,)