Sha256: c93c8622ac39b04706f742d21d0eb55959b212e3ad651d1296818e70ba45565e

Contents?: true

Size: 1.3 KB

Versions: 7

Compression:

Stored size: 1.3 KB

Contents

module Xmldsig
  class SignedDocument
    attr_accessor :document, :id_attr, :force

    def initialize(document, options = {})
      @document = if document.kind_of?(Nokogiri::XML::Document)
        document
      else
        Nokogiri::XML(document, nil, nil, Nokogiri::XML::ParseOptions::STRICT)
      end
      @id_attr  = options[:id_attr] if options[:id_attr]
      @force    = options[:force]
    end

    def validate(certificate = nil, &block)
      signatures.any? && signatures.all? { |signature| signature.valid?(certificate, &block) }
    end

    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

    def signed_nodes
      signatures.flat_map(&:references).map(&:referenced_node)
    end

    def signatures
      document.xpath("//ds:Signature", NAMESPACES).
          sort { |left, right| left.ancestors.size <=> right.ancestors.size }.
          collect { |node| Signature.new(node, @id_attr) } || []
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
xmldsig-0.6.3 lib/xmldsig/signed_document.rb
xmldsig-0.6.2 lib/xmldsig/signed_document.rb
xmldsig-0.6.1 lib/xmldsig/signed_document.rb
xmldsig-0.5.1 lib/xmldsig/signed_document.rb
xmldsig-0.5.0 lib/xmldsig/signed_document.rb
xmldsig-0.4.1 lib/xmldsig/signed_document.rb
xmldsig-0.4.0 lib/xmldsig/signed_document.rb