summaryrefslogtreecommitdiff
path: root/application.rb
blob: 9e4e3a84480f4d6711ac8516ea7f14619e470333 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# TODO
# run as service
# federation
# client post media
# client follow
# client get media
# server follow
# test with pleroma etc
require 'json'
require 'net/http'
require 'uri'
require 'base64'
require 'securerandom'
require 'fileutils'
require 'digest/sha2'
require 'nokogiri'

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 = File.join(SOCIAL_URL, USER)

class Application
  def call(env)
    code = 404
    type = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
    response = "Not found."

    case env['REQUEST_METHOD']

    when 'POST'
      input = env["rack.input"].read
      case env["REQUEST_PATH"]

      when "/inbox" # receive from server
        if verify(env)
          begin
            object = JSON.parse(input)
            case object["type"]
            when "Create"
              File.open(File.join("inbox", SecureRandom.uuid + ".json"), "w+") { |f| f.puts input }
            when "Delete"
              puts input
            when "Follow"
              File.open(File.join("followers", SecureRandom.uuid + ".json"), "w+") { |f| f.puts input }
              # TODO return accept activity
            when "Undo"
              puts input
            else
              puts input
            end
            code = 200
            response = "OK"
          rescue => e
            puts input, e.to_s
            response = "Request body contains invalid json."
          end
        else
          code = 403
          response = "Key verification failed for POST to #{env["REQUEST_URI"]}."
        end

      when "/outbox" # receive from client
        if auth(env)
          code, response = process input
        else
          code = 403
          response = "You are not allowed to POST to #{env["REQUEST_URI"]}."
        end

      end

    when 'GET'

      case env["REQUEST_URI"] # REQUEST_PATH does not contain queries

      when "/.well-known/webfinger?resource=acct:#{ACCOUNT}"
        type = "application/jrd+json"
        response = File.read("webfinger")
        code = 200

      when "/#{USER}"
        # TODO serve html
        response = File.read(USER)
        code = 200

      when "/inbox"
        if auth(env)
          type, response = format ordered_collection(env["REQUEST_PATH"]), env["HTTP_ACCEPT"]
          code = 200
        else
          code = 403
          response = "You are not allowed to GET #{env["REQUEST_URI"]}."
        end

      when %r{/[outbox|following|followers|likes|shares]}
        response = ordered_collection(env["REQUEST_PATH"]).to_json
        code = 200
      end

    end
    [code, { "Content-Type" => type }, [response]]
  end

  def process input
    date = Time.now.strftime("%Y-%m-%dT%H:%M:%S")
    # TODO media attachments, hashtags
    note = {
      "@context" => "https://www.w3.org/ns/activitystreams",
      "id" => File.join(SOCIAL_URL, "note", SecureRandom.uuid + ".json"),
      "type" => "Note",
      "attributedTo" => ACTOR,
      "published" => date,
      "content" => ""
    }
    create = {
      "@context" => "https://www.w3.org/ns/activitystreams",
      "id" => File.join(SOCIAL_URL, "create", SecureRandom.uuid + ".json"),
      "type" => "Create",
      "actor" => ACTOR,
      "object" => note,
      "published" => date,
    }
    recipients = []
    if /^@/.match input
      mentions, input = input.split("\n", 2)
      mentions.split(/, */).each do |m|
        recipients << actor(m.chomp)
      end
    end
    note["content"] = input.lines.select { |l| !l.empty? }.join("<br>")
    recipients += Dir[File.join("followers", "*.json")].collect { |f| JSON.parse(File.read(f))["actor"] }
    recipients.delete ACTOR
    recipients.uniq!
    note["to"] = recipients
    create["to"] = recipients

    save create
    save note
    FileUtils.ln_s File.join('..', path(create)), "outbox"

    responses = send create, recipients
    if responses.collect { |r| r.code.to_i }.uniq.max < 400
      code = 200
      response = "OK"
    else
      code = 502
      response = responses.select { |r| r.code.to_i >= 400 }.collect { |r| r.body }.uniq
    end
    [code, response]
  end

  def format response, accept
    if accept == "text/plain"
      response = response["orderedItems"].collect.with_index do |r, i|
        object = r["object"]
        doc = Nokogiri::HTML(object["content"])
        str = "#{i}\t#{object["published"]}\t#{}\n#{doc.text}"
        str << "\n" + object["attachment"].collect { |att|
                        `kitty +kitten icat #{att["url"]}`
                      }.join("\n") if object["attachment"]
        str
      end.join("\n\n")
      type = "text/plain"
    else
      response = response.to_json
      type = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
    end
    [type, response]
  end

  def actor account
    account = account.sub(/^@/, '').chomp
    user, server = account.split("@")
    header = { 'Accept' => "application/jrd+json" }
    uri = URI("https://" + server + "/.well-known/webfinger?resource=acct:#{account}")
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Get.new(uri.request_uri, header)
    response = http.request(request)
    JSON.parse(response.body)["links"].select { |l| l["rel"] == "self" }[0]["href"]
  end

  def path object
    object["id"].sub(SOCIAL_URL, '').sub('/', '')
  end

  def save object
    path = path object
    FileUtils.mkdir_p File.dirname(path)
    File.open(path, "w+") { |f| f.puts object.to_json }
  end

  def send object, urls
    puts object, urls
    # https://github.com/mastodon/mastodon/blob/main/app/lib/request.rb
    keypair = OpenSSL::PKey::RSA.new(File.read('private.pem'))
    responses = []
    urls.each do |url|
      date = Time.now.utc.httpdate
      uri = URI.parse(url)

      sha256 = OpenSSL::Digest::SHA256.new
      body = object.to_json
      digest = "SHA-256=" + sha256.base64digest(body)

      signed_string = "(request-target): post #{inbox uri}\nhost: #{uri.host}\ndate: #{date}\ndigest: #{digest}\ncontent-type: application/activity+json"
      signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), signed_string))
      signed_header = 'keyId="' + ACTOR + '#main-key",algorithm="rsa-sha256",headers="(request-target) host date digest content-type",signature="' + signature + '"'

      uri = URI.parse(get(url)["inbox"])
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      header = {
        'Content-Type' => 'application/activity+json',
        'Host' => uri.host,
        'Date' => date,
        'Digest' => digest,
        'Signature' => signed_header,
      }
      request = Net::HTTP::Post.new(uri.request_uri, header)
      request.body = body

      responses << http.request(request)
    end
    puts responses
    responses
  end

  def ordered_collection dir
    collection = dir.sub(/^\//, "")
    posts = Dir[File.join(collection, "*.json")].collect { |f|
              p f; JSON.parse(File.read f)
            }.sort_by { |o| o["published"] }
    {
      "@context" => "https://www.w3.org/ns/activitystreams",
      "summary" => "#{USER} #{collection}",
      "type" => "OrderedCollection",
      "totalItems" => posts.size,
      "orderedItems" => posts,
    }
  end

  def verify env
    # https://github.com/mastodon/mastodon/blob/main/app/controllers/concerns/signature_verification.rb
    # TODO verify digest
    signature_params = {}
    env["HTTP_SIGNATURE"].split(',').each do |pair|
      k, v = pair.split('=')
      signature_params[k] = v.gsub('"', '')
    end

    key_id = signature_params['keyId']
    headers = signature_params['headers']
    signature = Base64.decode64(signature_params['signature'])

    actor = get key_id
    key = OpenSSL::PKey::RSA.new(actor['publicKey']['publicKeyPem'])

    comparison = headers.split(' ').map do |signed_params_name|
      if signed_params_name == '(request-target)'
        '(request-target): post /inbox'
      elsif signed_params_name == 'content-type'
        "#{signed_params_name}: #{env["CONTENT_TYPE"]}"
      else
        "#{signed_params_name}: #{env["HTTP_" + signed_params_name.upcase]}"
      end
    end.join("\n")

    key.verify(OpenSSL::Digest.new('SHA256'), signature, comparison)
  end

  def get url
    uri = URI(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    header = { 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }
    request = Net::HTTP::Get.new(uri.request_uri, header)
    response = http.request(request)
    JSON.parse(response.body)
  end

  def inbox uri
    URI(get(uri)["inbox"]).request_uri
  end

  def auth env
    auth = Rack::Auth::Basic::Request.new(env)
    usr = File.read(".usr").chomp
    pwd = File.read(".pwd").chomp
    auth.provided? && auth.basic? && auth.credentials && auth.credentials == [usr, pwd]
  end
end