From 6b63f45421659e7a78698cb8d48447c615bb1272 Mon Sep 17 00:00:00 2001 From: Jason <81298350+Deutscher775@users.noreply.github.com> Date: Sun, 29 Dec 2024 23:19:38 +0100 Subject: [PATCH] also updated token handling when creating or deleting an endpoint --- src/api.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/api.py b/src/api.py index ee85d3d..9978bee 100644 --- a/src/api.py +++ b/src/api.py @@ -323,15 +323,12 @@ async def new_token(endpoint: int, return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."}) if master_token == Bot.config.MASTER_TOKEN: - with open(f"{pathlib.Path(__file__).parent.resolve()}/tokens.json", "r+") as tokens: - token = secrets.token_urlsafe(53) - data = json.load(tokens) - data[f"{endpoint}"] = token - tokens.seek(0) - json.dump(data, tokens) - tokens.truncate() - tokens.close() - return {f"token": token} + token = secrets.token_urlsafe(53) + exists = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint) + if exists: + await astroidapi.surrealdb_handler.TokenHandler.update_token(endpoint, token) + else: + await astroidapi.surrealdb_handler.TokenHandler.create_token(endpoint, token) else: return fastapi.responses.JSONResponse(status_code=403, content={"message": "The provided token is invalid."}) @@ -635,6 +632,7 @@ async def delete_endpoint(endpoint: int, if token == data_token or token == Bot.config.MASTER_TOKEN: try: await astroidapi.surrealdb_handler.delete(endpoint) + await astroidapi.surrealdb_handler.TokenHandler.delete_token(endpoint) return fastapi.responses.JSONResponse(status_code=200, content={"message": "Deleted."}) except FileNotFoundError: return fastapi.responses.JSONResponse(status_code=404, @@ -647,7 +645,8 @@ async def delete_endpoint(endpoint: int, except KeyError: if token == Bot.config.MASTER_TOKEN: try: - os.remove(f"{pathlib.Path(__file__).parent.resolve()}/endpoints/{endpoint}.json") + await astroidapi.surrealdb_handler.delete(endpoint) + await astroidapi.surrealdb_handler.TokenHandler.delete_token(endpoint) return fastapi.responses.JSONResponse(status_code=200, content={"message": "Deleted."}) except FileNotFoundError: return fastapi.responses.JSONResponse(status_code=404,