-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
62 lines (51 loc) · 2.09 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import base64
from datetime import datetime
import io
import random
import string
from typing import Optional
from uuid import UUID
from fastapi import Cookie, FastAPI, Request, Response
from captcha.image import ImageCaptcha
from fastapi.responses import StreamingResponse
from starlette.middleware.sessions import SessionMiddleware
cookies = dict()
app = FastAPI(docs_url='/docs')
app.add_middleware(SessionMiddleware,secret_key = "Some-secret-key")
@app.post("set_cookie/")
def create_cookie(rep:Response,id :UUID, username: str ):
rep.set_cookie(key="Username", value=username)
rep.set_cookie(key="Identifier", value=id)
# managing cookies
@app.get("get_cookie/")
def get_cookie(userkey: Optional[str] = Cookie(None), identifier: Optional[str] = Cookie(None)):
cookies['userkey'] = userkey
cookies['identifier'] = identifier
return cookies
@app.middleware("http")
def log_transaction_filter(request: Request, call_next):
start_time = datetime.now()
query_params = request.query_params
path_params = request.path_params
method = request.method
with open("request_log.txt", mode="a") as req_file:
content = f"method: {method}, query_params: {query_params}, path_params : {path_params}, recived: {datetime.now()}"
req_file.write(content)
response = call_next(request)
process_time = datetime.now() - start_time
# request.headers["X-TIME-PROCESS"] = str(process_time)
return response
def captcha_generator(size: int):
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(size))
def generate_captcha():
captcha: str = captcha_generator(5)
image = ImageCaptcha()
data = image.generate(captcha)
data = base64.b64encode(data.getvalue())
return {"data": data, "captcha": captcha}
@app.get('/start-session')
def start_session(request: Request):
captcha = generate_captcha()
request.session["captcha"] = captcha['captcha']
captcha_image = captcha["data"].decode("utf-8")
return StreamingResponse(io.BytesIO(base64.b64decode(captcha_image)), media_type="image/png")