Created
June 22, 2023 00:51
-
-
Save jtomchak/62f105e5f74d008e0a4c042e382a75c1 to your computer and use it in GitHub Desktop.
ActivityPub HTTP Signature and POST activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# Given a paylaod this will provide a signature | |
# and POST that payload to the target inbox | |
class SendMessageToInboxService < BaseService | |
@relay = 'https://acctrelay.moth.social' | |
class Error < StandardError; end | |
def call(target_host, content) | |
@target_host = target_host | |
@content = content | |
post_message_to_inbox | |
end | |
def post_message_to_inbox | |
sha256 = OpenSSL::Digest.new('SHA256') | |
digest = 'SHA-256=' + Base64.strict_encode64(sha256.digest(@content.to_json)) | |
date = Time.now.utc.httpdate | |
keypair = OpenSSL::PKey::RSA.new(ENV['PRIVATE_KEY']) | |
signed_string = "(request-target): post /inbox\nhost: #{@target_host}\ndate: #{date}\ndigest: #{digest}" | |
signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), signed_string)) | |
header = "keyId=\"https://acctrelay.moth.social/actor#main-key\",headers=\"(request-target) host date digest\",signature=\"#{signature}\"" | |
Rails.logger.info "CONTENT: #{@content}" | |
Rails.logger.info "TARGET_HOST: #{@target_host}" | |
Rails.logger.info "SHA256: #{sha256}" | |
Rails.logger.info "DIGEST HEADER: #{digest}" | |
Rails.logger.info "SIGNED_STRING: #{signed_string}" | |
Rails.logger.info "Header #{header}" | |
response = HTTP.headers({ 'Host': 'staging.moth.social', 'Date': date, 'Signature': header, 'Digest': digest }).post( | |
'https://staging.moth.social/inbox', json: @content | |
) | |
Rails.logger.info "RESPONSE:>>>> #{response.status}" | |
Rails.logger.info "RESPONSE_BODY:>>>> #{response.body}" | |
Rails.logger.info response.inspect | |
response | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment