Class: Dkim::SignedMail
- Inherits:
-
Object
- Object
- Dkim::SignedMail
- Includes:
- Options
- Defined in:
- lib/dkim/signed_mail.rb
Instance Attribute Summary
Attributes included from Options
#body_canonicalization, #domain, #expire, #header_canonicalization, #identity, #options, #private_key, #selector, #signable_headers, #signing_algorithm, #time
Instance Method Summary collapse
-
#canonical_body ⇒ String
Body of message in its canonical form.
-
#canonical_header ⇒ String
Signed headers of message in their canonical forms.
- #canonicalized_headers ⇒ Object
-
#dkim_header ⇒ DkimHeader
Constructed signature for the mail message.
-
#initialize(message, options = {}) ⇒ SignedMail
constructor
A new instance of SignedMail.
-
#signed_headers ⇒ Array<String>
Lowercased names of headers in the order they are signed.
-
#to_s ⇒ String
Message combined with calculated dkim header signature.
Constructor Details
#initialize(message, options = {}) ⇒ SignedMail
A new instance of SignedMail
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dkim/signed_mail.rb', line 17 def initialize , ={} = .to_s.gsub(/\r?\n/, "\r\n") headers, body = .split(/\r?\n\r?\n/, 2) @original_message = @headers = Header.parse headers @body = Body.new body # default options from Dkim.options @options = Dkim..merge() end |
Instance Method Details
#canonical_body ⇒ String
Returns Body of message in its canonical form.
45 46 47 |
# File 'lib/dkim/signed_mail.rb', line 45 def canonical_body @body.to_s(body_canonicalization) end |
#canonical_header ⇒ String
Returns Signed headers of message in their canonical forms.
40 41 42 |
# File 'lib/dkim/signed_mail.rb', line 40 def canonical_header canonicalized_headers.to_s(header_canonicalization) end |
#canonicalized_headers ⇒ Object
28 29 30 |
# File 'lib/dkim/signed_mail.rb', line 28 def canonicalized_headers CanonicalizedHeaders.new(@headers, signed_headers) end |
#dkim_header ⇒ DkimHeader
Returns Constructed signature for the mail message.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/dkim/signed_mail.rb', line 50 def dkim_header dkim_header = DkimHeader.new raise "A private key is required" unless private_key raise "A domain is required" unless domain raise "A selector is required" unless selector # Add basic DKIM info dkim_header['v'] = '1' dkim_header['a'] = signing_algorithm dkim_header['c'] = "#{header_canonicalization}/#{body_canonicalization}" dkim_header['d'] = domain dkim_header['i'] = identity if identity dkim_header['q'] = 'dns/txt' dkim_header['s'] = selector dkim_header['t'] = (time || Time.now).to_i dkim_header['x'] = expire.to_i if expire # Add body hash and blank signature dkim_header['bh']= digest_alg.digest(canonical_body) dkim_header['h'] = signed_headers.join(':') dkim_header['b'] = '' # Calculate signature based on intermediate signature header headers = canonical_header headers << dkim_header.to_s(header_canonicalization) dkim_header['b'] = private_key.sign(digest_alg, headers) dkim_header end |
#signed_headers ⇒ Array<String>
Returns lowercased names of headers in the order they are signed.
33 34 35 36 37 |
# File 'lib/dkim/signed_mail.rb', line 33 def signed_headers @headers.map(&:relaxed_key).select do |key| signable_headers.map(&:downcase).include?(key) end end |
#to_s ⇒ String
Returns Message combined with calculated dkim header signature.
82 83 84 |
# File 'lib/dkim/signed_mail.rb', line 82 def to_s dkim_header.to_s + "\r\n" + @original_message end |