Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / astakos.py @ 5f6ad491

History | View | Annotate | Download (3.6 kB)

1 8acb1f97 Kostas Papadimitriou
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 8acb1f97 Kostas Papadimitriou
# 
3 8acb1f97 Kostas Papadimitriou
# Redistribution and use in source and binary forms, with or
4 8acb1f97 Kostas Papadimitriou
# without modification, are permitted provided that the following
5 8acb1f97 Kostas Papadimitriou
# conditions are met:
6 8acb1f97 Kostas Papadimitriou
# 
7 8acb1f97 Kostas Papadimitriou
#   1. Redistributions of source code must retain the above
8 8acb1f97 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
9 8acb1f97 Kostas Papadimitriou
#      disclaimer.
10 8acb1f97 Kostas Papadimitriou
# 
11 8acb1f97 Kostas Papadimitriou
#   2. Redistributions in binary form must reproduce the above
12 8acb1f97 Kostas Papadimitriou
#      copyright notice, this list of conditions and the following
13 8acb1f97 Kostas Papadimitriou
#      disclaimer in the documentation and/or other materials
14 8acb1f97 Kostas Papadimitriou
#      provided with the distribution.
15 8acb1f97 Kostas Papadimitriou
# 
16 8acb1f97 Kostas Papadimitriou
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 8acb1f97 Kostas Papadimitriou
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 8acb1f97 Kostas Papadimitriou
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 8acb1f97 Kostas Papadimitriou
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 8acb1f97 Kostas Papadimitriou
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 8acb1f97 Kostas Papadimitriou
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 8acb1f97 Kostas Papadimitriou
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 8acb1f97 Kostas Papadimitriou
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 8acb1f97 Kostas Papadimitriou
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 8acb1f97 Kostas Papadimitriou
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 8acb1f97 Kostas Papadimitriou
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 8acb1f97 Kostas Papadimitriou
# POSSIBILITY OF SUCH DAMAGE.
28 8acb1f97 Kostas Papadimitriou
# 
29 8acb1f97 Kostas Papadimitriou
# The views and conclusions contained in the software and
30 8acb1f97 Kostas Papadimitriou
# documentation are those of the authors and should not be
31 8acb1f97 Kostas Papadimitriou
# interpreted as representing official policies, either expressed
32 8acb1f97 Kostas Papadimitriou
# or implied, of GRNET S.A.
33 8acb1f97 Kostas Papadimitriou
34 8acb1f97 Kostas Papadimitriou
from time import time, mktime
35 8acb1f97 Kostas Papadimitriou
from urlparse import urlparse
36 8acb1f97 Kostas Papadimitriou
from urllib import quote, unquote
37 8acb1f97 Kostas Papadimitriou
38 8acb1f97 Kostas Papadimitriou
from django.conf import settings
39 8acb1f97 Kostas Papadimitriou
from django.utils import simplejson as json
40 8acb1f97 Kostas Papadimitriou
41 27cfa807 Vangelis Koukis
from synnefo.lib.pool.http import get_http_connection
42 27cfa807 Vangelis Koukis
43 8acb1f97 Kostas Papadimitriou
44 8acb1f97 Kostas Papadimitriou
def authenticate(token, authentication_url='http://127.0.0.1:8000/im/authenticate'):
45 8acb1f97 Kostas Papadimitriou
    p = urlparse(authentication_url)
46 27cfa807 Vangelis Koukis
47 8acb1f97 Kostas Papadimitriou
    kwargs = {}
48 8acb1f97 Kostas Papadimitriou
    kwargs['headers'] = {}
49 8acb1f97 Kostas Papadimitriou
    kwargs['headers']['X-Auth-Token'] = token
50 8acb1f97 Kostas Papadimitriou
    kwargs['headers']['Content-Length'] = 0
51 27cfa807 Vangelis Koukis
52 27cfa807 Vangelis Koukis
    conn = get_http_connection(p.netloc, p.scheme)
