-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b4b61b
commit e9b3bbc
Showing
6 changed files
with
59 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,44 @@ | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class RemoveCookieFromVaryMiddleware: | ||
class AssistantMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request): | ||
response = self.get_response(request) | ||
|
||
self._set_logged_in_cookie(request, response) | ||
self._handle_iframe_cookie(request, response) | ||
self._cleanup_vary_header(response) | ||
|
||
return response | ||
|
||
def _set_logged_in_cookie(self, request, response): | ||
"""Set or update the 'logged-in' header based on authentication.""" | ||
cookie_name = "logged_in" | ||
if request.user.is_authenticated: | ||
response.headers["logged-in"] = 1 | ||
else: | ||
del response.headers["Vary"] | ||
response.headers["Vary"] = "iframe, logged-in" | ||
response.set_cookie(cookie_name, "1") | ||
elif request.COOKIES.get(cookie_name): | ||
response.delete_cookie(cookie_name) | ||
|
||
if "iframe" in request.GET: | ||
response.set_cookie("iframe", 1) | ||
def _handle_iframe_cookie(self, request, response): | ||
"""Manage iframe-related headers and cookies.""" | ||
iframe_in_request = "iframe" in request.GET | ||
iframe_cookie = response.cookies.get("iframe") | ||
|
||
if ( | ||
response.cookies.get("iframe") == "1" | ||
or request.COOKIES.get("iframe") == "1" | ||
): | ||
response.headers["iframe"] = 1 | ||
if iframe_in_request: | ||
response.set_cookie("iframe", "1") | ||
response.headers["iframe"] = "1" | ||
elif iframe_cookie and iframe_cookie.value == "1": | ||
response.headers["iframe"] = "1" | ||
else: | ||
# Ensure the iframe header is not lingering | ||
response.headers.pop("iframe", None) | ||
|
||
return response | ||
@staticmethod | ||
def _cleanup_vary_header(response): | ||
"""Helper to parse and return the Vary header as a list.""" | ||
vary_header = response.headers.get("Vary", "") | ||
return [ | ||
v.strip() | ||
for v in vary_header.split(",") | ||
if v.strip() and v.strip().lower() != "cookie" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters