Commit 970619f

mo <mo.khan@gmail.com>
2018-02-26 00:28:55
use xpath to parse attributes.
1 parent b952585
Changed files (2)
lib
spec
lib/saml/kit/assertion.rb
@@ -54,16 +54,10 @@ module Saml
       end
 
       def attributes
-        @attributes ||=
-          begin
-            attrs = assertion.fetch('AttributeStatement', {}).fetch('Attribute', [])
-            items = if attrs.is_a? Hash
-                      [[attrs['Name'], attrs['AttributeValue']]]
-                    else
-                      attrs.map { |item| [item['Name'], item['AttributeValue']] }
-                    end
-            Hash[items].with_indifferent_access
-          end
+        @attributes ||= @node.search("./saml:AttributeStatement/saml:Attribute", Saml::Kit::Document::NAMESPACES).inject({}) do |memo, item|
+          memo[item.attribute("Name").value] = item.at_xpath('./saml:AttributeValue', Saml::Kit::Document::NAMESPACES).try(:text)
+          memo
+        end.with_indifferent_access
       end
 
       def started_at
spec/saml/kit/assertion_spec.rb
@@ -5,13 +5,14 @@ RSpec.describe Saml::Kit::Assertion do
     end.assertion
   end
   let(:request) { instance_double(Saml::Kit::AuthenticationRequest, id: ::Xml::Kit::Id.generate, issuer: entity_id, assertion_consumer_service_url: FFaker::Internet.uri("https"), name_id_format: Saml::Kit::Namespaces::PERSISTENT, provider: nil, signed?: true, trusted?: true) }
-  let(:user) { User.new(name_id: SecureRandom.uuid, attributes: { }) }
+  let(:user) { User.new(name_id: SecureRandom.uuid, attributes: { id: SecureRandom.uuid }) }
   let(:entity_id) { FFaker::Internet.uri("https") }
 
   specify { expect(subject.issuer).to eql(entity_id) }
   specify { expect(subject.name_id).to eql(user.name_id) }
   specify { expect(subject.started_at.to_i).to eql(Time.now.utc.to_i) }
   specify { expect(subject.expired_at.to_i).to eql(Saml::Kit.configuration.session_timeout.since(Time.now).utc.to_i) }
+  specify { expect(subject.attributes).to eql("id" => user.attributes[:id]) }
 
   describe '#active?' do
     let(:configuration) do