Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.6 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
from time import time, mktime
35
from urlparse import urlparse
36
from urllib import quote, unquote
37

    
38
from django.conf import settings
39
from django.utils import simplejson as json
40

    
41
from synnefo.lib.pool.http import get_http_connection
42

    
43

    
44
def authenticate(token, authentication_url='http://127.0.0.1:8000/im/authenticate'):
45
    p = urlparse(authentication_url)
46

    
47
    kwargs = {}
48
    kwargs['headers'] = {}
49
    kwargs['headers']['X-Auth-Token'] = token
50
    kwargs['headers']['Content-Length'] = 0
51

    
52
    conn = get_http_connection(p.netloc, p.scheme)
53
    try:
54
        conn.request('GET', p.path, **kwargs)
55
        response = conn.getresponse()
56
        headers = response.getheaders()
57
        headers = dict((unquote(h), unquote(v)) for h,v in headers)
58
        length = response.getheader('content-length', None)
59
        data = response.read(length)
60
        status = int(response.status)
61
    finally:
62
        conn.close()
63

    
64
    if status < 200 or status >= 300:
65
        raise Exception(data, status)
66
    
67
    return json.loads(data)
68

    
69
def user_for_token(token, authentication_url, override_users):
70
    if not token:
71
        return None
72
    
73
    if override_users:
74
        try:
75
            return {'uniq': override_users[token].decode('utf8')}
76
        except:
77
            return None
78
    
79
    try:
80
        return authenticate(token, authentication_url)
81
    except Exception, e:
82
        # In case of Unauthorized response return None
83
        if e.args and e.args[-1] == 401:
84
            return None
85
        raise e
86

    
87
def get_user(request, authentication_url='http://127.0.0.1:8000/im/authenticate', override_users={}, fallback_token=None):
88
    request.user = None
89
    request.user_uniq = None
90
    
91
    # Try to find token in a parameter or in a request header.
92
    user = user_for_token(request.GET.get('X-Auth-Token'), authentication_url, override_users)
93
    if not user:
94
        user = user_for_token(request.META.get('HTTP_X_AUTH_TOKEN'), authentication_url, override_users)
95
    if not user:
96
        user = user_for_token(fallback_token, authentication_url, override_users)
97
    if not user:
98
        return
99
    
100
    request.user = user
101
    request.user_uniq = user['uniq']