Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
rest: Extract remote_server_path from rest_path.
Zixuan James Li5c49e4ba06f3
diff --git a/zerver/decorator.py b/zerver/decorator.pyindex 718f28a4022..9193981fe48 100644--- a/zerver/decorator.py+++ b/zerver/decorator.py@@ -62,9 +62,6 @@ from zerver.lib.users import is_2fa_verifiedfrom zerver.lib.utils import has_api_key_format, statsdfrom zerver.models import UserProfile, get_client, get_user_profile_by_api_key-if settings.ZILENCER_ENABLED:- from zilencer.models import RemoteZulipServer-if TYPE_CHECKING:from django.http.request import _ImmutableQueryDict@@ -242,18 +239,12 @@ def validate_api_key(api_key: str,allow_webhook_access: bool = False,client_name: Optional[str] = None,-) -> Union[UserProfile, "RemoteZulipServer"]:+) -> UserProfile:# Remove whitespace to protect users from trivial errors.api_key = api_key.strip()if role is not None:role = role.strip()- # If `role` doesn't look like an email, it might be a uuid.- if settings.ZILENCER_ENABLED and role is not None and "@" not in role:- from zilencer.auth import validate_remote_server-- return validate_remote_server(request, role, api_key)-user_profile = access_user_by_api_key(request, api_key, email=role)if user_profile.is_incoming_webhook and not allow_webhook_access:raise JsonableError(_("This API is not available to incoming webhook bots."))@@ -749,7 +740,6 @@ def authenticated_rest_api_view(# Now we try to do authentication or dietry:- # profile is a Union[UserProfile, RemoteZulipServer]profile = validate_api_key(request,role,diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.pyindex 0e799f8503a..31b19ef1703 100644--- a/zerver/tests/test_decorators.py+++ b/zerver/tests/test_decorators.py@@ -2015,6 +2015,11 @@ class RestAPITest(ZulipTestCase):self.assertEqual(result.status_code, 405)self.assert_in_response("Method Not Allowed", result)+ with self.settings(ZILENCER_ENABLED=True):+ result = self.client_patch("/json/remotes/push/register")+ self.assertEqual(result.status_code, 405)+ self.assert_in_response("Method Not Allowed", result)+def test_options_method(self) -> None:self.login("hamlet")result = self.client_options("/json/users")diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.pyindex 1635b23f5df..2820e68729b 100644--- a/zerver/tests/test_push_notifications.py+++ b/zerver/tests/test_push_notifications.py@@ -205,22 +205,6 @@ class PushBouncerNotificationTest(BouncerTestCase):hamlet = self.example_user("hamlet")- with self.assertLogs(level="WARNING") as warn_log:- result = self.api_post(- hamlet,- endpoint,- dict(user_id=user_id, token_kin=token_kind, token=token),- )- self.assertEqual(- warn_log.output,- [- "WARNING:root:User [email protected] (zulip) attempted to access API on wrong subdomain ()"- ],- )- self.assert_json_error(- result, "Account is not associated with this subdomain", status_code=401- )-# We need the root ('') subdomain to be in use for this next# test, since the push bouncer API is only available there:realm = get_realm("zulip")diff --git a/zilencer/auth.py b/zilencer/auth.pyindex 330107fa9cd..7407bdbe639 100644--- a/zilencer/auth.py+++ b/zilencer/auth.py@@ -1,14 +1,29 @@-from django.http import HttpRequest+from functools import wraps+from typing import Any, Callable++from django.http import HttpRequest, HttpResponse+from django.urls import path+from django.urls.resolvers import URLPatternfrom django.utils.crypto import constant_time_comparefrom django.utils.translation import gettext as _+from typing_extensions import Concatenate, ParamSpec-from zerver.decorator import process_client-from zerver.lib.exceptions import ErrorCode, JsonableError, RemoteServerDeactivatedError+from zerver.decorator import get_basic_credentials, process_client+from zerver.lib.exceptions import (+ ErrorCode,+ JsonableError,+ RemoteServerDeactivatedError,+ UnauthorizedError,+)+from zerver.lib.rate_limiter import rate_limitfrom zerver.lib.request import RequestNotes+from zerver.lib.rest import get_target_view_function_or_responsefrom zerver.lib.subdomains import get_subdomainfrom zerver.models import Realmfrom zilencer.models import RemoteZulipServer, get_remote_server_by_uuid+ParamT = ParamSpec("ParamT")+class InvalidZulipServerError(JsonableError):code = ErrorCode.INVALID_ZULIP_SERVER@@ -48,3 +63,39 @@ def validate_remote_server(RequestNotes.get_notes(request).remote_server = remote_serverprocess_client(request)return remote_server+++def authenticated_remote_server_view(+ view_func: Callable[Concatenate[HttpRequest, RemoteZulipServer, ParamT], HttpResponse]+) -> Callable[Concatenate[HttpRequest, ParamT], HttpResponse]:+ @wraps(view_func)+ def _wrapped_view_func(+ request: HttpRequest, /, *args: ParamT.args, **kwargs: ParamT.kwargs+ ) -> HttpResponse:+ role, api_key = get_basic_credentials(request)+ if "@" in role:+ raise JsonableError(_("Must validate with valid Zulip server API key"))+ try:+ remote_server = validate_remote_server(request, role, api_key)+ except JsonableError as e:+ raise UnauthorizedError(e.msg)++ rate_limit(request)+ return view_func(request, remote_server, *args, **kwargs)++ return _wrapped_view_func+++def remote_server_dispatch(request: HttpRequest, **kwargs: Any) -> HttpResponse:+ result = get_target_view_function_or_response(request, kwargs)+ if isinstance(result, HttpResponse):+ return result+ target_function, view_flags = result+ return authenticated_remote_server_view(target_function)(request, **kwargs)+++def remote_server_path(+ route: str,+ **handlers: Callable[Concatenate[HttpRequest, RemoteZulipServer, ParamT], HttpResponse],+) -> URLPattern:+ return path(route, remote_server_dispatch, handlers)diff --git a/zilencer/urls.py b/zilencer/urls.pyindex bdf00997dba..00bfb889822 100644--- a/zilencer/urls.py+++ b/zilencer/urls.py@@ -3,7 +3,7 @@ from typing import Anyfrom django.conf.urls import includefrom django.urls import path-from zerver.lib.rest import rest_path+from zilencer.auth import remote_server_pathfrom zilencer.views import (deactivate_remote_server,register_remote_push_device,@@ -19,16 +19,16 @@ i18n_urlpatterns: Any = []# Zilencer views following the REST API stylev1_api_and_json_patterns = [- rest_path("remotes/push/register", POST=register_remote_push_device),- rest_path("remotes/push/unregister", POST=unregister_remote_push_device),- rest_path("remotes/push/unregister/all", POST=unregister_all_remote_push_devices),- rest_path("remotes/push/notify", POST=remote_server_notify_push),+ remote_server_path("remotes/push/register", POST=register_remote_push_device),+ remote_server_path("remotes/push/unregister", POST=unregister_remote_push_device),+ remote_server_path("remotes/push/unregister/all", POST=unregister_all_remote_push_devices),+ remote_server_path("remotes/push/notify", POST=remote_server_notify_push),# Push signup doesn't use the REST API, since there's no auth.path("remotes/server/register", register_remote_server),- rest_path("remotes/server/deactivate", POST=deactivate_remote_server),+ remote_server_path("remotes/server/deactivate", POST=deactivate_remote_server),# For receiving table data used in analytics and billing- rest_path("remotes/server/analytics", POST=remote_server_post_analytics),- rest_path("remotes/server/analytics/status", GET=remote_server_check_analytics),+ remote_server_path("remotes/server/analytics", POST=remote_server_post_analytics),+ remote_server_path("remotes/server/analytics/status", GET=remote_server_check_analytics),]urlpatterns = [