Skip to content

Commit

Permalink
also updated token handling when creating or deleting an endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Dec 29, 2024
1 parent a503c0f commit 6b63f45
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."})
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 6b63f45

Please sign in to comment.