53 27cfa807 Vangelis Koukis
    try:
54 27cfa807 Vangelis Koukis
        conn.request('GET', p.path, **kwargs)
55 27cfa807 Vangelis Koukis
        response = conn.getresponse()
56 27cfa807 Vangelis Koukis
        headers = response.getheaders()
57 27cfa807 Vangelis Koukis
        headers = dict((unquote(h), unquote(v)) for h,v in headers)
58 27cfa807 Vangelis Koukis
        length = response.getheader('content-length', None)
59 27cfa807 Vangelis Koukis
        data = response.read(length)
60 27cfa807 Vangelis Koukis
        status = int(response.status)
61 27cfa807 Vangelis Koukis
    finally:
62 27cfa807 Vangelis Koukis
        conn.close()
63 27cfa807 Vangelis Koukis
64 8acb1f97 Kostas Papadimitriou
    if status < 200 or status >= 300:
65 27cfa807 Vangelis Koukis
        raise Exception(data, status)
66 8acb1f97 Kostas Papadimitriou
    
67 8acb1f97 Kostas Papadimitriou
    return json.loads(data)
68 8acb1f97 Kostas Papadimitriou
69 8acb1f97 Kostas Papadimitriou
def user_for_token(token, authentication_url, override_users):
70 8acb1f97 Kostas Papadimitriou
    if not token:
71 8acb1f97 Kostas Papadimitriou
        return None
72 8acb1f97 Kostas Papadimitriou
    
73 8acb1f97 Kostas Papadimitriou
    if override_users:
74 8acb1f97 Kostas Papadimitriou
        try:
75 8acb1f97 Kostas Papadimitriou
            return {'uniq': override_users[token].decode('utf8')}
76 8acb1f97 Kostas Papadimitriou
        except:
77 8acb1f97 Kostas Papadimitriou
            return None
78 8acb1f97 Kostas Papadimitriou
    
79 8acb1f97 Kostas Papadimitriou
    try:
80 9f1bbb33 Sofia Papagiannaki
        return authenticate(token, authentication_url)
81 1055f3c6 Sofia Papagiannaki
    except Exception, e:
82 1055f3c6 Sofia Papagiannaki
        # In case of Unauthorized response return None
83 1055f3c6 Sofia Papagiannaki
        if e.args and e.args[-1] == 401:
84 1055f3c6 Sofia Papagiannaki
            return None
85 1055f3c6 Sofia Papagiannaki
        raise e
86 8acb1f97 Kostas Papadimitriou
87 6b5b443b Antony Chazapis
def get_user(request, authentication_url='http://127.0.0.1:8000/im/authenticate', override_users={}, fallback_token=None):
88 8acb1f97 Kostas Papadimitriou
    request.user = None
89 8acb1f97 Kostas Papadimitriou
    request.user_uniq = None
90 8acb1f97 Kostas Papadimitriou
    
91 8acb1f97 Kostas Papadimitriou
    # Try to find token in a parameter or in a request header.
92 8acb1f97 Kostas Papadimitriou
    user = user_for_token(request.GET.get('X-Auth-Token'), authentication_url, override_users)
93 8acb1f97 Kostas Papadimitriou
    if not user:
94 8acb1f97 Kostas Papadimitriou
        user = user_for_token(request.META.get('HTTP_X_AUTH_TOKEN'), authentication_url, override_users)
95 8acb1f97 Kostas Papadimitriou
    if not user:
96 6b5b443b Antony Chazapis
        user = user_for_token(fallback_token, authentication_url, override_users)
97 6b5b443b Antony Chazapis
    if not user:
98 8acb1f97 Kostas Papadimitriou
        return
99 8acb1f97 Kostas Papadimitriou
    
100 8acb1f97 Kostas Papadimitriou
    request.user = user
101 8acb1f97 Kostas Papadimitriou
    request.user_uniq = user['uniq']