Mercurial > libervia-web
annotate libervia/web/pages/_bridge/page_meta.py @ 1548:66aa6e140ebb
browser: make `jid_search` more generic:
rel 423
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 09 Aug 2023 00:22:16 +0200 |
parents | eb00d593801d |
children |
rev | line source |
---|---|
1288 | 1 #!/usr/bin/env python3 |
2 | |
3 import json | |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
4 from libervia.backend.core.i18n import _ |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
5 from libervia.backend.core.log import getLogger |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
6 from libervia.frontends.bridge.bridge_frontend import BridgeException |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1509
diff
changeset
|
7 from libervia.web.server.constants import Const as C |
1288 | 8 |
9 | |
10 log = getLogger(__name__) | |
11 """access to restricted bridge""" | |
12 | |
13 name = "bridge" | |
14 on_data_post = "continue" | |
15 | |
1431
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
16 # bridge method allowed when no profile is connected |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
17 NO_SESSION_ALLOWED = ("contacts_get", "identities_base_get", "identities_get") |
1431
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
18 |
1288 | 19 |
20 def parse_url(self, request): | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
21 self.get_path_args(request, ["method_name"], min_args=1) |
1288 | 22 |
23 | |
24 async def render(self, request): | |
25 if request.method != b'POST': | |
26 log.warning(f"Bad method used with _bridge endpoint: {request.method.decode()}") | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
27 return self.page_error(request, C.HTTP_BAD_REQUEST) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
28 data = self.get_r_data(request) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
29 profile = self.get_profile(request) |
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
30 self.check_csrf(request) |
1288 | 31 method_name = data["method_name"] |
1431
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
32 if profile is None: |
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
33 if method_name in NO_SESSION_ALLOWED: |
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
34 # this method is allowed, we use the service profile |
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
35 profile = C.SERVICE_PROFILE |
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
36 else: |
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
37 log.warning("_bridge endpoint accessed without authorisation") |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
38 return self.page_error(request, C.HTTP_UNAUTHORIZED) |
1288 | 39 method_data = json.load(request.content) |
40 try: | |
41 bridge_method = getattr(self.host.restricted_bridge, method_name) | |
42 except AttributeError: | |
43 log.warning(_( | |
44 "{profile!r} is trying to access a bridge method not implemented in " | |
45 "RestrictedBridge: {method_name}").format( | |
46 profile=profile, method_name=method_name)) | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
47 return self.page_error(request, C.HTTP_BAD_REQUEST) |
1288 | 48 |
49 try: | |
50 args, kwargs = method_data['args'], method_data['kwargs'] | |
51 except KeyError: | |
52 log.warning(_( | |
53 "{profile!r} has sent a badly formatted method call: {method_data}" | |
54 ).format(profile=profile, method_data=method_data)) | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
55 return self.page_error(request, C.HTTP_BAD_REQUEST) |
1288 | 56 |
1296
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
57 if "profile" in kwargs or "profile_key" in kwargs: |
1288 | 58 log.warning(_( |
59 '"profile" key should not be in method kwargs, hack attempt? ' | |
60 "profile={profile}, method_data={method_data}" | |
61 ).format(profile=profile, method_data=method_data)) | |
1509
106bae41f5c8
massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents:
1431
diff
changeset
|
62 return self.page_error(request, C.HTTP_BAD_REQUEST) |
1288 | 63 |
1296
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
64 try: |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
65 ret = await bridge_method(*args, **kwargs, profile=profile) |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
66 except BridgeException as e: |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
67 request.setResponseCode(C.HTTP_PROXY_ERROR) |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
68 ret = { |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
69 "fullname": e.fullname, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
70 "message": e.message, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
71 "condition": e.condition, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
72 "module": e.module, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
73 "classname": e.classname, |
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
74 } |
1288 | 75 return json.dumps(ret) |