mirror of
https://github.com/dcs-retribution/dcs-retribution.git
synced 2025-11-10 15:41:24 +00:00
Add API key authentication.
We don't have any sensitive data, but we do access the file system. On the off chance that some phishing website decides to try to use Liberation as an attack vector, prevent access to the API by unauthorized applications. An API key is generated at each program start and passed to the front end via the QWebChannel.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
from . import debuggeometries, eventstream
|
||||
from .security import ApiKeyManager
|
||||
|
||||
app = FastAPI()
|
||||
app = FastAPI(dependencies=[Depends(ApiKeyManager.verify)])
|
||||
app.include_router(debuggeometries.router)
|
||||
app.include_router(eventstream.router)
|
||||
|
||||
15
game/server/security.py
Normal file
15
game/server/security.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import secrets
|
||||
|
||||
from fastapi import HTTPException, Security, status
|
||||
from fastapi.security import APIKeyHeader
|
||||
|
||||
API_KEY_HEADER = APIKeyHeader(name="X-API-Key")
|
||||
|
||||
|
||||
class ApiKeyManager:
|
||||
KEY = secrets.token_urlsafe()
|
||||
|
||||
@classmethod
|
||||
def verify(cls, api_key_header: str = Security(API_KEY_HEADER)) -> None:
|
||||
if api_key_header != cls.KEY:
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
Reference in New Issue
Block a user