Sha256: 1dd4590244192ffda540b71ba408832a51ab5825de986aeccdba22d4f11835ec

Contents?: true

Size: 1.58 KB

Versions: 8

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Saml
  module Kit
    # This class is responsible for validating and
    # parsing a SAML Response document.
    # {include:file:spec/examples/response_spec.rb}
    class Response < Document
      include Respondable
      extend Forwardable

      def_delegators :assertion, :name_id, :[], :attributes

      validate :must_be_valid_assertion
      validate :must_contain_single_assertion

      def initialize(
        xml,
        request_id: nil,
        configuration: Saml::Kit.configuration
      )
        @request_id = request_id
        super(xml, name: 'Response', configuration: configuration)
      end

      def assertion(private_keys = configuration.private_keys(use: :encryption))
        @assertion ||=
          begin
            node = assertion_nodes.last
            if node.nil?
              Saml::Kit::NullAssertion.new
            else
              Saml::Kit::Assertion.new(
                node,
                configuration: @configuration,
                private_keys: private_keys
              )
            end
          end
      end

      private

      def must_be_valid_assertion
        assertion.valid?
        assertion.errors.each do |attribute, error|
          attribute = :assertion if attribute == :base
          errors[attribute] << error
        end
      end

      def must_contain_single_assertion
        return if assertion_nodes.count <= 1

        errors[:base] << error_message(:must_contain_single_assertion)
      end

      def assertion_nodes
        search(Saml::Kit::Assertion::XPATH)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
saml-kit-1.1.0 lib/saml/kit/response.rb
saml-kit-1.0.31 lib/saml/kit/response.rb
saml-kit-1.0.30 lib/saml/kit/response.rb
saml-kit-1.0.29 lib/saml/kit/response.rb
saml-kit-1.0.28 lib/saml/kit/response.rb
saml-kit-1.0.27 lib/saml/kit/response.rb
saml-kit-1.0.26 lib/saml/kit/response.rb
saml-kit-1.0.25 lib/saml/kit/response.rb