Sha256: a22ac4022032f413fca2f23e4106f8a6f32882b769f7056a80ca089a350e661b

Contents?: true

Size: 1.02 KB

Versions: 4

Compression:

Stored size: 1.02 KB

Contents

module Xmldsig
  class SignedDocument
    attr_accessor :document, :id_attr

    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]
    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 { |signature| signature.sign(private_key, &block) }
      instruct ? @document.to_s : @document.root.to_s
    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

4 entries across 4 versions & 1 rubygems

Version Path
xmldsig-0.3.0 lib/xmldsig/signed_document.rb
xmldsig-0.2.10 lib/xmldsig/signed_document.rb
xmldsig-0.2.9 lib/xmldsig/signed_document.rb
xmldsig-0.2.8 lib/xmldsig/signed_document.rb