Fix #27. Also clear up error messages a bit

This commit is contained in:
2026-08-08 00:26:02 +02:00
parent c5934d05c6
commit 6b98e65a41
2 changed files with 15 additions and 18 deletions

View File

@@ -133,4 +133,4 @@ def checkAccess(key: str, db: Session):
logger.info("No more auths found")
return False
except exc.NoResultFound:
raise Exception("No Access with that key found, this might be a db error")
raise Exception("No AccessAuth with that key found!")

View File

@@ -77,7 +77,7 @@ def readFileOnCard(desfire: DESFire):
assert len(applications) == 1
assert applications[0] == get_list(MIFARE_APP_ID)
except AssertionError:
logger.error("No application found!")
logger.error("No application found! The card must be registered first!")
time.sleep(4)
return
# Then use the key derivation with that uid, the appid, the sysid
@@ -125,23 +125,13 @@ def DeleteCard():
aes_null_key = DESFireKey(aes_keysettings, "00" * 16)
desfire.select_application(0x0)
try:
try:
logger.debug("Auth1")
desfire.authenticate(0x0, aes_master_key)
except:
logger.debug("Auth2")
desfire.authenticate(0x0, aes_null_key)
except:
logger.debug("Auth3")
desfire.authenticate(0x0, desKey)
desfire.authenticate(0x0, desKey)
applications = desfire.get_application_ids()
logger.debug(f"Applications: {applications}")
if len(applications) == 0:
raise HTTPException(
status_code=status.HTTP_410_GONE, detail="No applications on card"
status_code=status.HTTP_410_GONE, detail="Card is empty."
)
desfire.select_application(MIFARE_APP_ID)
@@ -185,6 +175,13 @@ def WriteNewCard():
# get uid
uid = desfire.get_real_uid()
applications = desfire.get_application_ids()
logger.debug(f"Applications: {applications}")
if len(applications) >= 1:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="This Card already has a key!"
)
# Set default key
logger.debug("Setting default key...")
desfire.change_default_key(aes_null_key, 0x0)
@@ -270,12 +267,12 @@ def WriteNewCard():
return key, to_hex_string(data=uid, separator=":")
except Exception as e:
logger.error(f"Error in write function: {e}", exc_info=True)
logger.error(f"Error in write function: {e}", exc_info=False)
scannerThread.start()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Error: {e}"
)
class BackgroundScanner:
def __init__(self, db):
self.db = db
@@ -310,7 +307,7 @@ class BackgroundScanner:
logger.debug("READY after timout")
except Exception as e:
logger.error(f"Error in scan function: {e}", exc_info=True)
logger.error(f"Error in function _scan_loop: {e}", exc_info=False)
time.sleep(6)
def _read_card(self):
@@ -326,7 +323,7 @@ class BackgroundScanner:
rdata = readFileOnCard(desfire=desfire)
return rdata
except Exception as e:
logger.error(f"something went wrong: {e}")
logger.error(f"Error in function _read_card: {e}", exc_info=False)
time.sleep(5)
def _check_db(self, key):