Sha256: 4764d7b12dc517e139f10daf7d996a4701cfa044b07399a567bc26769fd69399

Contents?: true

Size: 1.78 KB

Versions: 8

Compression:

Stored size: 1.78 KB

Contents

require 'active_support/core_ext/array/wrap'

module SAML2
  class Conditions < Array
    attr_accessor :not_before, :not_on_or_after

    def valid?(options = {})
      now = options[:now] || Time.now
      return :invalid if not_before && now < not_before
      return :invalid if not_on_or_after && now >= not_on_or_after

      result = :valid
      each do |condition|
        this_result = condition.valid?(options)
        case this_result
        when :invalid
          return :invalid
        when :indeterminate
          result = :indeterminate
          when :valid
        else
          raise "unknown validity of #{condition}"
        end
      end
      result
    end

    def build(builder)
      builder['saml'].Conditions do |conditions|
        conditions.parent['NotBefore'] = not_before.iso8601 if not_before
        conditions.parent['NotOnOrAfter'] = not_on_or_after.iso8601 if not_on_or_after

        each do |condition|
          condition.build(conditions)
        end
      end
    end

    # Any unknown condition
    class Condition
      def valid?(_)
        :indeterminate
      end
    end

    class AudienceRestriction < Condition
      attr_accessor :audience

      def initialize(audience)
        @audience = audience
      end

      def valid?(options)
        Array.wrap(audience).include?(options[:audience]) ? :valid : :invalid
      end

      def build(builder)
        builder['saml'].AudienceRestriction do |audience_restriction|
          Array.wrap(audience).each do |single_audience|
            audience_restriction['saml'].Audience(single_audience)
          end
        end
      end
    end

    class OneTimeUse < Condition
      def valid?(_)
        :valid
      end

      def build(builder)
        builder['saml'].OneTimeUse
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
saml2-1.1.5 lib/saml2/conditions.rb
saml2-1.1.4 lib/saml2/conditions.rb
saml2-1.1.3 lib/saml2/conditions.rb
saml2-1.1.2 lib/saml2/conditions.rb
saml2-1.1.1 lib/saml2/conditions.rb
saml2-1.1.0 lib/saml2/conditions.rb
saml2-1.0.10 lib/saml2/conditions.rb
saml2-1.0.9 lib/saml2/conditions.rb