Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以使用FastAPI生成动态apiKey吗_Python_Api_Fastapi - Fatal编程技术网

Python 我可以使用FastAPI生成动态apiKey吗

Python 我可以使用FastAPI生成动态apiKey吗,python,api,fastapi,Python,Api,Fastapi,我正在使用FastAPI为机器学习模型创建API。我需要保护我的端点。我目前正在使用apiKey进行身份验证。我实现了以下身份验证: 到目前为止,我已经实施了: API_KEY = config('API_KEY') API_KEY_NAME = config("API_KEY_NAME") COOKIE_DOMAIN = config("COOKIE_DOMAIN") api_key_query = APIKeyQuery(name=API_KEY_

我正在使用FastAPI为机器学习模型创建API。我需要保护我的端点。我目前正在使用apiKey进行身份验证。我实现了以下身份验证:

到目前为止,我已经实施了:


API_KEY = config('API_KEY')
API_KEY_NAME = config("API_KEY_NAME")
COOKIE_DOMAIN = config("COOKIE_DOMAIN")

api_key_query = APIKeyQuery(name=API_KEY_NAME, auto_error=False)
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
api_key_cookie = APIKeyCookie(name=API_KEY_NAME, auto_error=False)


async def get_api_key(
    api_key_query: str = Security(api_key_query),
    api_key_header: str = Security(api_key_header),
    api_key_cookie: str = Security(api_key_cookie),
):

    if api_key_query == API_KEY:
        return api_key_query
    elif api_key_header == API_KEY:
        return api_key_header
    elif api_key_cookie == API_KEY:
        return api_key_cookie
    else:
        raise HTTPException(
            status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials"
        )
有没有办法通过生成动态密钥来实现身份验证?如果有人想使用我的端点,我可以生成一个唯一的密钥,他们可以使用它进行身份验证

有什么方法可以让我的端点安全吗


@app.get("/")
async def homepage():
    return "Welcome to the security test!"


@app.get("/logout")
async def route_logout_and_remove_cookie():
    response = RedirectResponse(url="/")
    response.delete_cookie(API_KEY_NAME, domain=COOKIE_DOMAIN)
    return response


@app.get("/secure_endpoint", tags=["test"])
async def get_open_api_endpoint(api_key: APIKey = Depends(get_api_key)):
    response = "How cool is this?"
    return response
    

@app.post('/api/model_pred')
async def face_detection(request: Request, image: UploadFile = File(...), api_key: APIKey = Depends(get_api_key)):
    pass