class Xml::Kit::Document
{include:file:spec/xml/kit/document_spec.rb}
Constants
- NAMESPACES
Attributes
document[R]
namespaces[R]
raw_xml[R]
Public Class Methods
new(raw_xml, namespaces: NAMESPACES)
click to toggle source
# File lib/xml/kit/document.rb, line 13 def initialize(raw_xml, namespaces: NAMESPACES) @raw_xml = raw_xml @namespaces = namespaces @document = ::Nokogiri::XML(raw_xml) end
Public Instance Methods
find_all(xpath)
click to toggle source
Returns all XML nodes found by searching the document with the provided XPath.
@param xpath [String] the XPath to use to search the document
# File lib/xml/kit/document.rb, line 29 def find_all(xpath) document.search(xpath, namespaces) end
find_by(xpath)
click to toggle source
Returns the first XML node found by searching the document with the provided XPath.
@param xpath [String] the XPath to use to search the document
# File lib/xml/kit/document.rb, line 22 def find_by(xpath) document.at_xpath(xpath, namespaces) end
to_xml(pretty: true)
click to toggle source
Return the XML document as a [String].
@param pretty [Boolean] return the XML string in a human readable format if true.
# File lib/xml/kit/document.rb, line 36 def to_xml(pretty: true) pretty ? document.to_xml(indent: 2) : raw_xml end
Private Instance Methods
invalid_signatures(id_attr: 'ID=$uri or @Id')
click to toggle source
# File lib/xml/kit/document.rb, line 50 def invalid_signatures(id_attr: 'ID=$uri or @Id') Xmldsig::SignedDocument .new(document, id_attr: id_attr) .signatures.find_all do |signature| x509_certificates.all? do |certificate| !signature.valid?(certificate) end end end
validate_certificates(now = Time.current)
click to toggle source
# File lib/xml/kit/document.rb, line 60 def validate_certificates(now = Time.current) return if find_by('//ds:Signature').nil? x509_certificates.each do |certificate| errors.add(:certificate, "Not valid before #{certificate.not_before}") if now < certificate.not_before errors.add(:certificate, "Not valid after #{certificate.not_after}") if now > certificate.not_after end end
validate_signatures()
click to toggle source
# File lib/xml/kit/document.rb, line 44 def validate_signatures invalid_signatures.flat_map(&:errors).uniq.each do |error| errors.add(error, 'is invalid') end end
x509_certificates()
click to toggle source
# File lib/xml/kit/document.rb, line 70 def x509_certificates find_all('//ds:KeyInfo/ds:X509Data/ds:X509Certificate').map do |item| Certificate.to_x509(item.text) end end