Statistics
| Branch: | Tag: | Revision:

root / snf-django-lib / snf_django / lib / astakos.py @ 34bd6588

History | View | Annotate | Download (5.4 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 astakosclient import AstakosClient
37
from astakosclient.errors import (Unauthorized, NoUUID, NoUserName,
38
                                  AstakosClientException)
39

    
40

    
41
def user_for_token(client, token, usage=False):
42
    if not token:
43
        return None
44

    
45
    try:
46
        return client.get_user_info(token, usage=True)
47
    except Unauthorized:
48
        return None
49

    
50

    
51
def get_user(request, astakos_url, fallback_token=None,
52
             usage=False, logger=None):
53
    request.user = None
54
    request.user_uniq = None
55

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

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

    
75

    
76
class UserCache(object):
77
    """uuid<->displayname user 'cache'"""
78

    
79
    def __init__(self, astakos_url, astakos_token, split=100, logger=None):
80
        if logger is None:
81
            logger = logging.getLogger(__name__)
82
        self.logger = logger
83

    
84
        self.astakos = AstakosClient(astakos_url, retry=2,
85
                                     use_pool=True, logger=logger)
86
        self.astakos_token = astakos_token
87
        self.users = {}
88

    
89
        self.split = split
90
        assert(self.split > 0), "split must be positive"
91

    
92
    def fetch_names(self, uuid_list):
93
        total = len(uuid_list)
94
        split = self.split
95
        count = 0
96

    
97
        for start in range(0, total, split):
98
            end = start + split
99
            try:
100
                names = self.astakos.service_get_usernames(
101
                    self.astakos_token, uuid_list[start:end])
102
                count += len(names)
103

    
104
                self.users.update(names)
105
            except AstakosClientException:
106
                pass
107
            except Exception as err:
108
                self.logger.error("Unexpected error while fetching "
109
                                  "user display names: %s" % repr(err))
110

    
111
        diff = (total - count)
112
        assert(diff >= 0), "fetched more displaynames than requested"
113

    
114
        if diff:
115
            self.logger.debug("Failed to fetch %d displaynames", diff)
116

    
117
    def get_uuid(self, name):
118
        uuid = name
119

    
120
        if not name in self.users:
121
            try:
122
                uuid = self.astakos.service_get_uuid(
123
                    self.astakos_token, name)
124
            except NoUUID:
125
                self.logger.debug("Failed to fetch uuid for %s", name)
126
            except AstakosClientException:
127
                pass
128
            except Exception as err:
129
                self.logger.error("Unexpected error while fetching "
130
                                  "user uuid %s: %s" % (name, repr(err)))
131
            finally:
132
                self.users[name] = uuid
133

    
134
        return self.users[name]
135

    
136
    def get_name(self, uuid):
137
        name = "-"
138

    
139
        if not uuid in self.users:
140
            try:
141
                name = self.astakos.service_get_username(
142
                    self.astakos_token, uuid)
143
            except NoUserName:
144
                self.logger.debug("Failed to fetch display name for %s", uuid)
145
            except AstakosClientException:
146
                pass
147
            except Exception as err:
148
                self.logger.error("Unexpected error while fetching "
149
                                  "user displayname %s: %s"
150
                                  % (uuid, repr(err)))
151
            finally:
152
                self.users[uuid] = name
153

    
154
        return self.users[uuid]