black & isort

This commit is contained in:
Gene Takavic
2022-08-14 10:52:58 -06:00
committed by Lee Salminen
parent 5af49e3801
commit 5b8d317441
6 changed files with 88 additions and 86 deletions
+52 -59
View File
@@ -17,19 +17,20 @@ from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
from lnbits.extensions.withdraw import get_withdraw_link
from . import boltcards_ext
from .nxp424 import decryptSUN, getSunMAC
from .crud import (
create_hit,
get_all_cards,
get_cards,
get_card,
create_card,
create_hit,
delete_card,
get_all_cards,
get_card,
get_cards,
get_hits,
update_card,
delete_card,
update_card_counter
update_card_counter,
)
from .models import CreateCardData
from .nxp424 import decryptSUN, getSunMAC
@boltcards_ext.get("/api/v1/cards")
async def api_cards(
@@ -42,32 +43,15 @@ async def api_cards(
return [card.dict() for card in await get_cards(wallet_ids)]
@boltcards_ext.post("/api/v1/cards", status_code=HTTPStatus.CREATED)
@boltcards_ext.put("/api/v1/cards/{card_id}", status_code=HTTPStatus.OK)
async def api_link_create_or_update(
# req: Request,
# req: Request,
data: CreateCardData,
card_id: str = None,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
'''
TODO: some checks
if data.uses > 250:
raise HTTPException(
detail="250 uses max.", status_code=HTTPStatus.BAD_REQUEST
)
if data.min_withdrawable < 1:
raise HTTPException(
detail="Min must be more than 1.", status_code=HTTPStatus.BAD_REQUEST
)
if data.max_withdrawable < data.min_withdrawable:
raise HTTPException(
detail="`max_withdrawable` needs to be at least `min_withdrawable`.",
status_code=HTTPStatus.BAD_REQUEST,
)
'''
if card_id:
card = await get_card(card_id)
if not card:
@@ -78,15 +62,12 @@ async def api_link_create_or_update(
raise HTTPException(
detail="Not your card.", status_code=HTTPStatus.FORBIDDEN
)
card = await update_card(
card_id, **data.dict()
)
card = await update_card(card_id, **data.dict())
else:
card = await create_card(
wallet_id=wallet.wallet.id, data=data
)
card = await create_card(wallet_id=wallet.wallet.id, data=data)
return card.dict()
@boltcards_ext.delete("/api/v1/cards/{card_id}")
async def api_link_delete(card_id, wallet: WalletTypeInfo = Depends(require_admin_key)):
card = await get_card(card_id)
@@ -97,13 +78,12 @@ async def api_link_delete(card_id, wallet: WalletTypeInfo = Depends(require_admi
)
if card.wallet != wallet.wallet.id:
raise HTTPException(
detail="Not your card.", status_code=HTTPStatus.FORBIDDEN
)
raise HTTPException(detail="Not your card.", status_code=HTTPStatus.FORBIDDEN)
await delete_card(card_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
@boltcards_ext.get("/api/v1/hits")
async def api_hits(
g: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
@@ -120,20 +100,33 @@ async def api_hits(
return [hit.dict() for hit in await get_hits(cards_ids)]
# /boltcards/api/v1/scan/?uid=00000000000000&ctr=000000&c=0000000000000000
@boltcards_ext.get("/api/v1/scan/")
async def api_scan(
uid, ctr, c,
request: Request
):
@boltcards_ext.get("/api/v1/scan/")
async def api_scan(uid, ctr, c, request: Request):
card = await get_card(uid, id_is_uid=True)
if card == None:
return {"status": "ERROR", "reason": "Unknown card."}
if c != getSunMAC(bytes.fromhex(uid), bytes.fromhex(ctr)[::-1], bytes.fromhex(card.file_key)).hex().upper():
if (
c
!= getSunMAC(
bytes.fromhex(uid), bytes.fromhex(ctr)[::-1], bytes.fromhex(card.file_key)
)
.hex()
.upper()
):
print(c)
print(getSunMAC(bytes.fromhex(uid), bytes.fromhex(ctr)[::-1], bytes.fromhex(card.file_key)).hex().upper())
print(
getSunMAC(
bytes.fromhex(uid),
bytes.fromhex(ctr)[::-1],
bytes.fromhex(card.file_key),
)
.hex()
.upper()
)
return {"status": "ERROR", "reason": "CMAC does not check."}
ctr_int = int(ctr, 16)
@@ -145,32 +138,32 @@ async def api_scan(
# gathering some info for hit record
ip = request.client.host
if request.headers['x-real-ip']:
ip = request.headers['x-real-ip']
elif request.headers['x-forwarded-for']:
ip = request.headers['x-forwarded-for']
if request.headers["x-real-ip"]:
ip = request.headers["x-real-ip"]
elif request.headers["x-forwarded-for"]:
ip = request.headers["x-forwarded-for"]
agent = request.headers['user-agent'] if 'user-agent' in request.headers else ''
agent = request.headers["user-agent"] if "user-agent" in request.headers else ""
await create_hit(card.id, ip, agent, card.counter, ctr_int)
link = await get_withdraw_link(card.withdraw, 0)
return link.lnurl_response(request)
# /boltcards/api/v1/scane/?e=00000000000000000000000000000000&c=0000000000000000
@boltcards_ext.get("/api/v1/scane/")
async def api_scane(
e, c,
request: Request
):
async def api_scane(e, c, request: Request):
card = None
counter = b''
counter = b""
# since this route is common to all cards I don't know whitch 'meta key' to use
# so I try one by one until decrypted uid matches
for cand in await get_all_cards():
if cand.meta_key:
card_uid, counter = decryptSUN(bytes.fromhex(e), bytes.fromhex(cand.meta_key))
card_uid, counter = decryptSUN(
bytes.fromhex(e), bytes.fromhex(cand.meta_key)
)
if card_uid.hex().upper() == cand.uid:
card = cand
@@ -187,17 +180,17 @@ async def api_scane(
ctr_int = int.from_bytes(counter, "little")
if ctr_int <= card.counter:
return {"status": "ERROR", "reason": "This link is already used."}
await update_card_counter(ctr_int, card.id)
# gathering some info for hit record
ip = request.client.host
if 'x-real-ip' in request.headers:
ip = request.headers['x-real-ip']
elif 'x-forwarded-for' in request.headers:
ip = request.headers['x-forwarded-for']
if "x-real-ip" in request.headers:
ip = request.headers["x-real-ip"]
elif "x-forwarded-for" in request.headers:
ip = request.headers["x-forwarded-for"]
agent = request.headers['user-agent'] if 'user-agent' in request.headers else ''
agent = request.headers["user-agent"] if "user-agent" in request.headers else ""
await create_hit(card.id, ip, agent, card.counter, ctr_int)