Skip to content

Commit

Permalink
Support credentials in URL with empty user, e.g. http://:password@host (
Browse files Browse the repository at this point in the history
  • Loading branch information
shuckc committed Jan 6, 2022
1 parent 5d70b8e commit a9b78b6
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/6494.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support credentials in URL with empty user, e.g. http://:password@host
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Chih-Yuan Chen
Chris AtLee
Chris Laws
Chris Moore
Chris Shucksmith
Christopher Schmitt
Claudiu Popa
Colin Dunklau
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ def from_url(cls, url: URL, *, encoding: str = "latin1") -> Optional["BasicAuth"
"""Create BasicAuth from url."""
if not isinstance(url, URL):
raise TypeError("url should be yarl.URL instance")
if url.user is None:
if url.user is None and url.password is None:
return None
return cls(url.user, url.password or "", encoding=encoding)
return cls(url.user or "", url.password or "", encoding=encoding)

def encode(self) -> str:
"""Encode credentials."""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,13 @@ def test_basic_auth_from_url(make_request: Any) -> None:
assert "python.org" == req.host


def test_basic_auth_no_user_from_url(make_request: Any) -> None:
req = make_request("get", "http://:1234@python.org")
assert "AUTHORIZATION" in req.headers
assert "Basic bmtpbToxMjM0" == req.headers["AUTHORIZATION"]
assert "python.org" == req.host


def test_basic_auth_from_url_overridden(make_request: Any) -> None:
req = make_request(
"get", "http://garbage@python.org", auth=aiohttp.BasicAuth("nkim", "1234")
Expand Down

0 comments on commit a9b78b6

Please sign in to comment.