Revision d65af928

b/snf-common/Changelog
1 1
Changelog
2 2
=========
3 3

  
4
v0.9.14
5
-------
6
* Added astakos client lib helper get_token_from_cookie
7

  
4 8
v0.9.7
5 9
------
6 10
* Provide a fallback token to user library
b/snf-common/synnefo/lib/astakos.py
1 1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2
# 
2
#
3 3
# Redistribution and use in source and binary forms, with or
4 4
# without modification, are permitted provided that the following
5 5
# conditions are met:
6
# 
6
#
7 7
#   1. Redistributions of source code must retain the above
8 8
#      copyright notice, this list of conditions and the following
9 9
#      disclaimer.
10
# 
10
#
11 11
#   2. Redistributions in binary form must reproduce the above
12 12
#      copyright notice, this list of conditions and the following
13 13
#      disclaimer in the documentation and/or other materials
14 14
#      provided with the distribution.
15
# 
15
#
16 16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
......
25 25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
28
#
29 29
# The views and conclusions contained in the software and
30 30
# documentation are those of the authors and should not be
31 31
# interpreted as representing official policies, either expressed
......
48 48
        conn = HTTPSConnection(p.netloc)
49 49
    else:
50 50
        raise Exception('Unknown URL scheme')
51
    
51

  
52 52
    kwargs = {}
53 53
    kwargs['headers'] = {}
54 54
    kwargs['headers']['X-Auth-Token'] = token
55 55
    kwargs['headers']['Content-Length'] = 0
56
    
56

  
57 57
    conn.request('GET', p.path, **kwargs)
58 58
    response = conn.getresponse()
59
    
59

  
60 60
    headers = response.getheaders()
61 61
    headers = dict((unquote(h), unquote(v)) for h,v in headers)
62 62
    length = response.getheader('content-length', None)
63 63
    data = response.read(length)
64 64
    status = int(response.status)
65
    
65

  
66 66
    if status < 200 or status >= 300:
67 67
        raise Exception(data, int(response.status))
68
    
68

  
69 69
    return json.loads(data)
70 70

  
71 71
def user_for_token(token, authentication_url, override_users):
72 72
    if not token:
73 73
        return None
74
    
74

  
75 75
    if override_users:
76 76
        try:
77 77
            return {'uniq': override_users[token].decode('utf8')}
78 78
        except:
79 79
            return None
80
    
80

  
81 81
    try:
82 82
        return authenticate(token, authentication_url)
83 83
    except:
......
86 86
def get_user(request, authentication_url='http://127.0.0.1:8000/im/authenticate', override_users={}, fallback_token=None):
87 87
    request.user = None
88 88
    request.user_uniq = None
89
    
89

  
90 90
    # Try to find token in a parameter or in a request header.
91 91
    user = user_for_token(request.GET.get('X-Auth-Token'), authentication_url, override_users)
92 92
    if not user:
......
95 95
        user = user_for_token(fallback_token, authentication_url, override_users)
96 96
    if not user:
97 97
        return
98
    
98

  
99 99
    request.user = user
100 100
    request.user_uniq = user['uniq']
101

  
102

  
103
def get_token_from_cookie(request, cookiename):
104
    """
105
    Extract token from the cookie name provided. Cookie should be in the same
106
    form as astakos service sets its cookie contents::
107

  
108
        <user_uniq>|<user_token>
109
    """
110
    try:
111
        cookie_content = unquote(request.COOKIES.get(cookiename, None))
112
        return cookie_content.split("|")[1]
113
    except AttributeError:
114
        pass
115

  
116
    return None

Also available in: Unified diff