Sha256: 1b825db4ab515cd236e275a6dbf02773993b8d8e945cd057e8b9173d9a6cfc25

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Saml
  module Kit
    module Bindings
      class UrlBuilder
        include Serializable
        attr_reader :configuration

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

        def build(saml_document, relay_state: nil)
          payload = canonicalize(saml_document, relay_state)
          if configuration.sign?
            "#{saml_document.destination}?#{payload}&Signature=#{signature_for(payload)}"
          else
            "#{saml_document.destination}?#{payload}"
          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)
          {
            saml_document.query_string_parameter => serialize(saml_document.to_xml),
            'RelayState' => relay_state,
            'SigAlg' => Saml::Kit::Namespaces::SHA256,
          }.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

3 entries across 3 versions & 1 rubygems

Version Path
saml-kit-0.2.7 lib/saml/kit/bindings/url_builder.rb
saml-kit-0.2.6 lib/saml/kit/bindings/url_builder.rb
saml-kit-0.2.5 lib/saml/kit/bindings/url_builder.rb