26 from passlib.hash
import sha256_crypt
30 from database_handler
import DatabaseHandler
32 from rapp_utilities
import RappUtilities
33 from rapp_exceptions
import RappError
35 from rapp_platform_ros_communications.srv
import (
36 AddNewUserFromPlatformSrv,
37 AddNewUserFromPlatformSrvResponse,
38 AddNewUserFromStoreSrv,
39 AddNewUserFromStoreSrvResponse,
40 UserTokenAuthenticationSrv,
41 UserTokenAuthenticationSrvResponse,
60 rapp_add_new_user_from_platform_topic = \
61 rospy.get_param(
"rapp_add_new_user_from_platform_topic")
62 if not rapp_add_new_user_from_platform_topic:
63 msg =
"Add new user from platform topic does not exist"
64 RappUtilities.rapp_print(msg,
'ERROR')
65 add_new_user_from_platform_srv = \
66 rospy.Service(rapp_add_new_user_from_platform_topic,
67 AddNewUserFromPlatformSrv,
71 rapp_add_new_user_from_store_topic = \
72 rospy.get_param(
"rapp_add_new_user_from_store_topic")
73 if not rapp_add_new_user_from_store_topic:
74 msg =
"Add new user from store topic does not exist"
75 RappUtilities.rapp_print(msg,
'ERROR')
76 add_new_user_srv = rospy.Service(rapp_add_new_user_from_store_topic,
77 AddNewUserFromStoreSrv,
81 authenticate_token_topic = \
82 rospy.get_param(
"rapp_authenticate_token_topic")
83 if not authenticate_token_topic:
84 rospy.logerr(
"Application authentication: " +
85 "Token authentication topic does not exist")
87 authenticate_token_service = \
88 rospy.Service(authenticate_token_topic,
89 UserTokenAuthenticationSrv,
93 login_from_store_topic = \
94 rospy.get_param(
"rapp_login_user_from_store")
95 if not login_from_store_topic:
96 msg =
"Login user from store topic does not exist"
97 RappUtilities.rapp_print(msg,
'ERROR')
98 login_user_from_store_srv = \
99 rospy.Service(login_from_store_topic,
105 rospy.get_param(
"rapp_login_user")
107 msg =
"Login user topic does not exist"
108 RappUtilities.rapp_print(msg,
'ERROR')
111 rospy.Service(login_topic,
121 res = AddNewUserFromPlatformSrvResponse()
125 req.creator_username, req.creator_password)
127 self._db_handler.validate_user_role(req.creator_username)
130 except RappError
as e:
137 except RappError
as e:
141 if unique_username !=
'':
142 res.error =
'Username exists'
143 res.suggested_username = unique_username
148 req.new_user_username,
149 req.new_user_password,
150 req.creator_username,
153 except RappError
as e:
163 res = AddNewUserFromStoreSrvResponse()
169 except RappError
as e:
173 if not self._db_handler.verify_store_device_token(req.device_token):
174 res.error =
'Invalid user'
181 except RappError
as e:
185 if unique_username !=
'':
186 res.error =
'Username exists'
187 res.suggested_username = unique_username
198 except RappError
as e:
208 res = UserLoginSrvResponse()
212 except RappError
as e:
216 if not self._db_handler.verify_platform_device_token(req.device_token):
217 res.error =
'Invalid user'
220 if self._db_handler.verify_active_robot_session(
221 req.username, req.device_token):
222 res.error =
'Session already active'
226 ''.join(random.SystemRandom().choice(string.ascii_letters +
227 string.digits + string.punctuation)
for _
in range(64))
228 hash_str = sha256_crypt.encrypt(rand_str)
229 index = hash_str.find(
'$', 3)
230 hash_str = hash_str[index+1:]
231 new_token = base64.b64encode(hash_str)
234 self._db_handler.write_new_application_token(
235 req.username, req.device_token, new_token)
236 except RappError
as e:
237 res.error =
'Wrong credentials'
240 res.token = new_token
249 res = UserLoginSrvResponse()
253 except RappError
as e:
257 if not self._db_handler.verify_store_device_token(req.device_token):
258 res.error =
'Invalid user'
262 self._db_handler.add_store_token_to_device(req.device_token)
263 except RappError
as e:
264 res.error =
'Wrong credentials'
267 if self._db_handler.verify_active_robot_session(
268 req.username, req.device_token):
269 res.error =
'Session already active'
274 ''.join(random.SystemRandom().choice(string.ascii_letters +
275 string.digits + string.punctuation)
for _
in range(64))
276 hash_str = sha256_crypt.encrypt(rand_str)
277 index = hash_str.find(
'$', 3)
278 hash_str = hash_str[index+1:]
279 res.token = base64.b64encode(hash_str)
282 self._db_handler.write_new_application_token(
283 req.username, req.device_token, res.token)
284 except RappError
as e:
285 res.error =
'Wrong credentials'
297 res = UserTokenAuthenticationSrvResponse()
300 if self._db_handler.verify_active_application_token(req.token):
301 res.username = self._db_handler.get_token_user(req.token)
303 res.error =
'Invalid token'
313 passwd = self._db_handler.get_user_password(username)
314 if bcrypt.hashpw(password, passwd) != passwd:
315 raise RappError(
"Wrong Credentials")
325 if self._db_handler.username_exists(username):
327 while True and counter <= 10:
330 ''.join(random.SystemRandom().choice(string.digits)
332 if not self._db_handler.username_exists(
333 username + suggestion):
334 return username + suggestion
335 raise RappError(
'Could specify a username suggestion')
347 if not re.match(
"^[\w\d_-]*$", username)
or len(username) < 5
or \
349 raise RappError(
'Invalid username characters')
356 creator_username, language):
358 password_hash = bcrypt.hashpw(new_user_password, bcrypt.gensalt())
360 self._db_handler.add_new_user(
361 new_user_username, password_hash, creator_username, language)
363 if __name__ ==
"__main__":
364 rospy.init_node(
'application_authentication_node')
def authenticate_token_callback
Authenticate token.
_db_handler
Handles the database queries.
def _add_new_user_to_db
Create password hash and store to the databse.
def _verify_user_credentials
Verify username and password.
def add_new_user_from_platform_callback
Add new platform user using platform credentials.
def login_from_store_callback
Login existing user using store device token.
def add_new_user_from_store_callback
Add new platform user using rapp_store credentials.
def __init__
ROS Service initializations.
Provides user management features.
def _validate_username_format
Verify that new username complies with a set of rules.
def login_callback
Login existing user using platform device token.
def _verify_new_username_uniqueness
Verify that new username is unique.