fix: return 200 status for lnurl errors and fix uniques (#43)
Some checks failed
/ release (push) Has been cancelled
/ pullrequest (push) Has been cancelled

This commit is contained in:
dni ⚡ 2024-07-23 15:15:04 +02:00 committed by GitHub
commit 4e6d61fa01
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,12 +1,12 @@
import json import json
from datetime import datetime from datetime import datetime
from http import HTTPStatus from http import HTTPStatus
from typing import Callable from typing import Callable, Optional
from urllib.parse import urlparse from urllib.parse import urlparse
import httpx import httpx
import shortuuid import shortuuid
from fastapi import APIRouter, HTTPException, Query, Request, Response from fastapi import APIRouter, HTTPException, Request, Response
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from fastapi.routing import APIRoute from fastapi.routing import APIRoute
from lnbits.core.crud import update_payment_extra from lnbits.core.crud import update_payment_extra
@ -30,15 +30,13 @@ class LNURLErrorResponseHandler(APIRoute):
async def custom_route_handler(request: Request) -> Response: async def custom_route_handler(request: Request) -> Response:
try: try:
response = await original_route_handler(request) response = await original_route_handler(request)
return response
except HTTPException as exc: except HTTPException as exc:
logger.debug(f"HTTPException: {exc}") logger.debug(f"HTTPException: {exc}")
response = JSONResponse( response = JSONResponse(
status_code=exc.status_code, status_code=200,
content={"status": "ERROR", "reason": f"{exc.detail}"}, content={"status": "ERROR", "reason": f"{exc.detail}"},
) )
except Exception as exc:
raise exc
return response return response
return custom_route_handler return custom_route_handler
@ -65,6 +63,13 @@ async def api_lnurl_response(request: Request, unique_hash: str):
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw is spent." status_code=HTTPStatus.NOT_FOUND, detail="Withdraw is spent."
) )
if link.is_unique:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
detail="This link requires an id_unique_hash.",
)
url = str( url = str(
request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash) request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash)
) )
@ -105,10 +110,10 @@ async def api_lnurl_response(request: Request, unique_hash: str):
}, },
) )
async def api_lnurl_callback( async def api_lnurl_callback(
unique_hash, unique_hash: str,
k1: str = Query(...), k1: str,
pr: str = Query(...), pr: str,
id_unique_hash=None, id_unique_hash: Optional[str] = None,
): ):
link = await get_withdraw_link_by_hash(unique_hash) link = await get_withdraw_link_by_hash(unique_hash)
@ -133,6 +138,12 @@ async def api_lnurl_callback(
detail=f"wait link open_time {link.open_time - now} seconds.", detail=f"wait link open_time {link.open_time - now} seconds.",
) )
if not id_unique_hash and link.is_unique:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="id_unique_hash is required for this link.",
)
if id_unique_hash: if id_unique_hash:
if check_unique_link(link, id_unique_hash): if check_unique_link(link, id_unique_hash):
await remove_unique_withdraw_link(link, id_unique_hash) await remove_unique_withdraw_link(link, id_unique_hash)