Merge lp:~mstoll-de/duplicity/b2-reauth into lp:~duplicity-team/duplicity/0.7-series

Proposed by Markus
Status: Merged
Merged at revision: 1219
Proposed branch: lp:~mstoll-de/duplicity/b2-reauth
Merge into: lp:~duplicity-team/duplicity/0.7-series
Diff against target: 63 lines (+20/-8)
1 file modified
duplicity/backends/b2backend.py (+20/-8)
To merge this branch: bzr merge lp:~mstoll-de/duplicity/b2-reauth
Reviewer Review Type Date Requested Status
duplicity-team Pending
Review via email: mp+296496@code.launchpad.net

Commit message

take care of error code 401 (which means the auth token did expired) by reauthenticating against b2 backend

Description of the change

fixes the bug #1588503 where an expired auth token did not lead to a reauthentication

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'duplicity/backends/b2backend.py'
2--- duplicity/backends/b2backend.py 2015-12-14 22:07:20 +0000
3+++ duplicity/backends/b2backend.py 2016-06-04 20:21:30 +0000
4@@ -61,8 +61,16 @@
5 raise BackendException("B2 requires a bucket name")
6 self.path = "/".join(self.url_parts)
7
8- id_and_key = self.account_id + ":" + account_key
9- basic_auth_string = 'Basic ' + base64.b64encode(id_and_key)
10+ self.id_and_key = self.account_id + ":" + account_key
11+ self._authorize();
12+
13+ try:
14+ self.find_or_create_bucket(self.bucket_name)
15+ except urllib2.HTTPError:
16+ raise FatalBackendException("Bucket cannot be created")
17+
18+ def _authorize(self):
19+ basic_auth_string = 'Basic ' + base64.b64encode(self.id_and_key)
20 headers = {'Authorization': basic_auth_string}
21
22 request = urllib2.Request(
23@@ -78,10 +86,6 @@
24 self.api_url = response_data['apiUrl']
25 self.download_url = response_data['downloadUrl']
26
27- try:
28- self.find_or_create_bucket(self.bucket_name)
29- except urllib2.HTTPError:
30- raise FatalBackendException("Bucket cannot be created")
31
32 def _get(self, remote_filename, local_path):
33 """
34@@ -254,6 +258,8 @@
35 If headers are not supplied, just send with an auth key
36 """
37 if headers is None:
38+ if self.auth_token is None:
39+ self._authorize();
40 headers = {'Authorization': self.auth_token}
41 if data_file is not None:
42 data = data_file
43@@ -265,12 +271,18 @@
44 for (k, v) in headers.iteritems()
45 )
46
47- with OpenUrl(url, data, encoded_headers) as resp:
48- out = resp.read()
49+ try:
50+ with OpenUrl(url, data, encoded_headers) as resp:
51+ out = resp.read()
52 try:
53 return json.loads(out)
54 except ValueError:
55 return out
56+ except urllib2.HTTPError as e:
57+ if e.code == 401:
58+ self.auth_token = None;
59+ log.Warn("Authtoken expired, will reauthenticate with next attempt")
60+ raise e
61
62 def get_file_info(self, filename):
63 """

Subscribers

People subscribed via source and target branches