Skip to content

Commit fc06eb7

Browse files
kwarunekrohitsanj
authored andcommitted
Add support to use awaitable object in password function. (MagicStack#889)
Add support to use awaitable object in password function. This will allow to pass lambda or `functools.partial` that returns `Future`.
1 parent 54ec77c commit fc06eb7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

asyncpg/connect_utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,9 @@ async def _connect_addr(
757757

758758
params_input = params
759759
if callable(params.password):
760-
if inspect.iscoroutinefunction(params.password):
761-
password = await params.password()
762-
else:
763-
password = params.password()
760+
password = params.password()
761+
if inspect.isawaitable(password):
762+
password = await password
764763

765764
params = params._replace(password=password)
766765
args = (addr, loop, config, connection_class, record_class, params_input)

tests/test_connect.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,25 @@ async def get_wrongpassword():
282282
user='password_user',
283283
password=get_wrongpassword)
284284

285+
async def test_auth_password_cleartext_callable_awaitable(self):
286+
async def get_correctpassword():
287+
return 'correctpassword'
288+
289+
async def get_wrongpassword():
290+
return 'wrongpassword'
291+
292+
conn = await self.connect(
293+
user='password_user',
294+
password=lambda: get_correctpassword())
295+
await conn.close()
296+
297+
with self.assertRaisesRegex(
298+
asyncpg.InvalidPasswordError,
299+
'password authentication failed for user "password_user"'):
300+
await self._try_connect(
301+
user='password_user',
302+
password=lambda: get_wrongpassword())
303+
285304
async def test_auth_password_md5(self):
286305
conn = await self.connect(
287306
user='md5_user', password='correctpassword')

0 commit comments

Comments
 (0)