root / snf-django-lib / snf_django / lib / astakos.py @ 837d85bb
History | View | Annotate | Download (5.3 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(token, astakos_auth_url, logger=None): |
42 |
if token is None: |
43 |
return None |
44 |
client = AstakosClient(token, astakos_auth_url, |
45 |
retry=2, use_pool=True, logger=logger) |
46 |
try:
|
47 |
return client.get_user_info()
|
48 |
except Unauthorized:
|
49 |
return None |
50 |
|
51 |
|
52 |
def get_user(request, astakos_auth_url, fallback_token=None, logger=None): |
53 |
request.user = None
|
54 |
request.user_uniq = None
|
55 |
|
56 |
# Try to find token in a parameter or in a request header.
|
57 |
user = user_for_token( |
58 |
request.GET.get('X-Auth-Token'), astakos_auth_url, logger)
|
59 |
if not user: |
60 |
user = user_for_token( |
61 |
request.META.get('HTTP_X_AUTH_TOKEN'), astakos_auth_url, logger)
|
62 |
if not user: |
63 |
user = user_for_token( |
64 |
fallback_token, astakos_auth_url, logger) |
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_auth_url, astakos_token, |
80 |
split=100, logger=None): |
81 |
if logger is None: |
82 |
logger = logging.getLogger(__name__) |
83 |
self.logger = logger
|
84 |
|
85 |
self.astakos = AstakosClient(astakos_token, astakos_auth_url,
|
86 |
retry=2, use_pool=True, logger=logger) |
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 = \ |
101 |
self.astakos.service_get_usernames(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(name)
|
123 |
except NoUUID:
|
124 |
self.logger.debug("Failed to fetch uuid for %s", name) |
125 |
except AstakosClientException:
|
126 |
pass
|
127 |
except Exception as err: |
128 |
self.logger.error("Unexpected error while fetching " |
129 |
"user uuid %s: %s" % (name, repr(err))) |
130 |
finally:
|
131 |
self.users[name] = uuid
|
132 |
|
133 |
return self.users[name] |
134 |
|
135 |
def get_name(self, uuid): |
136 |
name = "-"
|
137 |
|
138 |
if not uuid in self.users: |
139 |
try:
|
140 |
name = self.astakos.service_get_username(uuid)
|
141 |
except NoUserName:
|
142 |
self.logger.debug("Failed to fetch display name for %s", uuid) |
143 |
except AstakosClientException:
|
144 |
pass
|
145 |
except Exception as err: |
146 |
self.logger.error("Unexpected error while fetching " |
147 |
"user displayname %s: %s"
|
148 |
% (uuid, repr(err)))
|
149 |
finally:
|
150 |
self.users[uuid] = name
|
151 |
|
152 |
return self.users[uuid] |