root / snf-django-lib / snf_django / lib / astakos.py @ 93505a12
History | View | Annotate | Download (5.2 kB)
1 |
# Copyright 2011, 2012, 2013 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.authenticate()
|
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 |
request.user_uniq = user['access']['user']['id'] |
69 |
request.user = user |
70 |
return user
|
71 |
|
72 |
|
73 |
class UserCache(object): |
74 |
"""uuid<->displayname user 'cache'"""
|
75 |
|
76 |
def __init__(self, astakos_auth_url, astakos_token, |
77 |
split=100, logger=None): |
78 |
if logger is None: |
79 |
logger = logging.getLogger(__name__) |
80 |
self.logger = logger
|
81 |
|
82 |
self.astakos = AstakosClient(astakos_token, astakos_auth_url,
|
83 |
retry=2, use_pool=True, logger=logger) |
84 |
self.users = {}
|
85 |
|
86 |
self.split = split
|
87 |
assert(self.split > 0), "split must be positive" |
88 |
|
89 |
def fetch_names(self, uuid_list): |
90 |
total = len(uuid_list)
|
91 |
split = self.split
|
92 |
count = 0
|
93 |
|
94 |
for start in range(0, total, split): |
95 |
end = start + split |
96 |
try:
|
97 |
names = \ |
98 |
self.astakos.service_get_usernames(uuid_list[start:end])
|
99 |
count += len(names)
|
100 |
|
101 |
self.users.update(names)
|
102 |
except AstakosClientException:
|
103 |
pass
|
104 |
except Exception as err: |
105 |
self.logger.error("Unexpected error while fetching " |
106 |
"user display names: %s" % repr(err)) |
107 |
|
108 |
diff = (total - count) |
109 |
assert(diff >= 0), "fetched more displaynames than requested" |
110 |
|
111 |
if diff:
|
112 |
self.logger.debug("Failed to fetch %d displaynames", diff) |
113 |
|
114 |
def get_uuid(self, name): |
115 |
uuid = name |
116 |
|
117 |
if not name in self.users: |
118 |
try:
|
119 |
uuid = self.astakos.service_get_uuid(name)
|
120 |
except NoUUID:
|
121 |
self.logger.debug("Failed to fetch uuid for %s", name) |
122 |
except AstakosClientException:
|
123 |
pass
|
124 |
except Exception as err: |
125 |
self.logger.error("Unexpected error while fetching " |
126 |
"user uuid %s: %s" % (name, repr(err))) |
127 |
finally:
|
128 |
self.users[name] = uuid
|
129 |
|
130 |
return self.users[name] |
131 |
|
132 |
def get_name(self, uuid): |
133 |
name = "-"
|
134 |
|
135 |
if not uuid in self.users: |
136 |
try:
|
137 |
name = self.astakos.service_get_username(uuid)
|
138 |
except NoUserName:
|
139 |
self.logger.debug("Failed to fetch display name for %s", uuid) |
140 |
except AstakosClientException:
|
141 |
pass
|
142 |
except Exception as err: |
143 |
self.logger.error("Unexpected error while fetching " |
144 |
"user displayname %s: %s"
|
145 |
% (uuid, repr(err)))
|
146 |
finally:
|
147 |
self.users[uuid] = name
|
148 |
|
149 |
return self.users[uuid] |