diff --git a/app.py b/app.py index 7a374b5..cd28cad 100644 --- a/app.py +++ b/app.py @@ -1,11 +1,14 @@ #!/usr/bin/env python3 """Creatr App""" from flask import Flask, Response, request, Request, abort, jsonify + from demo.utils.checker import inbox_prechecker +from demo.activitypub import ME from demo import config -app = Flask(__name__) +app = Flask(__name__, + static_folder="demo/static",) def is_ap_requested(ap_request: Request) -> bool: """Check request accept headers.""" @@ -100,3 +103,12 @@ def wellknown_webfinger() -> Response: resp.headers["Content-Type"] = "application/jrd+json; charset=utf-8" return resp + + +@app.route(f"/meow/{config.USERNAME}") +def locate_user() -> Response: + """Return user ActivityPub response.""" + resp = jsonify(ME) + resp.headers["Content-Type"] = "application/activity+json" + + return resp diff --git a/demo/activitypub.py b/demo/activitypub.py index 3f54226..85db5d9 100644 --- a/demo/activitypub.py +++ b/demo/activitypub.py @@ -2,6 +2,61 @@ """ActivityPub settings, fetch & post""" import httpx from demo import config +from demo.utils.key import get_pubkey_as_pem + + +AS_CTX = "https://www.w3.org/ns/activitystreams" +AS_PUBLIC = "https://www.w3.org/ns/activitystreams#Public" + +AS_EXTENDED_CTX = [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + # AS ext + "Hashtag": "as:Hashtag", + "sensitive": "as:sensitive", + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "alsoKnownAs": {"@id": "as:alsoKnownAs", "@type": "@id"}, + "movedTo": {"@id": "as:movedTo", "@type": "@id"}, + # toot + "toot": "http://joinmastodon.org/ns#", + "featured": {"@id": "toot:featured", "@type": "@id"}, + "Emoji": "toot:Emoji", + "blurhash": "toot:blurhash", + "votersCount": "toot:votersCount", + }, +] + +ME = { + "@context": AS_EXTENDED_CTX, + "type": "Person", + "id": config.ID, + "following": config.BASE_URL + "/following", + "followers": config.BASE_URL + "/followers", + "featured": config.BASE_URL + "/featured", + "inbox": config.BASE_URL + "/inbox", + "outbox": config.BASE_URL + "/outbox", + "preferredUsername": config.USERNAME, + "name": config.NICKNAME, + "summary": config.ACTOR_SUMMARY, + "endpoints": { + "sharedInbox": config.BASE_URL + "/inbox", + }, + "url": config.ID + "/", # the path is important for Mastodon compat + "manuallyApprovesFollowers": False, + "attachment": [], + "icon": { + "mediaType": "image/png", + "type": "Image", + "url": config.AVATAR_URL, + }, + "publicKey": { + "id": f"{config.ID}#main-key", + "owner": config.ID, + "publicKeyPem": get_pubkey_as_pem(config.KEY_PATH), + }, + "tag": [] # TODO tag support +} def fetch( diff --git a/demo/config.py.simple b/demo/config.py.simple index 9316321..3119314 100644 --- a/demo/config.py.simple +++ b/demo/config.py.simple @@ -7,8 +7,11 @@ KEY_PATH = _ROOT_DIR / "data" / "key.pem" USER_AGENT = "COSCUP-demo" AP_CONTENT_TYPE = "application/activity+json" +ACTOR_SUMMARY = "I'm banana" USERNAME = "foo" -DOMAIN = "localhost" +NICKNAME = "foo" SCHEME = "http" +DOMAIN = "localhost" +BASE_URL = f"{SCHEME}://{DOMAIN}" ID = f"{SCHEME}://{DOMAIN}/user/{USERNAME}" -BASE_URL = f"{SCHEME}://{DOMAIN}/" +AVATAR_URL = f"{BASE_URL}/static/avatar.png" diff --git a/demo/static/avatar.png b/demo/static/avatar.png new file mode 100644 index 0000000..0e833db Binary files /dev/null and b/demo/static/avatar.png differ diff --git a/demo/utils/key.py b/demo/utils/key.py index c04124b..e637bb2 100644 --- a/demo/utils/key.py +++ b/demo/utils/key.py @@ -1,8 +1,5 @@ #!/usr/bin/env python3 """process Key.""" - -import sys - from pathlib import Path from Crypto.PublicKey import RSA from demo.config import KEY_PATH @@ -12,14 +9,3 @@ def get_pubkey_as_pem(key_path: Path) -> str: """Exporting public key from private key file.""" text = key_path.read_text() return RSA.import_key(text).public_key().export_key("PEM").decode("utf-8") - - -def gen_key(): - """Generate key.""" - if KEY_PATH.exists(): - print("is existing!") - sys.exit(2) - else: - k = RSA.generate(2048) - privkey_pem = k.exportKey("PEM").decode("utf-8") - KEY_PATH.write_text(privkey_pem)