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

Add support to use awaitable object in password function. #889

Merged
merged 4 commits into from
Mar 25, 2022
Merged
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
7 changes: 3 additions & 4 deletions asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,25 @@ 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')
Expand Down