def setUp(self): self.loop = asyncio.get_event_loop() self.app_aesgcm = futurefinity.web.Application( allow_keep_alive=False, security_secret=get_random_str(32), debug=True) self.app_hmac = futurefinity.web.Application( allow_keep_alive=False, security_secret=get_random_str(32), aes_security=False, debug=True) class TestHandler(futurefinity.web.RequestHandler): async def get(self, *args, **kwargs): cookie_value = self.get_secure_cookie("test_secure_cookie") if not cookie_value: cookie_value = get_random_str(100) self.set_secure_cookie("test_secure_cookie", cookie_value) return json.dumps([False, cookie_value]) return json.dumps([True, cookie_value]) self.app_aesgcm.add_handler("/test_secure_cookie", handler=TestHandler) self.app_hmac.add_handler("/test_secure_cookie", handler=TestHandler)
def assemble(self) -> Tuple[bytes, str]: """ Generate HTTP v1 Body to bytes. It will return the body in bytes and the content-type in str. """ body = b"" boundary = "----------FutureFinityFormBoundary" boundary += ensure_str(security.get_random_str(8)).lower() content_type = "multipart/form-data; boundary=" + boundary full_boundary = b"--" + ensure_bytes(boundary) for field_name, field_value in self.items(): body += full_boundary + _CRLF_BYTES_MARK if isinstance(field_value, str): body += b"Content-Disposition: form-data; " body += ensure_bytes("name=\"%s\"\r\n" % field_name) body += _CRLF_BYTES_MARK body += ensure_bytes(field_value) body += _CRLF_BYTES_MARK else: raise ProtocolError("Unknown Field Type") for file_field in self.files.values(): body += full_boundary + _CRLF_BYTES_MARK body += file_field.assemble() body += full_boundary + b"--" + _CRLF_BYTES_MARK return body, content_type
async def get(self, *args, **kwargs): cookie_value = self.get_secure_cookie("test_secure_cookie") if not cookie_value: cookie_value = get_random_str(100) self.set_secure_cookie("test_secure_cookie", cookie_value) return json.dumps([False, cookie_value]) return json.dumps([True, cookie_value])
def set_csrf_value(self): """ Set the csrf value. """ if not hasattr(self, "__csrf_value"): self.__csrf_value = self.get_cookie("_csrf", None) if not self.__csrf_value: self.__csrf_value = security.get_random_str(32) self.set_cookie("_csrf", self.__csrf_value, expires_days=1)
def _csrf_value(self): if not hasattr(self, "__csrf_value"): self.__csrf_value = security.get_random_str(32) self.set_secure_cookie("_csrf", self.__csrf_value, expires_days=1) return self.__csrf_value
def setUp(self): self.loop = asyncio.get_event_loop() self.app = futurefinity.web.Application( allow_keep_alive=False, csrf_protect=True, security_secret=get_random_str(32), debug=True)