Class: Sepa::SoapBuilder

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/sepa/soap_builder.rb

Overview

Builds a soap message with given parameters. This class is extended with proper bank module depending on bank.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#canonicalize_exclusively, #canonicalized_node, #cert_request_valid?, #check_validity_against_schema, #csr_to_binary, #decode, #encode, #extract_cert, #format_cert, #format_cert_request, #hmac, #iso_time, #load_body_template, #process_cert_value, #rsa_key, #set_node_id, #validate_signature, #verify_certificate_against_root_certificate, #x509_certificate, #xml_doc

Constructor Details

#initialize(params) ⇒ SoapBuilder

Initializes the Sepa::SoapBuilder with the params hash and then extends the Sepa::SoapBuilder with the correct bank module. The Sepa::SoapBuilder class is usually created by the client which handles parameter validation.

Parameters:

  • params (Hash)

    options hash



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sepa/soap_builder.rb', line 18

def initialize(params)
  @bank                        = params[:bank]
  @own_signing_certificate     = params[:own_signing_certificate]
  @command                     = params[:command]
  @content                     = params[:content]
  @customer_id                 = params[:customer_id]
  @bank_encryption_certificate = params[:bank_encryption_certificate]
  @environment                 = params[:environment]
  @file_reference              = params[:file_reference]
  @file_type                   = params[:file_type]
  @language                    = params[:language]
  @signing_private_key         = params[:signing_private_key]
  @status                      = params[:status]
  @target_id                   = params[:target_id]

  @application_request         = ApplicationRequest.new params
  @header_template             = load_header_template
  @template                    = load_body_template SOAP_TEMPLATE_PATH

  find_correct_bank_extension
end

Instance Attribute Details

#application_requestApplicationRequest (readonly)

Application request built with the same parameters as the soap

Returns:



11
12
13
# File 'lib/sepa/soap_builder.rb', line 11

def application_request
  @application_request
end

Instance Method Details

#add_body_to_headerNokogiri::XML (private)

Adds soap body to header template

Returns:

  • (Nokogiri::XML)

    the soap with added body as a nokogiri document



158
159
160
161
162
# File 'lib/sepa/soap_builder.rb', line 158

def add_body_to_header
  body = @template.at_css('env|Body')
  @header_template.root.add_child(body)
  @header_template
end

#build_certificate_requestNokogiri::XML (private)

Sets contents for certificate request

Returns:

  • (Nokogiri::XML)

    the template with contents added to it



79
80
81
# File 'lib/sepa/soap_builder.rb', line 79

def build_certificate_request
  set_body_contents
end

#build_common_requestNokogiri::XML (private)

Builds generic request which is a request made with commands:

  • Get User Info
  • Download File
  • Download File List
  • Upload File

Returns:

  • (Nokogiri::XML)

    the generic request soap



69
70
71
72
73
74
# File 'lib/sepa/soap_builder.rb', line 69

def build_common_request
  common_set_body_contents
  set_receiver_id
  process_header
  add_body_to_header
end

#calculate_digest(doc, node) ⇒ String (private)

TODO:

remove this method and use Utilities#calculate_digest

Calculates digest hash for the given node in the given document. The node is canonicalized exclusively before digest calculation.

Parameters:

  • doc (Nokogiri::XML)

    Document that contains the node

  • node (String)

    The name of the node

Returns:

  • (String)

    the base64 encoded string



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sepa/soap_builder.rb', line 102

def calculate_digest(doc, node)
  sha1 = OpenSSL::Digest::SHA1.new
  node = doc.at_css(node)

  canon_node = node.canonicalize(
    mode = Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0,
    inclusive_namespaces = nil, with_comments = false
  )

  encode(sha1.digest(canon_node)).gsub(/\s+/, "")
end

#calculate_signature(doc, node) ⇒ String (private)

TODO:

refactor to use canonicalization from utilities

Calculates signature for the given node in the given document. Uses the signing private key given to SoapBuilder for the signing. The node is canonicalized exclusively before signature calculation.

Parameters:

  • doc (Nokogiri::XML)

    Document that contains the node

  • node (String)

    Name of the node to calculate signature from

Returns:

  • (String)

    the base64 encoded signature



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sepa/soap_builder.rb', line 122

def calculate_signature(doc, node)
  sha1 = OpenSSL::Digest::SHA1.new
  node = doc.at_css(node)

  canon_signed_info_node = node.canonicalize(
    mode = Nokogiri::XML::XML_C14N_EXCLUSIVE_1_0,
    inclusive_namespaces = nil, with_comments = false
  )

  signature = @signing_private_key.sign(sha1, canon_signed_info_node)
  encode(signature).gsub(/\s+/, "")
end

#common_set_body_contentsObject (private)

Sets nodes for generic requests, application request is base64 encoded here.



217
218
219
220
221
222
223
224
# File 'lib/sepa/soap_builder.rb', line 217

def common_set_body_contents
  set_application_request
  set_node @template, 'bxd|SenderId',           @customer_id
  set_node @template, 'bxd|RequestId',          request_id
  set_node @template, 'bxd|Timestamp',          iso_time
  set_node @template, 'bxd|Language',           @language
  set_node @template, 'bxd|UserAgent',          "Sepa Transfer Library version #{VERSION}"
