formatting

This commit is contained in:
Hugh Rundle 2023-04-13 13:27:51 +10:00
parent e3261c6b88
commit a6676718cb
5 changed files with 24 additions and 11 deletions

View file

@ -540,7 +540,6 @@ async def sign_and_send(
digest = make_digest(data) digest = make_digest(data)
headers = { headers = {
"Date": now, "Date": now,
"Digest": digest, "Digest": digest,
@ -557,12 +556,18 @@ async def sign_and_send(
) )
logger.info("Trying again with legacy keyId") logger.info("Trying again with legacy keyId")
# try with incorrect keyId to enable communication with legacy Bookwyrm servers # try with incorrect keyId to enable communication with legacy Bookwyrm servers
legacy_signature = make_signature("post", sender, destination, now, digest, True) legacy_signature = make_signature(
"post", sender, destination, now, digest, True
)
headers["Signature"] = legacy_signature headers["Signature"] = legacy_signature
async with session.post(destination, data=data, headers=headers) as response: async with session.post(
destination, data=data, headers=headers
) as response:
if not response.ok: if not response.ok:
logger.exception( logger.exception(
"Failed to send broadcast with legacy keyId to %s: %s", destination, response.reason "Failed to send broadcast with legacy keyId to %s: %s",
destination,
response.reason,
) )
return response return response

View file

@ -22,7 +22,9 @@ def create_key_pair():
return private_key, public_key return private_key, public_key
def make_signature(method, sender, destination, date, digest=None, use_legacy_key=False): def make_signature(
method, sender, destination, date, digest=None, use_legacy_key=False
):
"""uses a private key to sign an outgoing message""" """uses a private key to sign an outgoing message"""
inbox_parts = urlparse(destination) inbox_parts = urlparse(destination)
signature_headers = [ signature_headers = [
@ -39,7 +41,11 @@ def make_signature(method, sender, destination, date, digest=None, use_legacy_ke
signer = pkcs1_15.new(RSA.import_key(sender.key_pair.private_key)) signer = pkcs1_15.new(RSA.import_key(sender.key_pair.private_key))
signed_message = signer.sign(SHA256.new(message_to_sign.encode("utf8"))) signed_message = signer.sign(SHA256.new(message_to_sign.encode("utf8")))
# For legacy reasons we need to use an incorrect keyId for older Bookwyrm versions # For legacy reasons we need to use an incorrect keyId for older Bookwyrm versions
key_id = f"{sender.remote_id}#main-key" if use_legacy_key else f"{sender.remote_id}/#main-key" key_id = (
f"{sender.remote_id}#main-key"
if use_legacy_key
else f"{sender.remote_id}/#main-key"
)
signature = { signature = {
"keyId": key_id, "keyId": key_id,
"algorithm": "rsa-sha256", "algorithm": "rsa-sha256",

View file

@ -137,7 +137,9 @@ def has_valid_signature(request, activity):
return False return False
if signature.key_id != remote_user.key_pair.remote_id: if signature.key_id != remote_user.key_pair.remote_id:
if signature.key_id != f"{remote_user.remote_id}#main-key": # legacy Bookwyrm if (
signature.key_id != f"{remote_user.remote_id}#main-key"
): # legacy Bookwyrm
raise ValueError("Wrong actor created signature.") raise ValueError("Wrong actor created signature.")
try: try: