SyncAPI/extensions/apikey.py
2023-01-18 14:25:22 +01:00

21 lines
915 B
Python

from uuid import uuid4
from models.apikey import APIKeyUpdated
from modules.app import app, get_api_key
from modules.security import passEncode
from modules.database import col_apikeys, col_expired
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())
col_apikeys.find_one_and_replace({"hash": passEncode(apikey)}, {"hash": passEncode(new_key)})
col_expired.insert_one({"hash": passEncode(apikey)})
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."})