Class: Smaak::Cavage04
- Inherits:
-
Object
- Object
- Smaak::Cavage04
- Defined in:
- lib/smaak/cavage_04.rb
Constant Summary collapse
- SPECIFICATION =
"https://datatracker.ietf.org/doc/draft-cavage-http-signatures/04/"
Instance Attribute Summary collapse
-
#adaptor ⇒ Object
readonly
Returns the value of attribute adaptor.
-
#headers_to_be_signed ⇒ Object
readonly
Returns the value of attribute headers_to_be_signed.
Class Method Summary collapse
Instance Method Summary collapse
- #compile_auth_header(signature) ⇒ Object
- #compile_signature_headers(auth_message) ⇒ Object
- #extract_signature ⇒ Object
- #extract_signature_headers ⇒ Object
-
#initialize(adaptor) ⇒ Cavage04
constructor
A new instance of Cavage04.
Constructor Details
#initialize(adaptor) ⇒ Cavage04
Returns a new instance of Cavage04.
9 10 11 12 13 |
# File 'lib/smaak/cavage_04.rb', line 9 def initialize(adaptor) raise ArgumentError.new("Must provide a valid request adaptor") if adaptor.nil? @adaptor = adaptor @headers_to_be_signed = Smaak::Cavage04::headers_to_be_signed + Smaak::headers_to_be_signed end |
Instance Attribute Details
#adaptor ⇒ Object (readonly)
Returns the value of attribute adaptor.
6 7 8 |
# File 'lib/smaak/cavage_04.rb', line 6 def adaptor @adaptor end |
#headers_to_be_signed ⇒ Object (readonly)
Returns the value of attribute headers_to_be_signed.
7 8 9 |
# File 'lib/smaak/cavage_04.rb', line 7 def headers_to_be_signed @headers_to_be_signed end |
Class Method Details
.headers_to_be_signed ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/smaak/cavage_04.rb', line 15 def self.headers_to_be_signed [ "(request-target)", "host", "date", "digest", "content-length" ] end |
Instance Method Details
#compile_auth_header(signature) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/smaak/cavage_04.rb', line 23 def compile_auth_header(signature) raise ArgumentError.new("invalid signature") if not Smaak::Utils::non_blank_string?(signature) ordered_headers = "" @adaptor.each_header do |header, value| ordered_headers = "#{ordered_headers} #{header}" if @headers_to_be_signed.include?(header) end ordered_headers = ordered_headers[1..ordered_headers.size] @adaptor.set_header("authorization", "Signature keyId=\"rsa-key-1\",algorithm=\"rsa-sha256\", headers=\"#{ordered_headers}\", signature=\"#{signature}\"") end |
#compile_signature_headers(auth_message) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/smaak/cavage_04.rb', line 33 def compile_signature_headers() body = @adaptor.body.nil? ? "" : @adaptor.body @adaptor.set_header("authorization", "") @adaptor.set_header("host", "#{@adaptor.host}") @adaptor.set_header("date", "#{gmt_now}") @adaptor.set_header("digest", "SHA-256=#{Digest::SHA256.hexdigest(body)}") @adaptor.set_header("x-smaak-recipient", "#{Smaak::Crypto::encode64(.recipient)}") @adaptor.set_header("x-smaak-identifier", "#{.identifier}") @adaptor.set_header("x-smaak-route-info", "#{.route_info}") @adaptor.set_header("x-smaak-psk", "#{.psk}") @adaptor.set_header("x-smaak-expires", "#{.expires}") @adaptor.set_header("x-smaak-nonce", "#{.nonce}") @adaptor.set_header("x-smaak-encrypt", "#{.encrypt}") @adaptor.set_header("content-type", "text/plain") @adaptor.set_header("content-length", "#{body.size}") signature_headers = "" @adaptor.each_header do |header, value| signature_headers = append_header(signature_headers, "#{header}: #{value}") if @headers_to_be_signed.include? header end signature_headers = prepend_header("(request-target)", "#{@adaptor.method.downcase} #{@adaptor.path}", signature_headers) end |
#extract_signature ⇒ Object
67 68 69 70 |
# File 'lib/smaak/cavage_04.rb', line 67 def extract_signature @adaptor.header("authorization") =~ /signature=\"([^"]*)\"/ $1 end |
#extract_signature_headers ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/smaak/cavage_04.rb', line 56 def extract_signature_headers @adaptor.header("authorization") =~ /headers=\"([^"]*)\",/ headers_order = $1.split(' ') signature_headers = "" headers_order.each do |header| signature_headers = append_header(signature_headers, "#{header}: #{@adaptor.header(header)}") end signature_headers = prepend_header("(request-target)", "#{@adaptor.method.downcase} #{@adaptor.path}", signature_headers) end |