Created
July 30, 2018 08:29
-
-
Save briehanlombaard/f972b98b30bfae6614f67a7fe22394f0 to your computer and use it in GitHub Desktop.
Mastodon ActivityPub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"@context": "https://www.w3.org/ns/activitystreams", | |
"id": "https://my-example.com/create-hello-world", | |
"type": "Create", | |
"actor": "https://my-example.com/actor", | |
"object": { | |
"id": "https://my-example.com/hello-world", | |
"type": "Note", | |
"published": "2018-06-23T17:17:11Z", | |
"attributedTo": "https://my-example.com/actor", | |
"inReplyTo": "https://mastodon.social/@Gargron/100254678717223630", | |
"content": "<p>Hello world</p>", | |
"to": "https://www.w3.org/ns/activitystreams#Public" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/ | |
import base64 | |
import requests | |
from datetime import datetime | |
from time import mktime | |
from wsgiref.handlers import format_date_time | |
from cryptography.hazmat.backends import default_backend | |
from cryptography.hazmat.primitives import hashes, serialization | |
from cryptography.hazmat.primitives.asymmetric import rsa, padding | |
private_key = rsa.generate_private_key( | |
public_exponent=65537, | |
key_size=2048, | |
backend=default_backend() | |
) | |
public_key = private_key.public_key() | |
pem = private_key.private_bytes( | |
encoding=serialization.Encoding.PEM, | |
format=serialization.PrivateFormat.PKCS8, | |
encryption_algorithm=serialization.BestAvailableEncryption(b'1234') | |
) | |
with open('private.pem', 'wb') as f: | |
f.write(pem) | |
del private_key, pem | |
document = open('create-hello-world.json').readlines() | |
date = format_date_time(mktime(datetime.utcnow().timetuple())) | |
with open('private.pem', 'rb') as key_file: | |
private_key = serialization.load_pem_private_key( | |
key_file.read(), | |
password='1234', | |
backend=default_backend() | |
) | |
signed_string = "(request-target): post /inbox\nhost: mastodon.social\ndate: %s" % date | |
signature = base64.encodestring(private_key.sign( | |
signed_string, | |
padding.PSS( | |
mgf=padding.MGF1(hashes.SHA256()), | |
salt_length=padding.PSS.MAX_LENGTH | |
), | |
hashes.SHA256() | |
)) | |
header = 'keyId="https://my-example.com/actor",headers="(request-target) host date",signature="' + signature.replace('\n', '\\n') + '"' | |
requests.post('https://mastodon.social/inbox', document, headers={ | |
'Host': 'mastodon.social', | |
'Date': date, | |
'Signature': header | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment