|
@@ -1,7 +1,6 @@
|
1
|
1
|
#-*- coding: utf-8 -*-
|
2
|
2
|
|
3
|
|
-## @package Lodel.user
|
4
|
|
-# @brief Defines Classes designed to handle users and user's context.
|
|
3
|
+## @package Lodel.user Defines classes designed to handler users and user's context
|
5
|
4
|
#
|
6
|
5
|
# Classes defined in this package are "helpers" for Lodel2 UI
|
7
|
6
|
|
|
@@ -20,7 +19,10 @@ class UserIdentity(object):
|
20
|
19
|
## @brief Constructor
|
21
|
20
|
# @note produce immutable instance
|
22
|
21
|
# @param user_id * : user id
|
23
|
|
- # @param username str : printable name for user identity
|
|
22
|
+ # @param username str : user name
|
|
23
|
+ # @param fullname str | None : user full name
|
|
24
|
+ # @param identified bool : set it to True if the user is identified
|
|
25
|
+ # @param authenticated bool : set it to True if the user is authenticated (force identified = True )
|
24
|
26
|
def __init__(self, user_id, username, fullname = None, identified = False, authenticated = False):
|
25
|
27
|
self.__user_id = user_id
|
26
|
28
|
self.__username = username
|
|
@@ -43,14 +45,17 @@ class UserIdentity(object):
|
43
|
45
|
def fullname(self):
|
44
|
46
|
return self.__fullname
|
45
|
47
|
|
|
48
|
+ ## @return True if the user is considered as authenticated
|
46
|
49
|
@property
|
47
|
50
|
def is_authenticated(self):
|
48
|
51
|
return self.__authenticated
|
49
|
52
|
|
|
53
|
+ ## @return True if the user is considered as identified
|
50
|
54
|
@property
|
51
|
55
|
def is_identified(self):
|
52
|
56
|
return self.__identified
|
53
|
57
|
|
|
58
|
+ ## @brief String representation of the instance
|
54
|
59
|
def __repr__(self):
|
55
|
60
|
return "User '{user_id}'( username = '{username}', fullname = '{fullname}', identified : {identified}, authentified : {auth}".format(
|
56
|
61
|
user_id = self.__user_id,
|
|
@@ -59,7 +64,8 @@ class UserIdentity(object):
|
59
|
64
|
identified = str(self.__identified),
|
60
|
65
|
auth = str(self.__authenticated),
|
61
|
66
|
)
|
62
|
|
-
|
|
67
|
+
|
|
68
|
+ ## @brief Human readable text representation of the instance
|
63
|
69
|
def __str__(self):
|
64
|
70
|
return self.__fullname
|
65
|
71
|
|
|
@@ -73,16 +79,11 @@ class UserIdentity(object):
|
73
|
79
|
|
74
|
80
|
## @brief Decorator class designed to register user authentication methods
|
75
|
81
|
#
|
76
|
|
-# Example :
|
77
|
|
-# <pre>
|
78
|
|
-# @authentication_method
|
79
|
|
-# def foo_auth(identity, proof):
|
80
|
|
-# if ok:
|
81
|
|
-# return True
|
82
|
|
-# else:
|
83
|
|
-# return False
|
84
|
|
-# </pre>
|
85
|
|
-#
|
|
82
|
+# @note Decorated functions are expected to take 2 arguments :
|
|
83
|
+# - identifier : the user identifier
|
|
84
|
+# - proof : a proof of identity
|
|
85
|
+# and are expected to return False if authentication fails. When authentication
|
|
86
|
+# is a success the function is expected to return a UserIdentity instance
|
86
|
87
|
class authentication_method(object):
|
87
|
88
|
|
88
|
89
|
## @brief Stores registered authentication functions
|
|
@@ -101,8 +102,9 @@ class authentication_method(object):
|
101
|
102
|
return self._method(identifier, proof)
|
102
|
103
|
|
103
|
104
|
## @brief Try to authenticate a user with registered functions
|
104
|
|
- # @param identity * : user id
|
|
105
|
+ # @param identifier * : user id
|
105
|
106
|
# @param proof * : user authentication proof
|
|
107
|
+ # @param cls
|
106
|
108
|
# @return False or a User Identity instance
|
107
|
109
|
@classmethod
|
108
|
110
|
def authenticate(cls, identifier, proof):
|
|
@@ -123,11 +125,13 @@ class authentication_method(object):
|
123
|
125
|
return res
|
124
|
126
|
|
125
|
127
|
## @return registered identification methods
|
|
128
|
+ # @param cls
|
126
|
129
|
@classmethod
|
127
|
130
|
def list_methods(cls):
|
128
|
131
|
return list(copy.copy(cls.__methods))
|
129
|
132
|
|
130
|
133
|
## @brief Unregister all authentication methods
|
|
134
|
+ # @param cls
|
131
|
135
|
# @warning REALLY NOT a good idead !
|
132
|
136
|
# @note implemented for testing purpose
|
133
|
137
|
@classmethod
|
|
@@ -137,7 +141,10 @@ class authentication_method(object):
|
137
|
141
|
|
138
|
142
|
## @brief Decorator class designed to register identification methods
|
139
|
143
|
#
|
140
|
|
-# The decorated methods should take one client_infos argument and returns a UserIdentity instance
|
|
144
|
+# @note The decorated functions are expected to take one argument :
|
|
145
|
+# - client_infos : datas for identification
|
|
146
|
+# and are expected to return False if identification fails. When identification is a success
|
|
147
|
+# the function is expected to return a UserIdentity instance
|
141
|
148
|
class identification_method(object):
|
142
|
149
|
|
143
|
150
|
## @brief Stores registered identification functions
|
|
@@ -155,7 +162,8 @@ class identification_method(object):
|
155
|
162
|
return self._method(client_infos)
|
156
|
163
|
|
157
|
164
|
## @brief Identify someone given datas
|
158
|
|
- # @param datas * : datas that may identify a user
|
|
165
|
+ # @param client_infos * : datas that may identify a user
|
|
166
|
+ # @param cls
|
159
|
167
|
# @return False if identification fails, else returns an UserIdentity instance
|
160
|
168
|
@classmethod
|
161
|
169
|
def identify(cls, client_infos):
|
|
@@ -177,11 +185,13 @@ class identification_method(object):
|
177
|
185
|
return res
|
178
|
186
|
|
179
|
187
|
## @return registered identification methods
|
|
188
|
+ # @param cls
|
180
|
189
|
@classmethod
|
181
|
190
|
def list_methods(cls):
|
182
|
191
|
return list(copy.copy(cls.__methods))
|
183
|
192
|
|
184
|
193
|
## @brief Unregister all identification methods
|
|
194
|
+ # @param cls
|
185
|
195
|
# @warning REALLY NOT a good idead !
|
186
|
196
|
# @note implemented for testing purpose
|
187
|
197
|
@classmethod
|
|
@@ -206,10 +216,9 @@ class UserContext(object):
|
206
|
216
|
raise NotImplementedError("Static class")
|
207
|
217
|
|
208
|
218
|
## @brief User context constructor
|
209
|
|
- # @param client str : client id (typically IP addr)
|
210
|
|
- # @param login str|None : given when a client try to be authenticated
|
211
|
|
- # @param proof str|None : given when a client try to be authenticated
|
|
219
|
+ # @param client_infos * : datas for client identification (typically IP address)
|
212
|
220
|
# @param **kwargs dict : context
|
|
221
|
+ # @param cls
|
213
|
222
|
# @todo find another exception to raise
|
214
|
223
|
@classmethod
|
215
|
224
|
def init(cls, client_infos, **kwargs):
|
|
@@ -235,6 +244,7 @@ class UserContext(object):
|
235
|
244
|
## @brief authenticate a user
|
236
|
245
|
# @param identifier * : user identifier
|
237
|
246
|
# @param proof * : proof of identity
|
|
247
|
+ # @param cls
|
238
|
248
|
# @throw an exception if fails
|
239
|
249
|
# @todo find a better exception to raise when auth fails
|
240
|
250
|
@classmethod
|