Sha256: af53c16c1bd4f5cc6fac8d307e1c82cdd716663d2fdeb5b92624e646c684213b

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

require "openssl"
require "options"

module SignedXml
  class Document
    include Logging

    attr_reader :doc

    def initialize(thing)
      if thing.is_a? Nokogiri::XML::Document
        @doc = thing
      else
        @doc = Nokogiri::XML(thing)
      end
    end

    def is_verifiable?
      signatures.any?
    end

    def is_verified?(arg = nil)
      unless is_verifiable?
        logger.warn "document cannot be verified because it contains no <Signature> elements"
        return false
      end

      if arg.respond_to? :public_key
        set_public_key_for_signatures(arg)
      elsif arg.respond_to? :has_key?
        set_certificate_store_for_signatures(arg)
      elsif !arg.nil?
        raise ArgumentError, "#{arg.inspect}:#{arg.class} must have a public key or be a hash of public keys"
      end

      signatures.all?(&:is_verified?)
    end

    def sign(private_key, certificate = nil)
      signatures.each { |sig| sig.sign(private_key, certificate) }
      self
    end

    def to_xml
      doc.to_xml
    end

    private

    def signatures
      @signatures ||= init_signatures
    end

    def init_signatures
      signatures = []
      doc.xpath("//ds:Signature", ds: XMLDSIG_NS).each do |signature_node|
        signatures << Signature.new(signature_node)
      end
      signatures
    end

    def set_public_key_for_signatures(certificate)
      signatures.each { |sig| sig.public_key = certificate.public_key }
    end

    def set_certificate_store_for_signatures(cert_store)
      signatures.each { |sig| sig.certificate_store = cert_store }
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
signed_xml-1.3.0 lib/signed_xml/document.rb
signed_xml-1.2.0 lib/signed_xml/document.rb
signed_xml-1.1.0 lib/signed_xml/document.rb
omniauth-islykill-0.9.8 lib/signed_xml/document.rb
signed_xml-1.0.1 lib/signed_xml/document.rb