Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_comp_ap_gateway/http_server.py @ 4262:d366d90a71aa
component AP Gateway: log invalid account in case of error.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 12 Jun 2024 22:34:19 +0200 |
parents | 49019947cc76 |
children | 0d7bb4df2343 |
rev | line source |
---|---|
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia ActivityPub Gateway |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
19 import time |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
20 import html |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
21 from typing import Optional, Dict, List, Any |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
22 import json |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
23 from urllib import parse |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
24 from collections import deque |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
25 import unicodedata |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
26 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
27 from twisted.web import http, resource as web_resource, server |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
28 from twisted.web import static |
3977
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
29 from twisted.web import util as web_util |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
30 from twisted.python import failure |
3833
381340b9a9ee
component AP gateway: convert XMPP mentions to AP:
Goffi <goffi@goffi.org>
parents:
3826
diff
changeset
|
31 from twisted.internet import defer |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
32 from twisted.words.protocols.jabber import jid, error |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
33 from wokkel import pubsub, rsm |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
34 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
35 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
36 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
37 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
38 from libervia.backend.core.core_types import SatXMPPEntity |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
39 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
40 from libervia.backend.tools.common import date_utils, uri |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
41 from libervia.backend.memory.sqla_mapping import SubscriptionState |
3729
86eea17cafa7
component AP gateway: split plugin in several files:
Goffi <goffi@goffi.org>
parents:
3728
diff
changeset
|
42 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
43 from .constants import ( |
4109
7067b0d73183
component AP gateway: fix AP media type in GET and POST requests headers
Goffi <goffi@goffi.org>
parents:
4104
diff
changeset
|
44 NS_AP, MEDIA_TYPE_AP, MEDIA_TYPE_AP_ALT, CONTENT_TYPE_WEBFINGER, CONTENT_TYPE_AP, |
7067b0d73183
component AP gateway: fix AP media type in GET and POST requests headers
Goffi <goffi@goffi.org>
parents:
4104
diff
changeset
|
45 TYPE_ACTOR, TYPE_INBOX, TYPE_SHARED_INBOX, TYPE_OUTBOX, TYPE_EVENT, AP_REQUEST_TYPES, |
7067b0d73183
component AP gateway: fix AP media type in GET and POST requests headers
Goffi <goffi@goffi.org>
parents:
4104
diff
changeset
|
46 PAGE_SIZE, ACTIVITY_TYPES_LOWER, ACTIVIY_NO_ACCOUNT_ALLOWED, SIGN_HEADERS, HS2019, |
7067b0d73183
component AP gateway: fix AP media type in GET and POST requests headers
Goffi <goffi@goffi.org>
parents:
4104
diff
changeset
|
47 SIGN_EXP, TYPE_FOLLOWERS, TYPE_FOLLOWING, TYPE_ITEM, TYPE_LIKE, TYPE_REACTION, |
7067b0d73183
component AP gateway: fix AP media type in GET and POST requests headers
Goffi <goffi@goffi.org>
parents:
4104
diff
changeset
|
48 ST_AP_CACHE |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
49 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
50 from .regex import RE_SIG_PARAM |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 log = getLogger(__name__) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
55 VERSION = unicodedata.normalize( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
56 'NFKD', |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
57 f"{C.APP_NAME} ActivityPub Gateway {C.APP_VERSION}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
58 ) |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 class HTTPAPGServer(web_resource.Resource): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 """HTTP Server handling ActivityPub S2S protocol""" |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 isLeaf = True |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 def __init__(self, ap_gateway): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 self.apg = ap_gateway |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
67 self._seen_digest = deque(maxlen=50) |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 super().__init__() |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
70 def response_code( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
71 self, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
72 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
73 http_code: int, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
74 msg: Optional[str] = None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
75 ) -> None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
76 """Log and set HTTP return code and associated message""" |
3833
381340b9a9ee
component AP gateway: convert XMPP mentions to AP:
Goffi <goffi@goffi.org>
parents:
3826
diff
changeset
|
77 if msg is not None: |
381340b9a9ee
component AP gateway: convert XMPP mentions to AP:
Goffi <goffi@goffi.org>
parents:
3826
diff
changeset
|
78 log.warning(msg) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
79 request.setResponseCode(http_code, None if msg is None else msg.encode()) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
80 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
81 def _on_request_error(self, failure_: failure.Failure, request: "HTTPRequest") -> None: |
4014
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
82 exc = failure_.value |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
83 if isinstance(exc, exceptions.NotFound): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
84 self.response_code( |
4014
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
85 request, |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
86 http.NOT_FOUND, |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
87 str(exc) |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
88 ) |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
89 else: |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
90 log.exception(f"Internal error: {failure_.value}") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
91 self.response_code( |
4014
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
92 request, |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
93 http.INTERNAL_SERVER_ERROR, |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
94 f"internal error: {failure_.value}" |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
95 ) |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
96 request.finish() |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
97 raise failure_ |
4ef473116499
component AP gateway (http): handle properly NotFound error:
Goffi <goffi@goffi.org>
parents:
4009
diff
changeset
|
98 |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
99 request.finish() |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
100 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
101 async def webfinger(self, request): |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 url_parsed = parse.urlparse(request.uri.decode()) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 query = parse.parse_qs(url_parsed.query) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 resource = query.get("resource", [""])[0] |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 account = resource[5:].strip() |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 if not resource.startswith("acct:") or not account: |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 return web_resource.ErrorPage( |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 http.BAD_REQUEST, "Bad Request" , "Invalid webfinger resource" |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 ).render(request) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
111 actor_url = self.apg.build_apurl(TYPE_ACTOR, account) |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 resp = { |
3882
1bd44367337d
component AP gateway: add `aliases` to webfinger data
Goffi <goffi@goffi.org>
parents:
3881
diff
changeset
|
114 "aliases": [actor_url], |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 "subject": resource, |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 "links": [ |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 { |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 "rel": "self", |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 "type": "application/activity+json", |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 "href": actor_url |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 } |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 ] |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 } |
4104
58bbc100f13b
component AP gateway: fix webfinger content type
Goffi <goffi@goffi.org>
parents:
4093
diff
changeset
|
124 request.setHeader("content-type", CONTENT_TYPE_WEBFINGER) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
125 request.write(json.dumps(resp).encode()) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
126 request.finish() |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
128 async def handle_undo_activity( |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
129 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
130 requestor_actor_id: str, |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
131 request: "HTTPRequest", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
132 data: dict, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
133 account_jid: jid.JID, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
134 node: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
135 ap_account: str, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
136 ap_url: str, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
137 signing_actor: str |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
138 ) -> None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
139 if node is None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
140 node = self.apg._m.namespace |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
141 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
142 object_ = data.get("object") |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
143 if isinstance(object_, str): |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
144 # we check first if it's not a cached object |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
145 ap_cache_key = f"{ST_AP_CACHE}{object_}" |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
146 value = await self.apg.client._ap_storage.get(ap_cache_key) |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
147 else: |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
148 value = None |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
149 if value is not None: |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
150 objects = [value] |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
151 # because we'll undo the activity, we can remove it from cache |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
152 await self.apg.client._ap_storage.remove(ap_cache_key) |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
153 else: |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
154 objects = await self.apg.ap_get_list(requestor_actor_id, data, "object") |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
155 for obj in objects: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
156 type_ = obj.get("type") |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
157 actor = await self.apg.ap_get_sender_actor(requestor_actor_id, obj) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
158 if actor != signing_actor: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
159 log.warning(f"ignoring object not attributed to signing actor: {data}") |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
160 continue |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
161 |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
162 if type_ == "Follow": |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
163 try: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
164 target_account = obj["object"] |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
165 except KeyError: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
166 log.warning(f'ignoring invalid object, missing "object" key: {data}') |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
167 continue |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
168 if not self.apg.is_local_url(target_account): |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
169 log.warning(f"ignoring unfollow request to non local actor: {data}") |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
170 continue |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
171 await self.apg._p.unsubscribe( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
172 client, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
173 account_jid, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
174 node, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
175 sender=client.jid, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
176 ) |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
177 elif type_ == "Announce": |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
178 # we can use directly the Announce object, as only the "id" field is |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
179 # needed |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
180 await self.apg.new_ap_delete_item(client, None, node, obj) |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
181 elif type_ == TYPE_LIKE: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
182 await self.handle_attachment_item(client, obj, {"noticed": False}) |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
183 elif type_ == TYPE_REACTION: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
184 await self.handle_attachment_item(client, obj, { |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
185 "reactions": {"operation": "update", "remove": [obj["content"]]} |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
186 }) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
187 else: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
188 log.warning(f"Unmanaged undo type: {type_!r}") |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
189 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
190 async def handle_follow_activity( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
191 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
192 requestor_actor_id: str, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
193 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
194 data: dict, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
195 account_jid: jid.JID, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
196 node: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
197 ap_account: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
198 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
199 signing_actor: str |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
200 ) -> None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
201 if node is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
202 node = self.apg._m.namespace |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
203 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
204 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
205 subscription = await self.apg._p.subscribe( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
206 client, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
207 account_jid, |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
208 node, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
209 # subscriptions from AP are always public |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
210 options=self.apg._pps.set_public_opt() |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
211 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
212 except pubsub.SubscriptionPending: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
213 log.info(f"subscription to node {node!r} of {account_jid} is pending") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
214 # TODO: manage SubscriptionUnconfigured |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
215 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
216 if subscription.state != "subscribed": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
217 # other states should raise an Exception |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
218 raise exceptions.InternalError('"subscribed" state was expected') |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
219 inbox = await self.apg.get_ap_inbox_from_id(signing_actor, use_shared=False) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
220 actor_id = self.apg.build_apurl(TYPE_ACTOR, ap_account) |
4023
78b5f356900c
component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents:
4016
diff
changeset
|
221 accept_data = self.apg.create_activity( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
222 "Accept", actor_id, object_=data |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
223 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
224 await self.apg.sign_and_post(inbox, actor_id, accept_data) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
225 await self.apg._c.synchronise(client, account_jid, node, resync=False) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
226 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
227 async def handle_accept_activity( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
228 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
229 requestor_actor_id: str, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
230 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
231 data: dict, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
232 account_jid: jid.JID, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
233 node: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
234 ap_account: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
235 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
236 signing_actor: str |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
237 ) -> None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
238 if node is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
239 node = self.apg._m.namespace |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
240 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
241 objects = await self.apg.ap_get_list(requestor_actor_id, data, "object") |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
242 for obj in objects: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
243 type_ = obj.get("type") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
244 if type_ == "Follow": |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
245 follow_node = await self.apg.host.memory.storage.get_pubsub_node( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
246 client, client.jid, node, with_subscriptions=True |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
247 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
248 if follow_node is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
249 log.warning( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
250 f"Received a follow accept on an unknown node: {node!r} at " |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
251 f"{client.jid}. Ignoring it" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
252 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
253 continue |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
254 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
255 sub = next( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
256 s for s in follow_node.subscriptions if s.subscriber==account_jid |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
257 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
258 except StopIteration: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
259 log.warning( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
260 "Received a follow accept on a node without subscription: " |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
261 f"{node!r} at {client.jid}. Ignoring it" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
262 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
263 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
264 if sub.state == SubscriptionState.SUBSCRIBED: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
265 log.warning(f"Already subscribed to {node!r} at {client.jid}") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
266 elif sub.state == SubscriptionState.PENDING: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
267 follow_node.subscribed = True |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
268 sub.state = SubscriptionState.SUBSCRIBED |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
269 await self.apg.host.memory.storage.add(follow_node) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
270 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
271 raise exceptions.InternalError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
272 f"Unhandled subscription state {sub.state!r}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
273 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
274 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
275 log.warning(f"Unmanaged accept type: {type_!r}") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
276 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
277 async def handle_delete_activity( |
3793
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
278 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
279 requestor_actor_id: str, |
3793
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
280 request: "HTTPRequest", |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
281 data: dict, |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
282 account_jid: Optional[jid.JID], |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
283 node: Optional[str], |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
284 ap_account: Optional[str], |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
285 ap_url: str, |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
286 signing_actor: str |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
287 ): |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
288 if node is None: |
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
289 node = self.apg._m.namespace |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
290 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
291 objects = await self.apg.ap_get_list(requestor_actor_id, data, "object") |
3793
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
292 for obj in objects: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
293 await self.apg.new_ap_delete_item(client, account_jid, node, obj) |
3793
b5c9021020df
component AP gateway: convert `Delete` AP activities to corresponding Pubsub `retract`:
Goffi <goffi@goffi.org>
parents:
3764
diff
changeset
|
294 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
295 async def handle_new_ap_items( |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
296 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
297 requestor_actor_id: str, |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
298 request: "HTTPRequest", |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
299 data: dict, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
300 account_jid: Optional[jid.JID], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
301 node: Optional[str], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
302 signing_actor: str, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
303 repeated: bool = False, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
304 ): |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
305 """Helper method to handle workflow for new AP items |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
306 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
307 accept globally the same parameter as for handle_create_activity |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
308 @param repeated: if True, the item is an item republished from somewhere else |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
309 """ |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
310 if "_repeated" in data: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
311 log.error( |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
312 '"_repeated" field already present in given AP item, this should not ' |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
313 f"happen. Ignoring object from {signing_actor}\n{data}" |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
314 ) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
315 raise exceptions.DataError("unexpected field in item") |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
316 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
317 objects = await self.apg.ap_get_list(requestor_actor_id, data, "object") |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
318 for obj in objects: |
3904 | 319 if node is None: |
320 if obj.get("type") == TYPE_EVENT: | |
321 node = self.apg._events.namespace | |
322 else: | |
323 node = self.apg._m.namespace | |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
324 sender = await self.apg.ap_get_sender_actor(requestor_actor_id, obj) |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
325 if repeated: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
326 # we don't check sender when item is repeated, as it should be different |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
327 # from post author in this case |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
328 sender_jid = await self.apg.get_jid_from_id( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
329 requestor_actor_id, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
330 sender |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
331 ) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
332 repeater_jid = await self.apg.get_jid_from_id( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
333 requestor_actor_id, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
334 signing_actor |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
335 ) |
3870
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
336 repeated_item_id = obj["id"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
337 if self.apg.is_local_url(repeated_item_id): |
3870
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
338 # the repeated object is from XMPP, we need to parse the URL to find |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
339 # the right ID |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
340 url_type, url_args = self.apg.parse_apurl(repeated_item_id) |
3870
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
341 if url_type != "item": |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
342 raise exceptions.DataError( |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
343 "local URI is not an item: {repeated_id}" |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
344 ) |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
345 try: |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
346 url_account, url_item_id = url_args |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
347 if not url_account or not url_item_id: |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
348 raise ValueError |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
349 except (RuntimeError, ValueError): |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
350 raise exceptions.DataError( |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
351 "local URI is invalid: {repeated_id}" |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
352 ) |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
353 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
354 url_jid, url_node = await self.apg.get_jid_and_node(url_account) |
3870
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
355 if ((url_jid != sender_jid |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
356 or url_node and url_node != self.apg._m.namespace)): |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
357 raise exceptions.DataError( |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
358 "announced ID doesn't match sender ({sender}): " |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
359 f"[repeated_item_id]" |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
360 ) |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
361 |
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
362 repeated_item_id = url_item_id |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
363 |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
364 obj["_repeated"] = { |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
365 "by": repeater_jid.full(), |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
366 "at": data.get("published"), |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
367 "uri": uri.build_xmpp_uri( |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
368 "pubsub", |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
369 path=sender_jid.full(), |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
370 node=self.apg._m.namespace, |
3870
bd84d289fc94
component AP gateway: fix item ID for XMPP items on Announce:
Goffi <goffi@goffi.org>
parents:
3869
diff
changeset
|
371 item=repeated_item_id |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
372 ) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
373 } |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
374 # we must use activity's id and targets, not the original item ones |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
375 for field in ("id", "to", "bto", "cc", "bcc"): |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
376 obj[field] = data.get(field) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
377 else: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
378 if sender != signing_actor: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
379 log.warning( |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
380 "Ignoring object not attributed to signing actor: {obj}" |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
381 ) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
382 continue |
3904 | 383 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
384 await self.apg.new_ap_item(client, account_jid, node, obj) |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
385 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
386 async def handle_create_activity( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
387 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
388 requestor_actor_id: str, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
389 request: "HTTPRequest", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
390 data: dict, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
391 account_jid: Optional[jid.JID], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
392 node: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
393 ap_account: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
394 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
395 signing_actor: str |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
396 ): |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
397 await self.handle_new_ap_items( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
398 requestor_actor_id, request, data, account_jid, node, signing_actor |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
399 ) |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
400 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
401 async def handle_update_activity( |
3904 | 402 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
403 requestor_actor_id: str, |
3904 | 404 request: "HTTPRequest", |
405 data: dict, | |
406 account_jid: Optional[jid.JID], | |
407 node: Optional[str], | |
408 ap_account: Optional[str], | |
409 ap_url: str, | |
410 signing_actor: str | |
411 ): | |
412 # Update is the same as create: the item ID stays the same, thus the item will be | |
413 # overwritten | |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
414 await self.handle_new_ap_items( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
415 requestor_actor_id, request, data, account_jid, node, signing_actor |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
416 ) |
3904 | 417 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
418 async def handle_announce_activity( |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
419 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
420 requestor_actor_id: str, |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
421 request: "HTTPRequest", |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
422 data: dict, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
423 account_jid: Optional[jid.JID], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
424 node: Optional[str], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
425 ap_account: Optional[str], |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
426 ap_url: str, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
427 signing_actor: str |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
428 ): |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
429 # we create a new item |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
430 await self.handle_new_ap_items( |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
431 requestor_actor_id, |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
432 request, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
433 data, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
434 account_jid, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
435 node, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
436 signing_actor, |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
437 repeated=True |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
438 ) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
439 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
440 async def handle_attachment_item( |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
441 self, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
442 client: SatXMPPEntity, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
443 data: dict, |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
444 attachment_data: dict |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
445 ) -> None: |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
446 target_ids = data.get("object") |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
447 if not target_ids: |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
448 raise exceptions.DataError("object should be set") |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
449 elif isinstance(target_ids, list): |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
450 try: |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
451 target_ids = [o["id"] for o in target_ids] |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
452 except (KeyError, TypeError): |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
453 raise exceptions.DataError(f"invalid object: {target_ids!r}") |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
454 elif isinstance(target_ids, dict): |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
455 obj_id = target_ids.get("id") |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
456 if not obj_id or not isinstance(obj_id, str): |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
457 raise exceptions.DataError(f"invalid object: {target_ids!r}") |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
458 target_ids = [obj_id] |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
459 elif isinstance(target_ids, str): |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
460 target_ids = [target_ids] |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
461 |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
462 # XXX: we have to cache AP items because some implementation (Pleroma notably) |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
463 # don't keep object accessible, and we need to be able to retrieve them for |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
464 # UNDO. Current implementation will grow, we need to add a way to flush it after |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
465 # a while. |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
466 # TODO: add a way to flush old cached AP items. |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
467 await client._ap_storage.aset(f"{ST_AP_CACHE}{data['id']}", data) |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
468 |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
469 for target_id in target_ids: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
470 if not self.apg.is_local_url(target_id): |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
471 log.debug(f"ignoring non local target ID: {target_id}") |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
472 continue |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
473 url_type, url_args = self.apg.parse_apurl(target_id) |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
474 if url_type != TYPE_ITEM: |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
475 log.warning(f"unexpected local URL for attachment on item {target_id}") |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
476 continue |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
477 try: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
478 account, item_id = url_args |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
479 except ValueError: |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
480 raise ValueError(f"invalid URL: {target_id}") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
481 author_jid, item_node = await self.apg.get_jid_and_node(account) |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
482 if item_node is None: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
483 item_node = self.apg._m.namespace |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
484 attachment_node = self.apg._pa.get_attachment_node_name( |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
485 author_jid, item_node, item_id |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
486 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
487 cached_node = await self.apg.host.memory.storage.get_pubsub_node( |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
488 client, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
489 author_jid, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
490 attachment_node, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
491 with_subscriptions=True, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
492 create=True |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
493 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
494 found_items, __ = await self.apg.host.memory.storage.get_items( |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
495 cached_node, item_ids=[client.jid.userhost()] |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
496 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
497 if not found_items: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
498 old_item_elt = None |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
499 else: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
500 found_item = found_items[0] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
501 old_item_elt = found_item.data |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
502 |
3980
9b5092225e46
component AP gateway: fix pubsub attachment plugin call following renaming.
Goffi <goffi@goffi.org>
parents:
3977
diff
changeset
|
503 item_elt = await self.apg._pa.apply_set_handler( |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
504 client, |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
505 {"extra": attachment_data}, |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
506 old_item_elt, |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
507 None |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
508 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
509 # we reparse the element, as there can be other attachments |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
510 attachments_data = self.apg._pa.items_2_attachment_data(client, [item_elt]) |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
511 # and we update the cache |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
512 await self.apg.host.memory.storage.cache_pubsub_items( |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
513 client, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
514 cached_node, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
515 [item_elt], |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
516 attachments_data or [{}] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
517 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
518 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
519 if self.apg.is_virtual_jid(author_jid): |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
520 # the attachment is on t a virtual pubsub service (linking to an AP item), |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
521 # we notify all subscribers |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
522 for subscription in cached_node.subscriptions: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
523 if subscription.state != SubscriptionState.SUBSCRIBED: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
524 continue |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
525 self.apg.pubsub_service.notifyPublish( |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
526 author_jid, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
527 attachment_node, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
528 [(subscription.subscriber, None, [item_elt])] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
529 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
530 else: |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
531 # the attachment is on an XMPP item, we publish it to the attachment node |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
532 await self.apg._p.send_items( |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
533 client, author_jid, attachment_node, [item_elt] |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
534 ) |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
535 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
536 async def handle_like_activity( |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
537 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
538 requestor_actor_id: str, |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
539 request: "HTTPRequest", |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
540 data: dict, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
541 account_jid: Optional[jid.JID], |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
542 node: Optional[str], |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
543 ap_account: Optional[str], |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
544 ap_url: str, |
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
545 signing_actor: str |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
546 ) -> None: |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
547 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
548 await self.handle_attachment_item(client, data, {"noticed": True}) |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
549 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
550 async def handle_emojireact_activity( |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
551 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
552 requestor_actor_id: str, |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
553 request: "HTTPRequest", |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
554 data: dict, |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
555 account_jid: Optional[jid.JID], |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
556 node: Optional[str], |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
557 ap_account: Optional[str], |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
558 ap_url: str, |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
559 signing_actor: str |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
560 ) -> None: |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
561 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
562 await self.handle_attachment_item(client, data, { |
3888
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
563 "reactions": {"operation": "update", "add": [data["content"]]} |
aa7197b67c26
component AP gateway: AP <=> XMPP reactions conversions:
Goffi <goffi@goffi.org>
parents:
3884
diff
changeset
|
564 }) |
3869
c0bcbcf5b4b7
component AP gateway: handle `Like` and `Undo`/`Like` activities:
Goffi <goffi@goffi.org>
parents:
3847
diff
changeset
|
565 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
566 async def handle_join_activity( |
3904 | 567 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
568 requestor_actor_id: str, |
3904 | 569 request: "HTTPRequest", |
570 data: dict, | |
571 account_jid: Optional[jid.JID], | |
572 node: Optional[str], | |
573 ap_account: Optional[str], | |
574 ap_url: str, | |
575 signing_actor: str | |
576 ) -> None: | |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
577 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
578 await self.handle_attachment_item(client, data, {"rsvp": {"attending": "yes"}}) |
3904 | 579 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
580 async def handle_leave_activity( |
3904 | 581 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
582 requestor_actor_id: str, |
3904 | 583 request: "HTTPRequest", |
584 data: dict, | |
585 account_jid: Optional[jid.JID], | |
586 node: Optional[str], | |
587 ap_account: Optional[str], | |
588 ap_url: str, | |
589 signing_actor: str | |
590 ) -> None: | |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
591 client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
592 await self.handle_attachment_item(client, data, {"rsvp": {"attending": "no"}}) |
3904 | 593 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
594 async def ap_actor_request( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
595 self, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
596 request: "HTTPRequest", |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
597 data: Optional[dict], |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
598 account_jid: jid.JID, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
599 node: Optional[str], |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
600 ap_account: str, |
3826
81c79b7cafa7
tests (unit/ap-gateway): tests for XMPP identity/vCard4 <=> AP actor data conversion:
Goffi <goffi@goffi.org>
parents:
3821
diff
changeset
|
601 ap_url: str, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
602 signing_actor: Optional[str] |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
603 ) -> dict: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
604 inbox = self.apg.build_apurl(TYPE_INBOX, ap_account) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
605 shared_inbox = self.apg.build_apurl(TYPE_SHARED_INBOX) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
606 outbox = self.apg.build_apurl(TYPE_OUTBOX, ap_account) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
607 followers = self.apg.build_apurl(TYPE_FOLLOWERS, ap_account) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
608 following = self.apg.build_apurl(TYPE_FOLLOWING, ap_account) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
609 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
610 # we have to use AP account as preferredUsername because it is used to retrieve |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
611 # actor handle (see https://socialhub.activitypub.rocks/t/how-to-retrieve-user-server-tld-handle-from-actors-url/2196) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
612 preferred_username = ap_account.split("@", 1)[0] |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
613 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
614 identity_data = await self.apg._i.get_identity(self.apg.client, account_jid) |
3904 | 615 if node and node.startswith(self.apg._events.namespace): |
616 events = outbox | |
617 else: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
618 events_account = await self.apg.get_ap_account_from_jid_and_node( |
3904 | 619 account_jid, self.apg._events.namespace |
620 ) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
621 events = self.apg.build_apurl(TYPE_OUTBOX, events_account) |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
622 |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
623 actor_data = { |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
624 "@context": [ |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
625 "https://www.w3.org/ns/activitystreams", |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
626 "https://w3id.org/security/v1" |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
627 ], |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
628 |
3994
69d970f974ff
component AP gateway: don't percent-encode `@`:
Goffi <goffi@goffi.org>
parents:
3992
diff
changeset
|
629 # XXX: Mastodon doesn't like percent-encode arobas, so we have to unescape it |
69d970f974ff
component AP gateway: don't percent-encode `@`:
Goffi <goffi@goffi.org>
parents:
3992
diff
changeset
|
630 # if it is escaped |
69d970f974ff
component AP gateway: don't percent-encode `@`:
Goffi <goffi@goffi.org>
parents:
3992
diff
changeset
|
631 "id": ap_url.replace("%40", "@"), |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
632 "type": "Person", |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
633 "preferredUsername": preferred_username, |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
634 "inbox": inbox, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
635 "outbox": outbox, |
3904 | 636 "events": events, |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
637 "followers": followers, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
638 "following": following, |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
639 "publicKey": { |
3826
81c79b7cafa7
tests (unit/ap-gateway): tests for XMPP identity/vCard4 <=> AP actor data conversion:
Goffi <goffi@goffi.org>
parents:
3821
diff
changeset
|
640 "id": f"{ap_url}#main-key", |
81c79b7cafa7
tests (unit/ap-gateway): tests for XMPP identity/vCard4 <=> AP actor data conversion:
Goffi <goffi@goffi.org>
parents:
3821
diff
changeset
|
641 "owner": ap_url, |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
642 "publicKeyPem": self.apg.public_key_pem |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
643 }, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
644 "endpoints": { |
3904 | 645 "sharedInbox": shared_inbox, |
646 "events": events, | |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
647 }, |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
648 } |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
649 |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
650 if identity_data.get("nicknames"): |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
651 actor_data["name"] = identity_data["nicknames"][0] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
652 if identity_data.get("description"): |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
653 # description is plain text while summary expects HTML |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
654 actor_data["summary"] = html.escape(identity_data["description"]) |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
655 if identity_data.get("avatar"): |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
656 avatar_data = identity_data["avatar"] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
657 try: |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
658 filename = avatar_data["filename"] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
659 media_type = avatar_data["media_type"] |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
660 except KeyError: |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
661 log.error(f"incomplete avatar data: {identity_data!r}") |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
662 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
663 avatar_url = self.apg.build_apurl("avatar", filename) |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
664 actor_data["icon"] = { |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
665 "type": "Image", |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
666 "url": avatar_url, |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
667 "mediaType": media_type |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
668 } |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
669 |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
670 return actor_data |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
671 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
672 def get_canonical_url(self, request: "HTTPRequest") -> str: |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
673 return parse.urljoin( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
674 f"https://{self.apg.public_url}", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
675 request.path.decode().rstrip("/") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
676 # we unescape "@" for the same reason as in [ap_actor_request] |
3994
69d970f974ff
component AP gateway: don't percent-encode `@`:
Goffi <goffi@goffi.org>
parents:
3992
diff
changeset
|
677 ).replace("%40", "@") |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
678 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
679 def query_data_2_rsm_request( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
680 self, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
681 query_data: Dict[str, List[str]] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
682 ) -> rsm.RSMRequest: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
683 """Get RSM kwargs to use with RSMRequest from query data""" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
684 page = query_data.get("page") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
685 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
686 if page == ["first"]: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
687 return rsm.RSMRequest(max_=PAGE_SIZE, before="") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
688 elif page == ["last"]: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
689 return rsm.RSMRequest(max_=PAGE_SIZE) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
690 else: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
691 for query_key in ("index", "before", "after"): |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
692 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
693 kwargs={query_key: query_data[query_key][0], "max_": PAGE_SIZE} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
694 except (KeyError, IndexError, ValueError): |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
695 pass |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
696 else: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
697 return rsm.RSMRequest(**kwargs) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
698 raise ValueError(f"Invalid query data: {query_data!r}") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
699 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
700 async def ap_outbox_page_request( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
701 self, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
702 request: "HTTPRequest", |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
703 data: Optional[dict], |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
704 account_jid: jid.JID, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
705 node: Optional[str], |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
706 ap_account: str, |
3729
86eea17cafa7
component AP gateway: split plugin in several files:
Goffi <goffi@goffi.org>
parents:
3728
diff
changeset
|
707 ap_url: str, |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
708 query_data: Dict[str, List[str]] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
709 ) -> dict: |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
710 if node is None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
711 node = self.apg._m.namespace |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
712 # we only keep useful keys, and sort to have consistent URL which can |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
713 # be used as ID |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
714 url_keys = sorted(set(query_data) & {"page", "index", "before", "after"}) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
715 query_data = {k: query_data[k] for k in url_keys} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
716 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
717 items, metadata = await self.apg._p.get_items( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
718 client=self.apg.client, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
719 service=account_jid, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
720 node=node, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
721 rsm_request=self.query_data_2_rsm_request(query_data), |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
722 extra = {C.KEY_USE_CACHE: False} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
723 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
724 except error.StanzaError as e: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
725 log.warning(f"Can't get data from pubsub node {node} at {account_jid}: {e}") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
726 return {} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
727 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
728 base_url = self.get_canonical_url(request) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
729 url = f"{base_url}?{parse.urlencode(query_data, True)}" |
3904 | 730 if node and node.startswith(self.apg._events.namespace): |
731 ordered_items = [ | |
732 await self.apg.ap_events.event_data_2_ap_item( | |
733 self.apg._events.event_elt_2_event_data(item), | |
734 account_jid | |
735 ) | |
736 for item in reversed(items) | |
737 ] | |
738 else: | |
739 ordered_items = [ | |
740 await self.apg.mb_data_2_ap_item( | |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
741 self.apg.client, |
4023
78b5f356900c
component AP gateway: handle attachments
Goffi <goffi@goffi.org>
parents:
4016
diff
changeset
|
742 await self.apg._m.item_2_mb_data( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
743 self.apg.client, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
744 item, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
745 account_jid, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
746 node |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
747 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
748 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
749 for item in reversed(items) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
750 ] |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
751 ret_data = { |
3904 | 752 "@context": ["https://www.w3.org/ns/activitystreams"], |
753 "id": url, | |
754 "type": "OrderedCollectionPage", | |
755 "partOf": base_url, | |
756 "orderedItems": ordered_items | |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
757 } |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
758 |
4009
48e8b3dba793
component AP gateway (http/outbox): return data when no RSM is available
Goffi <goffi@goffi.org>
parents:
4005
diff
changeset
|
759 if "rsm" not in metadata: |
48e8b3dba793
component AP gateway (http/outbox): return data when no RSM is available
Goffi <goffi@goffi.org>
parents:
4005
diff
changeset
|
760 # no RSM available, we return what we have |
48e8b3dba793
component AP gateway (http/outbox): return data when no RSM is available
Goffi <goffi@goffi.org>
parents:
4005
diff
changeset
|
761 return ret_data |
48e8b3dba793
component AP gateway (http/outbox): return data when no RSM is available
Goffi <goffi@goffi.org>
parents:
4005
diff
changeset
|
762 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
763 # AP OrderedCollection must be in reversed chronological order, thus the opposite |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
764 # of what we get with RSM (at least with Libervia Pubsub) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
765 if not metadata["complete"]: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
766 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
767 last= metadata["rsm"]["last"] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
768 except KeyError: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
769 last = None |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
770 ret_data["prev"] = f"{base_url}?{parse.urlencode({'after': last})}" |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
771 if metadata["rsm"]["index"] != 0: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
772 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
773 first= metadata["rsm"]["first"] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
774 except KeyError: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
775 first = None |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
776 ret_data["next"] = f"{base_url}?{parse.urlencode({'before': first})}" |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
777 |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
778 return ret_data |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
779 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
780 async def ap_outbox_request( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
781 self, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
782 request: "HTTPRequest", |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
783 data: Optional[dict], |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
784 account_jid: jid.JID, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
785 node: Optional[str], |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
786 ap_account: str, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
787 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
788 signing_actor: Optional[str] |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
789 ) -> dict: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
790 if node is None: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
791 node = self.apg._m.namespace |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
792 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
793 parsed_url = parse.urlparse(request.uri.decode()) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
794 query_data = parse.parse_qs(parsed_url.query) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
795 if query_data: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
796 return await self.ap_outbox_page_request( |
3995
27e5294649c2
component AP gateway: fix missing argument when calling APOutboxPageRequest
Goffi <goffi@goffi.org>
parents:
3994
diff
changeset
|
797 request, data, account_jid, node, ap_account, ap_url, query_data |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
798 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
799 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
800 # XXX: we can't use disco#info here because this request won't work on a bare jid |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
801 # due to security considerations of XEP-0030 (we don't have presence |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
802 # subscription). |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
803 # The current workaround is to do a request as if RSM was available, and actually |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
804 # check its availability according to result. |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
805 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
806 __, metadata = await self.apg._p.get_items( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
807 client=self.apg.client, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
808 service=account_jid, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
809 node=node, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
810 max_items=0, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
811 rsm_request=rsm.RSMRequest(max_=0), |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
812 extra = {C.KEY_USE_CACHE: False} |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
813 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
814 except error.StanzaError as e: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
815 log.warning(f"Can't get data from pubsub node {node} at {account_jid}: {e}") |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
816 return {} |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
817 try: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
818 items_count = metadata["rsm"]["count"] |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
819 except KeyError: |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
820 log.warning( |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
821 f"No RSM metadata found when requesting pubsub node {node} at " |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
822 f"{account_jid}, defaulting to items_count=20" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
823 ) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
824 items_count = 20 |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
825 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
826 url = self.get_canonical_url(request) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
827 url_first_page = f"{url}?{parse.urlencode({'page': 'first'})}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
828 url_last_page = f"{url}?{parse.urlencode({'page': 'last'})}" |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
829 return { |
3904 | 830 "@context": ["https://www.w3.org/ns/activitystreams"], |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
831 "id": url, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
832 "totalItems": items_count, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
833 "type": "OrderedCollection", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
834 "first": url_first_page, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
835 "last": url_last_page, |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
836 } |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
837 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
838 async def ap_inbox_request( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
839 self, |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
840 requestor_actor_id: str, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
841 request: "HTTPRequest", |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
842 data: Optional[dict], |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
843 account_jid: Optional[jid.JID], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
844 node: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
845 ap_account: Optional[str], |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
846 ap_url: str, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
847 signing_actor: Optional[str] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
848 ) -> None: |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
849 assert data is not None |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
850 if signing_actor is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
851 raise exceptions.InternalError("signing_actor must be set for inbox requests") |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
852 await self.check_signing_actor(requestor_actor_id, data, signing_actor) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
853 activity_type = (data.get("type") or "").lower() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
854 if not activity_type in ACTIVITY_TYPES_LOWER: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
855 return self.response_code( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
856 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
857 http.UNSUPPORTED_MEDIA_TYPE, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
858 f"request is not an activity, ignoring" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
859 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
860 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
861 if account_jid is None and activity_type not in ACTIVIY_NO_ACCOUNT_ALLOWED: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
862 return self.response_code( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
863 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
864 http.UNSUPPORTED_MEDIA_TYPE, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
865 f"{activity_type.title()!r} activity must target an account" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
866 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
867 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
868 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
869 method = getattr(self, f"handle_{activity_type}_activity") |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
870 except AttributeError: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
871 return self.response_code( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
872 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
873 http.UNSUPPORTED_MEDIA_TYPE, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
874 f"{activity_type.title()} activity is not yet supported" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
875 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
876 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
877 await method( |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
878 requestor_actor_id, request, data, account_jid, node, ap_account, ap_url, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
879 signing_actor |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
880 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
881 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
882 async def ap_followers_request( |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
883 self, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
884 request: "HTTPRequest", |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
885 data: Optional[dict], |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
886 account_jid: jid.JID, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
887 node: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
888 ap_account: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
889 ap_url: str, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
890 signing_actor: Optional[str] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
891 ) -> dict: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
892 if node is None: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
893 node = self.apg._m.namespace |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
894 client = self.apg.client |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
895 subscribers = await self.apg._pps.get_public_node_subscriptions( |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
896 client, account_jid, node |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
897 ) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
898 followers = [] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
899 for subscriber in subscribers.keys(): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
900 if self.apg.is_virtual_jid(subscriber): |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
901 # the subscriber is an AP user subscribed with this gateway |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
902 ap_account = self.apg._e.unescape(subscriber.user) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
903 else: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
904 # regular XMPP user |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
905 ap_account = await self.apg.get_ap_account_from_jid_and_node(subscriber, node) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
906 followers.append(ap_account) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
907 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
908 url = self.get_canonical_url(request) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
909 return { |
3904 | 910 "@context": ["https://www.w3.org/ns/activitystreams"], |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
911 "type": "OrderedCollection", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
912 "id": url, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
913 "totalItems": len(subscribers), |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
914 "first": { |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
915 "type": "OrderedCollectionPage", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
916 "id": url, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
917 "orderedItems": followers |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
918 } |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
919 } |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
920 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
921 async def ap_following_request( |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
922 self, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
923 request: "HTTPRequest", |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
924 data: Optional[dict], |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
925 account_jid: jid.JID, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
926 node: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
927 ap_account: Optional[str], |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
928 ap_url: str, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
929 signing_actor: Optional[str] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
930 ) -> dict[str, Any]: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
931 client = self.apg.client |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
932 subscriptions = await self.apg._pps.subscriptions( |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
933 client, account_jid, node |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
934 ) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
935 following = [] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
936 for sub_dict in subscriptions: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
937 service = jid.JID(sub_dict["service"]) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
938 if self.apg.is_virtual_jid(service): |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
939 # the subscription is to an AP actor with this gateway |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
940 ap_account = self.apg._e.unescape(service.user) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
941 else: |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
942 # regular XMPP user |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
943 ap_account = await self.apg.get_ap_account_from_jid_and_node( |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
944 service, sub_dict["node"] |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
945 ) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
946 following.append(ap_account) |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
947 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
948 url = self.get_canonical_url(request) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
949 return { |
3904 | 950 "@context": ["https://www.w3.org/ns/activitystreams"], |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
951 "type": "OrderedCollection", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
952 "id": url, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
953 "totalItems": len(subscriptions), |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
954 "first": { |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
955 "type": "OrderedCollectionPage", |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
956 "id": url, |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
957 "orderedItems": following |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
958 } |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
959 } |
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
960 |
4015
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
961 def _get_to_log( |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
962 self, |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
963 request: "HTTPRequest", |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
964 data: Optional[dict] = None, |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
965 ) -> List[str]: |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
966 """Get base data to logs in verbose mode""" |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
967 from pprint import pformat |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
968 to_log = [ |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
969 "", |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
970 f"<<< got {request.method.decode()} request - {request.uri.decode()}" |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
971 ] |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
972 if data is not None: |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
973 to_log.append(pformat(data)) |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
974 if self.apg.verbose>=3: |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
975 headers = "\n".join( |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
976 f" {k.decode()}: {v.decode()}" |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
977 for k,v in request.getAllHeaders().items() |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
978 ) |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
979 to_log.append(f" headers:\n{headers}") |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
980 return to_log |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
981 |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
982 def get_requestor_actor_id( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
983 self, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
984 data: dict|None = None, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
985 uri_extra_args: list[str]|None = None |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
986 ) -> str: |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
987 """Find the actor ID of the requestor. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
988 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
989 The requestor here is actually the local actor which will do the requests to |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
990 achieve the task (e.g. retrieve external actor data), not the requestor of the |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
991 received AP request. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
992 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
993 It will notably be used as requestor actor ID to sign HTTP requests. We need to |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
994 sign GET request too to access instance checking HTTP GET signature (e.g. Mastodon |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
995 instances set in "secure mode"). |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
996 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
997 We look for the destinee of the request and check if it's a local actor, and |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
998 default to a generic one if we can't find it. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
999 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1000 Destinee is first checked in data if any, otherwise in request URI. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1001 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1002 @param data: parsed JSON data of original AP request, if any. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1003 @param uri_extra_args: arguments of the AP request as returned by |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1004 [self.apg.parse_apurl]. It is most of time the destinee of the request. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1005 @return: requestor_actor_id to use to sign HTTP request needed to answer the |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1006 original request. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1007 """ |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1008 # We first check for destinee in data. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1009 if data: |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1010 try: |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1011 for to_ in data["to"]: |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1012 if self.apg.is_local_url(to_): |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1013 url_type, url_args = self.apg.parse_apurl(to_) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1014 if url_type != TYPE_ACTOR or not url_args: |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1015 continue |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1016 ap_account = url_args[0] |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1017 if ( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1018 not ap_account.endswith(f"@{self.apg.public_url}") |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1019 or ap_account.count("@") != 1 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1020 ): |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1021 continue |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1022 return to_ |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1023 except KeyError: |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1024 pass |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1025 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1026 # If nothing relevant, we try URI arguments. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1027 if uri_extra_args: |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1028 ap_account = uri_extra_args[0] |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1029 if ( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1030 ap_account.endswith(f"@{self.apg.public_url}") |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1031 and ap_account.count("@") == 1 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1032 ): |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1033 return self.apg.build_apurl(TYPE_ACTOR, ap_account) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1034 |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1035 # Still nothing, we'll have to use a generic actor. |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1036 log.warning( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1037 "Can't find destinee in \"to\" field, using generic requestor for signature." |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1038 ) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1039 return self.apg.build_apurl( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1040 TYPE_ACTOR, f"libervia@{self.apg.public_url}" |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1041 ) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1042 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1043 async def ap_request( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1044 self, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1045 request: "HTTPRequest", |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1046 data: dict|None = None, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1047 signing_actor: str|None = None, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1048 requestor_actor_id: str|None = None, |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1049 ) -> None: |
3847
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
1050 if self.apg.verbose: |
4015
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
1051 to_log = self._get_to_log(request, data) |
3847
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
1052 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1053 path = request.path.decode() |
3729
86eea17cafa7
component AP gateway: split plugin in several files:
Goffi <goffi@goffi.org>
parents:
3728
diff
changeset
|
1054 ap_url = parse.urljoin( |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1055 f"https://{self.apg.public_url}", |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1056 path |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1057 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1058 request_type, extra_args = self.apg.parse_apurl(ap_url) |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1059 |
4109
7067b0d73183
component AP gateway: fix AP media type in GET and POST requests headers
Goffi <goffi@goffi.org>
parents:
4104
diff
changeset
|
1060 header_accept = request.getHeader("accept") or "" |
7067b0d73183
component AP gateway: fix AP media type in GET and POST requests headers
Goffi <goffi@goffi.org>
parents:
4104
diff
changeset
|
1061 if ((MEDIA_TYPE_AP not in header_accept |
7067b0d73183
component AP gateway: fix AP media type in GET and POST requests headers
Goffi <goffi@goffi.org>
parents:
4104
diff
changeset
|
1062 and MEDIA_TYPE_AP_ALT not in header_accept |
3977
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1063 and request_type in self.apg.html_redirect)): |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1064 # this is not a AP request, and we have a redirections for it |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1065 kw = {} |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1066 if extra_args: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1067 kw["jid"], kw["node"] = await self.apg.get_jid_and_node(extra_args[0]) |
3977
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1068 kw["jid_user"] = kw["jid"].user |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1069 if kw["node"] is None: |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1070 kw["node"] = self.apg._m.namespace |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1071 if len(extra_args) > 1: |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1072 kw["item"] = extra_args[1] |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1073 else: |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1074 kw["item"] = "" |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1075 else: |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1076 kw["jid"], kw["jid_user"], kw["node"], kw["item"] = "", "", "", "" |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1077 |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1078 redirections = self.apg.html_redirect[request_type] |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1079 for redirection in redirections: |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1080 filters = redirection["filters"] |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1081 if not filters: |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1082 break |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1083 # if we have filter, they must all match |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1084 elif all(v in kw[k] for k,v in filters.items()): |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1085 break |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1086 else: |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1087 # no redirection is matching |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1088 redirection = None |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1089 |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1090 if redirection is not None: |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1091 kw = {k: parse.quote(str(v), safe="") for k,v in kw.items()} |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1092 target_url = redirection["url"].format(**kw) |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1093 content = web_util.redirectTo(target_url.encode(), request) |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1094 request.write(content) |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1095 request.finish() |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1096 return |
6fa4ca0c047e
component AP gateway: HTML redirection:
Goffi <goffi@goffi.org>
parents:
3904
diff
changeset
|
1097 |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1098 if requestor_actor_id is None: |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1099 requestor_actor_id = self.get_requestor_actor_id( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1100 data, extra_args |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1101 ) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1102 if len(extra_args) == 0: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1103 if request_type != "shared_inbox": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1104 raise exceptions.DataError(f"Invalid request type: {request_type!r}") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1105 ret_data = await self.ap_inbox_request( |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1106 requestor_actor_id, request, data, None, None, None, ap_url, signing_actor |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1107 ) |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
1108 elif request_type == "avatar": |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
1109 if len(extra_args) != 1: |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
1110 raise exceptions.DataError("avatar argument expected in URL") |
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
1111 avatar_filename = extra_args[0] |
4212 | 1112 avatar_path = self.apg.host.common_cache.get_path(avatar_filename) |
3821
0b1c30ff2cbb
component AP: XMPP identity => AP actor data converstion:
Goffi <goffi@goffi.org>
parents:
3807
diff
changeset
|
1113 return static.File(str(avatar_path)).render(request) |
3846
cc13efdd8360
component AP gateway: return item when `item` URL is used:
Goffi <goffi@goffi.org>
parents:
3844
diff
changeset
|
1114 elif request_type == "item": |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1115 ret_data = await self.apg.ap_get_local_object(ap_url) |
3904 | 1116 if "@context" not in ret_data: |
1117 ret_data["@context"] = [NS_AP] | |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1118 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1119 if len(extra_args) > 1: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1120 log.warning(f"unexpected extra arguments: {extra_args!r}") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1121 ap_account = extra_args[0] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1122 account_jid, node = await self.apg.get_jid_and_node(ap_account) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1123 if request_type not in AP_REQUEST_TYPES.get( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1124 request.method.decode().upper(), [] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1125 ): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1126 raise exceptions.DataError(f"Invalid request type: {request_type!r}") |
4093
13b1079c27ec
component AP gateway: fix method retrieval in `ap_request`:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1127 method = getattr(self, f"ap_{request_type}_request") |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1128 ret_data = await method( |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1129 requestor_actor_id, request, data, account_jid, node, ap_account, ap_url, signing_actor |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1130 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1131 if ret_data is not None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1132 request.setHeader("content-type", CONTENT_TYPE_AP) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1133 request.write(json.dumps(ret_data).encode()) |
3881
2e4a0f6050bd
component AP gateway: better verbose logging:
Goffi <goffi@goffi.org>
parents:
3870
diff
changeset
|
1134 if self.apg.verbose: |
2e4a0f6050bd
component AP gateway: better verbose logging:
Goffi <goffi@goffi.org>
parents:
3870
diff
changeset
|
1135 to_log.append(f"--- RET (code: {request.code})---") |
3847
aaa4e7815ba8
component AP gateway: new `verbose` attribute in AP gateway to activate debug logs:
Goffi <goffi@goffi.org>
parents:
3846
diff
changeset
|
1136 if self.apg.verbose>=2: |
3881
2e4a0f6050bd
component AP gateway: better verbose logging:
Goffi <goffi@goffi.org>
parents:
3870
diff
changeset
|
1137 if ret_data is not None: |
4016
97df34151c6d
component AP gateway (http): fix missing `pformat` module in verbose mode
Goffi <goffi@goffi.org>
parents:
4015
diff
changeset
|
1138 from pprint import pformat |
3881
2e4a0f6050bd
component AP gateway: better verbose logging:
Goffi <goffi@goffi.org>
parents:
3870
diff
changeset
|
1139 to_log.append(f"{pformat(ret_data)}") |
2e4a0f6050bd
component AP gateway: better verbose logging:
Goffi <goffi@goffi.org>
parents:
3870
diff
changeset
|
1140 to_log.append("---") |
2e4a0f6050bd
component AP gateway: better verbose logging:
Goffi <goffi@goffi.org>
parents:
3870
diff
changeset
|
1141 log.info("\n".join(to_log)) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1142 request.finish() |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1143 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1144 async def ap_post_request(self, request: "HTTPRequest") -> None: |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1145 try: |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1146 data = json.load(request.content) |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1147 if not isinstance(data, dict): |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1148 log.warning(f"JSON data should be an object (uri={request.uri.decode()})") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1149 self.response_code( |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1150 request, |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1151 http.BAD_REQUEST, |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1152 f"invalid body, was expecting a JSON object" |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1153 ) |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1154 request.finish() |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1155 return |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1156 except (json.JSONDecodeError, ValueError) as e: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1157 self.response_code( |
3981
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1158 request, |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1159 http.BAD_REQUEST, |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1160 f"invalid json in inbox request: {e}" |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1161 ) |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1162 request.finish() |
acc9dfc8ba8d
component AP gateway: parse body immediately on `POST` request:
Goffi <goffi@goffi.org>
parents:
3980
diff
changeset
|
1163 return |
4005
54a6b44f173b
component AP gateway: reset stream position after getting payload:
Goffi <goffi@goffi.org>
parents:
3996
diff
changeset
|
1164 else: |
54a6b44f173b
component AP gateway: reset stream position after getting payload:
Goffi <goffi@goffi.org>
parents:
3996
diff
changeset
|
1165 request.content.seek(0) |
54a6b44f173b
component AP gateway: reset stream position after getting payload:
Goffi <goffi@goffi.org>
parents:
3996
diff
changeset
|
1166 |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1167 requestor_actor_id = self.get_requestor_actor_id(data) |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1168 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1169 try: |
3983
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1170 if data["type"] == "Delete" and data["actor"] == data["object"]: |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1171 # we don't handle actor deletion |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1172 request.setResponseCode(http.ACCEPTED) |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1173 log.debug(f"ignoring actor deletion ({data['actor']})") |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1174 # TODO: clean data in cache coming from this actor, maybe with a tombstone |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1175 request.finish() |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1176 return |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1177 except KeyError: |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1178 pass |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1179 |
31c3d6652115
component AP gateway: ignore actor delection notifications:
Goffi <goffi@goffi.org>
parents:
3982
diff
changeset
|
1180 try: |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1181 signing_actor = await self.check_signature(requestor_actor_id, request) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1182 except exceptions.EncryptionError as e: |
4015
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
1183 if self.apg.verbose: |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
1184 to_log = self._get_to_log(request) |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
1185 to_log.append(f" body: {request.content.read()!r}") |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
1186 request.content.seek(0) |
2913313ca58f
component AP gateway (http): add verbose log when signature verification fails on POST request
Goffi <goffi@goffi.org>
parents:
4014
diff
changeset
|
1187 log.info("\n".join(to_log)) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1188 self.response_code( |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1189 request, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1190 http.FORBIDDEN, |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1191 f"invalid signature: {e}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1192 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1193 request.finish() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1194 return |
3982
74f7c10a48bc
component AP gateway: properly close the HTTP connection on `checkSignature` exception:
Goffi <goffi@goffi.org>
parents:
3981
diff
changeset
|
1195 except Exception as e: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1196 self.response_code( |
3982
74f7c10a48bc
component AP gateway: properly close the HTTP connection on `checkSignature` exception:
Goffi <goffi@goffi.org>
parents:
3981
diff
changeset
|
1197 request, |
74f7c10a48bc
component AP gateway: properly close the HTTP connection on `checkSignature` exception:
Goffi <goffi@goffi.org>
parents:
3981
diff
changeset
|
1198 http.INTERNAL_SERVER_ERROR, |
74f7c10a48bc
component AP gateway: properly close the HTTP connection on `checkSignature` exception:
Goffi <goffi@goffi.org>
parents:
3981
diff
changeset
|
1199 f"Can't check signature: {e}" |
74f7c10a48bc
component AP gateway: properly close the HTTP connection on `checkSignature` exception:
Goffi <goffi@goffi.org>
parents:
3981
diff
changeset
|
1200 ) |
74f7c10a48bc
component AP gateway: properly close the HTTP connection on `checkSignature` exception:
Goffi <goffi@goffi.org>
parents:
3981
diff
changeset
|
1201 request.finish() |
74f7c10a48bc
component AP gateway: properly close the HTTP connection on `checkSignature` exception:
Goffi <goffi@goffi.org>
parents:
3981
diff
changeset
|
1202 return |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1203 |
3844
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1204 request.setResponseCode(http.ACCEPTED) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1205 |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1206 digest = request.getHeader("digest") |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1207 if digest in self._seen_digest: |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1208 log.debug(f"Ignoring duplicated request (digest: {digest!r})") |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1209 request.finish() |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1210 return |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1211 self._seen_digest.append(digest) |
65e5718e7710
component AP gateway: `Announce` activity implementation:
Goffi <goffi@goffi.org>
parents:
3833
diff
changeset
|
1212 |
3802
983df907d456
component AP gateway: set POST default response code to 202 (accepted)
Goffi <goffi@goffi.org>
parents:
3793
diff
changeset
|
1213 # default response code, may be changed, e.g. in case of exception |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
1214 try: |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1215 return await self.ap_request( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1216 request, data, signing_actor, requestor_actor_id=requestor_actor_id |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1217 ) |
3764
125c7043b277
comp AP gateway: publish, (un)subscribe/(un)follow, public subscription/following/followers:
Goffi <goffi@goffi.org>
parents:
3745
diff
changeset
|
1218 except Exception as e: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1219 self._on_request_error(failure.Failure(e), request) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1220 |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1221 async def check_signing_actor( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1222 self, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1223 requestor_actor_id: str, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1224 data: dict, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1225 signing_actor: str |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1226 ) -> None: |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1227 """That that signing actor correspond to actor declared in data |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1228 |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1229 @param requestor_actor_id: ID of the actor doing the request. |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1230 @param data: request payload |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1231 @param signing_actor: actor ID of the signing entity, as returned by |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1232 check_signature |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1233 @raise exceptions.NotFound: no actor found in data |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1234 @raise exceptions.EncryptionError: signing actor doesn't match actor in data |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1235 """ |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1236 actor = await self.apg.ap_get_sender_actor(requestor_actor_id, data) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1237 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1238 if signing_actor != actor: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1239 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1240 f"signing actor ({signing_actor}) doesn't match actor in data ({actor})" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1241 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1242 |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1243 async def check_signature( |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1244 self, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1245 requestor_actor_id: str, |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1246 request: "HTTPRequest" |
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1247 ) -> str: |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1248 """Check and validate HTTP signature |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1249 |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1250 @param requestor_actor_id: ID of the actor doing the request. |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1251 @return: id of the signing actor |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1252 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1253 @raise exceptions.EncryptionError: signature is not present or doesn't match |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1254 """ |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1255 signature = request.getHeader("Signature") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1256 if signature is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1257 raise exceptions.EncryptionError("No signature found") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1258 sign_data = { |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1259 m["key"]: m["uq_value"] or m["quoted_value"][1:-1] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1260 for m in RE_SIG_PARAM.finditer(signature) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1261 } |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1262 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1263 key_id = sign_data["keyId"] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1264 except KeyError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1265 raise exceptions.EncryptionError('"keyId" is missing from signature') |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1266 algorithm = sign_data.get("algorithm", HS2019) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1267 signed_headers = sign_data.get( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1268 "headers", |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1269 "(created)" if algorithm==HS2019 else "date" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1270 ).lower().split() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1271 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1272 headers_to_check = SIGN_HEADERS[None] + SIGN_HEADERS[request.method] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1273 except KeyError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1274 raise exceptions.InternalError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1275 f"there should be a list of headers for {request.method} method" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1276 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1277 if not headers_to_check: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1278 raise exceptions.InternalError("headers_to_check must not be empty") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1279 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1280 for header in headers_to_check: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1281 if isinstance(header, tuple): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1282 if len(set(header).intersection(signed_headers)) == 0: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1283 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1284 f"at least one of following header must be signed: {header}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1285 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1286 elif header not in signed_headers: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1287 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1288 f"the {header!r} header must be signed" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1289 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1290 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1291 body = request.content.read() |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1292 request.content.seek(0) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1293 headers = {} |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1294 for to_sign in signed_headers: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1295 if to_sign == "(request-target)": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1296 method = request.method.decode().lower() |
3884
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1297 uri = request.uri.decode() |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1298 headers[to_sign] = f"{method} /{uri.lstrip('/')}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1299 elif to_sign in ("(created)", "(expires)"): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1300 if algorithm != HS2019: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1301 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1302 f"{to_sign!r} pseudo-header can only be used with {HS2019} " |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1303 "algorithm" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1304 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1305 key = to_sign[1:-1] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1306 value = sign_data.get(key) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1307 if not value: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1308 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1309 "{key!r} parameter is missing from signature" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1310 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1311 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1312 if float(value) < 0: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1313 raise ValueError |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1314 except ValueError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1315 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1316 f"{to_sign} must be a Unix timestamp" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1317 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1318 headers[to_sign] = value |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1319 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1320 value = request.getHeader(to_sign) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1321 if not value: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1322 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1323 f"value of header {to_sign!r} is missing!" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1324 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1325 elif to_sign == "host": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1326 # we check Forwarded/X-Forwarded-Host headers |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1327 # as we need original host if a proxy has modified the header |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1328 forwarded = request.getHeader("forwarded") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1329 if forwarded is not None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1330 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1331 host = [ |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1332 f[5:] for f in forwarded.split(";") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1333 if f.startswith("host=") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1334 ][0] or None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1335 except IndexError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1336 host = None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1337 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1338 host = None |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1339 if host is None: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1340 host = request.getHeader("x-forwarded-host") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1341 if host: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1342 value = host |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1343 elif to_sign == "digest": |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1344 hashes = { |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1345 algo.lower(): hash_ for algo, hash_ in ( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1346 digest.split("=", 1) for digest in value.split(",") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1347 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1348 } |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1349 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1350 given_digest = hashes["sha-256"] |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1351 except KeyError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1352 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1353 "Only SHA-256 algorithm is currently supported for digest" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1354 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1355 __, computed_digest = self.apg.get_digest(body) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1356 if given_digest != computed_digest: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1357 raise exceptions.EncryptionError( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1358 f"SHA-256 given and computed digest differ:\n" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1359 f"given: {given_digest!r}\ncomputed: {computed_digest!r}" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1360 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1361 headers[to_sign] = value |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1362 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1363 # date check |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1364 limit_ts = time.time() + SIGN_EXP |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1365 if "(created)" in headers: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1366 created = float(headers["created"]) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1367 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1368 created = date_utils.date_parse(headers["date"]) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1369 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1370 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1371 try: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1372 expires = float(headers["expires"]) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1373 except KeyError: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1374 pass |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1375 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1376 if expires < created: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1377 log.warning( |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1378 f"(expires) [{expires}] set in the past of (created) [{created}] " |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1379 "ignoring it according to specs" |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1380 ) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1381 else: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1382 limit_ts = min(limit_ts, expires) |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1383 |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1384 if created > limit_ts: |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1385 raise exceptions.EncryptionError("Signature has expired") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1386 |
3884
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1387 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1388 return await self.apg.check_signature( |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1389 requestor_actor_id, |
3884
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1390 sign_data["signature"], |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1391 key_id, |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1392 headers |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1393 ) |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1394 except exceptions.EncryptionError: |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1395 method, url = headers["(request-target)"].rsplit(' ', 1) |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1396 headers["(request-target)"] = f"{method} {parse.unquote(url)}" |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1397 log.debug( |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1398 "Using workaround for (request-target) encoding bug in signature, " |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1399 "see https://github.com/mastodon/mastodon/issues/18871" |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1400 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1401 return await self.apg.check_signature( |
4259
49019947cc76
component AP Gateway: implement HTTP GET signature.
Goffi <goffi@goffi.org>
parents:
4212
diff
changeset
|
1402 requestor_actor_id, |
3884
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1403 sign_data["signature"], |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1404 key_id, |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1405 headers |
cea52400623d
component AP gateway: work around encoding bug in Mastodon:
Goffi <goffi@goffi.org>
parents:
3882
diff
changeset
|
1406 ) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1407 |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1408 def render(self, request): |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1409 request.setHeader("server", VERSION) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1410 return super().render(request) |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1411 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1412 def render_GET(self, request): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1413 path = request.path.decode().lstrip("/") |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1414 if path.startswith(".well-known/webfinger"): |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1415 defer.ensureDeferred(self.webfinger(request)) |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1416 return server.NOT_DONE_YET |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1417 elif path.startswith(self.apg.ap_path): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1418 d = defer.ensureDeferred(self.ap_request(request)) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1419 d.addErrback(self._on_request_error, request) |
3728
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1420 return server.NOT_DONE_YET |
b15644cae50d
component AP gateway: JID/node ⟺ AP outbox conversion:
Goffi <goffi@goffi.org>
parents:
3684
diff
changeset
|
1421 |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1422 return web_resource.NoResource().render(request) |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1423 |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1424 def render_POST(self, request): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1425 path = request.path.decode().lstrip("/") |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1426 if not path.startswith(self.apg.ap_path): |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1427 return web_resource.NoResource().render(request) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4023
diff
changeset
|
1428 defer.ensureDeferred(self.ap_post_request(request)) |
3745
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1429 return server.NOT_DONE_YET |
a8c7e5cef0cb
comp AP gateway: signature checking, caching and threads management:
Goffi <goffi@goffi.org>
parents:
3729
diff
changeset
|
1430 |
3682
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1431 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1432 class HTTPRequest(server.Request): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1433 pass |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1434 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1435 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1436 class HTTPServer(server.Site): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1437 requestFactory = HTTPRequest |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1438 |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1439 def __init__(self, ap_gateway): |
7c990aaa49d3
comp AP Gateway: ActivityPub Component first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1440 super().__init__(HTTPAPGServer(ap_gateway)) |