Commit fd37d75

mo <mo.khan@gmail.com>
2017-12-16 18:46:54
build metadata with both SP and IDP.
1 parent 90c7e3b
lib/saml/kit/builders/templates/identity_provider_metadata.builder
@@ -1,32 +1,21 @@
-xml.instruct!
-xml.EntityDescriptor entity_descriptor_options do
-  signature_for(reference_id: id, xml: xml)
-  xml.IDPSSODescriptor idp_sso_descriptor_options do
-    configuration.certificates(use: :signing).each do |certificate|
-      render certificate, xml: xml
-    end
-    configuration.certificates(use: :encryption).each do |certificate|
-      render certificate, xml: xml
-    end
-    logout_urls.each do |item|
-      xml.SingleLogoutService Binding: item[:binding], Location: item[:location]
-    end
-    name_id_formats.each do |format|
-      xml.NameIDFormat format
-    end
-    single_sign_on_urls.each do |item|
-      xml.SingleSignOnService Binding: item[:binding], Location: item[:location]
-    end
-    attributes.each do |attribute|
-      xml.tag! 'saml:Attribute', Name: attribute
-    end
+signature_for(reference_id: id, xml: xml)
+xml.IDPSSODescriptor descriptor_options do
+  configuration.certificates(use: :signing).each do |certificate|
+    render certificate, xml: xml
   end
-  xml.Organization do
-    xml.OrganizationName organization_name, 'xml:lang': "en"
-    xml.OrganizationDisplayName organization_name, 'xml:lang': "en"
-    xml.OrganizationURL organization_url, 'xml:lang': "en"
+  configuration.certificates(use: :encryption).each do |certificate|
+    render certificate, xml: xml
   end
-  xml.ContactPerson contactType: "technical" do
-    xml.Company "mailto:#{contact_email}"
+  logout_urls.each do |item|
+    xml.SingleLogoutService Binding: item[:binding], Location: item[:location]
+  end
+  name_id_formats.each do |format|
+    xml.NameIDFormat format
+  end
+  single_sign_on_urls.each do |item|
+    xml.SingleSignOnService Binding: item[:binding], Location: item[:location]
+  end
+  attributes.each do |attribute|
+    xml.tag! 'saml:Attribute', Name: attribute
   end
 end
lib/saml/kit/builders/templates/metadata.builder
@@ -1,5 +1,13 @@
-#xml.instruct!
-#xml.EntityDescriptor entity_descriptor_options do
-#end
-render identity_provider, xml: xml
-render service_provider, xml: xml
+xml.instruct!
+xml.EntityDescriptor entity_descriptor_options do
+  render identity_provider, xml: xml
+  render service_provider, xml: xml
+  xml.Organization do
+    xml.OrganizationName organization_name, 'xml:lang': "en"
+    xml.OrganizationDisplayName organization_name, 'xml:lang': "en"
+    xml.OrganizationURL organization_url, 'xml:lang': "en"
+  end
+  xml.ContactPerson contactType: "technical" do
+    xml.Company "mailto:#{contact_email}"
+  end
+end
lib/saml/kit/builders/templates/service_provider_metadata.builder
@@ -1,29 +1,18 @@
-xml.instruct!
-xml.EntityDescriptor entity_descriptor_options do
-  signature_for(reference_id: id, xml: xml)
-  xml.SPSSODescriptor descriptor_options do
-    configuration.certificates(use: :signing).each do |certificate|
-      render certificate, xml: xml
-    end
-    configuration.certificates(use: :encryption).each do |certificate|
-      render certificate, xml: xml
-    end
-    logout_urls.each do |item|
-      xml.SingleLogoutService Binding: item[:binding], Location: item[:location]
-    end
-    name_id_formats.each do |format|
-      xml.NameIDFormat format
-    end
-    acs_urls.each_with_index do |item, index|
-      xml.AssertionConsumerService Binding: item[:binding], Location: item[:location], index: index, isDefault: index == 0 ? true : false
-    end
+signature_for(reference_id: id, xml: xml)
+xml.SPSSODescriptor descriptor_options do
+  configuration.certificates(use: :signing).each do |certificate|
+    render certificate, xml: xml
   end
-  xml.Organization do
-    xml.OrganizationName organization_name, 'xml:lang': "en"
-    xml.OrganizationDisplayName organization_name, 'xml:lang': "en"
-    xml.OrganizationURL organization_url, 'xml:lang': "en"
+  configuration.certificates(use: :encryption).each do |certificate|
+    render certificate, xml: xml
   end
-  xml.ContactPerson contactType: "technical" do
-    xml.Company "mailto:#{contact_email}"
+  logout_urls.each do |item|
+    xml.SingleLogoutService Binding: item[:binding], Location: item[:location]
+  end
+  name_id_formats.each do |format|
+    xml.NameIDFormat format
+  end
+  acs_urls.each_with_index do |item, index|
+    xml.AssertionConsumerService Binding: item[:binding], Location: item[:location], index: index, isDefault: index == 0 ? true : false
   end
 end
lib/saml/kit/builders/identity_provider_metadata.rb
@@ -43,7 +43,7 @@ module Saml
           }
         end
 
-        def idp_sso_descriptor_options
+        def descriptor_options
           {
             WantAuthnRequestsSigned: want_authn_requests_signed,
             protocolSupportEnumeration: Namespaces::PROTOCOL,
lib/saml/kit/builders/metadata.rb
@@ -4,11 +4,12 @@ module Saml
       class Metadata
         include Templatable
 
-        attr_reader :id
-        attr_reader :entity_id
+        attr_accessor :entity_id
+        attr_accessor :id
+        attr_accessor :organization_name, :organization_url, :contact_email
         attr_reader :configuration
-        attr_reader :service_provider
         attr_reader :identity_provider
+        attr_reader :service_provider
 
         def initialize(configuration: Saml::Kit.configuration)
           @id = Id.generate
spec/saml/builders/metadata_spec.rb
@@ -30,5 +30,27 @@ RSpec.describe Saml::Kit::Builders::Metadata do
       expect(hash_result['EntityDescriptor']['IDPSSODescriptor']['SingleSignOnService']).to be_present
       expect(hash_result['EntityDescriptor']['IDPSSODescriptor']['SingleSignOnService']['Location']).to eql(url)
     end
+
+    it 'builds metadata for both IDP and SP' do
+      result = subject.build do |builder|
+        builder.build_service_provider do |x|
+          x.add_assertion_consumer_service(url, binding: :http_post)
+        end
+        builder.build_identity_provider do |x|
+          x.add_single_sign_on_service(url, binding: :http_post)
+        end
+      end
+
+      hash_result = Hash.from_xml(result.to_xml)
+      expect(hash_result['EntityDescriptor']).to be_present
+      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']).to be_present
+      expect(hash_result['EntityDescriptor']['SPSSODescriptor']).to be_present
+
+      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']['SingleSignOnService']).to be_present
+      expect(hash_result['EntityDescriptor']['IDPSSODescriptor']['SingleSignOnService']['Location']).to eql(url)
+      expect(hash_result['EntityDescriptor']['SPSSODescriptor']['AssertionConsumerService']).to be_present
+      expect(hash_result['EntityDescriptor']['SPSSODescriptor']['AssertionConsumerService']['Location']).to eql(url)
+    end
+
   end
 end