Class: SunatInvoice::Signature

Inherits:
Model
  • Object
show all
Includes:
Utils
Defined in:
lib/sunat_invoice/signature.rb

Constant Summary collapse

C14N_ALGORITHM =
'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'
DIGEST_ALGORITHM =
'http://www.w3.org/2000/09/xmldsig#sha1'
SIGNATURE_ALGORITHM =
'http://www.w3.org/2000/09/xmldsig#rsa-sha1'
TRANSFORMATION_ALGORITHM =
'http://www.w3.org/2000/09/xmldsig#enveloped-signature'

Constants included from Utils

Utils::UBL_NAMESPACES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#amount_xml, #ubl_ext

Methods inherited from Model

#initialize

Constructor Details

This class inherits a constructor from SunatInvoice::Model

Instance Attribute Details

#providerObject

Returns the value of attribute provider.



16
17
18
# File 'lib/sunat_invoice/signature.rb', line 16

def provider
  @provider
end

Instance Method Details

#sign(invoice_xml) ⇒ Object



37
38
39
# File 'lib/sunat_invoice/signature.rb', line 37

def sign(invoice_xml)
  Xmldsig::SignedDocument.new(invoice_xml, id_attr: provider.signature_location_id).sign(private_key)
end

#signature_ext(xml) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/sunat_invoice/signature.rb', line 41

def signature_ext(xml)
  ubl_ext(xml) do
    xml['ds'].Signature(Id: provider.signature_location_id) do
      signed_info xml
      signature_value xml
    end
  end
end

#signature_value(xml) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/sunat_invoice/signature.rb', line 64

def signature_value(xml)
  xml['ds'].SignatureValue
  xml['ds'].KeyInfo do
    xml['ds'].X509Data do
      xml['ds'].X509Certificate encoded_certificate
    end
  end
end

#signed_info(xml) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sunat_invoice/signature.rb', line 50

def signed_info(xml)
  xml['ds'].SignedInfo do
    xml['ds'].CanonicalizationMethod Algorithm: C14N_ALGORITHM
    xml['ds'].SignatureMethod Algorithm: SIGNATURE_ALGORITHM
    xml['ds'].Reference URI: '' do
      xml['ds'].Transforms do
        xml['ds'].Transform Algorithm: TRANSFORMATION_ALGORITHM
      end
      xml['ds'].DigestMethod Algorithm: DIGEST_ALGORITHM
      xml['ds'].DigestValue
    end
  end
end

#signer_data(xml) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sunat_invoice/signature.rb', line 18

def signer_data(xml)
  xml['cac'].Signature do
    xml['cbc'].ID provider.signature_id
    xml['cac'].SignatoryParty do
      xml['cac'].PartyIdentification do
        xml['cbc'].ID provider.ruc
      end
      xml['cac'].PartyName do
        xml['cbc'].Name provider.name
      end
    end
    xml['cac'].DigitalSignatureAttachment do
      xml['cac'].ExternalReference do
        xml['cbc'].URI "##{provider.signature_location_id}"
      end
    end
  end
end