class Epics::Client
Attributes
Public Class Methods
Source
# File lib/epics/client.rb, line 11 def initialize(keys_content, passphrase, url, host_id, user_id, partner_id, options = {}) self.keys_content = keys_content.respond_to?(:read) ? keys_content.read : keys_content if keys_content self.passphrase = passphrase self.keys = extract_keys if keys_content self.url = url self.host_id = host_id self.user_id = user_id self.partner_id = partner_id self.locale = options[:locale] || Epics::DEFAULT_LOCALE self.product_name = options[:product_name] || Epics::DEFAULT_PRODUCT_NAME self.debug_mode = !!options[:debug_mode] self.x_509_certificates_content = { a: options[:x_509_certificate_a_content], x: options[:x_509_certificate_x_content], e: options[:x_509_certificate_e_content] } end
Source
# File lib/epics/client.rb, line 72 def self.setup(passphrase, url, host_id, user_id, partner_id, keysize = 2048, options = {}) client = new(nil, passphrase, url, host_id, user_id, partner_id, options) client.keys = %w(A006 X002 E002).each_with_object({}) do |type, memo| memo[type] = Epics::Key.new( OpenSSL::PKey::RSA.generate(keysize) ) end client end
Public Instance Methods
Source
# File lib/epics/client.rb, line 144 def AZV(document) upload(Epics::AZV, document) end
Source
# File lib/epics/client.rb, line 216 def BKA(from, to) download_and_unzip(Epics::BKA, from: from, to: to) end
Source
# File lib/epics/client.rb, line 156 def C2S(document) upload(Epics::C2S, document) end
Source
# File lib/epics/client.rb, line 220 def C52(from, to) download_and_unzip(Epics::C52, from: from, to: to) end
Source
# File lib/epics/client.rb, line 224 def C53(from, to) download_and_unzip(Epics::C53, from: from, to: to) end
Source
# File lib/epics/client.rb, line 228 def C54(from, to) download_and_unzip(Epics::C54, from: from, to: to) end
Source
# File lib/epics/client.rb, line 232 def C5N(from, to) download_and_unzip(Epics::C5N, from: from, to: to) end
Source
# File lib/epics/client.rb, line 188 def CCS(document) upload(Epics::CCS, document) end
Source
# File lib/epics/client.rb, line 180 def CCT(document) upload(Epics::CCT, document) end
Source
# File lib/epics/client.rb, line 148 def CD1(document) upload(Epics::CD1, document) end
Source
# File lib/epics/client.rb, line 152 def CDB(document) upload(Epics::CDB, document) end
Source
# File lib/epics/client.rb, line 160 def CDD(document) upload(Epics::CDD, document) end
Source
# File lib/epics/client.rb, line 172 def CDS(document) upload(Epics::CDS, document) end
Source
# File lib/epics/client.rb, line 208 def CDZ(from = nil, to = nil) download_and_unzip(Epics::CDZ, from: from, to: to) end
Source
# File lib/epics/client.rb, line 184 def CIP(document) upload(Epics::CIP, document) end
Source
# File lib/epics/client.rb, line 212 def CRZ(from = nil, to = nil) download_and_unzip(Epics::CRZ, from: from, to: to) end
Source
# File lib/epics/client.rb, line 200 def FDL(format, from = nil, to = nil) download(Epics::FDL, file_format: format, from: from, to: to ) end
Source
# File lib/epics/client.rb, line 252 def HAA Nokogiri::XML(download(Epics::HAA)).at_xpath("//xmlns:OrderTypes", xmlns: "urn:org:ebics:H004").content.split(/\s/) end
Source
# File lib/epics/client.rb, line 277 def HAC(from = nil, to = nil) download(Epics::HAC, from: from, to: to) end
Source
# File lib/epics/client.rb, line 118 def HEV res = post(url, Epics::HEV.new(self).to_xml).body res.doc.xpath("//xmlns:VersionNumber", xmlns: 'http://www.ebics.org/H000').each_with_object({}) do |node, versions| versions[node['ProtocolVersion']] = node.content end end
Source
# File lib/epics/client.rb, line 110 def HIA post(url, Epics::HIA.new(self).to_xml).body.ok? end
Source
# File lib/epics/client.rb, line 125 def HPB Nokogiri::XML(download(Epics::HPB)).xpath("//xmlns:PubKeyValue", xmlns: "urn:org:ebics:H004").each do |node| type = node.parent.last_element_child.content modulus = Base64.decode64(node.at_xpath(".//*[local-name() = 'Modulus']").content) exponent = Base64.decode64(node.at_xpath(".//*[local-name() = 'Exponent']").content) sequence = [] sequence << OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(modulus, 2)) sequence << OpenSSL::ASN1::Integer.new(OpenSSL::BN.new(exponent, 2)) bank = OpenSSL::PKey::RSA.new(OpenSSL::ASN1::Sequence(sequence).to_der) self.keys["#{host_id.upcase}.#{type}"] = Epics::Key.new(bank) end [bank_x, bank_e] end
Source
# File lib/epics/client.rb, line 256 def HTD Nokogiri::XML(download(Epics::HTD)).tap do |htd| @iban ||= htd.at_xpath("//xmlns:AccountNumber[@international='true']", xmlns: "urn:org:ebics:H004").text rescue nil @bic ||= htd.at_xpath("//xmlns:BankCode[@international='true']", xmlns: "urn:org:ebics:H004").text rescue nil @name ||= htd.at_xpath("//xmlns:Name", xmlns: "urn:org:ebics:H004").text rescue nil @order_types ||= htd.search("//xmlns:OrderTypes", xmlns: "urn:org:ebics:H004").map{|o| o.content.split(/\s/) }.delete_if{|o| o == ""}.flatten end.to_xml end
Source
# File lib/epics/client.rb, line 114 def INI post(url, Epics::INI.new(self).to_xml).body.ok? end
Source
# File lib/epics/client.rb, line 273 def PTK(from, to) download(Epics::PTK, from: from, to: to) end
Source
# File lib/epics/client.rb, line 196 def STA(from = nil, to = nil) download(Epics::STA, from: from, to: to) end
Source
# File lib/epics/client.rb, line 204 def VMK(from = nil, to = nil) download(Epics::VMK, from: from, to: to) end
Source
# File lib/epics/client.rb, line 192 def XCT(document) upload(Epics::XCT, document) end
Source
# File lib/epics/client.rb, line 176 def XDS(document) upload(Epics::XDS, document) end
Source
# File lib/epics/client.rb, line 164 def XE2(document) upload(Epics::XE2, document) end
Source
# File lib/epics/client.rb, line 168 def XE3(document) upload(Epics::XE3, document) end
Source
# File lib/epics/client.rb, line 236 def Z01(from, to) download_and_unzip(Epics::Z01, from: from, to: to) end
Source
# File lib/epics/client.rb, line 240 def Z52(from, to) download_and_unzip(Epics::Z52, from: from, to: to) end
Source
# File lib/epics/client.rb, line 244 def Z53(from, to) download_and_unzip(Epics::Z53, from: from, to: to) end
Source
# File lib/epics/client.rb, line 248 def Z54(from, to) download_and_unzip(Epics::Z54, from: from, to: to) end
Source
# File lib/epics/client.rb, line 98 def credit(document) self.CCT(document) end
Source
# File lib/epics/client.rb, line 102 def debit(document, type = :CDD) self.public_send(type, document) end
Source
# File lib/epics/client.rb, line 94 def header_request @header_request ||= Epics::HeaderRequest.new(self) end
Source
# File lib/epics/client.rb, line 85 def ini_letter(bankname) letter_renderer.render(bankname) end
Source
# File lib/epics/client.rb, line 29 def inspect "#<#{self.class}:#{self.object_id} @keys=#{self.keys.keys}, @user_id=\"#{self.user_id}\", @partner_id=\"#{self.partner_id}\"" end
Source
# File lib/epics/client.rb, line 81 def letter_renderer @letter_renderer ||= Epics::LetterRenderer.new(self) end
Source
# File lib/epics/client.rb, line 68 def order_types @order_types ||= (self.HTD; @order_types) end
Source
# File lib/epics/client.rb, line 89 def save_ini_letter(bankname, path) File.write(path, ini_letter(bankname)) path end
Source
# File lib/epics/client.rb, line 285 def save_keys(path) File.write(path, dump_keys) end
Source
# File lib/epics/client.rb, line 106 def statements(from, to, type = :STA) self.public_send(type, from: from, to: to) end
Source
# File lib/epics/client.rb, line 289 def x_509_certificate(type) content = x_509_certificates_content[type.to_sym] return if content.nil? || content.empty? Epics::X509Certificate.new(content) end
Source
# File lib/epics/client.rb, line 295 def x_509_certificate_hash(type) content = x_509_certificates_content[type.to_sym] return if content.nil? || content.empty? cert = OpenSSL::X509::Certificate.new(content) Digest::SHA256.hexdigest(cert.to_der).upcase end
Private Instance Methods
Source
# File lib/epics/client.rb, line 336 def connection @connection ||= Faraday.new(headers: { 'Content-Type' => 'text/xml', user_agent: "EPICS v#{Epics::VERSION}"}, ssl: { verify: verify_ssl? }) do |faraday| faraday.use Epics::XMLSIG, { client: self } faraday.use Epics::ParseEbics, { client: self} # faraday.use MyAdapter faraday.response :logger, ::Logger.new(STDOUT), bodies: true if debug_mode # log requests/response to STDOUT end end
Source
# File lib/epics/client.rb, line 368 def decrypt(data) data = Base64.strict_decode64(data) salt = data[0..7] data = data[8..-1] cipher = setup_cipher(:decrypt, self.passphrase, salt) cipher.update(data) + cipher.final end
Source
# File lib/epics/client.rb, line 316 def download(order_type, *args, **options) document = order_type.new(self, *args, **options) res = post(url, document.to_xml).body document.transaction_id = res.transaction_id if res.segmented? && res.last_segment? post(url, document.to_receipt_xml).body end res.order_data end
Source
# File lib/epics/client.rb, line 328 def download_and_unzip(order_type, *args, **options) [].tap do |entries| Zip::File.open_buffer(StringIO.new(download(order_type, *args, **options))).each do |zipfile| entries << zipfile.get_input_stream.read end end end
Source
# File lib/epics/client.rb, line 351 def dump_keys JSON.dump(keys.each_with_object({}) {|(k,v),m| m[k]= encrypt(v.key.to_pem)}) end
Source
# File lib/epics/client.rb, line 361 def encrypt(data) salt = OpenSSL::Random.random_bytes(8) cipher = setup_cipher(:encrypt, self.passphrase, salt) Base64.strict_encode64([salt, cipher.update(data) + cipher.final].join) end
Source
# File lib/epics/client.rb, line 345 def extract_keys JSON.load(self.keys_content).each_with_object({}) do |(type, key), memo| memo[type] = Epics::Key.new(decrypt(key)) if key end end
Source
# File lib/epics/client.rb, line 355 def new_cipher # Re-using the cipher between keys has weird behaviours with openssl3 # Using a fresh key instead of memoizing it on the client simplifies things OpenSSL::Cipher.new('aes-256-cbc') end
Source
# File lib/epics/client.rb, line 377 def setup_cipher(method, passphrase, salt) cipher = new_cipher cipher.send(method) cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(passphrase, salt, 1, cipher.key_len) cipher end
Source
# File lib/epics/client.rb, line 304 def upload(order_type, document) order = order_type.new(self, document) res = post(url, order.to_xml).body order.transaction_id = res.transaction_id order_id = res.order_id res = post(url, order.to_transfer_xml).body return res.transaction_id, [res.order_id, order_id].detect { |id| id.to_s.chars.any? } end
Source
# File lib/epics/client.rb, line 384 def verify_ssl? ENV['EPICS_VERIFY_SSL'] != 'false' end