Class: Sepa::ApplicationRequest
- Inherits:
-
Object
- Object
- Sepa::ApplicationRequest
- Includes:
- Utilities
- Defined in:
- lib/sepa/application_request.rb
Overview
Add return values for content modifying methods to signal whether they succeeded or not
Contains functionality to build the application request
Instance Method Summary collapse
-
#add_node_to_root(node, content: nil) ⇒ Object
private
Adds node to the root of the application request and content to it if specified.
-
#add_target_id_after(node) ⇒ Object
private
Adds target id to the application request after a specific node because the schema defines a sequence.
-
#add_value_to_signature(node, value) ⇒ Object
private
Adds value to signature node.
-
#calculate_digest ⇒ String
private
Calculates the digest of #application_request.
-
#calculate_signature ⇒ String
private
Calculates the application request's signature value.
-
#initialize(params = {}) ⇒ ApplicationRequest
constructor
Initializes the ApplicationRequest with a params hash.
-
#pretty_command ⇒ Object
private
Converts #command to string, removes underscores and capitalizes it.
-
#process_signature ⇒ Object
private
Removes signature from the application request, calculates the application request's digest, calculates the signature and adds needed values to signature node.
-
#remove_node(node, xmlns) ⇒ Object
private
Removes a node from #application_request.
-
#set_common_nodes ⇒ Object
private
Sets contents for nodes that are common to all requests except when #command is
:get_bank_certificateor:create_certificate. -
#set_create_certificate_nodes ⇒ Object
private
Sets nodes' contents for Danske Bank's create certificate request.
-
#set_download_file_list_nodes ⇒ Object
private
Sets nodes' contents for download file list request.
-
#set_download_file_nodes ⇒ Object
private
Sets nodes' values for download file request.
-
#set_get_bank_certificate_nodes ⇒ Object
private
Sets Danske Bank's get bank certificate request's contents.
-
#set_get_certificate_nodes ⇒ Object
private
Sets nodes' contents for Nordea's and OP's get certificate request.
-
#set_node(node, value) ⇒ Object
private
Sets node to value.
-
#set_node_b(node, value) ⇒ Object
private
Sets node to base64 encoded value.
-
#set_nodes_contents ⇒ Object
private
Determines which content setting method to call depending on #command.
-
#set_renew_certificate_nodes ⇒ Object
private
Sets nodes' contents for Nordea's and OP's renew certificate request.
-
#set_service_certificates_nodes ⇒ Object
private
Sets nodes' contents for OP's get service certificates request.
-
#set_upload_file_nodes ⇒ Object
private
Sets nodes' contents for upload file request.
-
#to_base64 ⇒ String
Base64 encodes the whole application request.
-
#to_nokogiri ⇒ Nokogiri::XML::Document
Returns the application request as a Nokogiri document.
-
#to_xml ⇒ String
Sets the nodes in the application request, processes signature and then returns the application request as an xml document.
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 = {}) ⇒ ApplicationRequest
Consider not using instance_variable_set so that all the available instance variables can easily be seen.
Initializes the Sepa::ApplicationRequest with a params hash. The application request is usually initialized by the SoapBuilder. The xml template of the application request is also loaded here.
19 20 21 22 23 24 25 26 |
# File 'lib/sepa/application_request.rb', line 19 def initialize(params = {}) # Set all params as instance variables params.each do |key, value| instance_variable_set("@#{key}", value) end @application_request = load_body_template AR_TEMPLATE_PATH end |
Instance Method Details
#add_node_to_root(node, content: nil) ⇒ Object (private)
Adds node to the root of the application request and content to it if specified
184 185 186 187 188 189 190 191 192 |
# File 'lib/sepa/application_request.rb', line 184 def add_node_to_root(node, content: nil) unless node.is_a? Nokogiri::XML::Node node = Nokogiri::XML::Node.new node, @application_request end @application_request.root.add_child node set_node(node.name, content) if content end |
#add_target_id_after(node) ⇒ Object (private)
Adds target id to the application request after a specific node because the schema defines a
sequence. Target id is only added if #bank is :nordea
252 253 254 255 256 257 258 |
# File 'lib/sepa/application_request.rb', line 252 def add_target_id_after(node) return unless @bank == :nordea target_id = Nokogiri::XML::Node.new 'TargetId', @application_request target_id.content = @target_id @application_request.at(node).add_next_sibling target_id end |
#add_value_to_signature(node, value) ⇒ Object (private)
Remove this method and use #set_node method
Adds value to signature node
209 210 211 212 213 |
# File 'lib/sepa/application_request.rb', line 209 def add_value_to_signature(node, value) dsig = 'http://www.w3.org/2000/09/xmldsig#' sig = @application_request.at_css("dsig|#{node}", 'dsig' => dsig) sig.content = value end |
#calculate_digest ⇒ String (private)
Use the digest calculation method in Utilities instead of implementing the functionality again here.
Calculates the digest of #application_request
199 200 201 202 |
# File 'lib/sepa/application_request.rb', line 199 def calculate_digest sha1 = OpenSSL::Digest::SHA1.new encode(sha1.digest(@application_request.canonicalize)) end |
#calculate_signature ⇒ String (private)
Move to Utilities
Calculates the application request's signature value. Uses #signing_private_key for the calculation.
220 221 222 223 224 225 226 |
# File 'lib/sepa/application_request.rb', line 220 def calculate_signature sha1 = OpenSSL::Digest::SHA1.new dsig = 'http://www.w3.org/2000/09/xmldsig#' node = @application_request.at_css("dsig|SignedInfo", 'dsig' => dsig) signature = @signing_private_key.sign(sha1, node.canonicalize) encode signature end |
#pretty_command ⇒ Object (private)
Converts #command to string, removes underscores and capitalizes it.
77 78 79 |
# File 'lib/sepa/application_request.rb', line 77 def pretty_command @command.to_s.split(/[\W_]/).map {|c| c.capitalize}.join end |
#process_signature ⇒ Object (private)
Removes signature from the application request, calculates the application request's digest, calculates the signature and adds needed values to signature node. Also adds #own_signing_certificate to the signature node.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/sepa/application_request.rb', line 231 def process_signature # No signature for Certificate Requests return if %i( create_certificate get_bank_certificate get_certificate get_service_certificates ).include? @command signature_node = remove_node('Signature', 'http://www.w3.org/2000/09/xmldsig#') digest = calculate_digest add_node_to_root(signature_node) add_value_to_signature('DigestValue', digest) add_value_to_signature('SignatureValue', calculate_signature) add_value_to_signature('X509Certificate', format_cert(@own_signing_certificate)) end |
#remove_node(node, xmlns) ⇒ Object (private)
Move to Utilities and move document to parameters
Removes a node from #application_request
179 180 181 |
# File 'lib/sepa/application_request.rb', line 179 def remove_node(node, xmlns) @application_request.at_css("xmlns|#{node}", 'xmlns' => xmlns).remove end |
#set_common_nodes ⇒ Object (private)
Sets contents for nodes that are common to all requests except when #command is
:get_bank_certificate or :create_certificate. #environment is upcased here.
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/sepa/application_request.rb', line 163 def set_common_nodes return if @command == :get_bank_certificate return if @command == :create_certificate set_node('Environment', @environment.to_s.upcase) set_node("CustomerId", @customer_id) set_node("Timestamp", iso_time) set_node("SoftwareId", "Sepa Transfer Library version #{VERSION}") set_node("Command", pretty_command) unless @command == :renew_certificate end |
#set_create_certificate_nodes ⇒ Object (private)
Raise error if #bank is other than Nordea like in #set_get_bank_certificate_nodes
Sets nodes' contents for Danske Bank's create certificate request. Environment is set to
customertest if #environment is :test
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/sepa/application_request.rb', line 147 def set_create_certificate_nodes set_node("tns|CustomerId", @customer_id) set_node("tns|KeyGeneratorType", 'software') set_node("tns|EncryptionCertPKCS10", format_cert_request(@encryption_csr)) set_node("tns|SigningCertPKCS10", format_cert_request(@signing_csr)) set_node("tns|Timestamp", iso_time) set_node("tns|RequestId", @request_id) @environment = 'customertest' if @environment == :test set_node("tns|Environment", @environment) set_node("tns|PIN", @pin) end |
#set_download_file_list_nodes ⇒ Object (private)
Sets nodes' contents for download file list request
118 119 120 121 122 |
# File 'lib/sepa/application_request.rb', line 118 def set_download_file_list_nodes add_target_id_after 'Environment' set_node("Status", @status) add_node_to_root 'FileType', content: @file_type if @file_type.present? end |
#set_download_file_nodes ⇒ Object (private)
Sets nodes' values for download file request
89 90 91 92 93 94 |
# File 'lib/sepa/application_request.rb', line 89 def set_download_file_nodes add_target_id_after 'FileReferences' set_node("Status", @status) add_node_to_root 'FileType', content: @file_type if @file_type.present? set_node("FileReference", @file_reference) end |
#set_get_bank_certificate_nodes ⇒ Object (private)
Investigate a better way to set the bank's root certificate's serial instead of hardcoding it
Sets Danske Bank's get bank certificate request's contents
101 102 103 104 105 106 107 108 |
# File 'lib/sepa/application_request.rb', line 101 def set_get_bank_certificate_nodes raise 'OnlyWorksWithDanske' if @bank != :danske # Root Cert Serial Hardcoded to Danske set_node("elem|BankRootCertificateSerialNo", '1111110002') set_node("elem|Timestamp", iso_time) set_node("elem|RequestId", @request_id) end |
#set_get_certificate_nodes ⇒ Object (private)
Sets nodes' contents for Nordea's and OP's get certificate request
125 126 127 128 129 130 |
# File 'lib/sepa/application_request.rb', line 125 def set_get_certificate_nodes set_node "Service", "MATU" if @bank == :op set_node "TransferKey", @pin if @bank == :op set_node "HMAC", hmac(@pin, csr_to_binary(@signing_csr)) if @bank == :nordea set_node "Content", format_cert_request(@signing_csr) end |
#set_node(node, value) ⇒ Object (private)
Sets node to value
60 61 62 |
# File 'lib/sepa/application_request.rb', line 60 def set_node(node, value) @application_request.at_css(node).content = value end |
#set_node_b(node, value) ⇒ Object (private)
rename
Sets node to base64 encoded value
69 70 71 |
# File 'lib/sepa/application_request.rb', line 69 def set_node_b(node, value) set_node node, encode(value) end |
#set_nodes_contents ⇒ Object (private)
Determines which content setting method to call depending on #command
82 83 84 85 86 |
# File 'lib/sepa/application_request.rb', line 82 def set_nodes_contents method = "set_#{@command}_nodes" send(method) if self.class.private_method_defined? method end |
#set_renew_certificate_nodes ⇒ Object (private)
Sets nodes' contents for Nordea's and OP's renew certificate request
133 134 135 136 |
# File 'lib/sepa/application_request.rb', line 133 def set_renew_certificate_nodes set_node "Service", "service" if @bank == :nordea set_node "Content", format_cert_request(@signing_csr) end |
#set_service_certificates_nodes ⇒ Object (private)
Sets nodes' contents for OP's get service certificates request
139 140 141 |
# File 'lib/sepa/application_request.rb', line 139 def set_service_certificates_nodes set_node("Service", "MATU") end |
#set_upload_file_nodes ⇒ Object (private)
Sets nodes' contents for upload file request
111 112 113 114 115 |
# File 'lib/sepa/application_request.rb', line 111 def set_upload_file_nodes set_node_b("Content", @content) set_node("FileType", @file_type) add_target_id_after 'Environment' end |
#to_base64 ⇒ String
Base64 encodes the whole application request
43 44 45 |
# File 'lib/sepa/application_request.rb', line 43 def to_base64 encode to_xml end |
#to_nokogiri ⇒ Nokogiri::XML::Document
Returns the application request as a Nokogiri document
50 51 52 |
# File 'lib/sepa/application_request.rb', line 50 def to_nokogiri Nokogiri::XML to_xml end |
#to_xml ⇒ String
This method is obviously doing too much
Sets the nodes in the application request, processes signature and then returns the application request as an xml document.
33 34 35 36 37 38 |
# File 'lib/sepa/application_request.rb', line 33 def to_xml set_common_nodes set_nodes_contents process_signature @application_request.to_xml end |