2023-10-13 22:19:40 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SignedRequestHelpers
|
|
|
|
def get(path, headers: nil, sign_with: nil, **args)
|
2023-12-02 00:53:15 +09:00
|
|
|
return super(path, headers: headers, **args) if sign_with.nil?
|
2023-10-13 22:19:40 +09:00
|
|
|
|
|
|
|
headers ||= {}
|
|
|
|
headers['Date'] = Time.now.utc.httpdate
|
2024-06-03 18:15:58 +09:00
|
|
|
headers['Host'] = Rails.configuration.x.local_domain
|
2023-10-13 22:19:40 +09:00
|
|
|
signed_headers = headers.merge('(request-target)' => "get #{path}").slice('(request-target)', 'Host', 'Date')
|
|
|
|
|
|
|
|
key_id = ActivityPub::TagManager.instance.key_uri_for(sign_with)
|
|
|
|
keypair = sign_with.keypair
|
|
|
|
signed_string = signed_headers.map { |key, value| "#{key.downcase}: #{value}" }.join("\n")
|
|
|
|
signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), signed_string))
|
|
|
|
|
|
|
|
headers['Signature'] = "keyId=\"#{key_id}\",algorithm=\"rsa-sha256\",headers=\"#{signed_headers.keys.join(' ').downcase}\",signature=\"#{signature}\""
|
|
|
|
|
2023-12-02 00:53:15 +09:00
|
|
|
super(path, headers: headers, **args)
|
2023-10-13 22:19:40 +09:00
|
|
|
end
|
|
|
|
end
|