Sha256: 852d871f569d47e1811a1c24397e93c6f2b3925dce152bd5884d22def0a7aefb

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Xml
  module Kit
    # @!visibility private
    class Signatures # :nodoc:
      attr_reader :key_pair, :signature_method, :digest_method

      # @!visibility private
      def initialize(key_pair:, signature_method:, digest_method:)
        @digest_method = digest_method
        @key_pair = key_pair
        @signature_method = signature_method
      end

      # @!visibility private
      def sign_with(key_pair)
        @key_pair = key_pair
      end

      # @!visibility private
      def build(reference_id)
        return nil if key_pair.nil?

        ::Xml::Kit::Signature.new(
          reference_id,
          certificate: key_pair.certificate,
          signature_method: signature_method,
          digest_method: digest_method
        )
      end

      # @!visibility private
      def complete(raw_xml)
        return raw_xml if key_pair.nil?

        private_key = key_pair.private_key
        Xmldsig::SignedDocument.new(raw_xml).sign(private_key, false)
      end

      # @!visibility private
      def self.sign(xml: ::Builder::XmlMarkup.new, key_pair:, signature_method: :SHA256, digest_method: :SHA256)
        signatures = new(
          key_pair: key_pair,
          signature_method: signature_method,
          digest_method: digest_method
        )
        yield xml, XmlSignatureTemplate.new(xml, signatures)
        signatures.complete(xml.target!)
      end

      class XmlSignatureTemplate # :nodoc:
        # @!visibility private
        attr_reader :signatures, :xml

        # @!visibility private
        def initialize(xml, signatures)
          @signatures = signatures
          @xml = xml
        end

        # @!visibility private
        def template(reference_id)
          Template.new(signatures.build(reference_id)).to_xml(xml: xml)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
xml-kit-0.2.0 lib/xml/kit/signatures.rb
xml-kit-0.1.14 lib/xml/kit/signatures.rb
xml-kit-0.1.13 lib/xml/kit/signatures.rb
xml-kit-0.1.12 lib/xml/kit/signatures.rb