Commit 5d8a9e6

mo <mo.khan@gmail.com>
2017-12-02 03:13:22
parse or many attributes
1 parent aaf2156
Changed files (2)
lib
saml
spec
lib/saml/kit/response.rb
@@ -20,11 +20,15 @@ module Saml
       end
 
       def attributes
-        @attributes ||= Hash[
-          assertion.fetch('AttributeStatement', {}).fetch('Attribute', []).map do |item|
-            [item['Name'].to_sym, item['AttributeValue']]
+        @attributes ||=
+          begin
+            attrs = assertion.fetch('AttributeStatement', {}).fetch('Attribute', [])
+            if attrs.is_a? Hash
+              { attrs["Name"] => attrs["AttributeValue"] }.with_indifferent_access
+            else
+              Hash[attrs.map { |item| [item['Name'].to_sym, item['AttributeValue']] }].with_indifferent_access
+            end
           end
-        ].with_indifferent_access
       end
 
       def started_at
spec/saml/response_spec.rb
@@ -400,4 +400,26 @@ XML
       expect(subject.attributes).to be_present
     end
   end
+
+  describe "parsing" do
+    let(:user) { double(:user, name_id_for: SecureRandom.uuid, assertion_attributes_for: attributes) }
+    let(:request) { double(:request, id: Saml::Kit::Id.generate, signed?: true, trusted?: true, provider: nil, assertion_consumer_service_url: FFaker::Internet.uri("https"), name_id_format: '', issuer: FFaker::Internet.uri("https")) }
+    let(:attributes) { { name: 'mo' } }
+
+    it 'returns the name id' do
+      subject = described_class.build(user, request)
+      expect(subject.name_id).to eql(user.name_id_for)
+    end
+
+    it 'returns the single attributes' do
+      subject = described_class.build(user, request)
+      expect(subject.attributes).to eql('name' => 'mo')
+    end
+
+    it 'returns the multiple attributes' do
+      attributes[:age] = 33
+      subject = described_class.build(user, request)
+      expect(subject.attributes).to eql('name' => 'mo', 'age' => '33')
+    end
+  end
 end