Sha256: 069400044668aec59651756ccf0010a849d3d6c8936d645e0c4c3ca0ef0303ec

Contents?: true

Size: 1.96 KB

Versions: 18

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Plugins
    #
    # https://gitlab.com/honeyryderchuck/httpx/wikis/Authentication#ntlm-authentication
    #
    module NTLMAuthentication
      NTLMParams = Struct.new(:user, :domain, :password)

      class << self
        def load_dependencies(_klass)
          require "base64"
          require "ntlm"
        end

        def extra_options(options)
          options.merge(max_concurrent_requests: 1)
        end
      end

      module OptionsMethods
        def option_ntlm(value)
          raise TypeError, ":ntlm must be a #{NTLMParams}" unless value.is_a?(NTLMParams)

          value
        end
      end

      module InstanceMethods
        def ntlm_authentication(user, password, domain = nil)
          with(ntlm: NTLMParams.new(user, domain, password))
        end

        alias_method :ntlm_auth, :ntlm_authentication

        def send_requests(*requests)
          requests.flat_map do |request|
            ntlm = request.options.ntlm

            if ntlm
              request.headers["authorization"] = "NTLM #{NTLM.negotiate(domain: ntlm.domain).to_base64}"
              probe_response = wrap { super(request).first }

              if !probe_response.is_a?(ErrorResponse) && probe_response.status == 401 &&
                 probe_response.headers.key?("www-authenticate") &&
                 (challenge = probe_response.headers["www-authenticate"][/NTLM (.*)/, 1])

                challenge = Base64.decode64(challenge)
                ntlm_challenge = NTLM.authenticate(challenge, ntlm.user, ntlm.domain, ntlm.password).to_base64

                request.transition(:idle)

                request.headers["authorization"] = "NTLM #{ntlm_challenge}"
                super(request)
              else
                probe_response
              end
            else
              super(request)
            end
          end
        end
      end
    end
    register_plugin :ntlm_authentication, NTLMAuthentication
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
httpx-0.19.8 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.19.7 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.19.6 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.19.5 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.19.4 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.19.3 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.19.2 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.19.1 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.19.0 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.18.7 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.18.6 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.18.5 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.18.4 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.18.3 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.18.2 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.18.1 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.18.0 lib/httpx/plugins/ntlm_authentication.rb
httpx-0.17.0 lib/httpx/plugins/ntlm_authentication.rb