Commit 90b104a

mo <mo@mokhan.ca>
2018-03-17 01:35:52
extract conditions class.
1 parent 80cceb9
lib/saml/kit/assertion.rb
@@ -1,7 +1,5 @@
 # frozen_string_literal: true
 
-require 'saml/kit/attribute_statement'
-
 module Saml
   module Kit
     # This class validates the Assertion
@@ -11,10 +9,13 @@ module Saml
       include ActiveModel::Validations
       include Translatable
       include XmlParseable
+      extend Forwardable
       XPATH = [
         '/samlp:Response/saml:Assertion',
         '/samlp:Response/saml:EncryptedAssertion'
       ].join('|')
+      def_delegators :conditions, :started_at, :expired_at, :audiences
+      def_delegators :attribute_statement, :attributes
 
       validate :must_be_decryptable
       validate :must_match_issuer, if: :decryptable?
@@ -61,22 +62,13 @@ module Saml
         now > drifted_started_at && !expired?(now)
       end
 
-      def attributes
-        xpath = './saml:AttributeStatement'
-        AttributeStatement.new(search(xpath)).attributes
-      end
-
-      def started_at
-        parse_iso8601(at_xpath('./saml:Conditions/@NotBefore').try(:value))
-      end
-
-      def expired_at
-        parse_iso8601(at_xpath('./saml:Conditions/@NotOnOrAfter').try(:value))
+      def attribute_statement
+        @attribute_statement ||=
+          AttributeStatement.new(search('./saml:AttributeStatement'))
       end
 
-      def audiences
-        xpath = './saml:Conditions/saml:AudienceRestriction/saml:Audience'
-        search(xpath).map(&:text)
+      def conditions
+        @conditions ||= Conditions.new(search('./saml:Conditions'))
       end
 
       def encrypted?
@@ -106,13 +98,6 @@ module Saml
         Saml::Kit.logger.error(error)
       end
 
-      def parse_iso8601(value)
-        DateTime.parse(value)
-      rescue StandardError => error
-        Saml::Kit.logger.error(error)
-        Time.at(0).to_datetime
-      end
-
       def must_match_issuer
         return if audiences.empty? || audiences.include?(configuration.entity_id)
         errors[:audience] << error_message(:must_match_issuer)
lib/saml/kit/attribute_statement.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: true
 
 module Saml
   module Kit
lib/saml/kit/conditions.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+
+module Saml
+  module Kit
+    class Conditions
+      include XmlParseable
+
+      attr_reader :content
+
+      def initialize(node)
+        @to_nokogiri = node
+        @content = node.to_s
+      end
+
+      def started_at
+        parse_iso8601(at_xpath('./@NotBefore').try(:value))
+      end
+
+      def expired_at
+        parse_iso8601(at_xpath('./@NotOnOrAfter').try(:value))
+      end
+
+      def audiences
+        search('./saml:AudienceRestriction/saml:Audience').map(&:text)
+      end
+
+      private
+
+      def parse_iso8601(value)
+        DateTime.parse(value)
+      rescue StandardError => error
+        Saml::Kit.logger.error(error)
+        Time.at(0).to_datetime
+      end
+    end
+  end
+end
lib/saml/kit.rb
@@ -32,8 +32,10 @@ require 'saml/kit/namespaces'
 require 'saml/kit/document'
 
 require 'saml/kit/assertion'
+require 'saml/kit/attribute_statement'
 require 'saml/kit/authentication_request'
 require 'saml/kit/bindings'
+require 'saml/kit/conditions'
 require 'saml/kit/configuration'
 require 'saml/kit/default_registry'
 require 'saml/kit/logout_response'