Class: Epics::GenericUploadRequest

Inherits:
GenericRequest show all
Defined in:
lib/epics/generic_upload_request.rb

Direct Known Subclasses

AZV, CCT, CD1, CDB, CDD

Instance Attribute Summary collapse

Attributes inherited from GenericRequest

#client, #transaction_id

Instance Method Summary collapse

Methods inherited from GenericRequest

#auth_signature, #nonce, #root, #timestamp, #to_receipt_xml, #to_transfer_xml, #to_xml

Constructor Details

#initialize(client, document) ⇒ GenericUploadRequest

Returns a new instance of GenericUploadRequest.



5
6
7
8
9
# File 'lib/epics/generic_upload_request.rb', line 5

def initialize(client, document)
  super(client)
  self.document = document
  self.key ||= cipher.random_key
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



3
4
5
# File 'lib/epics/generic_upload_request.rb', line 3

def document
  @document
end

#keyObject

Returns the value of attribute key.



2
3
4
# File 'lib/epics/generic_upload_request.rb', line 2

def key
  @key
end

Instance Method Details

#bodyObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/epics/generic_upload_request.rb', line 19

def body
  Nokogiri::XML::Builder.new do |xml|
    xml.body {
      xml.DataTransfer {
        xml.DataEncryptionInfo(authenticate: true) {
          xml.EncryptionPubKeyDigest(client.bank_e.public_digest, Version: 'E002', Algorithm: "http://www.w3.org/2001/04/xmlenc#sha256")
          xml.TransactionKey Base64.encode64(client.bank_e.key.public_encrypt(self.key)).gsub(/\n/,'')
        }
        xml.SignatureData(encrypted_order_signature, authenticate: true)
      }
    }
  end.doc.root
end

#cipherObject



11
12
13
# File 'lib/epics/generic_upload_request.rb', line 11

def cipher
  @cipher ||= OpenSSL::Cipher.new("aes-128-cbc")
end

#digesterObject



15
16
17
# File 'lib/epics/generic_upload_request.rb', line 15

def digester
  @digester ||= OpenSSL::Digest::SHA256.new
end

#encrypt(d) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/epics/generic_upload_request.rb', line 50

def encrypt(d)
  cipher.reset
  cipher.encrypt
  cipher.padding = 0
  cipher.key = self.key
  (cipher.update(pad(d)) + cipher.final)
end

#encrypted_order_dataObject



58
59
60
61
62
# File 'lib/epics/generic_upload_request.rb', line 58

def encrypted_order_data
  dst = Zlib::Deflate.deflate(document)

  Base64.encode64(encrypt(dst)).gsub(/\n/,'')
end

#encrypted_order_signatureObject



64
65
66
67
68
# File 'lib/epics/generic_upload_request.rb', line 64

def encrypted_order_signature
  dst = Zlib::Deflate.deflate(order_signature)

  Base64.encode64(encrypt(dst)).gsub(/\n/,'')
end

#order_signatureObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/epics/generic_upload_request.rb', line 33

def order_signature
  Nokogiri::XML::Builder.new do |xml|
    xml.UserSignatureData('xmlns' => 'http://www.ebics.org/S001', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation' => 'http://www.ebics.org/S001 http://www.ebics.org/S001/ebics_signature.xsd') {
      xml.OrderSignatureData {
        xml.SignatureVersion "A006"
        xml.SignatureValue signature_value
        xml.PartnerID partner_id
        xml.UserID user_id
      }
    }
  end.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML, encoding: 'utf-8')
end

#pad(d) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/epics/generic_upload_request.rb', line 70

def pad(d)
  len = cipher.block_size*((d.size / cipher.block_size)+1)

  d.ljust(len, [0].pack("C*")).tap do |padded|
    padded[-1] = [len - d.size].pack("C*")
  end
end

#signature_valueObject



46
47
48
# File 'lib/epics/generic_upload_request.rb', line 46

def signature_value
  client.a.sign( digester.digest(document.gsub(/\n|\r/, "")) )
end