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

Support credentials in URL with empty user (#6494) (#6495) #8926

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
1 change: 1 addition & 0 deletions CHANGES/6494.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for URL credentials with empty (zero-length) username, e.g. ``https://:password@host`` -- by :user:`shuckc`
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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 @@ -408,8 +408,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 @@ -164,9 +164,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 @@ -453,6 +453,13 @@ def test_basic_auth_from_url(make_request) -> None:
assert "python.org" == req.host


def test_basic_auth_no_user_from_url(make_request) -> 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) -> None:
req = make_request(
"get", "http://[email protected]", auth=aiohttp.BasicAuth("nkim", "1234")
Expand Down
8 changes: 8 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ 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 is not None
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
Loading