Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Something should be fixed: the language support for Python 3.x and the http signature part #1

Closed
Yoxem opened this issue Jun 5, 2022 · 3 comments

Comments

@Yoxem
Copy link

Yoxem commented Jun 5, 2022

@timmot
Copy link
Owner

timmot commented Jun 6, 2022

Try using the following for private_key_text, I believe you need to use a bytestring instead of an ascii string.

private_key_text = open('./lianlok/private.pem', 'rb').read()

@Yoxem
Copy link
Author

Yoxem commented Jun 6, 2022

Thank you, but the problem have another: it should have a digest that is a converted string from json of request body via sha256 and base64.

The result is here:

from cryptography.hazmat.backends import default_backend as crypto_default_backend
from cryptography.hazmat.primitives import serialization as crypto_serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

from urllib.parse import urlparse
import base64
import datetime
import requests
import json
import hashlib

target_id = "https://dest.instance.social/users/peter"

recipient_url = target_id
recipient_inbox = target_id + "/inbox"



sender_url = "https://example.net/users/john"
sender_key = "https://example.net/users/john#main-key"

activity_id =  "https://example.net/users/john/follows/test"


# The following is to sign the HTTP request as defined in HTTP Signatures.
private_key_text = open('./lianlok/private.pem', 'rb').read() # load from file

private_key = crypto_serialization.load_pem_private_key(
    private_key_text,
    password=None,
    backend=crypto_default_backend()
)

current_date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')

recipient_parsed = urlparse(recipient_inbox)
recipient_host = recipient_parsed.netloc
recipient_path = recipient_parsed.path

# Now that the header is set up, we will construct the message
follow_request_message = {
    "@context": "https://www.w3.org/ns/activitystreams",
    "id": "https://example.net/users/john",
    "type": "Follow",
    "actor": sender_url,
    "object": recipient_url
}

# generating digest
request_message_json = json.dumps(follow_request_message)
digest = base64.b64encode(hashlib.sha256(request_message_json.__str__().encode('utf-8')).digest())

signature_text = b'(request-target): post %s\ndigest: SHA-256=%s\nhost: %s\ndate: %s' % (recipient_path.encode('utf-8'), digest, recipient_host.encode('utf-8'), current_date.encode('utf-8'))

raw_signature = private_key.sign(
    signature_text,
    padding.PKCS1v15(),
    hashes.SHA256()
)

signature_header = 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) digest host date",signature="%s"' % (sender_key, base64.b64encode(raw_signature).decode('utf-8'))

headers = {
    'Date': current_date,
    'Content-Type': 'application/activity+json',
    'Host': recipient_host,
    'Digest': "SHA-256="+digest.decode('utf-8'),
    'Signature': signature_header
}




r = requests.post(recipient_inbox, headers=headers, json=follow_request_message)

Then it gives:

<Response [202]>

Therefore, the digest-adding can be added to the tutorial.

Close the issue.

@Yoxem Yoxem closed this as completed Jun 6, 2022
@Yoxem Yoxem changed the title Something should be fixed: the language support for Python 3.x and the html signature part Something should be fixed: the language support for Python 3.x and the http signature part Jun 6, 2022
@fireindark707
Copy link

I suggest to merge the code of this issue into the current readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants