async def _get_account_by_id(account_id): if account_id: async with db.AsgardDBSession() as s: try: return (await s.query(AccountDB).filter(AccountDB.id == account_id ).one()) except NoResultFound: return None
async def check_auth_token(token): async with db.AsgardDBSession() as session: return await _build_user_instance( session, UserDB.tx_authkey == token, { "event": "auth-failed", "token-type": "apikey", "error": "Key not found", "token": token, }, )
async def check_jwt_token(jwt_token): try: payload = jwt.decode(jwt_token, key=SECRET_KEY) except Exception as e: logger.warning({ "event": "invalid-token", "token-type": "jwt", "error": str(e), "token": jwt_token, }) return None async with db.AsgardDBSession() as session: return await _build_user_instance( session, UserDB.tx_email == payload["user"]["email"], { "event": "auth-failed", "token-type": "JWT", "error": "user not found", "email": payload["user"]["email"], }, )