[lint] main.py
This commit is contained in:
parent
1751a78a59
commit
68ae04e72a
1 changed files with 21 additions and 11 deletions
28
app/main.py
28
app/main.py
|
@ -31,21 +31,25 @@ from app.boxes import _send_create
|
||||||
from app.orgpython import to_html
|
from app.orgpython import to_html
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _check_0rtt_early_data(request: Request) -> None:
|
def _check_0rtt_early_data(request: Request) -> None:
|
||||||
"""Disable TLS1.3 0-RTT requests for non-GET."""
|
"""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")
|
raise fastapi.HTTPException(status_code=425, detail="Too early")
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(
|
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.remove()
|
||||||
logger.add(sys.stdout, level="DEBUG" if DEBUG else "INFO")
|
logger.add(sys.stdout, level="DEBUG" if DEBUG else "INFO")
|
||||||
logger.add("output.log", level="DEBUG")
|
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."""
|
"""Simple wrap JSONresponse return ActivityPub response."""
|
||||||
media_type = "application/activity+json"
|
media_type = "application/activity+json"
|
||||||
|
|
||||||
|
@ -80,7 +84,7 @@ async def index(
|
||||||
async def inbox(
|
async def inbox(
|
||||||
request: Request,
|
request: Request,
|
||||||
db_session: AsyncSession = Depends(get_db_session),
|
db_session: AsyncSession = Depends(get_db_session),
|
||||||
httpsig_checker = Depends(precheck.inbox_prechecker),
|
httpsig_checker=Depends(precheck.inbox_prechecker),
|
||||||
) -> Response:
|
) -> Response:
|
||||||
"""ActivityPub inbox endpoint."""
|
"""ActivityPub inbox endpoint."""
|
||||||
payload = await request.json()
|
payload = await request.json()
|
||||||
|
@ -111,13 +115,19 @@ async def outbox(
|
||||||
logger.info(content)
|
logger.info(content)
|
||||||
post_token = request.headers.get("Authorization")
|
post_token = request.headers.get("Authorization")
|
||||||
|
|
||||||
if POST_TOKEN is None \
|
def _check_post_token() -> bool:
|
||||||
and POST_TOKEN != post_token.split(" ")[1]: # type: ignore
|
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!")
|
logger.warning("Non-valid post token!")
|
||||||
return Response(status_code=406)
|
return Response(status_code=406)
|
||||||
|
|
||||||
logger.info("True token")
|
logger.info("True token")
|
||||||
|
|
||||||
content = to_html(content).replace("\n", "")
|
content = to_html(content).replace("\n", "")
|
||||||
|
|
||||||
await _send_create(
|
await _send_create(
|
||||||
|
@ -205,7 +215,7 @@ async def nodeinfo():
|
||||||
},
|
},
|
||||||
"protocols": ["activitypub"],
|
"protocols": ["activitypub"],
|
||||||
"services": {"inbound": [], "outbound": []},
|
"services": {"inbound": [], "outbound": []},
|
||||||
"usage": {"users": {"total": 1}, "localPosts": 0}, #TODO
|
"usage": {"users": {"total": 1}, "localPosts": 0}, # TODO
|
||||||
"openRegistrations": False,
|
"openRegistrations": False,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue