def deleteUser(user: User): _userLogger.info(f"Processing Delete:{user.username}") query = users.objects(username=user.username) if query.count() == 0: _userLogger.info( f"Username {user.username} does not exists to delete. Raising Exception." ) raise HTTPException( status_code=HTTPStatus.CONFLICT, detail=f"The user {user.username} does not exist or is not logged in", ) _userLogger.info(f"User Exists. Checking if logged in of {user.username}") db_user = query[0] if not db_user.logged_in: raise HTTPException( status_code=HTTPStatus.CONFLICT, detail=f"The user {user.username} does not exist or is not logged in.", ) try: _hasher.verify(db_user.password, user.password) except (VerificationError, VerifyMismatchError): raise HTTPException( status_code=HTTPStatus.CONFLICT, detail=f"The user {user.username} does not exist or is not logged in.", ) users.delete(db_user) _userLogger.info(f"Successfully deleted {user.username}") return {"detail": f"Successfully deleted {user.username}"}
def getPublicKey(username: str): query = users.objects(username=username) if query.count() == 0: raise HTTPException( status_code=HTTPStatus.CONFLICT, detail=f"The user {username} does not exist", ) return {"detail": query[0].public_key}
def loggedIn(request: Request, address: str = Form("address")): """Returns True or False depending on a users logged in state Args: username (str): Username to be checked Returns: bool: True if logged in, False if logged out. """ if not request.client.host == Config.Potion_IP: raise HTTPException(status_code=HTTPStatus.UNAUTHORIZED) query = users.objects(logged_in=address) return True if query.count() > 0 else False
def loginUser(request: Request, user: User): _userLogger.info(f"Logging in {user.username} with key {user.api_key}") if not Helper.validate_APIKey(user.api_key): raise HTTPException( status_code=HTTPStatus.BAD_REQUEST, detail="Invalid API Key" ) query = users.objects(username=user.username) if query.count() == 0: raise HTTPException( status_code=HTTPStatus.BAD_REQUEST, detail="Invalid password or username", ) db_user = query[0] try: _hasher.verify(db_user.password, user.password) except (VerificationError, VerifyMismatchError): raise HTTPException( status_code=HTTPStatus.BAD_REQUEST, detail="Invalid Password or username", ) db_user.logged_in = request.client.host db_user.save() return {"detail": f"Successful Login for {user.username}"}
def usernameExists(username: str): query = users.objects(username=username) if query.count() > 0: return {"detail": "true"} return {"detail": "false"}