Class: Epics::GenericUploadRequest

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

Direct Known Subclasses

AZV, CCT, CD1, CDB, CDD, XCT

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.



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

def initialize(client, document)
  super(client)
  self.document = document
  self.key = cipher.random_key
  self.iv = 0.chr * cipher.iv_len
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



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

def document
  @document
end

#ivObject

Returns the value of attribute iv.



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

def iv
  @iv
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



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

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



13
14
15
# File 'lib/epics/generic_upload_request.rb', line 13

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

#digesterObject



17
18
19
# File 'lib/epics/generic_upload_request.rb', line 17

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

#encrypt(d) ⇒ Object



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

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

#encrypted_order_dataObject



60
61
62
63
64
# File 'lib/epics/generic_upload_request.rb', line 60

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

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

#encrypted_order_signatureObject



66
67
68
69
70
# File 'lib/epics/generic_upload_request.rb', line 66

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

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

#order_signatureObject



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

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



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

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



48
49
50
# File 'lib/epics/generic_upload_request.rb', line 48

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