Added wallet authentication to creat, get and delete tinyurls

This commit is contained in:
ben
2023-01-24 21:26:18 +00:00
parent 7322d27aa3
commit c0c4c86808
4 changed files with 76 additions and 25 deletions
+58 -13
View File
@@ -48,6 +48,7 @@ from lnbits.utils.exchange_rates import (
from .. import core_app, db
from ..crud import (
create_tinyurl,
delete_tinyurl,
get_payments,
get_standalone_payment,
get_tinyurl,
@@ -715,23 +716,67 @@ async def websocket_update_get(item_id: str, data: str):
@core_app.post("/api/v1/tinyurl")
async def api_create_tinyurl(url: str, endless: bool = False):
tinyurl = await get_tinyurl_by_url(url)
if tinyurl:
return tinyurl
return await create_tinyurl(url, endless)
async def api_create_tinyurl(
url: str, endless: bool = False, wallet: WalletTypeInfo = Depends(get_key_type)
):
tinyurls = await get_tinyurl_by_url(url)
try:
for tinyurl in tinyurls:
if tinyurl:
if tinyurl.wallet == wallet.wallet.inkey:
return tinyurl
return await create_tinyurl(url, endless, wallet.wallet.inkey)
except:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Unable to create tinyurl"
)
@core_app.get("/api/v1/tinyurl/{tinyurl_id}")
async def api_get_tinyurl(tinyurl_id: str):
return await get_tinyurl(tinyurl_id)
async def api_get_tinyurl(
tinyurl_id: str, wallet: WalletTypeInfo = Depends(get_key_type)
):
try:
tinyurl = await get_tinyurl(tinyurl_id)
if tinyurl.wallet == wallet.wallet.inkey:
return tinyurl
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Wrong key provided."
)
except:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Unable to fetch tinyurl"
)
@core_app.delete("/api/v1/tinyurl/{tinyurl_id}")
async def api_delete_tinyurl(
tinyurl_id: str, wallet: WalletTypeInfo = Depends(get_key_type)
):
try:
tinyurl = await get_tinyurl(tinyurl_id)
if tinyurl.wallet == wallet.wallet.inkey:
await delete_tinyurl(tinyurl_id)
return {"deleted": True}
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Wrong key provided."
)
except:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Unable to delete"
)
@core_app.get("/t/{tinyurl_id}")
async def api_tinyurl(tinyurl_id: str):
tinyurl = await get_tinyurl(tinyurl_id)
if tinyurl:
response = RedirectResponse(url=tinyurl.url)
return response
else:
return
try:
tinyurl = await get_tinyurl(tinyurl_id)
if tinyurl:
response = RedirectResponse(url=tinyurl.url)
return response
else:
return
except:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="unable to find tinyurl"
)