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
173
174
175
176
177
178
179
180
181
182
183
|
# TODO check POST to outbox with mastinator.com
require 'json'
require 'net/http'
require 'uri'
require 'base64'
USER = "pdp8"
WWW_DOMAIN = "pdp8.info"
WWW_URL = "https://#{WWW_DOMAIN}"
SOCIAL_DOMAIN = "social.#{WWW_DOMAIN}"
ACCOUNT = "#{USER}@#{SOCIAL_DOMAIN}"
SOCIAL_URL = "https://#{SOCIAL_DOMAIN}"
ACTOR = URI.join(SOCIAL_URL, USER)
MATRIX = "@#{USER}:matrix.#{WWW_DOMAIN}"
class Application
def call(env)
code = 404
type = "application/activity+json"
case env['REQUEST_METHOD']
when 'POST'
case env["REQUEST_URI"]
when "/inbox" # receive from server
if verify(env)
save JSON.parse(env["rack.input"].gets), "inbox"
code = 200
response = "OK"
else
code = 401
response = "not verified"
end
when "/outbox" # receive from client
# TODO auth
if auth(env)
input = JSON.parse(env["rack.input"].gets)
input["type"] ? activity = input : activity = activity(input) # expand object to create activity
save activity, "outbox"
deliver ["to", "bto", "cc", "bcc", "audience"].collect { |d| activity[d] }.flatten.uniq
case activity["type"]
when "Create"
when "Update"
when "Delete"
when "Follow"
when "Remove"
when "Like"
when "Block"
when "Undo"
end
code = 200
response = "OK"
else
code = 403
response = "forbidden"
end
end
when 'GET'
case env["REQUEST_URI"]
when "/.well-known/webfinger?resource=acct:#{ACCOUNT}"
type = "application/jrd+json"
response = File.read("webfinger")
code = 200
when "/#{USER}"
response = File.read(USER)
code = 200
when %r{/[inbox|outbox|following|followers|likes|shares]}
response = ordered_collection env["REQUEST_URI"]
code = 200
end
end
[code, { "Content-Type" => type }, [response]]
end
def activity object
date = Time.now.strftime("%Y-%m-%dT%H:%M:%S.%3N")
object["id"] = "TODO"
object["attributedTo"] = ACTOR
object["published"] = date
{
"@context" => "https://www.w3.org/ns/activitystreams",
"type" => "Create",
"id" => "https://example.net/~mallory/87374",
"actor" => ACTOR,
"object" => object,
"published" => date,
"to" => object["to"],
"cc" => object["cc"]
}
end
def save activity, dir
date = Time.now.strftime("%Y-%m-%dT%H:%M:%S.%3N")
File.open(File.join(dir, date), "w+") { |f| f.puts activity.to_json }
end
def deliver addr
p addr
keypair = OpenSSL::PKey::RSA.new(File.read('private.pem'))
addr.each do |url|
date = Time.now.utc.httpdate
uri = URI.parse(url)
signed_string = "(request-target): post /inbox\nhost: #{uri.host}\ndate: #{date}"
signed_string = keypair.sign(OpenSSL::Digest::SHA256.new, signed_string)
signature = Base64.urlsafe_encode64(signed_string).encode("UTF-8")
signed_header = 'keyId="#{url}",headers="(request-target) host date",signature="' + signature + '"'
inbox_url = JSON.parse(Net::HTTP.get(uri,
{ 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }))["inbox"]
inbox = URI.parse(inbox_url)
http = Net::HTTP.new(inbox.host, inbox.port)
http.use_ssl = true
# http.verify_mode = OpenSSL::SSL::VERIFY_NONE
header = {
'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'Host' => inbox.host,
'Date' => date,
'Signature' => signed_header,
}
request = Net::HTTP::Post.new(inbox.request_uri, header)
request.body = document.to_json
response = http.request(request)
puts(response.body, response.code)
end
end
def ordered_collection dir
collection = dir.sub(/^\//, "")
posts = Dir[File.join(collection, "*.json")].sort.reverse.collect { |f| p f; JSON.parse(File.read f) }
{
"@context" => "https://www.w3.org/ns/activitystreams",
"summary" => "#{USER} #{collection}",
"type" => "OrderedCollection",
"totalItems" => posts.size,
"orderedItems" => posts,
}.to_json
end
def verify env
begin
signature_header = env["HTTP_SIGNATURE"].split(',').each do |pair|
pair.gsub('"', '').split('=')
end.to_h
key_id = signature_header['keyId']
headers = signature_header['headers']
signature = Base64.urlsafe_decode64(signature_header['signature'].encode("ascii-8bit"))
uri = URI(key_id)
res = Net::HTTP.get_response(uri)
actor = JSON.parse(res.body)
key = OpenSSL::PKey::RSA.new(actor['publicKey']['publicKeyPem'])
comparison = headers.split(' ').map do |signed_header_name|
if signed_header_name == '(request-target)'
'(request-target): post /inbox'
else
"#{signed_header_name}: #{env["HTTP_" + signed_header_name.upcase]}"
end
end.join("\n").encode("ascii-8bit")
key.verify(OpenSSL::Digest::SHA256.new, signature, comparison)
rescue
false
end
end
def auth
true
end
end
|