Pull out make_signature to separate into function.
This commit is contained in:
parent
15cce156a0
commit
05842b5c18
|
@ -63,37 +63,38 @@ def broadcast_task(sender_id, activity, recipients):
|
||||||
return errors
|
return errors
|
||||||
|
|
||||||
|
|
||||||
def sign_and_send(sender, activity, destination):
|
def make_signature(sender, destination, date):
|
||||||
''' crpyto whatever and http junk '''
|
|
||||||
inbox_parts = urlparse(destination)
|
inbox_parts = urlparse(destination)
|
||||||
now = http_date()
|
|
||||||
signature_headers = [
|
signature_headers = [
|
||||||
'(request-target): post %s' % inbox_parts.path,
|
'(request-target): post %s' % inbox_parts.path,
|
||||||
'host: %s' % inbox_parts.netloc,
|
'host: %s' % inbox_parts.netloc,
|
||||||
'date: %s' % now
|
'date: %s' % date,
|
||||||
]
|
]
|
||||||
message_to_sign = '\n'.join(signature_headers)
|
message_to_sign = '\n'.join(signature_headers)
|
||||||
|
|
||||||
if not sender.private_key:
|
|
||||||
# this shouldn't happen. it would be bad if it happened.
|
|
||||||
raise ValueError('No private key found for sender')
|
|
||||||
signer = pkcs1_15.new(RSA.import_key(sender.private_key))
|
signer = pkcs1_15.new(RSA.import_key(sender.private_key))
|
||||||
signed_message = signer.sign(SHA256.new(message_to_sign.encode('utf8')))
|
signed_message = signer.sign(SHA256.new(message_to_sign.encode('utf8')))
|
||||||
|
|
||||||
signature = {
|
signature = {
|
||||||
'keyId': '%s#main-key' % sender.actor,
|
'keyId': '%s#main-key' % sender.actor,
|
||||||
'algorithm': 'rsa-sha256',
|
'algorithm': 'rsa-sha256',
|
||||||
'headers': '(request-target) host date',
|
'headers': '(request-target) host date',
|
||||||
'signature': b64encode(signed_message).decode('utf8'),
|
'signature': b64encode(signed_message).decode('utf8'),
|
||||||
}
|
}
|
||||||
signature = ','.join('%s="%s"' % (k, v) for (k, v) in signature.items())
|
return ','.join('%s="%s"' % (k, v) for (k, v) in signature.items())
|
||||||
|
|
||||||
|
def sign_and_send(sender, activity, destination):
|
||||||
|
''' crpyto whatever and http junk '''
|
||||||
|
now = http_date()
|
||||||
|
|
||||||
|
if not sender.private_key:
|
||||||
|
# this shouldn't happen. it would be bad if it happened.
|
||||||
|
raise ValueError('No private key found for sender')
|
||||||
|
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
destination,
|
destination,
|
||||||
data=json.dumps(activity),
|
data=json.dumps(activity),
|
||||||
headers={
|
headers={
|
||||||
'Date': now,
|
'Date': now,
|
||||||
'Signature': signature,
|
'Signature': make_signature(sender, destination, now),
|
||||||
'Content-Type': 'application/activity+json; charset=utf-8',
|
'Content-Type': 'application/activity+json; charset=utf-8',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue