Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Remove client application headers from API responses (#4635)

Krystle Salazar4a708c19b9d0
diff --git a/api/api/middleware/response_headers_middleware.py b/api/api/middleware/response_headers_middleware.py
deleted file mode 100644
index 740c46503..000000000
--- a/api/api/middleware/response_headers_middleware.py
+++ /dev/null
@@ -1,28 +0,0 @@
-from rest_framework.request import Request
-
-from api.models.oauth import ThrottledApplication
-
-
-def response_headers_middleware(get_response):
- """
- Add standard response headers used by Nginx logging.
-
- These headers help Openverse more easily and directly connect
- individual requests to each other. This is particularly useful
- when evaluating traffic patterns from individual source IPs
- to identify malicious requesters or request patterns.
- """
-
- def middleware(request: Request):
- response = get_response(request)
-
- if not (hasattr(request, "auth") and hasattr(request.auth, "application")):
- return response
-
- application: ThrottledApplication = request.auth.application
- response["x-ov-client-application-name"] = application.name
- response["x-ov-client-application-verified"] = application.verified
-
- return response
-
- return middleware
diff --git a/api/conf/oauth2_extensions.py b/api/conf/oauth2_extensions.py
index 5bc12a784..0a5316a3f 100644
--- a/api/conf/oauth2_extensions.py
+++ b/api/conf/oauth2_extensions.py
@@ -3,12 +3,16 @@ from rest_framework.exceptions import (
PermissionDenied,
)
+import structlog
from drf_spectacular.authentication import TokenScheme
from oauth2_provider.contrib.rest_framework import (
OAuth2Authentication as BaseOAuth2Authentication,
)
+logger = structlog.get_logger(__name__)
+
+
class OAuth2Authentication(BaseOAuth2Authentication):
# Required by schema extension
keyword = "Bearer"
@@ -30,6 +34,12 @@ class OAuth2Authentication(BaseOAuth2Authentication):
if application := getattr(auth, "application", None):
if application.revoked:
raise PermissionDenied()
+ logger.info(
+ "client_application_authentication",
+ application_id=application.id,
+ application_name=application.name,
+ application_verified=application.verified,
+ )
return user_auth_tuple
diff --git a/api/conf/settings/base.py b/api/conf/settings/base.py
index c5041be5f..8eda1f488 100644
--- a/api/conf/settings/base.py
+++ b/api/conf/settings/base.py
@@ -23,7 +23,6 @@ MIDDLEWARE = [
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
- "api.middleware.response_headers_middleware.response_headers_middleware",
]
# Storage
diff --git a/api/test/integration/test_auth.py b/api/test/integration/test_auth.py
index de55c7480..c24613530 100644
--- a/api/test/integration/test_auth.py
+++ b/api/test/integration/test_auth.py
@@ -168,35 +168,6 @@ def test_auth_rate_limit_reporting(
assert res_data["verified"] is False
- "verified",
- (True, False),
-)
-def test_auth_response_headers(
- api_client, verified, test_auth_tokens_registration, test_auth_token_exchange
-):
- if verified:
- _integration_verify_most_recent_token(api_client)
-
- token = test_auth_token_exchange["access_token"]
-
- res = api_client.get("/v1/images/", HTTP_AUTHORIZATION=f"Bearer {token}")
-
- assert (
- res.headers["x-ov-client-application-name"]
- == test_auth_tokens_registration["name"]
- )
- assert res.headers["x-ov-client-application-verified"] == str(verified)
-
-
-def test_unauthed_response_headers(api_client):
- res = api_client.get("/v1/images")
-
- assert "x-ov-client-application-name" not in res.headers
- assert "x-ov-client-application-verified" not in res.headers
-
-
@pytest.mark.django_db
@pytest.mark.parametrize(
"sort_dir, exp_indexed_on",