Class: As2::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/as2/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_info: nil, partner: nil, on_signature_failure: nil, &block) ⇒ Server



17
18
19
20
21
22
# File 'lib/as2/server.rb', line 17

def initialize(server_info: nil, partner: nil, on_signature_failure: nil, &block)
  @block = block
  @server_info = server_info || Config.server_info
  @partner = partner
  @signature_failure_handler = on_signature_failure
end

Instance Attribute Details

#logger=(value) ⇒ Object

Sets the attribute logger



9
10
11
# File 'lib/as2/server.rb', line 9

def logger=(value)
  @logger = value
end

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/as2/server.rb', line 24

def call(env)
  if env['HTTP_AS2_TO'] != @server_info.name
    return send_error(env, "Invalid destination name #{env['HTTP_AS2_TO']}")
  end

  partner = @partner || Config.partners[env['HTTP_AS2_FROM']]

  if !partner || env['HTTP_AS2_FROM'] != partner.name
    return send_error(env, "Invalid partner name #{env['HTTP_AS2_FROM']}")
  end

  request = Rack::Request.new(env)
  message = Message.new(request.body.read, @server_info.pkey, @server_info.certificate)

  unless message.valid_signature?(partner.certificate)
    if @signature_failure_handler
      @signature_failure_handler.call({
        env: env,
        smime_string: message.decrypted_message,
        verification_error: message.verification_error
      })
    else
      raise "Could not verify signature"
    end
  end

  if @block
    begin
      @block.call message.attachment.filename, message.attachment.body
    rescue Exception => e
      return send_error(env, e.message)
    end
  end

  send_mdn(env, message.mic, message.mic_algorithm)
end

#send_mdn(env, mic, mic_algorithm, failed = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/as2/server.rb', line 61

def send_mdn(env, mic, mic_algorithm, failed = nil)
  # rules for MDN construction are covered in
  # https://datatracker.ietf.org/doc/html/rfc4130#section-7.4.2

  options = {
    'Reporting-UA' => @server_info.name,
    'Original-Recipient' => "rfc822; #{@server_info.name}",
    'Final-Recipient' => "rfc822; #{@server_info.name}",
    'Original-Message-ID' => env['HTTP_MESSAGE_ID']
  }
  if failed
    options['Disposition'] = 'automatic-action/MDN-sent-automatically; failed'
    options['Failure'] = failed
    text_body = "There was an error with the AS2 transmission.\r\n\r\n#{failed}"
  else
    options['Disposition'] = 'automatic-action/MDN-sent-automatically; processed'
    text_body = "The AS2 message has been received successfully"
  end
  options['Received-Content-MIC'] = "#{mic}, #{mic_algorithm}" if mic

  report = MimeGenerator::Part.new
  report['Content-Type'] = 'multipart/report; report-type=disposition-notification'

  text = MimeGenerator::Part.new
  text['Content-Type'] = 'text/plain'
  text['Content-Transfer-Encoding'] = '7bit'
  text.body = text_body
  report.add_part text

  notification = MimeGenerator::Part.new
  notification['Content-Type'] = 'message/disposition-notification'
  notification['Content-Transfer-Encoding'] = '7bit'
  notification.body = options.map{|n, v| "#{n}: #{v}"}.join("\r\n")
  report.add_part notification

  msg_out = StringIO.new

  report.write msg_out

  pkcs7 = OpenSSL::PKCS7.sign @server_info.certificate, @server_info.pkey, msg_out.string
  pkcs7.detached = true
  smime_signed = OpenSSL::PKCS7.write_smime pkcs7, msg_out.string

  content_type = smime_signed[/^Content-Type: (.+?)$/m, 1]
  # smime_signed.sub!(/\A.+?^(?=---)/m, '')

  headers = {}
  headers['Content-Type'] = content_type
  # TODO: if MIME-Version header is actually needed, should extract it out of smime_signed.
  headers['MIME-Version'] = '1.0'
  headers['Message-ID'] = As2.generate_message_id(@server_info)
  headers['AS2-From'] = @server_info.name
  headers['AS2-To'] = env['HTTP_AS2_FROM']
  headers['AS2-Version'] = '1.0'
  headers['Connection'] = 'close'

  [200, headers, ["\r\n" + smime_signed]]
end