main
 1# frozen_string_literal: true
 2
 3module Saml
 4  module Kit
 5    module Bindings
 6      # This class is a base class for SAML bindings.
 7      # {include:file:spec/saml/kit/bindings/binding_spec.rb}
 8      class Binding
 9        attr_reader :binding, :location
10
11        def initialize(binding:, location:)
12          @binding = binding
13          @location = location
14        end
15
16        def binding?(other)
17          binding == other
18        end
19
20        def serialize(*)
21          []
22        end
23
24        def deserialize(_params)
25          raise ArgumentError, 'Unsupported binding'
26        end
27
28        def to_h
29          { binding: binding, location: location }
30        end
31
32        def ==(other)
33          to_s == other.to_s
34        end
35
36        def eql?(other)
37          self == other
38        end
39
40        def hash
41          to_s.hash
42        end
43
44        def to_s
45          "#{location}#{binding}"
46        end
47
48        def inspect
49          to_h.inspect
50        end
51
52        protected
53
54        def saml_param_from(params)
55          parameters = {
56            SAMLRequest: params[:SAMLRequest] || params['SAMLRequest'],
57            SAMLResponse: params[:SAMLResponse] || params['SAMLResponse'],
58          }
59          return parameters[:SAMLRequest] if parameters[:SAMLRequest].present?
60          return parameters[:SAMLResponse] if parameters[:SAMLResponse].present?
61
62          message = 'SAMLRequest or SAMLResponse parameter is required.'
63          raise ArgumentError, message
64        end
65      end
66    end
67  end
68end