Class: Smaak::Cavage04

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adaptor) ⇒ Cavage04

Returns a new instance of Cavage04.

Raises:

  • (ArgumentError)


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

#adaptorObject (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_signedObject (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_signedObject



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

Raises:

  • (ArgumentError)


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(auth_message)
  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(auth_message.recipient)}")
  @adaptor.set_header("x-smaak-identifier", "#{auth_message.identifier}")
  @adaptor.set_header("x-smaak-route-info", "#{auth_message.route_info}")
  @adaptor.set_header("x-smaak-psk", "#{auth_message.psk}")
  @adaptor.set_header("x-smaak-expires", "#{auth_message.expires}")
  @adaptor.set_header("x-smaak-nonce", "#{auth_message.nonce}")
  @adaptor.set_header("x-smaak-encrypt", "#{auth_message.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_signatureObject



67
68
69
70
# File 'lib/smaak/cavage_04.rb', line 67

def extract_signature
  @adaptor.header("authorization") =~ /signature=\"([^"]*)\"/
  $1
end

#extract_signature_headersObject



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