From d635057cb576c5570c5ceba5945cc5339b0f41ab Mon Sep 17 00:00:00 2001 From: pdp8 Date: Mon, 11 Sep 2023 21:09:26 +0200 Subject: new create format, outbox refactoring --- server.rb | 107 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 48 insertions(+), 59 deletions(-) (limited to 'server.rb') diff --git a/server.rb b/server.rb index f44e8f0..2bcb102 100644 --- a/server.rb +++ b/server.rb @@ -5,7 +5,7 @@ post '/inbox' do begin @activity = JSON.parse @body rescue StandardError => e - p @body + p e, @body halt 400 end # deleted actors return 403 => verification error @@ -80,7 +80,7 @@ helpers do def follow update_collection FOLLOWERS, @activity['actor'] - outbox 'Accept', @activity, [@activity['actor']] + create_activity 'Accept', @activity, [@activity['actor']] end def accept @@ -96,7 +96,7 @@ helpers do def undo case @activity['object']['type'] when 'Follow' - update_collection FOLLOWERS, @activity['object']['actor'], true + update_collection FOLLOWERS, @activity['object']['actor'], 'delete' when 'Create', 'Announce' file = find_file @activity['object']['object'] FileUtils.rm(file) if file @@ -119,7 +119,7 @@ helpers do end def move - outbox 'Follow', @activity['target'], [@activity['target']] if @activity['actor'] == @activity['object'] + create_activity 'Follow', @activity['target'], [@activity['target']] if @activity['actor'] == @activity['object'] end def handle_activity @@ -187,59 +187,48 @@ helpers do halt 403 unless key.verify(OpenSSL::Digest.new('SHA256'), signature, comparison) end - def actor_inbox(url) - actor = fetch url - return unless actor - - if actor['endpoints'] and actor['endpoints']['sharedInbox'] - actor['endpoints']['sharedInbox'] - elsif actor['inbox'] - actor['inbox'] - end - end - - def outbox(type, object, to) # https://github.com/mastodon/mastodon/blob/main/app/lib/request.rb - to = [to] if to.is_a?(String) - inboxes = [] - to.uniq.each do |url| - next if [ACTOR, 'https://www.w3.org/ns/activitystreams#Public'].include? url - - if url == FOLLOWERS_URL - JSON.load_file(FOLLOWERS)['orderedItems'].each do |follower| - inboxes << actor_inbox(follower) - end - next - end - inboxes << actor_inbox(url) - end - - # add date and id, save - activity_path = save_activity({ - '@context' => 'https://www.w3.org/ns/activitystreams', - 'type' => type, - 'actor' => ACTOR, - 'object' => object, - 'to' => to - }, OUTBOX) - - # p activity_path - body = File.read(activity_path) - sha256 = OpenSSL::Digest.new('SHA256') - digest = "SHA-256=#{sha256.base64digest(body)}" - keypair = OpenSSL::PKey::RSA.new(File.read('private.pem')) - - inboxes.compact.uniq.each do |inbox| - uri = URI(inbox) - httpdate = Time.now.utc.httpdate - string = "(request-target): post #{uri.request_uri}\nhost: #{uri.host}\ndate: #{httpdate}\ndigest: #{digest}\ncontent-type: #{CONTENT_TYPE}" - signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), string)) - signed_header = "keyId=\"#{ACTOR}#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date digest content-type\",signature=\"#{signature}\"" - - # Net::HTTP fails with OpenSSL error - curl( - "-X POST -H 'Host: #{uri.host}' -H 'Date: #{httpdate}' -H 'Digest: #{digest}' -H 'Signature: #{signed_header}' --data-binary '@#{activity_path}'", inbox - ) - end - activity_path - end + # def outbox(type, object, to) # https://github.com/mastodon/mastodon/blob/main/app/lib/request.rb + # to = [to] if to.is_a?(String) + # inboxes = [] + # to.uniq.each do |url| + # next if [ACTOR, 'https://www.w3.org/ns/activitystreams#Public'].include? url + # + # if url == FOLLOWERS_URL + # JSON.load_file(FOLLOWERS)['orderedItems'].each do |follower| + # inboxes << actor_inbox(follower) + # end + # next + # end + # inboxes << actor_inbox(url) + # end + # + # # add date and id, save + # activity_path = save_activity({ + # '@context' => 'https://www.w3.org/ns/activitystreams', + # 'type' => type, + # 'actor' => ACTOR, + # 'object' => object, + # 'to' => to + # }, OUTBOX) + # + # # p activity_path + # body = File.read(activity_path) + # sha256 = OpenSSL::Digest.new('SHA256') + # digest = "SHA-256=#{sha256.base64digest(body)}" + # keypair = OpenSSL::PKey::RSA.new(File.read('private.pem')) + # + # inboxes.compact.uniq.each do |inbox| + # uri = URI(inbox) + # httpdate = Time.now.utc.httpdate + # string = "(request-target): post #{uri.request_uri}\nhost: #{uri.host}\ndate: #{httpdate}\ndigest: #{digest}\ncontent-type: #{CONTENT_TYPE}" + # signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), string)) + # signed_header = "keyId=\"#{ACTOR}#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date digest content-type\",signature=\"#{signature}\"" + # + # # Net::HTTP fails with OpenSSL error + # curl( + # "-X POST -H 'Host: #{uri.host}' -H 'Date: #{httpdate}' -H 'Digest: #{digest}' -H 'Signature: #{signed_header}' --data-binary '@#{activity_path}'", inbox + # ) + # end + # activity_path + # end end -- cgit v1.2.3