From 50ed1a5430784b1356883ce3070f54581b590923 Mon Sep 17 00:00:00 2001 From: Krzysztof Warunek Date: Tue, 15 Feb 2022 06:46:54 +0100 Subject: [PATCH 1/2] Add support to use awaitable object in password function. This will allow to pass lambda or `functools.partial` that returns `Future`. --- asyncpg/connect_utils.py | 7 +++---- tests/test_connect.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/asyncpg/connect_utils.py b/asyncpg/connect_utils.py index f98935f5..c09bf5e0 100644 --- a/asyncpg/connect_utils.py +++ b/asyncpg/connect_utils.py @@ -757,10 +757,9 @@ async def _connect_addr( params_input = params if callable(params.password): - if inspect.iscoroutinefunction(params.password): - password = await params.password() - else: - password = params.password() + password = params.password() + if inspect.isawaitable(password): + password = await password params = params._replace(password=password) args = (addr, loop, config, connection_class, record_class, params_input) diff --git a/tests/test_connect.py b/tests/test_connect.py index b78f4f48..d54f6b8f 100644 --- a/tests/test_connect.py +++ b/tests/test_connect.py @@ -282,6 +282,26 @@ async def get_wrongpassword(): user='password_user', password=get_wrongpassword) + async def test_auth_password_cleartext_callable_awaitable(self): + async def get_correctpassword(): + return 'correctpassword' + + async def get_wrongpassword(): + return 'wrongpassword' + + conn = await self.connect( + user='password_user', + password=lambda: get_correctpassword()) + await conn.close() + + with self.assertRaisesRegex( + asyncpg.InvalidPasswordError, + 'password authentication failed for user "password_user"'): + await self._try_connect( + user='password_user', + password=lambda: get_wrongpassword()) + + async def test_auth_password_md5(self): conn = await self.connect( user='md5_user', password='correctpassword') From a2b59e01489e141f19d17b18fcdb26c0ad399528 Mon Sep 17 00:00:00 2001 From: Krzysztof Warunek Date: Fri, 18 Feb 2022 10:20:45 +0100 Subject: [PATCH 2/2] fix fmt --- tests/test_connect.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_connect.py b/tests/test_connect.py index d54f6b8f..34ffbb34 100644 --- a/tests/test_connect.py +++ b/tests/test_connect.py @@ -301,7 +301,6 @@ async def get_wrongpassword(): user='password_user', password=lambda: get_wrongpassword()) - async def test_auth_password_md5(self): conn = await self.connect( user='md5_user', password='correctpassword')