end

#find_correct_bank_extensionObject (private)

Extends the class with proper module depending on bank



50
51
52
# File 'lib/sepa/soap_builder.rb', line 50

def find_correct_bank_extension
  extend("Sepa::#{@bank.capitalize}SoapRequest".constantize)
end

#find_correct_buildNokogiri::XML (private)

Determines which soap request to build based on command. Certificate requests are built differently than generic requests.

Returns:

  • (Nokogiri::XML)

    the soap as a nokogiri document



58
59
60
# File 'lib/sepa/soap_builder.rb', line 58

def find_correct_build
  STANDARD_COMMANDS.include?(@command) ? build_common_request : build_certificate_request
end

#load_header_templateNokogiri::XML (private)

Loads soap header template to be later populated

Returns:

  • (Nokogiri::XML)

    the header as Nokogiri document



138
139
140
141
# File 'lib/sepa/soap_builder.rb', line 138

def load_header_template
  path = File.open("#{SOAP_TEMPLATE_PATH}/header.xml")
  Nokogiri::XML(path)
end

#process_headerObject (private)

TODO:

split into smaller methods

Add needed information to soap header. Mainly security related stuff. The process is as follows:

  1. The reference id of the security token is set using #set_token_id method
  2. Created and expires timestamps are set. Expires is set to be 5 minutes after creation.
  3. Timestamp reference id is set with Utilities#set_node_id method
  4. The digest of timestamp node is calculated and set to correct node
  5. The reference id of body is set with Utilities#set_node_id
  6. The digest of body is calculated and set to correct node
  7. The signature of SignedInfo node is calculated and added to correct node
  8. Own signing certificate is formatted (Begin and end certificate removed and linebreaks removed) and embedded in the soap


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/sepa/soap_builder.rb', line 176

def process_header
  set_token_id

  set_node(@header_template, 'wsu|Created', iso_time)
  set_node(@header_template, 'wsu|Expires', (Time.now.utc + 300).iso8601)

  timestamp_id = set_node_id(@header_template, OASIS_UTILITY, 'Timestamp', 0)

  timestamp_digest = calculate_digest(@header_template, 'wsu|Timestamp')
  dsig = "dsig|Reference[URI='##{timestamp_id}'] dsig|DigestValue"
  set_node(@header_template, dsig, timestamp_digest)

  body_id = set_node_id(@template, ENVELOPE, 'Body', 1)

  body_digest = calculate_digest(@template, 'env|Body')
  dsig = "dsig|Reference[URI='##{body_id}'] dsig|DigestValue"
  set_node(@header_template, dsig, body_digest)

  signature = calculate_signature(@header_template, 'dsig|SignedInfo')
  set_node(@header_template, 'dsig|SignatureValue', signature)

  formatted_cert = format_cert(@own_signing_certificate)
  set_node(@header_template, 'wsse|BinarySecurityToken', formatted_cert)
end

#request_idString (private)

Generates a random request id

Returns:

  • (String)

    hexnumeric request id



212
213
214
# File 'lib/sepa/soap_builder.rb', line 212

def request_id
  SecureRandom.hex(17)
end

#set_application_requestObject (private)



226
227
228
# File 'lib/sepa/soap_builder.rb', line 226

def set_application_request
  set_node @template, 'bxd|ApplicationRequest', @application_request.to_base64
end

#set_body_contentsNokogiri::XML (private)

Sets soap body contents. Application request is base64 encoded here.

Returns:

  • (Nokogiri::XML)

    the soap with contents added to it



86
87
88
89
90
91
92
93
# File 'lib/sepa/soap_builder.rb', line 86

def set_body_contents
  set_node @template, "ApplicationRequest", @application_request.to_base64, namespace: cert_ns
  set_node @template, "SenderId",           @customer_id,                   namespace: cert_ns
  set_node @template, "RequestId",          request_id,                     namespace: cert_ns
  set_node @template, "Timestamp",          iso_time,                       namespace: cert_ns

  @template
end

#set_node(doc, node, value, namespace: nil) ⇒ Object (private)

Sets value to a node's content in the given document

Parameters:

  • doc (Nokogiri::XML)

    The document that contains the node

  • node (String)

    The name of the node which value is about to be set

  • value (#to_s)

    The value which will be set to the node



147
148
149
150
151
152
153
# File 'lib/sepa/soap_builder.rb', line 147

def set_node(doc, node, value, namespace: nil)
  if namespace
    doc.at("xmlns|#{node}", xmlns: namespace).content = value
  else
    doc.at(node).content = value
  end
end

#set_token_idObject (private)

Generates a random token id and sets it to correct node



202
203
204
205
206
207
# File 'lib/sepa/soap_builder.rb', line 202

def set_token_id
  security_token_id = "token-#{SecureRandom.uuid}"

  @header_template.at('wsse|BinarySecurityToken')['wsu:Id'] = security_token_id
  @header_template.at('wsse|Reference')['URI'] = "##{security_token_id}"
end

#to_xmlString

Returns the soap as raw xml

Returns:

  • (String)

    the soap as xml



43
44
45
# File 'lib/sepa/soap_builder.rb', line 43

def to_xml
  find_correct_build.to_xml
end