mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-30 13:10:47 +00:00
Added api key authentication to example portoflio api
This commit is contained in:
parent
28a5f3bdee
commit
737ebc115b
1 changed files with 16 additions and 2 deletions
|
|
@ -1,9 +1,23 @@
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI, Depends, HTTPException, status
|
||||||
|
from fastapi.security import APIKeyHeader
|
||||||
|
|
||||||
|
API_KEY = "example-api-key"
|
||||||
|
API_KEY_NAME = "X-API-KEY"
|
||||||
|
|
||||||
|
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
@app.get("/portfolio/{portfolio_id}")
|
async def validate_api_key(api_key_header: str = Depends(api_key_header)):
|
||||||
|
if api_key_header != API_KEY:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_403_FORBIDDEN, detail="Could not validate credentials"
|
||||||
|
)
|
||||||
|
return api_key_header
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/portfolio/{portfolio_id}", dependencies=[Depends(validate_api_key)])
|
||||||
async def get_portfolio(portfolio_id: int):
|
async def get_portfolio(portfolio_id: int):
|
||||||
return {
|
return {
|
||||||
"portfolio_id": portfolio_id,
|
"portfolio_id": portfolio_id,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue