Files
Backend/sensor_external_api/authentication.py
T

23 lines
797 B
Python
Raw Normal View History

2026-04-05 00:57:25 +03:30
from django.conf import settings
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
class SensorExternalAPIKeyAuthentication(BaseAuthentication):
keyword = "Api-Key"
def authenticate(self, request):
provided_key = request.headers.get("X-API-Key") or request.headers.get("Authorization")
expected_key = getattr(settings, "SENSOR_EXTERNAL_API_KEY", "12345")
if not provided_key:
raise AuthenticationFailed("API key is required.")
if provided_key.startswith(f"{self.keyword} "):
provided_key = provided_key[len(self.keyword) + 1 :]
if provided_key != expected_key:
raise AuthenticationFailed("Invalid API key.")
return (None, None)