Commit eee4717

mo <mo@mokhan.ca>
2017-12-12 19:58:55
reduce duplication between signatures and signature.
1 parent 6fc2745
Changed files (4)
lib/saml/kit/builders/templates/nil_class.builder
lib/saml/kit/signature.rb
@@ -1,35 +1,22 @@
 module Saml
   module Kit
     class Signature
-      attr_reader :sign, :xml
-      attr_reader :configuration
+      attr_reader :signatures
+      attr_reader :xml
 
-      def initialize(xml, configuration:, sign: true)
-        @configuration = configuration
-        @sign = sign
+      def initialize(xml, signatures)
+        @signatures = signatures
         @xml = xml
       end
 
       def template(reference_id)
-        return unless sign
-        signature = signatures.build(reference_id)
-        Template.new(signature).to_xml(xml: xml)
-      end
-
-      def finalize
-        signatures.complete(xml.target!)
+        Template.new(signatures.build(reference_id)).to_xml(xml: xml)
       end
 
       def self.sign(sign: true, xml: ::Builder::XmlMarkup.new, configuration: Saml::Kit.configuration)
-        signature = new(xml, sign: sign, configuration: configuration)
-        yield xml, signature
-        signature.finalize
-      end
-
-      private
-
-      def signatures
-        @signatures ||= Saml::Kit::Signatures.new(configuration: configuration, sign: sign)
+        signatures = Saml::Kit::Signatures.new(configuration: configuration, sign: sign)
+        yield xml, new(xml, signatures)
+        signatures.complete(xml.target!)
       end
     end
   end
lib/saml/kit/signatures.rb
@@ -9,20 +9,15 @@ module Saml
       end
 
       def build(reference_id)
+        return nil unless sign
         Saml::Kit::Builders::XmlSignature.new(reference_id, configuration: configuration, sign: sign)
       end
 
       def complete(raw_xml)
         return raw_xml unless sign
-
+        private_key = configuration.signing_private_key
         Xmldsig::SignedDocument.new(raw_xml).sign(private_key)
       end
-
-      private
-
-      def private_key
-        configuration.signing_private_key
-      end
     end
   end
 end
lib/saml/kit/template.rb
@@ -18,7 +18,8 @@ module Saml
       end
 
       def template_path
-        File.join(File.expand_path(File.dirname(__FILE__)), "builders/templates/#{template_name}")
+        root_path = File.expand_path(File.dirname(__FILE__))
+        File.join(root_path, "builders/templates/", template_name)
       end
 
       def template