Define basic library structure

This commit is contained in:
havlong
2022-12-15 18:31:39 +03:00
parent 231751bb58
commit e184c49a1f
9 changed files with 99 additions and 8 deletions

View File

@@ -0,0 +1,10 @@
class ClientAPI:
client_id: str
client_secret: str
def __init__(self, client_id: str, client_secret: str):
self.client_id = client_id
self.client_secret = client_secret
def get_token(self) -> str:
return self.client_secret

View File

@@ -0,0 +1,15 @@
from ed25519.keys import VerifyingKey
class SecurityHelper:
public_key: VerifyingKey
def __init__(self, public_key: str):
self.public_key = VerifyingKey(public_key.encode(), encoding='base64')
def check(self, http: bytes, signature: bytes) -> bool:
try:
self.public_key.verify(signature, http, encoding='base64')
return True
except AssertionError:
return False

View File

@@ -0,0 +1,38 @@
from typing import List
from fastapi import FastAPI, Request, HTTPException
from client import ClientAPI
from security import SecurityHelper
from server_utils.models import PongModel, Intent
def VueApp(client_api: ClientAPI, security_helper: SecurityHelper, intents: List[Intent], debug: bool = False):
app = FastAPI(debug=debug, title='VueApp', description='Vue Application Server', version='0.0.1')
async def check_request(request: Request) -> bool:
if 'X-Signature-Ed25519' not in request.headers or 'X-Signature-Timestamp' not in request.headers:
return False
body_to_verify = await request.body()
timestamp = request.headers['X-Signature-Timestamp'].encode('utf-8')
signature = request.headers['X-Signature-Ed25519'].encode('utf-8')
return security_helper.check(timestamp + body_to_verify, signature)
@app.post(path='/ping', response_model=PongModel)
async def handle_ping(request: Request) -> PongModel:
ok = await check_request(request)
if not ok:
raise HTTPException(status_code=401, detail='Verification of signature failed')
return PongModel(message='pong', token=client_api.get_token())
if Intent.INLINE_QUERY in intents:
@app.post(path='/auto', response_model=PongModel)
async def handle_autocomplete(request: Request) -> PongModel:
ok = await check_request(request)
if not ok:
raise HTTPException(status_code=401, detail='Verification of signature failed')
return PongModel(message='pong', token=client_api.get_token())
return app

View File

@@ -0,0 +1,20 @@
from enum import Enum, auto
from typing import Optional
from pydantic import BaseModel, Field
class PingModel(BaseModel):
nickname: str
class PongModel(BaseModel):
message: str = Field(default='ping', title='Message', description='Message answered on pings')
token: Optional[str] = Field(default=None, title='Token', description='Token for API to check')
class Intent(Enum):
PAGE_APP = auto()
PAGE_MENTIONS = auto()
INLINE_QUERY = auto()
INTERACTIONS = auto()