Statistics
| Branch: | Tag: | Revision:

root / snf-django-lib / snf_django / lib / astakos.py @ 81a906f8

History | View | Annotate | Download (4.1 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 astakosclient import AstakosClient
35
from astakosclient.errors import Unauthorized
36

    
37

    
38
def user_for_token(client, token, usage=False):
39
    if not token:
40
        return None
41

    
42
    try:
43
        return client.get_user_info(token, usage=True)
44
    except Unauthorized:
45
        return None
46

    
47

    
48
def get_user(request, astakos_url, fallback_token=None,
49
             usage=False, logger=None):
50
    request.user = None
51
    request.user_uniq = None
52

    
53
    client = AstakosClient(astakos_url, retry=2, use_pool=True, logger=logger)
54
    # Try to find token in a parameter or in a request header.
55
    user = user_for_token(client, request.GET.get('X-Auth-Token'), usage=usage)
56
    if not user:
57
        user = user_for_token(client,
58
                              request.META.get('HTTP_X_AUTH_TOKEN'),
59
                              usage=usage)
60
    if not user:
61
        user = user_for_token(client, fallback_token, usage=usage)
62
    if not user:
63
        return None
64

    
65
    # use user uuid, instead of email, keep email/displayname reference
66
    # to user_id
67
    request.user_uniq = user['uuid']
68
    request.user = user
69
    request.user_id = user.get('displayname')
70
    return user
71

    
72

    
73
class UserCache(object):
74
    """uuid<->displayname user 'cache'"""
75

    
76
    def __init__(self, astakos_url, astakos_token, split=100, logger=None):
77
        self.astakos = AstakosClient(astakos_url, retry=2,
78
                                     use_pool=True, logger=logger)
79
        self.astakos_token = astakos_token
80
        self.users = {}
81

    
82
        self.split = split
83
        assert(self.split > 0), "split must be positive"
84

    
85
    def fetch_names(self, uuid_list):
86
        total = len(uuid_list)
87
        split = self.split
88

    
89
        for start in range(0, total, split):
90
            end = start + split
91
            try:
92
                names = self.astakos.service_get_usernames(
93
                    self.astakos_token, uuid_list[start:end])
94
                self.users.update(names)
95
            except:
96
                pass
97

    
98
    def get_uuid(self, name):
99
        if not name in self.users:
100
            try:
101
                self.users[name] = \
102
                    self.astakos.service.get_uuid(
103
                        self.astakos_token, name)
104
            except:
105
                self.users[name] = name
106

    
107
        return self.users[name]
108

    
109
    def get_name(self, uuid):
110
        """Do the uuid-to-email resolving"""
111

    
112
        if not uuid in self.users:
113
            try:
114
                self.users[uuid] = \
115
                    self.astakos.get_username(
116
                        self.astakos_token, uuid)
117
            except:
118
                self.users[uuid] = "-"
119

    
120
        return self.users[uuid]