From 83a91d89ae622686fc38ea112666ff00b9204205 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Wed, 5 Feb 2025 23:09:52 +0300 Subject: [PATCH] Add token-based authentication for HTML and JSON post retrieval endpoints --- api_server.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/api_server.py b/api_server.py index 614041a..0727f32 100644 --- a/api_server.py +++ b/api_server.py @@ -344,7 +344,16 @@ def calculate_cache_stats(): @app.get("/html/{channel}/{post_id}", response_class=HTMLResponse) @app.get("/post/html/{channel}/{post_id}", response_class=HTMLResponse) -async def get_post_html(channel: str, post_id: int): +@app.get("/html/{channel}/{post_id}/{token}", response_class=HTMLResponse) +@app.get("/post/html/{channel}/{post_id}/{token}", response_class=HTMLResponse) +async def get_post_html(channel: str, post_id: int, token: str | None = None): + if Config["token"]: + if token != Config["token"]: + logger.error(f"Invalid token for HTML post: {token}, expected: {Config['token']}") + raise HTTPException(status_code=403, detail="Invalid token") + else: + logger.info(f"Valid token for HTML post: {token}") + try: parser = PostParser(client.client) html_content = await parser.get_post(channel, post_id, 'html') @@ -359,7 +368,16 @@ async def get_post_html(channel: str, post_id: int): @app.get("/json/{channel}/{post_id}") @app.get("/post/json/{channel}/{post_id}") -async def get_post(channel: str, post_id: int): +@app.get("/json/{channel}/{post_id}/{token}") +@app.get("/post/json/{channel}/{post_id}/{token}") +async def get_post(channel: str, post_id: int, token: str | None = None): + if Config["token"]: + if token != Config["token"]: + logger.error(f"Invalid token for JSON post: {token}, expected: {Config['token']}") + raise HTTPException(status_code=403, detail="Invalid token") + else: + logger.info(f"Valid token for JSON post: {token}") + try: parser = PostParser(client.client) json_content = await parser.get_post(channel, post_id, 'json')