Add a secondary route with the card_uid appended to it.

This commit is contained in:
Lee Salminen 2022-08-21 08:58:38 -06:00
parent db83d803f8
commit 4242a82029
2 changed files with 25 additions and 12 deletions

View File

@ -82,6 +82,14 @@ async def get_card(card_id: str) -> Optional[Card]:
return Card.parse_obj(card) return Card.parse_obj(card)
async def get_card_by_uid(card_uid: str) -> Optional[Card]:
row = await db.fetchone("SELECT * FROM boltcards.cards WHERE uid = ?", (card_uid,))
if not row:
return None
card = dict(**row)
return Card.parse_obj(card)
async def get_card_by_otp(otp: str) -> Optional[Card]: async def get_card_by_otp(otp: str) -> Optional[Card]:
row = await db.fetchone("SELECT * FROM boltcards.cards WHERE otp = ?", (otp,)) row = await db.fetchone("SELECT * FROM boltcards.cards WHERE otp = ?", (otp,))

View File

@ -25,6 +25,7 @@ from .crud import (
get_all_cards, get_all_cards,
get_card, get_card,
get_card_by_otp, get_card_by_otp,
get_card_by_uid,
get_cards, get_cards,
get_hits, get_hits,
update_card, update_card,
@ -131,25 +132,29 @@ async def api_hits(
# /boltcards/api/v1/scan?p=00000000000000000000000000000000&c=0000000000000000 # /boltcards/api/v1/scan?p=00000000000000000000000000000000&c=0000000000000000
@boltcards_ext.get("/api/v1/scan") @boltcards_ext.get("/api/v1/scan")
async def api_scane(p, c, request: Request): @boltcards_ext.get("/api/v1/scan/{card_uid}")
async def api_scane(p, c, card_uid: str = None, request: Request):
# some wallets send everything as lower case, no bueno # some wallets send everything as lower case, no bueno
p = p.upper() p = p.upper()
c = c.upper() c = c.upper()
card = None card = None
counter = b"" counter = b""
# since this route is common to all cards I don't know whitch 'meta key' to use if not card_uid:
# so I try one by one until decrypted uid matches # since this route is common to all cards I don't know whitch 'meta key' to use
for cand in await get_all_cards(): # so I try one by one until decrypted uid matches
if cand.k1: for cand in await get_all_cards():
try: if cand.k1:
card_uid, counter = decryptSUN(bytes.fromhex(p), bytes.fromhex(cand.k1)) try:
card_uid, counter = decryptSUN(bytes.fromhex(p), bytes.fromhex(cand.k1))
if card_uid.hex().upper() == cand.uid.upper(): if card_uid.hex().upper() == cand.uid.upper():
card = cand card = cand
break break
except: except:
continue continue
else:
card = await get_card_by_uid(card_uid)
if card == None: if card == None:
return {"status": "ERROR", "reason": "Unknown card."} return {"status": "ERROR", "reason": "Unknown card."}