Mercurial > libervia-web
comparison libervia/pages/_bridge/page_meta.py @ 1509:106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:44:11 +0200 |
parents | 7472d5a88006 |
children |
comparison
equal
deleted
inserted
replaced
1508:ec3ad9abf9f9 | 1509:106bae41f5c8 |
---|---|
12 | 12 |
13 name = "bridge" | 13 name = "bridge" |
14 on_data_post = "continue" | 14 on_data_post = "continue" |
15 | 15 |
16 # bridge method allowed when no profile is connected | 16 # bridge method allowed when no profile is connected |
17 NO_SESSION_ALLOWED = ("getContacts", "identitiesBaseGet", "identitiesGet") | 17 NO_SESSION_ALLOWED = ("contacts_get", "identities_base_get", "identities_get") |
18 | 18 |
19 | 19 |
20 def parse_url(self, request): | 20 def parse_url(self, request): |
21 self.getPathArgs(request, ["method_name"], min_args=1) | 21 self.get_path_args(request, ["method_name"], min_args=1) |
22 | 22 |
23 | 23 |
24 async def render(self, request): | 24 async def render(self, request): |
25 if request.method != b'POST': | 25 if request.method != b'POST': |
26 log.warning(f"Bad method used with _bridge endpoint: {request.method.decode()}") | 26 log.warning(f"Bad method used with _bridge endpoint: {request.method.decode()}") |
27 return self.pageError(request, C.HTTP_BAD_REQUEST) | 27 return self.page_error(request, C.HTTP_BAD_REQUEST) |
28 data = self.getRData(request) | 28 data = self.get_r_data(request) |
29 profile = self.getProfile(request) | 29 profile = self.get_profile(request) |
30 self.checkCSRF(request) | 30 self.check_csrf(request) |
31 method_name = data["method_name"] | 31 method_name = data["method_name"] |
32 if profile is None: | 32 if profile is None: |
33 if method_name in NO_SESSION_ALLOWED: | 33 if method_name in NO_SESSION_ALLOWED: |
34 # this method is allowed, we use the service profile | 34 # this method is allowed, we use the service profile |
35 profile = C.SERVICE_PROFILE | 35 profile = C.SERVICE_PROFILE |
36 else: | 36 else: |
37 log.warning("_bridge endpoint accessed without authorisation") | 37 log.warning("_bridge endpoint accessed without authorisation") |
38 return self.pageError(request, C.HTTP_UNAUTHORIZED) | 38 return self.page_error(request, C.HTTP_UNAUTHORIZED) |
39 method_data = json.load(request.content) | 39 method_data = json.load(request.content) |
40 try: | 40 try: |
41 bridge_method = getattr(self.host.restricted_bridge, method_name) | 41 bridge_method = getattr(self.host.restricted_bridge, method_name) |
42 except AttributeError: | 42 except AttributeError: |
43 log.warning(_( | 43 log.warning(_( |
44 "{profile!r} is trying to access a bridge method not implemented in " | 44 "{profile!r} is trying to access a bridge method not implemented in " |
45 "RestrictedBridge: {method_name}").format( | 45 "RestrictedBridge: {method_name}").format( |
46 profile=profile, method_name=method_name)) | 46 profile=profile, method_name=method_name)) |
47 return self.pageError(request, C.HTTP_BAD_REQUEST) | 47 return self.page_error(request, C.HTTP_BAD_REQUEST) |
48 | 48 |
49 try: | 49 try: |
50 args, kwargs = method_data['args'], method_data['kwargs'] | 50 args, kwargs = method_data['args'], method_data['kwargs'] |
51 except KeyError: | 51 except KeyError: |
52 log.warning(_( | 52 log.warning(_( |
53 "{profile!r} has sent a badly formatted method call: {method_data}" | 53 "{profile!r} has sent a badly formatted method call: {method_data}" |
54 ).format(profile=profile, method_data=method_data)) | 54 ).format(profile=profile, method_data=method_data)) |
55 return self.pageError(request, C.HTTP_BAD_REQUEST) | 55 return self.page_error(request, C.HTTP_BAD_REQUEST) |
56 | 56 |
57 if "profile" in kwargs or "profile_key" in kwargs: | 57 if "profile" in kwargs or "profile_key" in kwargs: |
58 log.warning(_( | 58 log.warning(_( |
59 '"profile" key should not be in method kwargs, hack attempt? ' | 59 '"profile" key should not be in method kwargs, hack attempt? ' |
60 "profile={profile}, method_data={method_data}" | 60 "profile={profile}, method_data={method_data}" |
61 ).format(profile=profile, method_data=method_data)) | 61 ).format(profile=profile, method_data=method_data)) |
62 return self.pageError(request, C.HTTP_BAD_REQUEST) | 62 return self.page_error(request, C.HTTP_BAD_REQUEST) |
63 | 63 |
64 try: | 64 try: |
65 ret = await bridge_method(*args, **kwargs, profile=profile) | 65 ret = await bridge_method(*args, **kwargs, profile=profile) |
66 except BridgeException as e: | 66 except BridgeException as e: |
67 request.setResponseCode(C.HTTP_PROXY_ERROR) | 67 request.setResponseCode(C.HTTP_PROXY_ERROR) |