This repository was archived by the owner on Aug 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathhttplib2conn.py
More file actions
executable file
·112 lines (94 loc) · 3.78 KB
/
httplib2conn.py
File metadata and controls
executable file
·112 lines (94 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Connection Module
See COPYING for license information
"""
import six
from object_storage import errors
from object_storage.transport import BaseAuthentication, \
BaseAuthenticatedConnection, Response
import httplib2
from object_storage.utils import json, unicode_urlencode
import logging
logger = logging.getLogger(__name__)
class AuthenticatedConnection(BaseAuthenticatedConnection):
"""
Connection that will authenticate if it isn't already
and retry once if an auth error is returned.
"""
def __init__(self, auth, debug=False, **kwargs):
if debug:
httplib2.debuglevel = 4
self.token = None
self.storage_url = None
self.http = httplib2.Http()
self.http.disable_ssl_certificate_validation = True
self.auth = auth
if not self.auth.authenticated:
self.auth.authenticate()
self._authenticate()
def make_request(self, method, url=None, headers=None, formatter=None,
params=None, data=None, *args, **kwargs):
""" Makes a request """
headers = headers or {}
headers.update(self.get_headers())
if params:
url = "%s?%s" % (url, unicode_urlencode(params))
def _make_request(headers):
logger.debug("%s %s %s" % (method, url, headers))
res, content = self.http.request(url, method,
headers=headers,
body=data)
response = Response()
response.headers = res
response.status_code = int(res.status)
response.content = content
return response
response = _make_request(headers)
if response.status_code == 401:
self.auth.authenticate()
self._authenticate()
headers.update(self.auth_headers)
response = _make_request(headers)
response.raise_for_status()
if formatter:
return formatter(response)
return response
class Authentication(BaseAuthentication):
"""
Authentication class.
"""
def __init__(self, username, api_key, auth_token=None, *args, **kwargs):
super(Authentication, self).__init__(*args, **kwargs)
self.username = username
self.api_key = api_key
self.auth_token = auth_token
if self.auth_token:
self.authenticated = True
@property
def auth_headers(self):
return {'X-Auth-Token': self.auth_token}
def authenticate(self):
""" Does authentication """
headers = {'X-Storage-User': self.username,
'X-Storage-Pass': self.api_key,
'Content-Length': '0'}
http = httplib2.Http()
http.disable_ssl_certificate_validation = True
res, content = http.request(self.auth_url, 'GET', headers=headers)
response = Response()
response.headers = res
response.status_code = int(res.status)
response.content = content
if response.status_code == 401:
raise errors.AuthenticationError('Invalid Credentials')
response.raise_for_status()
try:
storage_options = json.loads(response.content if isinstance(response.content, six.string_types) else response.content.decode('utf8'))['storage']
except ValueError:
raise errors.StorageURLNotFound("Could not parse services JSON.")
self.auth_token = response.headers['x-auth-token']
self.storage_url = self.get_storage_url(storage_options)
if not self.storage_url:
self.storage_url = response.headers['x-storage-url']
if not self.auth_token or not self.storage_url:
raise errors.AuthenticationError('Invalid Authentication Response')