Revision 7bffb0bd snf-common/synnefo/lib/astakos.py
b/snf-common/synnefo/lib/astakos.py | ||
---|---|---|
35 | 35 |
|
36 | 36 |
from time import time, mktime |
37 | 37 |
from urlparse import urlparse, urlsplit, urlunsplit |
38 |
from urllib import quote, unquote |
|
38 |
from urllib import quote, unquote, urlencode
|
|
39 | 39 |
|
40 | 40 |
from django.conf import settings |
41 | 41 |
from django.utils import simplejson as json |
... | ... | |
76 | 76 |
|
77 | 77 |
conn = get_http_connection(p.netloc, p.scheme) |
78 | 78 |
try: |
79 |
conn.request('GET', p.path, **kwargs) |
|
79 |
conn.request('GET', p.path + '?' + p.query, **kwargs)
|
|
80 | 80 |
response = conn.getresponse() |
81 | 81 |
headers = response.getheaders() |
82 | 82 |
headers = dict((unquote(h), unquote(v)) for h,v in headers) |
... | ... | |
93 | 93 |
|
94 | 94 |
|
95 | 95 |
def authenticate( |
96 |
token, authentication_url='http://127.0.0.1:8000/im/authenticate'): |
|
96 |
token, authentication_url='http://127.0.0.1:8000/im/authenticate', |
|
97 |
usage=False): |
|
98 |
|
|
99 |
if usage: |
|
100 |
authentication_url += "?usage=1" |
|
101 |
|
|
97 | 102 |
return call(token, authentication_url) |
98 | 103 |
|
104 |
|
|
99 | 105 |
@retry(3) |
100 | 106 |
def get_username( |
101 | 107 |
token, |
... | ... | |
128 | 134 |
return data.get('uuid') |
129 | 135 |
|
130 | 136 |
|
131 |
def user_for_token(token, authentication_url, override_users): |
|
137 |
def user_for_token(token, authentication_url, override_users, usage=False):
|
|
132 | 138 |
if not token: |
133 | 139 |
return None |
134 | 140 |
|
... | ... | |
139 | 145 |
return None |
140 | 146 |
|
141 | 147 |
try: |
142 |
return authenticate(token, authentication_url) |
|
148 |
return authenticate(token, authentication_url, usage=usage)
|
|
143 | 149 |
except Exception, e: |
144 | 150 |
# In case of Unauthorized response return None |
145 | 151 |
if e.args and e.args[-1] == 401: |
... | ... | |
150 | 156 |
request, |
151 | 157 |
authentication_url='http://127.0.0.1:8000/im/authenticate', |
152 | 158 |
override_users={}, |
153 |
fallback_token=None): |
|
159 |
fallback_token=None, |
|
160 |
usage=False): |
|
154 | 161 |
request.user = None |
155 | 162 |
request.user_uniq = None |
156 | 163 |
|
157 | 164 |
# Try to find token in a parameter or in a request header. |
158 | 165 |
user = user_for_token( |
159 |
request.GET.get('X-Auth-Token'), authentication_url, override_users) |
|
166 |
request.GET.get('X-Auth-Token'), authentication_url, override_users, |
|
167 |
usage=usage) |
|
160 | 168 |
if not user: |
161 | 169 |
user = user_for_token( |
162 | 170 |
request.META.get('HTTP_X_AUTH_TOKEN'), |
163 | 171 |
authentication_url, |
164 |
override_users) |
|
172 |
override_users, |
|
173 |
usage=usage) |
|
165 | 174 |
if not user: |
166 | 175 |
user = user_for_token( |
167 |
fallback_token, authentication_url, override_users) |
|
176 |
fallback_token, authentication_url, override_users, |
|
177 |
usage=usage) |
|
168 | 178 |
if not user: |
169 | 179 |
logger.warning("Cannot retrieve user details from %s", |
170 | 180 |
authentication_url) |
171 |
return |
|
181 |
return None
|
|
172 | 182 |
|
173 | 183 |
# use user uuid, instead of email, keep email/username reference to user_id |
174 | 184 |
request.user_uniq = user['uuid'] |
Also available in: Unified diff