From 66c795a824e2931c69e2ded5e3682658e7d2c409 Mon Sep 17 00:00:00 2001 From: rscmbbng Date: Wed, 28 Nov 2018 13:29:22 +0100 Subject: [PATCH] initial commit --- .gitignore | 4 +++ start.py | 61 ++++++++++++++++++++++++++++++++++++++++ templates/actor.json | 17 +++++++++++ templates/webfinger.json | 11 ++++++++ 4 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 start.py create mode 100644 templates/actor.json create mode 100644 templates/webfinger.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0d407c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv/ +*.pem +__pycache__ +*.pyc diff --git a/start.py b/start.py new file mode 100644 index 0000000..651e6d3 --- /dev/null +++ b/start.py @@ -0,0 +1,61 @@ +import flask, os +from flask import request +from flask import Response + + +# 51.15.76.123 - - [13/Nov/2018:21:06:48 +0100] "GET /.well-known/webfinger?resource=acct:r@roelof.info HTTP/1.1" 404 4021 "-" "http.rb/3.3.0 (Mastodon/2.6.1; +https://post.lurk.org/)" +# 51.15.76.123 - - [13/Nov/2018:21:06:48 +0100] "GET /.well-known/host-meta HTTP/1.1" 404 4021 "-" "http.rb/3.3.0 (Mastodon/2.6.1; +https://post.lurk.org/) + +#https://blog.joinmastodon.org/2018/06/how-to-implement-a-basic-activitypub-server/ + +#https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/ + + +#Config +DOMAIN = 'https://poep.club' +USERNAME = 'The Username' + +def public_key(): + if not os.path.exists('public.pem'): + os.system('openssl genrsa -out private.pem 2048') + os.system('openssl rsa -in private.pem -outform PEM -pubout -out public.pem') + else: + public_key = open('public.pem').read() + public_key = public_key.replace('\n','\\n') #public key shouldn't contain verbatim linebreaks + return public_key + +public_key() #generate public_key on first launch + +#Flask +app = flask.Flask(__name__) + +@app.route('/') +def index(): + return 'test' + +@app.route('/.well-known/webfinger') +def finger(): + """ + Respond to webfinger queries with a json object pointing at the actor + """ + if request.args.get('resource'): + query = request.args.get('resource') + + actor = query.split(':')[1].split('@')[0] # from 'acct:alice@my-example.com' to 'alice' + + #"subject": "acct:alice@my-example.com", + + json = flask.render_template('webfinger.json', query=query, actor=actor, domain=DOMAIN) # render our ActivityPub answer + + return Response(response=json, status=200, mimetype="application/json") # return that as a json object + +@app.route('/users/') +def profile(actor): + json = flask.render_template('actor.json', username=USERNAME, actor=actor, domain=DOMAIN, public_key=public_key()) # render our ActivityPub answer + return Response(response=json, status=200, mimetype="application/json") # return that as a json object + + +if __name__ == '__main__': + app.debug =True + app.run() + diff --git a/templates/actor.json b/templates/actor.json new file mode 100644 index 0000000..d6082ca --- /dev/null +++ b/templates/actor.json @@ -0,0 +1,17 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1" + ], + + "id": "{{domain}}/users/{{actor}}", + "type": "Person", + "preferredUsername": "{{username}}", + "inbox": "{{domain}}/users/{{actor}}/inbox", + + "publicKey": { + "id": "{{domain}}/users/{{actor}}#main-key", + "owner": "{{domain}}/users/{{actor}}", + "publicKeyPem": "{{public_key}}" + } +} \ No newline at end of file diff --git a/templates/webfinger.json b/templates/webfinger.json new file mode 100644 index 0000000..77a3cb0 --- /dev/null +++ b/templates/webfinger.json @@ -0,0 +1,11 @@ +{ + "subject": "{{query}}", + + "links": [ + { + "rel": "self", + "type": "application/activity+json", + "href": "{{domain}}/{{actor}}" + } + ] + } \ No newline at end of file