[lint] main.py
All checks were successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful

This commit is contained in:
SouthFox 2023-07-29 20:49:12 +08:00
parent 1751a78a59
commit 68ae04e72a

View file

@ -31,21 +31,25 @@ from app.boxes import _send_create
from app.orgpython import to_html
def _check_0rtt_early_data(request: Request) -> None:
"""Disable TLS1.3 0-RTT requests for non-GET."""
if request.headers.get("Early-Data", None) == "1" and request.method != "GET":
if request.headers.get("Early-Data", None) == "1" \
and request.method != "GET":
raise fastapi.HTTPException(status_code=425, detail="Too early")
app = FastAPI(
docs_url=None, redoc_url=None, dependencies=[Depends(_check_0rtt_early_data)]
docs_url=None, redoc_url=None,
dependencies=[Depends(_check_0rtt_early_data)]
)
logger.remove()
logger.add(sys.stdout, level="DEBUG" if DEBUG else "INFO")
logger.add("output.log", level="DEBUG")
class ActivityPubResponse(JSONResponse): #pylint: disable=too-few-public-methods
# pylint: disable=too-few-public-methods
class ActivityPubResponse(JSONResponse):
"""Simple wrap JSONresponse return ActivityPub response."""
media_type = "application/activity+json"
@ -80,7 +84,7 @@ async def index(
async def inbox(
request: Request,
db_session: AsyncSession = Depends(get_db_session),
httpsig_checker = Depends(precheck.inbox_prechecker),
httpsig_checker=Depends(precheck.inbox_prechecker),
) -> Response:
"""ActivityPub inbox endpoint."""
payload = await request.json()
@ -111,13 +115,19 @@ async def outbox(
logger.info(content)
post_token = request.headers.get("Authorization")
if POST_TOKEN is None \
and POST_TOKEN != post_token.split(" ")[1]: # type: ignore
def _check_post_token() -> bool:
if post_token is None:
return False
if POST_TOKEN != post_token.split(" ")[1]:
return False
return True
if not _check_post_token():
logger.warning("Non-valid post token!")
return Response(status_code=406)
logger.info("True token")
content = to_html(content).replace("\n", "")
await _send_create(
@ -205,7 +215,7 @@ async def nodeinfo():
},
"protocols": ["activitypub"],
"services": {"inbound": [], "outbound": []},
"usage": {"users": {"total": 1}, "localPosts": 0}, #TODO
"usage": {"users": {"total": 1}, "localPosts": 0}, # TODO
"openRegistrations": False,
"metadata": {},
},