From c66a074f36e2255a66a672aeb563150b784d2e88 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sat, 17 May 2014 22:38:28 +0200 Subject: [PATCH] Add GuessAuth to authentication methods --- build.sh | 7 ++++- example.cfg | 10 ++++--- .../storage/dav/servers/radicale/__init__.py | 3 +-- vdirsyncer/storage/http.py | 26 ++++++++++++------- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/build.sh b/build.sh index bd61376..f7704d6 100755 --- a/build.sh +++ b/build.sh @@ -12,7 +12,12 @@ _davserver() { } install_build_tests() { - $PIP_INSTALL coverage pytest pytest-xprocess git+https://github.com/geier/leif + $PIP_INSTALL \ + coverage \ + pytest \ + pytest-xprocess \ + git+https://github.com/geier/leif \ + git+https://github.com/sigmavirus24/requests-toolbelt _davserver $DAV_SERVER if [ "$TRAVIS" = "true" ]; then export CFLAGS=-O0 # speed up builds of packages which don't have wheels diff --git a/example.cfg b/example.cfg index a7a8110..c9a7b6a 100644 --- a/example.cfg +++ b/example.cfg @@ -57,9 +57,11 @@ fileext = .vcf [storage bob_contacts_remote] type = carddav url = https://owncloud.example.com/remote.php/carddav/addressbooks/bob/ -# There are two different authentication types: `basic` or `digest` -#auth = basic -# For http auth, if empty, no auth will be used +# Auth types. If you know yours, set it explicitly for performance. +# - basic +# - digest +# - guess (default) +#auth = guess #username = # The password can also be fetched from the system password storage or netrc #password = @@ -85,7 +87,7 @@ fileext = .ics [storage bob_calendar_remote] type = caldav url = https://owncloud.example.com/remote.php/caldav/calendars/bob/ -#auth = basic +#auth = guess #username = #password = #verify = True diff --git a/tests/storage/dav/servers/radicale/__init__.py b/tests/storage/dav/servers/radicale/__init__.py index e6c2c9f..d7fe6ab 100644 --- a/tests/storage/dav/servers/radicale/__init__.py +++ b/tests/storage/dav/servers/radicale/__init__.py @@ -66,8 +66,7 @@ def do_the_radicale_dance(tmpdir): import radicale.auth.http def is_authenticated(user, password): - assert user == 'bob' and password == 'bob' - return True + return user == 'bob' and password == 'bob' radicale.auth.http.is_authenticated = is_authenticated if storage_backend == 'filesystem': diff --git a/vdirsyncer/storage/http.py b/vdirsyncer/storage/http.py index 81ae148..25228a3 100644 --- a/vdirsyncer/storage/http.py +++ b/vdirsyncer/storage/http.py @@ -16,17 +16,25 @@ USERAGENT = 'vdirsyncer' def prepare_auth(auth, username, password): - if auth == 'basic': - return (username, password) - elif auth == 'digest': - from requests.auth import HTTPDigestAuth - return HTTPDigestAuth(username, password) - elif auth is None: - if username and password: + if username and password: + if auth == 'basic': return (username, password) - return None + elif auth == 'digest': + from requests.auth import HTTPDigestAuth + return HTTPDigestAuth(username, password) + elif auth == 'guess' or auth is None: + import requests_toolbelt + if not hasattr(requests_toolbelt, 'GuessAuth'): + raise RuntimeError('Your version of requests_toolbelt is too ' + 'old.') + return requests_toolbelt.GuessAuth(username, password) + else: + raise ValueError('Unknown authentication method: {}'.format(auth)) + elif auth: + raise ValueError('For {} authentication, you need to specify username ' + 'and password.'.format(auth)) else: - raise ValueError('Unknown authentication method: {}'.format(auth)) + return None def prepare_verify(verify):