Commit 84af061

mo <mo.khan@gmail.com>
2017-12-28 17:56:05
extract EncryptedAssertion class.
1 parent 4fb61ae
lib/saml/kit/builders/templates/assertion.builder
@@ -1,29 +1,27 @@
-encryption_for(xml: xml) do |xml|
-  xml.Assertion(assertion_options) do
-    xml.Issuer issuer
-    signature_for(reference_id: reference_id, xml: xml)
-    xml.Subject do
-      xml.NameID name_id, Format: name_id_format
-      xml.SubjectConfirmation Method: Saml::Kit::Namespaces::BEARER do
-        xml.SubjectConfirmationData "", subject_confirmation_data_options
-      end
+xml.Assertion(assertion_options) do
+  xml.Issuer issuer
+  signature_for(reference_id: reference_id, xml: xml)
+  xml.Subject do
+    xml.NameID name_id, Format: name_id_format
+    xml.SubjectConfirmation Method: Saml::Kit::Namespaces::BEARER do
+      xml.SubjectConfirmationData "", subject_confirmation_data_options
     end
-    xml.Conditions conditions_options do
-      xml.AudienceRestriction do
-        xml.Audience request.issuer
-      end
+  end
+  xml.Conditions conditions_options do
+    xml.AudienceRestriction do
+      xml.Audience request.issuer
     end
-    xml.AuthnStatement authn_statement_options do
-      xml.AuthnContext do
-        xml.AuthnContextClassRef Saml::Kit::Namespaces::PASSWORD
-      end
+  end
+  xml.AuthnStatement authn_statement_options do
+    xml.AuthnContext do
+      xml.AuthnContextClassRef Saml::Kit::Namespaces::PASSWORD
     end
-    if assertion_attributes.any?
-      xml.AttributeStatement do
-        assertion_attributes.each do |key, value|
-          xml.Attribute Name: key, NameFormat: Saml::Kit::Namespaces::URI, FriendlyName: key do
-            xml.AttributeValue value.to_s
-          end
+  end
+  if assertion_attributes.any?
+    xml.AttributeStatement do
+      assertion_attributes.each do |key, value|
+        xml.Attribute Name: key, NameFormat: Saml::Kit::Namespaces::URI, FriendlyName: key do
+          xml.AttributeValue value.to_s
         end
       end
     end
lib/saml/kit/builders/templates/encrypted_assertion.builder
@@ -0,0 +1,5 @@
+xml.EncryptedAssertion xmlns: Saml::Kit::Namespaces::ASSERTION do
+  encryption_for(xml: xml) do |xml|
+    render assertion, xml: xml
+  end
+end
lib/saml/kit/builders/assertion.rb
@@ -5,7 +5,7 @@ module Saml
         include XmlTemplatable
         extend Forwardable
 
-        def_delegators :@response_builder, :encrypt, :embed_signature, :request, :issuer, :reference_id, :now, :configuration, :user, :version, :destination, :encryption_certificate
+        def_delegators :@response_builder, :embed_signature, :request, :issuer, :reference_id, :now, :configuration, :user, :version, :destination
 
         def initialize(response_builder)
           @response_builder = response_builder
lib/saml/kit/builders/encrypted_assertion.rb
@@ -0,0 +1,20 @@
+module Saml
+  module Kit
+    module Builders
+      class EncryptedAssertion
+        include XmlTemplatable
+        extend Forwardable
+
+        attr_reader :assertion
+        attr_reader :encrypt
+        def_delegators :@response_builder, :configuration, :encryption_certificate
+
+        def initialize(response_builder, assertion)
+          @response_builder = response_builder
+          @assertion = assertion
+          @encrypt = true
+        end
+      end
+    end
+  end
+end
lib/saml/kit/builders/response.rb
@@ -38,6 +38,11 @@ module Saml
 
         def assertion
           @assertion ||= Saml::Kit::Builders::Assertion.new(self)
+          if encrypt
+            Saml::Kit::Builders::EncryptedAssertion.new(self, @assertion)
+          else
+            @assertion
+          end
         end
 
         def response_options
lib/saml/kit/builders.rb
@@ -1,6 +1,7 @@
 require 'saml/kit/xml_templatable'
 require 'saml/kit/builders/assertion'
 require 'saml/kit/builders/authentication_request'
+require 'saml/kit/builders/encrypted_assertion'
 require 'saml/kit/builders/identity_provider_metadata'
 require 'saml/kit/builders/logout_request'
 require 'saml/kit/builders/logout_response'
xml-kit/lib/xml/kit/builders/templates/xml_encryption.builder
@@ -1,16 +1,14 @@
-xml.EncryptedAssertion xmlns: Saml::Kit::Namespaces::ASSERTION do
-  xml.EncryptedData xmlns: ::Xml::Kit::Namespaces::XMLENC do
-    xml.EncryptionMethod Algorithm: "http://www.w3.org/2001/04/xmlenc#aes256-cbc"
-    xml.KeyInfo xmlns: ::Xml::Kit::Namespaces::XMLDSIG do
-      xml.EncryptedKey xmlns: ::Xml::Kit::Namespaces::XMLENC do
-        xml.EncryptionMethod Algorithm: "http://www.w3.org/2001/04/xmlenc#rsa-1_5"
-        xml.CipherData do
-          xml.CipherValue Base64.encode64(public_key.public_encrypt(key))
-        end
+xml.EncryptedData xmlns: ::Xml::Kit::Namespaces::XMLENC do
+  xml.EncryptionMethod Algorithm: "http://www.w3.org/2001/04/xmlenc#aes256-cbc"
+  xml.KeyInfo xmlns: ::Xml::Kit::Namespaces::XMLDSIG do
+    xml.EncryptedKey xmlns: ::Xml::Kit::Namespaces::XMLENC do
+      xml.EncryptionMethod Algorithm: "http://www.w3.org/2001/04/xmlenc#rsa-1_5"
+      xml.CipherData do
+        xml.CipherValue Base64.encode64(public_key.public_encrypt(key))
       end
     end
-    xml.CipherData do
-      xml.CipherValue Base64.encode64(iv + encrypted)
-    end
+  end
+  xml.CipherData do
+    xml.CipherValue Base64.encode64(iv + encrypted)
   end
 end