summaryrefslogtreecommitdiff
path: root/post.rb
blob: ed7a964b63cf0728a84d5bc1fbd96ef053957f7a (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
#!/usr/bin/env ruby
require 'json'
require 'time'
require 'openssl'
require 'base64'
require 'net/http'
require 'uri'

document      = { "a" => 2 } # .to_json
date          = Time.now.utc.httpdate
keypair       = OpenSSL::PKey::RSA.new(File.read('private.pem'))
signed_string = "(request-target): post /inbox\nhost: social.pdp8.info\ndate: #{date}"
signed_string = keypair.sign(OpenSSL::Digest::SHA256.new, signed_string)
signature = Base64.urlsafe_encode64(signed_string).encode("UTF-8")
signed_header = 'keyId="https://social.pdp8.info/pdp8",headers="(request-target) host date",signature="' + signature + '"'

uri = URI.parse("https://social.pdp8.info/inbox")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
header = {
  'Host' => 'social.pdp8.info',
  'Date' => date,
  'Signature' => signed_header,
}
request = Net::HTTP::Post.new(uri.request_uri, header)
request.body = document.to_json

response = http.request(request)
puts(response.body, response.code)