-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
73 lines (60 loc) · 1.94 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
from typing import Optional
from ecies import decrypt, encrypt
from litestar import Litestar, MediaType, Request, Response, post
from litestar.enums import RequestEncodingType
from litestar.openapi import OpenAPIConfig
from litestar.openapi.datastructures import ResponseSpec
from litestar.openapi.plugins import SwaggerRenderPlugin
from litestar.params import Body
from msgspec import Struct
class RequestException(Exception):
def __init__(self, detail: str):
self.detail = detail
class Payload(Struct):
data: str
prv: Optional[str] = None
pub: Optional[str] = None
class RequestError(Struct):
detail: str
@post(
"/",
responses={
400: ResponseSpec(data_container=RequestError),
},
)
async def encrypt_decrypt(
data: Payload = Body(media_type=RequestEncodingType.URL_ENCODED),
) -> str:
if data.prv and data.data:
try:
decrypted = decrypt(data.prv, bytes.fromhex(data.data))
try:
return decrypted.decode()
except ValueError:
return decrypted.hex()
except ValueError:
raise RequestException(detail="Invalid private key or data")
elif data.pub and data:
try:
encrypted = encrypt(data.pub, data.data.encode())
return encrypted.hex()
except ValueError:
raise RequestException(detail="Invalid public key or data")
else:
raise RequestException(detail="Invalid request")
def request_exception_handler(_: Request, exc: RequestException) -> Response:
return Response(
media_type=MediaType.JSON,
content={"detail": exc.detail},
status_code=400,
)
app = Litestar(
route_handlers=[encrypt_decrypt],
exception_handlers={RequestException: request_exception_handler},
openapi_config=OpenAPIConfig(
"eciespy demo",
version="0.1.0",
path="/docs",
render_plugins=[SwaggerRenderPlugin()],
),
)