예제 #1
0
 def __check_for_require_params(self, auth_token, mac_address, otp_modulus,
                                otp_exponent, main_key, backup_key):
     """
     Check if the params is qualified
     :params auth_token mac_address public_key:
     :return user_id or responseObject:
     """
     if not auth_token:
         return CommonResponseObject.unauthorized_token_response()
     user_id = User.decode_auth_token(auth_token)
     if not main_key:
         return CommonResponseObject.fail_response(
             'Please provide the main key',
             status.HTTP_412_PRECONDITION_FAILED)
     if not backup_key:
         return CommonResponseObject.fail_response(
             'Please provide the backup_key',
             status.HTTP_412_PRECONDITION_FAILED)
     if isinstance(user_id, str):
         return CommonResponseObject.unauthorized_token_response()
     if not isinstance(mac_address, str):
         return CommonResponseObject.fail_response(
             'Please provide your Mac address',
             status.HTTP_412_PRECONDITION_FAILED)
     user = User.get_user_by_id(user_id)
     if not user:
         return CommonResponseObject.unauthorized_token_response()
     if DatabaseCheck.is_mac_address_existed(mac_address):
         return CommonResponseObject.fail_response(
             'Your device is the root device or already requested for authorization',
             status.HTTP_202_ACCEPTED)
     return user
예제 #2
0
 def __check_for_require_params(self,auth_token,mac_address,key_mod,key_ex):
     """
     Check if the params is qualified
     :params auth_token mac_address public_key:
     :return user_id or responseObject:
     """
     if not auth_token:#check if auth_token is available
         return CommonResponseObject.unauthorized_token_response()
     #get user_id and key from the auth_token
     user_id= User.decode_auth_token(auth_token)
     if isinstance(user_id,str):#check if user_id is valid
         return CommonResponseObject.unauthorized_token_response()
     if not mac_address: #check if mac_address is valid
         return CommonResponseObject.fail_response(
             'Please provide your MAC address',
             status.HTTP_412_PRECONDITION_FAILED)
     #check if key is valid
     modulus, exponent = User.decode_public_key(auth_token)
     key = RSAPair.get_RSA_by_public(modulus)
     if not key:#check if key is existed
         return CommonResponseObject.response(
             'Some errors occured, provided key does not exists')
     user = User.get_user_by_id(user_id) #retrieve the user entity
     if not user: #check if the user is existed
         return CommonResponseObject.unauthorized_token_response()
     #check if the mac_address is stored
     if DatabaseCheck.is_mac_address_existed(mac_address):
         if DatabaseCheck.is_root_by_mac(mac_address):
             return CommonResponseObject.fail_response(
                 'Your device is the root device',
                 status.HTTP_202_ACCEPTED)
         return CommonResponseObject.fail_response(
             'Your device is already authorized',
             status.HTTP_202_ACCEPTED)
     return user,key
예제 #3
0
 def __check_for_require_params(self,auth_token,mac_address,encrypted_key):
     """
     Check if the params is qualified, return error json response
     if any requisite does not meet, else check and return user
     entity for the corresponding user id
     :params:
         :auth_token:
         :mac_address:
         :public_key:
     :return:
         :user_id: or :responseObject:
     """
     if not auth_token: # Check if the auth_token is valid
         return CommonResponseObject.unauthorized_token_response()
     user_id = User.decode_auth_token(auth_token)
     if isinstance(user_id,str): # Check if user_id is provided
         return CommonResponseObject.unauthorized_token_response()
     if not isinstance(mac_address,str): # Check if mac address is provided
         return CommonResponseObject.fail_response(
             'Please provide your Mac address',
             status.HTTP_412_PRECONDITION_FAILED)
     if not encrypted_key:#check if encrypted_key is provided
         return CommonResponseObject.fail_response(
             'Please provide your encrypted key for authorization',
             status.HTTP_412_PRECONDITION_FAILED)
     user = User.get_user_by_id(user_id) #get user from the database
     if not user:#if user is not available
         return CommonResponseObject.unauthorized_token_response()
     return user
예제 #4
0
 def get(self):
     # Get the access token from the header
     auth_token = RequestUtils.get_access_token(request)
     if auth_token:
         response = User.decode_auth_token(auth_token)
         if not isinstance(response,str):
             device_list = DeviceList.get_device_by_user_id(response)
             data = [device.serialize() for device in device_list]
             return CommonResponseObject.success_response(data)
         return CommonResponseObject.fail_response(response,status.HTTP_401_UNAUTHORIZED)
     else:
         return CommonResponseObject.unauthorized_token_response()
예제 #5
0
 def get(self):
     auth_header = request.headers.get('Authorization')
     if auth_header:
         auth_token = auth_header.split(" ")[1]
     else:
         auth_token = ''
     if auth_token:
         response = User.decode_auth_token(auth_token)
         if not isinstance(response, str):
             user = User.get_user_by_id(response)
             data = json.dumps(dict(encrypted_key=user.encrypted_key))
             return CommonResponseObject.success_response(data)
         return CommonResponseObject.token_response(response)
     else:
         return CommonResponseObject.unauthorized_token_response()