-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify Location when behind of FB Hosting
- Loading branch information
Showing
1 changed file
with
30 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
|
||
from django.http import HttpRequest | ||
from django.urls import reverse | ||
from django.utils.deprecation import MiddlewareMixin | ||
from django.utils.http import urlencode | ||
|
||
|
||
class TroubleshootingMiddleware(MiddlewareMixin): | ||
def process_request(self, request: HttpRequest): | ||
|
||
if request.path not in [reverse("troubleshoot:session")]: | ||
return | ||
print(f"{request.path=}") | ||
print(f"{request.user.username=}") | ||
print(f"{request.session.session_key=}") | ||
print(f"{request.COOKIES}") | ||
try: | ||
print(f"{request.session["_auth_user_id"]=}") | ||
except KeyError: | ||
print("No key `_auth_user_id`") | ||
class TroubleshootingMiddleware: | ||
def __init__(self, get_response): | ||
self.get_response = get_response | ||
|
||
def __call__(self, request: HttpRequest): | ||
if request.path in [reverse("troubleshoot:session")]: | ||
print(f"{request.path=}") | ||
print(f"{request.user.username=}") | ||
print(f"{request.session.session_key=}") | ||
print(f"{request.COOKIES}") | ||
try: | ||
print(f"{request.session["_auth_user_id"]=}") | ||
except KeyError: | ||
print("No key `_auth_user_id`") | ||
|
||
response = self.get_response(request) | ||
|
||
if "Location" in response.headers and "X-Forwarded-Host" in request.headers: | ||
url_location = response.headers["Location"] | ||
url = [] | ||
for url_fragment in url_location.split("&"): | ||
print(url_fragment) | ||
if url_fragment.startswith("redirect_uri"): | ||
url += [urlencode({"redirect_uri": f"https://{request.headers["X-Forwarded-Host"]}/accounts/discord/login/callback/"})] | ||
else: | ||
url += [url_fragment] | ||
response.headers["Location"] = "&".join(url) | ||
|
||
return response |