mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-10-31 22:19:00 +00:00
make get_legacy_key more DRY
This commit is contained in:
parent
a6676718cb
commit
c7adb62831
1 changed files with 13 additions and 16 deletions
|
@ -529,7 +529,7 @@ async def async_broadcast(recipients: List[str], sender, data: str):
|
|||
|
||||
|
||||
async def sign_and_send(
|
||||
session: aiohttp.ClientSession, sender, data: str, destination: str
|
||||
session: aiohttp.ClientSession, sender, data: str, destination: str, **kwargs
|
||||
):
|
||||
"""Sign the messages and send them in an asynchronous bundle"""
|
||||
now = http_date()
|
||||
|
@ -539,11 +539,14 @@ async def sign_and_send(
|
|||
raise ValueError("No private key found for sender")
|
||||
|
||||
digest = make_digest(data)
|
||||
signature = make_signature(
|
||||
"post", sender, destination, now, digest, kwargs.get("use_legacy_key")
|
||||
)
|
||||
|
||||
headers = {
|
||||
"Date": now,
|
||||
"Digest": digest,
|
||||
"Signature": make_signature("post", sender, destination, now, digest, False),
|
||||
"Signature": signature,
|
||||
"Content-Type": "application/activity+json; charset=utf-8",
|
||||
"User-Agent": USER_AGENT,
|
||||
}
|
||||
|
@ -554,21 +557,15 @@ async def sign_and_send(
|
|||
logger.exception(
|
||||
"Failed to send broadcast to %s: %s", destination, response.reason
|
||||
)
|
||||
logger.info("Trying again with legacy keyId")
|
||||
# try with incorrect keyId to enable communication with legacy Bookwyrm servers
|
||||
legacy_signature = make_signature(
|
||||
"post", sender, destination, now, digest, True
|
||||
)
|
||||
headers["Signature"] = legacy_signature
|
||||
async with session.post(
|
||||
destination, data=data, headers=headers
|
||||
) as response:
|
||||
if not response.ok:
|
||||
logger.exception(
|
||||
"Failed to send broadcast with legacy keyId to %s: %s",
|
||||
destination,
|
||||
response.reason,
|
||||
if kwargs.get("use_legacy_key") is not True:
|
||||
# try with incorrect keyId to enable communication with legacy Bookwyrm servers
|
||||
logger.info("Trying again with legacy keyId header value")
|
||||
|
||||
asyncio.ensure_future(
|
||||
sign_and_send(
|
||||
session, sender, data, destination, use_legacy_key=True
|
||||
)
|
||||
)
|
||||
|
||||
return response
|
||||
except asyncio.TimeoutError:
|
||||
|
|
Loading…
Reference in a new issue