def post(self): # get the post data post_data = request.get_json() if post_data is None: return CommonResponseObject.fail_response( 'Please provide required data', status.HTTP_404_NOT_FOUND) try: # fetch the user data user = User.get_user_by_email(post_data.get('email')) if user and not user.is_confirmed: return CommonResponseObject.fail_response( 'Please confirm your email address which is sent to your email', status.HTTP_403_FORBIDDEN) mac_address = post_data.get('mac_address') if not mac_address: return CommonResponseObject.fail_response( 'Please provide your MAC address', status.HTTP_412_PRECONDITION_FAILED) if user and bcrypt.check_password_hash(user.password, post_data.get('password')): device = DeviceList.get_device_by_user_id_and_mac( user.id, mac_address) root = DeviceList.get_root_device(user.id) auth_token = DatabaseCheck.prepare_auth_token( user.id, mac_address, None if not device else device.main_key, True if root else False) if auth_token: return CommonResponseObject.login_success(auth_token) else: return CommonResponseObject.login_user_not_exist() except Exception as e: print(e) return CommonResponseObject.login_exception()
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.')