Mercurial > libervia-web
annotate libervia/pages/_bridge/page_meta.py @ 1443:3821168b9459 v0.8.0b1
v0.8.0b1 release
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 18 Jun 2021 16:21:28 +0200 |
parents | 7472d5a88006 |
children | 106bae41f5c8 |
rev | line source |
---|---|
1288 | 1 #!/usr/bin/env python3 |
2 | |
3 import json | |
4 from sat.core.i18n import _ | |
5 from sat.core.log import getLogger | |
1296
b1215347b5c3
pages (bridge): better handling of errors:
Goffi <goffi@goffi.org>
parents:
1288
diff
changeset
|
6 from sat_frontends.bridge.bridge_frontend import BridgeException |
1288 | 7 from libervia.server.constants import Const as C |
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 |
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
17 NO_SESSION_ALLOWED = ("getContacts", "identitiesBaseGet", "identitiesGet") |
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): | |
21 self.getPathArgs(request, ["method_name"], min_args=1) | |
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()}") | |
27 return self.pageError(request, C.HTTP_BAD_REQUEST) | |
28 data = self.getRData(request) | |
29 profile = self.getProfile(request) | |
30 self.checkCSRF(request) | |
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") |
7472d5a88006
browser(bridge): allow some bridge methods for session profile:
Goffi <goffi@goffi.org>
parents:
1296
diff
changeset
|
38 return self.pageError(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)) | |
47 return self.pageError(request, C.HTTP_BAD_REQUEST) | |
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)) | |
55 return self.pageError(request, C.HTTP_BAD_REQUEST) | |
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)) | |
62 return self.pageError(request, C.HTTP_BAD_REQUEST) | |
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) |