billing: Return login URL for AJAX request session timeout for servers.

This commit is contained in:
Aman Agrawal
2024-02-21 06:28:34 +00:00
committed by Tim Abbott
parent 16135fbac6
commit ca96868b0e

View File

@@ -2,9 +2,8 @@ from functools import wraps
from typing import Callable, Optional
from urllib.parse import urlencode, urljoin
import orjson
from django.conf import settings
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect
from django.http import HttpRequest, HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import render
from django.urls import reverse
from typing_extensions import Concatenate, ParamSpec
@@ -23,6 +22,16 @@ from zilencer.models import RemoteRealm
ParamT = ParamSpec("ParamT")
def session_expired_ajax_response(login_url: str) -> JsonResponse: # nocoverage
return JsonResponse(
{
"error_message": "Remote billing authentication expired",
"login_url": login_url,
},
status=401,
)
def is_self_hosting_management_subdomain(request: HttpRequest) -> bool:
subdomain = get_subdomain(request)
return subdomain == settings.SELF_HOSTING_MANAGEMENT_SUBDOMAIN
@@ -107,15 +116,7 @@ def authenticated_remote_realm_management_endpoint(
# Return error for AJAX requests with url.
if request.headers.get("x-requested-with") == "XMLHttpRequest": # nocoverage
return HttpResponse(
orjson.dumps(
{
"error_message": "Remote billing authentication expired",
"login_url": url,
}
),
status=401,
)
return session_expired_ajax_response(url)
return HttpResponseRedirect(url)
@@ -187,6 +188,10 @@ def authenticated_remote_server_management_endpoint(
query = urlencode({"next_page": page_type})
url = append_url_query_string(url, query)
# Return error for AJAX requests with url.
if request.headers.get("x-requested-with") == "XMLHttpRequest": # nocoverage
return session_expired_ajax_response(url)
return HttpResponseRedirect(url)
assert remote_billing_user is not None