-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
172 lines (154 loc) · 5.07 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import time, json, base64, requests
from flask import Flask, request, jsonify
from urllib.parse import urlparse
app = Flask(__name__)
app.config.from_pyfile("config.ini")
@app.route("/")
def index():
return "<p>Hi.</p>"
@app.route("/.well-known/webfinger")
def wf():
return jsonify(
{
"subject": "acct:%(USERNAME)s@%(HOSTNAME)s" % app.config,
"links": [
{
"rel": "self",
"type": "application/activity+json",
"href": "https://%(HOSTNAME)s/u/%(USERNAME)s" % app.config,
}
],
}
)
@app.route("/u/<username>")
def user(username):
my_url = "https://%(HOSTNAME)s/u/%(USERNAME)s" % app.config
return (
jsonify(
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
],
"id": my_url,
"type": "Person",
"following": "https://%(HOSTNAME)s/following" % app.config,
"followers": "https://%(HOSTNAME)s/followers" % app.config,
"inbox": "https://%(HOSTNAME)s/inbox" % app.config,
"outbox": "https://%(HOSTNAME)s/outbox" % app.config,
"preferredUsername": app.config["USERNAME"],
"name": app.config["USERNAME"],
"summary": "",
"url": my_url,
"manuallyApprovesFollowers": False,
"discoverable": True,
"indexable": False,
"published": "2024-04-04T00:00:00Z",
"memorial": False,
"publicKey": {
"id": my_url + "#main-key",
"owner": my_url,
"publicKeyPem": open("public.key", "r").read(),
},
"icon": {
"type": "Image",
"mediaType": "image/jpeg",
"url": app.config["ICON"],
},
}
),
200,
{"Content-type": "application/activity+json"},
)
@app.route("/following")
def following():
my_url = "https://%(HOSTNAME)s/u/%(USERNAME)s" % app.config
return (
jsonify(
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://%(HOSTNAME)s/following" % app.config,
"type": "Collection",
"totalItems": 0,
"items": [],
}
),
200,
{"Content-type": "application/activity+json"},
)
@app.route("/followers")
def followers():
my_url = "https://%(HOSTNAME)s/u/%(USERNAME)s" % app.config
return (
jsonify(
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://%(HOSTNAME)s/followers" % app.config,
"type": "Collection",
"totalItems": 0,
"items": [],
}
),
200,
{"Content-type": "application/activity+json"},
)
@app.route("/inbox", methods=["POST"])
def inbox():
j = request.get_json(force=True)
if j.get('type') == 'Follow':
p = urlparse(j.get('actor'))
send(p.netloc, '/inbox', str(time.time()), 'Accept', j)
return ("", 202, {})
def sign_and_send(body, hostname, path):
from email.utils import formatdate
from Crypto.Hash import SHA256
from Crypto.Signature import pkcs1_15
from Crypto.PublicKey import RSA
now = formatdate(usegmt=True)
pk = RSA.import_key(open("private.key").read())
post_hash = SHA256.new(body.encode("u8")).digest()
post_hash_decoded = base64.b64encode(post_hash).decode()
headers = (
"(request-target): post "
+ path
+ "\n"
+ "host: "
+ hostname
+ "\n"
+ "date: "
+ now
+ "\n"
+ "digest: SHA-256="
+ post_hash_decoded
)
signature = base64.b64encode(
pkcs1_15.new(pk).sign(SHA256.new(headers.encode("u8")))
).decode() # such transcode, wow
keyId = "https://%(HOSTNAME)s/u/%(USERNAME)s#main-key" % app.config
sig_header = (
'keyId="%s", algorithm="rsa-sha256", headers="(request-target) host date digest", signature="%s"'
% (keyId, signature)
)
headers = {
"signature": sig_header,
"host": hostname,
"digest": "SHA-256=" + post_hash_decoded,
"content-type": "application/activity+json",
"date": now,
}
res = requests.post("https://%s%s" % (hostname, path), data=body, headers=headers)
return res.text
def send(server, uri, id_, type_, object_):
my_url = "https://%(HOSTNAME)s/u/%(USERNAME)s" % app.config
data = json.dumps(
{
"@context": [
"https://www.w3.org/ns/activitystreams",
],
"id": my_url + "/" + id_,
"actor": my_url,
"type": type_,
"object": object_
}
)
return sign_and_send(data, server, uri)