Sha256: 984d6836a95a6597bc643d97fb5045ba13bf7405f4c94c36bdc5d49ee089bfa5

Contents?: true

Size: 1.82 KB

Versions: 19

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module Saml
  module Kit
    module Bindings
      # This class is responsible for
      # generating a url as per the
      # rules for the HTTP redirect binding
      # specification.
      # https://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf
      # {include:file:spec/saml/kit/bindings/url_builder_spec.rb}
      class UrlBuilder
        include Serializable
        attr_reader :configuration

        def initialize(configuration: Saml::Kit.configuration)
          @configuration = configuration
        end

        def build(document, relay_state: nil)
          destination = document.destination
          if configuration.sign?
            payload = canonicalize(document, relay_state)
            "#{destination}?#{payload}&Signature=#{signature_for(payload)}"
          else
            "#{destination}?" + to_query_string(
              document.query_string_parameter => serialize(document.to_xml),
              'RelayState' => relay_state
            )
          end
        end

        private

        def signature_for(payload)
          private_key = configuration.private_keys(use: :signing).last
          encode(private_key.sign(OpenSSL::Digest::SHA256.new, payload))
        end

        def canonicalize(saml_document, relay_state)
          xml = saml_document.to_xml
          to_query_string(
            saml_document.query_string_parameter => serialize(xml),
            'RelayState' => relay_state,
            'SigAlg' => ::Xml::Kit::Namespaces::SHA256
          )
        end

        def to_query_string(query_params)
          query_params.map do |(key, value)|
            value.present? ? "#{key}=#{escape(value)}" : nil
          end.compact.join('&')
        end

        def serialize(value)
          encode(deflate(value))
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
saml-kit-1.3.0 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.2.0 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.1.0 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.31 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.30 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.29 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.28 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.27 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.26 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.25 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.24 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.23 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.22 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.21 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.20 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.19 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.18 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.17 lib/saml/kit/bindings/url_builder.rb
saml-kit-1.0.16 lib/saml/kit/bindings/url_builder.rb