Module: Xml::Kit::Templatable

Included in:
Certificate, EncryptedKey, KeyInfo, KeyInfo::KeyValue
Defined in:
lib/xml/kit/templatable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#digest_methodObject

Allows you to specify the digest method algorithm. (Default: SHA256) A list of digest methods can be found in [Xml::Kit::Signature].



22
23
24
# File 'lib/xml/kit/templatable.rb', line 22

def digest_method
  @digest_method
end

#embed_signatureObject

Can be used to disable embeding a signature. By default a signature will be embedded if a signing certificate is available.



9
10
11
# File 'lib/xml/kit/templatable.rb', line 9

def embed_signature
  @embed_signature
end

#encryptObject

Used to enable/disable encrypting the document.



12
13
14
# File 'lib/xml/kit/templatable.rb', line 12

def encrypt
  @encrypt
end

#encryption_certificateObject

The [Xml::Kit::Certificate] that contains the public key to use for encrypting the document.



18
19
20
# File 'lib/xml/kit/templatable.rb', line 18

def encryption_certificate
  @encryption_certificate
end

#signature_methodObject

Allows you to specify the signature method algorithm. (Default: SHA256) A list of signature methods can be found in [Xml::Kit::Signature].



26
27
28
# File 'lib/xml/kit/templatable.rb', line 26

def signature_method
  @signature_method
end

#signing_key_pairObject

The [Xml::Kit::KeyPair] to use for generating a signature.



15
16
17
# File 'lib/xml/kit/templatable.rb', line 15

def signing_key_pair
  @signing_key_pair
end

Instance Method Details

#asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM) ⇒ Object

This method is abstract.

Provides a default RSA asymmetric cipher. Can be overridden to provide custom ciphers.

Since:

  • 0.3.0



78
79
80
81
82
83
84
85
86
87
# File 'lib/xml/kit/templatable.rb', line 78

def asymmetric_cipher(algorithm: Crypto::RsaCipher::ALGORITHM)
  unless encryption_certificate
    raise Xml::Kit::Error, 'encryption_certificate is not specified.'
  end

  @asymmetric_cipher ||= Crypto.cipher_for(
    algorithm,
    encryption_certificate.public_key
  )
end

#encrypt_data_for(xml:, key_info: nil) {|temp| ... } ⇒ Object

Parameters:

  • xml (Builder::XmlMarkup)

    the xml builder instance

  • key_info (Xml::Kit::KeyInfo) (defaults to: nil)

    the key info to render in the EncryptedData

Yields:

  • (temp)

Since:

  • 0.3.0



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xml/kit/templatable.rb', line 61

def encrypt_data_for(xml:, key_info: nil)
  return yield xml unless encrypt?

  temp = ::Builder::XmlMarkup.new
  yield temp
  ::Xml::Kit::EncryptedData.new(
    signatures.complete(temp.target!),
    symmetric_cipher: symmetric_cipher,
    asymmetric_cipher: asymmetric_cipher,
    key_info: key_info
  ).to_xml(xml: xml)
end

#encrypt_key_for(xml:, id:, key_info: nil) ⇒ Object

Parameters:

  • xml (Builder::XmlMarkup)

    the xml builder instance

  • id (String)

    the id of EncryptedKey element

Since:

  • 0.3.0



39
40
41
42
43
44
45
46
# File 'lib/xml/kit/templatable.rb', line 39

def encrypt_key_for(xml:, id:, key_info: nil)
  ::Xml::Kit::EncryptedKey.new(
    id: id,
    asymmetric_cipher: asymmetric_cipher,
    symmetric_cipher: symmetric_cipher,
    key_info: key_info
  ).to_xml(xml: xml)
end

#encrypt_with(certificate) ⇒ Object

Allows you to specify which public key to use for generating an XML encrypted element.

Parameters:

  • certificate (Xml::Kit::Certificate)

    the certificate containing the public key to use for encryption.



121
122
123
124
# File 'lib/xml/kit/templatable.rb', line 121

def encrypt_with(certificate)
  self.encrypt = true
  self.encryption_certificate = certificate
end

#encryption_for(*args, &block) ⇒ Object

Deprecated.

Use #encrypt_data_for instead of this



49
50
51
52
53
54
# File 'lib/xml/kit/templatable.rb', line 49

def encryption_for(*args, &block)
  ::Xml::Kit.deprecate(
    'encryption_for is deprecated. Use encrypt_data_for instead.'
  )
  encrypt_data_for(*args, &block)
end

#render(model, options) ⇒ Object



97
98
99
# File 'lib/xml/kit/templatable.rb', line 97

def render(model, options)
  ::Xml::Kit::Template.new(model).to_xml(options)
end

#sign_with(key_pair, signature_method: :SHA256, digest_method: :SHA256) ⇒ Object

Allows you to specify which key pair to use for generating an XML digital signature.

Parameters:



110
111
112
113
114
115
116
# File 'lib/xml/kit/templatable.rb', line 110

def sign_with(key_pair, signature_method: :SHA256, digest_method: :SHA256)
  self.signing_key_pair = key_pair
  self.embed_signature = true
  self.signature_method = signature_method
  self.digest_method = digest_method
  signatures.sign_with(key_pair)
end

#signature_for(reference_id:, xml:) ⇒ Object



101
102
103
104
105
# File 'lib/xml/kit/templatable.rb', line 101

def signature_for(reference_id:, xml:)
  return unless sign?

  signatures.build(reference_id).to_xml(xml: xml)
end

#symmetric_cipherObject

This method is abstract.

Provides a default aes256-cbc symmetric cipher. Can be overridden to provide custom ciphers.

Since:

  • 0.3.0



93
94
95
# File 'lib/xml/kit/templatable.rb', line 93

def symmetric_cipher
  @symmetric_cipher ||= Crypto::SymmetricCipher.new
end

#to_xml(xml: ::Builder::XmlMarkup.new, pretty: false) ⇒ Object

Returns the generated XML document with an XML Digital Signature and XML Encryption.



29
30
31
32
# File 'lib/xml/kit/templatable.rb', line 29

def to_xml(xml: ::Builder::XmlMarkup.new, pretty: false)
  result = signatures.complete(render(self, xml: xml))
  pretty ? Nokogiri::XML(result).to_xml(indent: 2) : result
end