Commit 1e5e862

mo <mo@mokhan.ca>
2017-12-11 22:11:22
remove duplication.
1 parent 1c38f34
Changed files (2)
lib
saml
kit
lib/saml/kit/builders/templates/signature.builder
@@ -1,20 +0,0 @@
-xml.Signature "xmlns" => Saml::Kit::Namespaces::XMLDSIG do
-  xml.SignedInfo do
-    xml.CanonicalizationMethod Algorithm: "http://www.w3.org/2001/10/xml-exc-c14n#"
-    xml.SignatureMethod Algorithm: signature_method
-    xml.Reference URI: "##{reference_id}" do
-      xml.Transforms do
-        xml.Transform Algorithm: "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
-        xml.Transform Algorithm: "http://www.w3.org/2001/10/xml-exc-c14n#"
-      end
-      xml.DigestMethod Algorithm: digest_method
-      xml.DigestValue ""
-    end
-  end
-  xml.SignatureValue ""
-  xml.KeyInfo do
-    xml.X509Data do
-      xml.X509Certificate stripped_signing_certificate
-    end
-  end
-end
lib/saml/kit/signature.rb
@@ -1,65 +1,23 @@
 module Saml
   module Kit
     class Signature
-      SIGNATURE_METHODS = {
-        SHA1: "http://www.w3.org/2000/09/xmldsig#rsa-sha1",
-        SHA224: "http://www.w3.org/2001/04/xmldsig-more#rsa-sha224",
-        SHA256: "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256",
-        SHA384: "http://www.w3.org/2001/04/xmldsig-more#rsa-sha384",
-        SHA512: "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512",
-      }.freeze
-      DIGEST_METHODS = {
-        SHA1: "http://www.w3.org/2000/09/xmldsig#SHA1",
-        SHA224: "http://www.w3.org/2001/04/xmldsig-more#sha224",
-        SHA256: "http://www.w3.org/2001/04/xmlenc#sha256",
-        SHA384: "http://www.w3.org/2001/04/xmldsig-more#sha384",
-        SHA512: "http://www.w3.org/2001/04/xmlenc#sha512",
-      }.freeze
-
       attr_reader :sign, :xml
-      attr_reader :stripped_signing_certificate
-      attr_reader :private_key
       attr_reader :configuration
 
       def initialize(xml, configuration:, sign: true)
         @configuration = configuration
-        @private_key = configuration.signing_private_key
-        @reference_ids = []
         @sign = sign
-        @stripped_signing_certificate = configuration.stripped_signing_certificate
         @xml = xml
       end
 
-      def signature_method
-        SIGNATURE_METHODS[configuration.signature_method]
-      end
-
-      def digest_method
-        DIGEST_METHODS[configuration.digest_method]
-      end
-
       def template(reference_id)
         return unless sign
-        return if reference_id.blank?
-        @reference_ids << reference_id
-        Template.new(self).to_xml(xml: xml)
-      end
-
-      def reference_id
-        @reference_ids.last
+        signature = signatures.build(reference_id)
+        Template.new(signature).to_xml(xml: xml)
       end
 
       def finalize
-        sign ? apply_to(xml.target!) : xml.target!
-      end
-
-      def apply_to(raw_xml)
-        return raw_xml unless sign
-
-        @reference_ids.each do |reference_id|
-          raw_xml = Xmldsig::SignedDocument.new(raw_xml).sign(private_key)
-        end
-        raw_xml
+        signatures.complete(xml.target!)
       end
 
       def self.sign(sign: true, xml: ::Builder::XmlMarkup.new, configuration: Saml::Kit.configuration)
@@ -67,6 +25,12 @@ module Saml
         yield xml, signature
         signature.finalize
       end
+
+      private
+
+      def signatures
+        @signatures ||= Saml::Kit::Signatures.new(configuration: configuration, sign: sign)
+      end
     end
   end
 end