RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
database_handler.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 #Copyright 2015 RAPP
5 
6 #Licensed under the Apache License, Version 2.0 (the "License");
7 #you may not use this file except in compliance with the License.
8 #You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 #Unless required by applicable law or agreed to in writing, software
13 #distributed under the License is distributed on an "AS IS" BASIS,
14 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #See the License for the specific language governing permissions and
16 #limitations under the License.
17 
18 # Authors: Aris Thallas, Athanassios Kintsakis
19 # contact: aris.thallas@{iti.gr, gmail.com}
20 
21 import rospy
22 
23 from rapp_utilities import RappUtilities
24 from rapp_exceptions import RappError
25 
26 from rapp_platform_ros_communications.srv import (
27  addStoreTokenToDeviceSrv,
28  addStoreTokenToDeviceSrvRequest,
29  getUserOntologyAliasSrv,
30  getUserOntologyAliasSrvRequest,
31  registerUserOntologyAliasSrv,
32  registerUserOntologyAliasSrvRequest,
33  checkIfUserExistsSrv,
34  checkIfUserExistsSrvRequest,
35  getUserLanguageSrv,
36  getUserLanguageSrvRequest,
37  getUserPasswordSrv,
38  getUserPasswordSrvRequest,
39  getUsernameAssociatedWithApplicationTokenSrv,
40  getUsernameAssociatedWithApplicationTokenSrvRequest,
41  createNewPlatformUserSrv,
42  createNewPlatformUserSrvRequest,
43  createNewApplicationTokenSrv,
44  createNewApplicationTokenSrvRequest,
45  checkActiveApplicationTokenSrv,
46  checkActiveApplicationTokenSrvRequest,
47  checkActiveRobotSessionSrv,
48  checkActiveRobotSessionSrvRequest,
49  validateUserRoleSrv,
50  validateUserRoleSrvRequest,
51  validateExistingPlatformDeviceTokenSrv,
52  validateExistingPlatformDeviceTokenSrvRequest
53 )
54 
55 
56 ## @class DatabaseHandler
57 # @brief Handles database functions for authentication manager
58 class DatabaseHandler(object):
59 
60  ## @brief ROS Service initializations
61  def __init__(self):
62  user_exists_topic = rospy.get_param(
63  "rapp_mysql_wrapper_check_if_user_exists_service_topic")
64  if not user_exists_topic:
65  RappUtilities.rapp_print(
66  'rapp_mysql_wrapper_check_if_user_exists_service_topic ' +
67  'NOT FOUND', 'ERROR')
68  rospy.wait_for_service(user_exists_topic)
69  ## Service proxy to query database for user existance
70  #
71  # (see database_handler.username_exists)
73  rospy.ServiceProxy(user_exists_topic, checkIfUserExistsSrv,
74  persistent=True)
75 
76  get_passwd_topic = rospy.get_param(
77  "rapp_mysql_wrapper_get_user_password_service_topic")
78  if not get_passwd_topic:
79  RappUtilities.rapp_print(
80  'rapp_mysql_wrapper_get_user_password_service_topic ' +
81  'NOT FOUND', 'ERROR')
82  rospy.wait_for_service(get_passwd_topic)
83  ## Service proxy to query database for user password
84  #
85  # (see database_handler.get_user_password)
87  rospy.ServiceProxy(get_passwd_topic, getUserPasswordSrv,
88  persistent=True)
89 
90  verify_appl_token_topic = rospy.get_param(
91  "rapp_mysql_wrapper_check_active_" +
92  "application_token_service_topic")
93  if not verify_appl_token_topic:
94  RappUtilities.rapp_print(
95  "rapp_mysql_wrapper_check_active_" +
96  "application_token_service_topic NOT FOUND", 'ERROR')
97  rospy.wait_for_service(verify_appl_token_topic)
98  ## Service proxy to query database for application token validity
99  #
100  # (see database_handler.verify_active_application_token)
101  self._verify_appl_token_proxy = rospy.ServiceProxy(
102  verify_appl_token_topic, checkActiveApplicationTokenSrv,
103  persistent=True)
104 
105  get_token_user_topic = rospy.get_param(
106  "rapp_mysql_wrapper_get_username_associated_" +
107  "with_application_token_service_topic")
108  if not get_token_user_topic:
109  RappUtilities.rapp_print(
110  'rapp_mysql_wrapper_get_username_associated_with' +
111  '_application_token_service_topic NOT FOUND', 'ERROR')
112  rospy.wait_for_service(get_token_user_topic)
113  ## Service proxy to query database for the user associated with a token
114  #
115  # (see database_handler.get_token_user)
116  self._get_token_user_proxy = rospy.ServiceProxy(
117  get_token_user_topic, getUsernameAssociatedWithApplicationTokenSrv,
118  persistent=True)
119 
120  verify_robot_session_topic = rospy.get_param(
121  "rapp_mysql_wrapper_check_active_" +
122  "robot_session_service_topic")
123  if not verify_robot_session_topic:
124  RappUtilities.rapp_print(
125  "rapp_mysql_wrapper_check_active_" +
126  "robot_session_service_topic NOT FOUND", 'ERROR')
127  rospy.wait_for_service(verify_robot_session_topic)
128  ## Service proxy to query database for active user session
129  #
130  # (see database_handler.verify_active_robot_session)
131  self._verify_robot_session_proxy = rospy.ServiceProxy(
132  verify_robot_session_topic, checkActiveRobotSessionSrv,
133  persistent=True)
134 
135  add_new_user_topic = rospy.get_param(
136  "rapp_mysql_wrapper_create_new_platform_user_service_topic")
137  if not add_new_user_topic:
138  RappUtilities.rapp_print(
139  'rapp_mysql_wrapper_create_new_platform_user_service_topic ' +
140  'NOT FOUND', 'ERROR')
141  rospy.wait_for_service(add_new_user_topic)
142  ## Service proxy to add new user to the database
143  #
144  # (see database_handler.add_new_user)
146  rospy.ServiceProxy(add_new_user_topic, createNewPlatformUserSrv,
147  persistent=True)
148 
149  create_new_appl_token_topic = rospy.get_param(
150  "rapp_mysql_wrapper_create_new_application_token_service_topic")
151  if not create_new_appl_token_topic:
152  RappUtilities.rapp_print(
153  'rapp_mysql_wrapper_create_new_application_token_' +
154  'create_new_appl_token_topic NOT FOUND', 'ERROR')
155  rospy.wait_for_service(create_new_appl_token_topic)
156  ## Service proxy to add new application token to the database
157  #
158  # (see database_handler.write_new_application_token)
159  self._create_new_app_token_proxy = rospy.ServiceProxy(
160  create_new_appl_token_topic, createNewApplicationTokenSrv,
161  persistent=True)
162 
163  add_store_token_to_device_topic = rospy.get_param(
164  "rapp_mysql_wrapper_add_store_token_to_device_topic")
165  if not add_store_token_to_device_topic:
166  RappUtilities.rapp_print(
167  'rapp_mysql_wrapper_add_store_token_to_device_topic NOT FOUND',
168  'ERROR')
169  rospy.wait_for_service(add_store_token_to_device_topic)
170  ## Service proxy to add new device token from store to the database
171  #
172  # (see database_handler.add_store_token_to_device)
173  self._add_store_token_to_device_proxy = rospy.ServiceProxy(
174  add_store_token_to_device_topic, addStoreTokenToDeviceSrv,
175  persistent=True)
176 
177  validate_user_role_topic = rospy.get_param(
178  "rapp_mysql_wrapper_validate_user_role_topic")
179  if not validate_user_role_topic:
180  RappUtilities.rapp_print(
181  'rapp_mysql_wrapper_validate_user_role_topic NOT FOUND',
182  'ERROR')
183  rospy.wait_for_service(validate_user_role_topic)
184  ## Service proxy to query the database for the user's role
185  #
186  # (see database_handler.validate_user_role)
187  self._validate_user_role_proxy = rospy.ServiceProxy(
188  validate_user_role_topic, validateUserRoleSrv,
189  persistent=True)
190 
191  validate_existing_device_topic = rospy.get_param(
192  "rapp_mysql_wrapper_validate_existing_platform_device_token_topic")
193  if not validate_existing_device_topic:
194  RappUtilities.rapp_print(
195  'rapp_mysql_wrapper_validate_' +
196  'existing_platform_device_token_topic NOT FOUND',
197  'ERROR')
198  rospy.wait_for_service(validate_existing_device_topic)
199  ## Service proxy to query the database for a device token
200  #
201  # (see database_handler.verify_platform_device_token)
202  self._validate_existing_device_token_proxy = rospy.ServiceProxy(
203  validate_existing_device_topic,
204  validateExistingPlatformDeviceTokenSrv, persistent=True)
205 
206  ## @brief Verify that the device token exists in store database
207  #
208  # @param device_token [string] The token
209  #
210  # @return status [bool] True if token exists, false otherwise
211  def verify_store_device_token(self, device_token):
212  # TODO
213  # pass
214  return True
215 
216  ## @brief Verify that the device token exists in platform database
217  #
218  # @param device_token [string] The token
219  #
220  # @return status [bool] True if token exists, false otherwise
221  def verify_platform_device_token(self, device_token):
222  req = validateExistingPlatformDeviceTokenSrvRequest()
223  req.device_token = device_token
224 
225  response = self._validate_existing_device_token_proxy(req)
226  return response.success
227 
228  ## @brief Verify that username exists in platform database
229  #
230  # @param username [string] The username
231  #
232  # @return status [bool] True if username exists, false otherwise
233  def username_exists(self, username):
234  req = checkIfUserExistsSrvRequest()
235  req.username = username
236 
237  response = self._username_exists_proxy(req)
238  if response.success:
239  return response.user_exists
240  else:
241  return False
242 
243  ## @brief Retrieve user's password from platform database
244  #
245  # @param username [string] The username
246  #
247  # @return password [string] Password associated with username
248  #
249  # @exception RappError Username does not exist
250  def get_user_password(self, username):
251  req = getUserPasswordSrvRequest()
252  req.username = username
253 
254  response = self._get_user_passwd_proxy(req)
255  if response.success:
256  return response.password
257  else:
258  raise RappError('Wrong credentials')
259 
260  ## @brief Check if the application_token is active/valid
261  #
262  # @param token [string] The user's application token
263  #
264  # @return status [bool] True if it is valid, flase otherwise
265  def verify_active_application_token(self, application_token):
266 
267  req = checkActiveApplicationTokenSrvRequest()
268  req.application_token = application_token
269 
270  response = self._verify_appl_token_proxy(req)
271  if response.success:
272  return response.application_token_exists
273  else:
274  return False
275 
276  ## @brief Retrieve username associated with application_token from platform database
277  #
278  # @param token [string] The user's application token
279  #
280  # @return username [string] The username
281  def get_token_user(self, app_token):
282  req = getUsernameAssociatedWithApplicationTokenSrvRequest()
283  req.application_token = app_token
284 
285  response = self._get_token_user_proxy(req)
286  return response.username
287 
288  ## @brief Check if there is an active session (token) for the specified user and
289  # store_token (device_token)
290  #
291  # @param username [string] The username
292  # @param store_token [string] The device_token associated with the appl_token
293  #
294  # @return status [bool] True if token exists, false otherwise
295  def verify_active_robot_session(self, username, store_token):
296  req = checkActiveRobotSessionSrvRequest()
297  req.username = username
298  req.device_token = store_token
299 
300  response = self._verify_robot_session_proxy(req)
301  if response.success:
302  return response.application_token_exists
303  else:
304  return False
305 
306  ## @brief Write new user to the platform database
307  #
308  # @param username [string] The username
309  # @param password [string] The user's password
310  # @param store_token [string] The user's store token
311  # @param language [string] The user's language
312  #
313  # @return status [bool] True if token exists, false otherwise
314  #
315  # @exception RappError Failed to create the new user
316  def add_new_user(self, username, password, creator, language):
317  req = createNewPlatformUserSrvRequest()
318  req.username = username
319  req.password = password
320  req.creator_username = creator
321  req.language = language
322 
323  response = self._add_new_user_proxy(req)
324  if response.success:
325  RappUtilities.rapp_print('Succesfully wrote new user', 'DEBUG')
326  else:
327  RappUtilities.rapp_print(response.error, 'ERROR')
328  msg = 'Could not write new user to the database'
329  RappUtilities.rapp_print(msg, 'ERROR')
330  raise RappError(msg)
331 
332  ## @brief Write new token to the platform database
333  #
334  # @param username [string] The user issuing the token
335  # @param store_token [string] The device_token of the application token
336  # @param app_token [string] The user's application token
337  #
338  # @exception RappError Failed to write token
339  def write_new_application_token(self, username, store_token, appl_token):
340  req = createNewApplicationTokenSrvRequest()
341  req.username = username
342  req.store_token = store_token
343  req.application_token = appl_token
344 
345  response = self._create_new_app_token_proxy(req)
346  if response.success:
347  RappUtilities.rapp_print('Succesfully wrote new token', 'DEBUG')
348  else:
349  msg = 'Could not write new application token to the database'
350  RappUtilities.rapp_print(msg, 'ERROR')
351  raise RappError(msg)
352 
353  ## @brief Write store token the device table
354  #
355  # @param store_token [string] The device_token of the application token
356  #
357  # @exception RappError Failed to write token
358  def add_store_token_to_device(self, store_token):
359  req = addStoreTokenToDeviceSrvRequest()
360  req.store_token = store_token
361  res = self._add_store_token_to_device_proxy(req)
362 
363  if res.error == '':
364  RappUtilities.rapp_print('Succesfully wrote new token', 'DEBUG')
365  else:
366  RappUtilities.rapp_print(res.error, 'ERROR')
367  msg = 'Could not write store token to the database'
368  RappUtilities.rapp_print(msg, 'ERROR')
369  raise RappError(msg)
370 
371  ## @brief Check if user has an admin/root role
372  #
373  # @param username [string] The user's username
374  #
375  # @exception RappError Invalid user role
376  def validate_user_role(self, username):
377  req = validateUserRoleSrvRequest()
378  req.username = username
379  res = self._validate_user_role_proxy(req)
380 
381  if res.error == '':
382  RappUtilities.rapp_print('Proper user role', 'DEBUG')
383  else:
384  # RappUtilities.rapp_print(res.trace, 'ERROR')
385  msg = 'Invalid user role'
386  RappUtilities.rapp_print(msg, 'ERROR')
387  raise RappError(msg)
def verify_active_robot_session
Check if there is an active session (token) for the specified user and store_token (device_token) ...
_verify_appl_token_proxy
Service proxy to query database for application token validity.
def verify_active_application_token
Check if the application_token is active/valid.
_username_exists_proxy
Service proxy to query database for user existance.
_validate_existing_device_token_proxy
Service proxy to query the database for a device token.
_add_new_user_proxy
Service proxy to add new user to the database.
Handles database functions for authentication manager.
_validate_user_role_proxy
Service proxy to query the database for the user's role.
_add_store_token_to_device_proxy
Service proxy to add new device token from store to the database.
def get_token_user
Retrieve username associated with application_token from platform database.
def write_new_application_token
Write new token to the platform database.
def verify_platform_device_token
Verify that the device token exists in platform database.
_get_user_passwd_proxy
Service proxy to query database for user password.
_create_new_app_token_proxy
Service proxy to add new application token to the database.
def get_user_password
Retrieve user's password from platform database.
def verify_store_device_token
Verify that the device token exists in store database.
def username_exists
Verify that username exists in platform database.
_get_token_user_proxy
Service proxy to query database for the user associated with a token.
_verify_robot_session_proxy
Service proxy to query database for active user session.