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 06c226f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 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/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ def update_host(self, url: URL) -> None:

# basic auth info
username, password = url.user, url.password
if username:
self.auth = helpers.BasicAuth(username, password or "")
if username or password:
self.auth = helpers.BasicAuth(username or "", password or "")

def update_version(self, version: Union[http.HttpVersion, str]) -> None:
"""Convert request version to two elements tuple.
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://:[email protected]")
assert "AUTHORIZATION" in req.headers
assert "Basic OjEyMzQ=" == req.headers["AUTHORIZATION"]
assert "python.org" == req.host


def test_basic_auth_from_url_overridden(make_request: Any) -> None:
req = make_request(
"get", "http://[email protected]", auth=aiohttp.BasicAuth("nkim", "1234")
Expand Down
7 changes: 7 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ def test_basic_auth_from_url() -> None:
assert auth.password == "pass"


def test_basic_auth_no_user_from_url() -> None:
url = URL("http://:[email protected]")
auth = helpers.BasicAuth.from_url(url)
assert auth.login == ""
assert auth.password == "pass"


def test_basic_auth_from_not_url() -> None:
with pytest.raises(TypeError):
helpers.BasicAuth.from_url("http://user:[email protected]")
Expand Down

0 comments on commit 06c226f

Please sign in to comment.