root / snf-common / synnefo / lib / astakos.py @ 83204389
History | View | Annotate | Download (4.3 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import logging |
35 |
|
36 |
from time import time, mktime |
37 |
from urlparse import urlparse |
38 |
from urllib import quote, unquote |
39 |
|
40 |
from django.conf import settings |
41 |
from django.utils import simplejson as json |
42 |
|
43 |
from synnefo.lib.pool.http import get_http_connection |
44 |
|
45 |
logger = logging.getLogger(__name__) |
46 |
|
47 |
def authenticate(token, authentication_url='http://127.0.0.1:8000/im/authenticate'): |
48 |
p = urlparse(authentication_url) |
49 |
|
50 |
kwargs = {} |
51 |
kwargs['headers'] = {}
|
52 |
kwargs['headers']['X-Auth-Token'] = token |
53 |
kwargs['headers']['Content-Length'] = 0 |
54 |
|
55 |
conn = get_http_connection(p.netloc, p.scheme) |
56 |
try:
|
57 |
conn.request('GET', p.path, **kwargs)
|
58 |
response = conn.getresponse() |
59 |
headers = response.getheaders() |
60 |
headers = dict((unquote(h), unquote(v)) for h,v in headers) |
61 |
length = response.getheader('content-length', None) |
62 |
data = response.read(length) |
63 |
status = int(response.status)
|
64 |
finally:
|
65 |
conn.close() |
66 |
|
67 |
if status < 200 or status >= 300: |
68 |
raise Exception(data, status) |
69 |
|
70 |
return json.loads(data)
|
71 |
|
72 |
def user_for_token(token, authentication_url, override_users): |
73 |
if not token: |
74 |
return None |
75 |
|
76 |
if override_users:
|
77 |
try:
|
78 |
return {'uniq': override_users[token].decode('utf8')} |
79 |
except:
|
80 |
return None |
81 |
|
82 |
try:
|
83 |
return authenticate(token, authentication_url)
|
84 |
except Exception, e: |
85 |
# In case of Unauthorized response return None
|
86 |
if e.args and e.args[-1] == 401: |
87 |
return None |
88 |
raise e
|
89 |
|
90 |
def get_user(request, authentication_url='http://127.0.0.1:8000/im/authenticate', override_users={}, fallback_token=None): |
91 |
request.user = None
|
92 |
request.user_uniq = None
|
93 |
|
94 |
# Try to find token in a parameter or in a request header.
|
95 |
user = user_for_token(request.GET.get('X-Auth-Token'), authentication_url, override_users)
|
96 |
if not user: |
97 |
user = user_for_token(request.META.get('HTTP_X_AUTH_TOKEN'), authentication_url, override_users)
|
98 |
if not user: |
99 |
user = user_for_token(fallback_token, authentication_url, override_users) |
100 |
if not user: |
101 |
logger.warning("Cannot retrieve user details from %s",
|
102 |
authentication_url) |
103 |
return
|
104 |
|
105 |
# use user uuid, instead of email, keep email/username reference to user_id
|
106 |
request.user_uniq = user['uuid']
|
107 |
request.user = user |
108 |
request.user_id = user['username']
|
109 |
return user
|
110 |
|
111 |
|
112 |
def get_token_from_cookie(request, cookiename): |
113 |
"""
|
114 |
Extract token from the cookie name provided. Cookie should be in the same
|
115 |
form as astakos service sets its cookie contents::
|
116 |
|
117 |
<user_uniq>|<user_token>
|
118 |
"""
|
119 |
try:
|
120 |
cookie_content = unquote(request.COOKIES.get(cookiename, None))
|
121 |
return cookie_content.split("|")[1] |
122 |
except AttributeError: |
123 |
pass
|
124 |
|
125 |
return None |