Skip to content

Commit 60352f8

Browse files
Hugo Osvaldo BarreraWhyNotHugo
authored andcommitted
Untangle auth handling
This was a bit entangled and messed up due to recent changes.
1 parent b720101 commit 60352f8

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

tests/storage/test_http.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from aiohttp import BasicAuth
23
from aioresponses import CallbackResult
34
from aioresponses import aioresponses
45

@@ -88,8 +89,8 @@ def test_readonly_param(aio_connector):
8889
def test_prepare_auth():
8990
assert prepare_auth(None, "", "") is None
9091

91-
assert prepare_auth(None, "user", "pwd") == ("user", "pwd")
92-
assert prepare_auth("basic", "user", "pwd") == ("user", "pwd")
92+
assert prepare_auth(None, "user", "pwd") == BasicAuth("user", "pwd")
93+
assert prepare_auth("basic", "user", "pwd") == BasicAuth("user", "pwd")
9394

9495
with pytest.raises(ValueError) as excinfo:
9596
assert prepare_auth("basic", "", "pwd")
@@ -109,7 +110,8 @@ def test_prepare_auth_guess(monkeypatch):
109110
import requests_toolbelt.auth.guess
110111

111112
assert isinstance(
112-
prepare_auth("guess", "user", "pwd"), requests_toolbelt.auth.guess.GuessAuth
113+
prepare_auth("guess", "user", "pwd"),
114+
requests_toolbelt.auth.guess.GuessAuth,
113115
)
114116

115117
monkeypatch.delattr(requests_toolbelt.auth.guess, "GuessAuth")

vdirsyncer/http.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _detect_faulty_requests(): # pragma: no cover
3636
def prepare_auth(auth, username, password):
3737
if username and password:
3838
if auth == "basic" or auth is None:
39-
return (username, password)
39+
return aiohttp.BasicAuth(username, password)
4040
elif auth == "digest":
4141
from requests.auth import HTTPDigestAuth
4242

@@ -59,8 +59,8 @@ def prepare_auth(auth, username, password):
5959
"You need to specify username and password "
6060
"for {} authentication.".format(auth)
6161
)
62-
else:
63-
return None
62+
63+
return None
6464

6565

6666
def prepare_verify(verify, verify_fingerprint):
@@ -126,8 +126,6 @@ async def request(
126126

127127
session.hooks = {"response": _fix_redirects}
128128

129-
func = session.request
130-
131129
# TODO: rewrite using
132130
# https://docs.aiohttp.org/en/stable/client_advanced.html#client-tracing
133131
logger.debug("=" * 20)
@@ -140,12 +138,7 @@ async def request(
140138

141139
kwargs.pop("cert", None) # TODO XXX FIXME!
142140

143-
auth = kwargs.pop("auth", None)
144-
if auth:
145-
kwargs["auth"] = aiohttp.BasicAuth(*auth)
146-
147-
r = func(method, url, ssl=ssl, **kwargs)
148-
r = await r
141+
r = await session.request(method, url, ssl=ssl, **kwargs)
149142

150143
# See https://github.com/kennethreitz/requests/issues/2042
151144
content_type = r.headers.get("Content-Type", "")

vdirsyncer/storage/dav.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,10 @@ def __init__(
385385
):
386386
self._settings = {
387387
"cert": prepare_client_cert(auth_cert),
388-
"auth": prepare_auth(auth, username, password),
389388
}
389+
auth = prepare_auth(auth, username, password)
390+
if auth:
391+
self._settings["auth"] = auth
390392
self._settings.update(prepare_verify(verify, verify_fingerprint))
391393

392394
self.useragent = useragent

vdirsyncer/storage/http.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ def __init__(
3535
*,
3636
connector,
3737
**kwargs
38-
):
38+
) -> None:
3939
super().__init__(**kwargs)
4040

4141
self._settings = {
42-
"auth": prepare_auth(auth, username, password),
4342
"cert": prepare_client_cert(auth_cert),
4443
"latin1_fallback": False,
4544
}
45+
auth = prepare_auth(auth, username, password)
46+
if auth:
47+
self._settings["auth"] = auth
4648
self._settings.update(prepare_verify(verify, verify_fingerprint))
4749

4850
self.username, self.password = username, password

0 commit comments

Comments
 (0)