Example #1
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
Example #2
0
 def post(self):
     """
     Add root device
     """
     #Get authentication token
     auth_token = RequestUtils.get_access_token(request)
     #Get post data
     post_data = request.get_json()
     mac_address = post_data.get('mac_address')
     os = post_data.get('os') or "Unknown"
     backup_key = post_data.get('backup_key')
     otp_modulus = post_data.get('otp_modulus')
     otp_exponent = post_data.get('otp_exponent')
     main_key = post_data.get('main_key')
     root = post_data.get('is_root')
     user = self.__check_for_require_params(auth_token, mac_address,
                                            otp_modulus, otp_exponent,
                                            main_key, backup_key)
     if not isinstance(user, User):
         return user
     root_device = DeviceList.get_root_device(user.id)
     if root_device and root:
         return CommonResponseObject.fail_response(
             'The account already register a root device',
             status.HTTP_202_ACCEPTED)
     device = DeviceList(user,
                         mac_address=mac_address,
                         main_key=main_key,
                         backup_key=backup_key,
                         otp_modulus=otp_modulus,
                         otp_exponent=otp_exponent,
                         os=os,
                         is_root=root)
     try:
         db.session.add(device)
         db.session.commit()
         modulus, exponent = User.decode_public_key(auth_token)
         auth_token = User.encode_auth_token(user.id, str(modulus),
                                             str(exponent), main_key)
         return CommonResponseObject.login_success(
             auth_token, 'You are able to encrypt your file now')
     except Exception as e:
         print(e)
         return CommonResponseObject.fail_response(
             'Some error occured, please try again.')