SyncAPI/extensions/apikey.py

21 lines
915 B
Python
Raw Normal View History

2023-01-16 16:22:51 +02:00
from uuid import uuid4
from models.apikey import APIKeyUpdated
from modules.app import app, get_api_key
2023-01-18 15:25:22 +02:00
from modules.security import passEncode
from modules.database import col_apikeys, col_expired
2023-01-16 16:22:51 +02:00
from fastapi import Depends
from fastapi.responses import UJSONResponse
from fastapi.openapi.models import APIKey
@app.put("/apikey", response_class=UJSONResponse, response_model=APIKeyUpdated, description="Update API key")
async def apikey_put(apikey: APIKey = Depends(get_api_key)):
new_key = str(uuid4())
2023-01-18 15:25:22 +02:00
col_apikeys.find_one_and_replace({"hash": passEncode(apikey)}, {"hash": passEncode(new_key)})
col_expired.insert_one({"hash": passEncode(apikey)})
2023-01-16 16:22:51 +02:00
2023-01-17 11:54:23 +02:00
return UJSONResponse({"apikey": new_key})
@app.get("/apikey", response_class=UJSONResponse, description="Check API key")
async def apikey_check(apikey: APIKey = Depends(get_api_key)):
return UJSONResponse({"detail": "This key is valid."})