summaryrefslogtreecommitdiff
path: root/application.rb
blob: 76188552ea63b560f8a7a2123590be29c360c732 (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
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"
        type = "text/plain"
        signature_header = {}
        if env["HTTP_SIGNATURE"].split(',')
          env["HTTP_SIGNATURE"].split(',').each do |pair|
            k, v = pair.split('=')
            signature_header[k] = v.gsub('"', '')
          end
          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_string = 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")
          if key.verify(OpenSSL::Digest::SHA256.new, signature, comparison_string.encode("ascii-8bit"))
            input = JSON.parse(env["rack.input"].gets)
            # p input
            code = 200
            response = "OK"
          else
            code = 401
            response = 'Request signature could not be verified'
          end
        else
          code = 401
          response = 'Request signature could not be verified'
        end

      end

    when 'GET'

      case env["REQUEST_URI"]

      when "/.well-known/webfinger?resource=acct:#{ACCOUNT}"
        code = 200
        type = "application/jrd+json"
        response = {
          "subject" => "acct:#{ACCOUNT}",
          "links" => [
            {
              "rel" => "self",
              "type" => "application/activity+json",
              "href" => ACTOR
            }
          ]
        }

      when "/#{USER}"
        code = 200
        response = {
          "@context" => ["https://www.w3.org/ns/activitystreams"],
          "id" => ACTOR,
          "type" => "Person",
          "preferredUsername" => USER,
          "name" => USER,
          "inbox" => URI.join(SOCIAL_URL, "inbox"),
          "outbox" => URI.join(SOCIAL_URL, "outbox"),
          "following" => URI.join(SOCIAL_URL, "following"),
          "followers" => URI.join(SOCIAL_URL, "followers"),
          "liked" => URI.join(SOCIAL_URL, "liked"),
          "icon" => {
            "type" => "Image",
            "url" => "https://pdp8.info/pdp8.png"
          },
          "attachment": [
            {
              "type": "PropertyValue",
              "name": "Web",
              "value": "<a href=\"#{WWW_URL}\">#{WWW_DOMAIN}</a>"
            },
            {
              "type": "PropertyValue",
              "name": "Fediverse",
              "value": "<a rel=\"me\" href=\"#{ACTOR}\">@#{ACCOUNT}</a>"
            },
            {
              "type": "PropertyValue",
              "name": "Matrix",
              "value": "<a rel=\"me\" href=\"https://matrix.to/#/#{MATRIX}\">#{MATRIX}</a>"
            }
          ],
          "publicKey" => {
            "@context" => "https://w3id.org/security/v1",
            "@type" => "Key",
            "id" => "#{ACTOR}#main-key",
            "owner" => ACTOR,
            "publicKeyPem" => File.read("public.pem")
          }
        }

      when "/outbox"
        code = 200
        type = "application/activity+json"
        response = {
          "@context" => "https://www.w3.org/ns/activitystreams",
          "summary" => "",
          "type" => "OrderedCollection",
          "totalItems" => 2,
          # TODO generate items from src/www
          "orderedItems" => [
            {
              "type" => "Note",
              "name" => "A Simple Note",
              "tag" => [
                {
                  "type" => "Hashtag",
                  "name" => "#activitypub",
                  "href" => "https://s3lph.me/activitypub/tags/activitypub"
                },
              ]
            },
            {
              "type" => "Note",
              "name" => "Another Simple Note"
            }
          ]
        }
      when "/following"
      when "/followers"
      when "/liked"
      end

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