Class: Xmldsig::SignedDocument
- Inherits:
-
Object
- Object
- Xmldsig::SignedDocument
- Defined in:
- lib/xmldsig/signed_document.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#force ⇒ Object
Returns the value of attribute force.
-
#id_attr ⇒ Object
Returns the value of attribute id_attr.
Instance Method Summary collapse
-
#initialize(document, options = {}) ⇒ SignedDocument
constructor
A new instance of SignedDocument.
- #sign(private_key = nil, instruct = true, &block) ⇒ Object
- #signatures ⇒ Object
- #signed_nodes ⇒ Object
- #validate(certificate = nil, &block) ⇒ Object
Constructor Details
#initialize(document, options = {}) ⇒ SignedDocument
Returns a new instance of SignedDocument.
5 6 7 8 9 10 11 12 13 |
# File 'lib/xmldsig/signed_document.rb', line 5 def initialize(document, = {}) @document = if document.kind_of?(Nokogiri::XML::Document) document else Nokogiri::XML(document, nil, nil, Nokogiri::XML::ParseOptions::STRICT) end @id_attr = [:id_attr] if [:id_attr] @force = [:force] end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
3 4 5 |
# File 'lib/xmldsig/signed_document.rb', line 3 def document @document end |
#force ⇒ Object
Returns the value of attribute force.
3 4 5 |
# File 'lib/xmldsig/signed_document.rb', line 3 def force @force end |
#id_attr ⇒ Object
Returns the value of attribute id_attr.
3 4 5 |
# File 'lib/xmldsig/signed_document.rb', line 3 def id_attr @id_attr end |
Instance Method Details
#sign(private_key = nil, instruct = true, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/xmldsig/signed_document.rb', line 19 def sign(private_key = nil, instruct = true, &block) signatures.reverse.each do |signature| signature.sign(private_key, &block) if signature.unsigned? || force end if instruct @document.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML) else @document.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION) end end |
#signatures ⇒ Object
35 36 37 38 39 |
# File 'lib/xmldsig/signed_document.rb', line 35 def signatures document.xpath("//ds:Signature", NAMESPACES). sort { |left, right| left.ancestors.size <=> right.ancestors.size }. collect { |node| Signature.new(node, @id_attr) } || [] end |
#signed_nodes ⇒ Object
31 32 33 |
# File 'lib/xmldsig/signed_document.rb', line 31 def signed_nodes signatures.flat_map(&:references).map(&:referenced_node) end |
#validate(certificate = nil, &block) ⇒ Object
15 16 17 |
# File 'lib/xmldsig/signed_document.rb', line 15 def validate(certificate = nil, &block) signatures.any? && signatures.all? { |signature| signature.valid?(certificate, &block) } end |