Module: HTTPX::Plugins::DigestAuthentication::InstanceMethods

Defined in:
lib/httpx/plugins/digest_authentication.rb

Instance Method Summary collapse

Instance Method Details

#digest_authentication(user, password) ⇒ Object Also known as: digest_auth


29
30
31
# File 'lib/httpx/plugins/digest_authentication.rb', line 29

def digest_authentication(user, password)
  branch(default_options.with_digest(Digest.new(user, password)))
end

#request(*args, **options) ⇒ Object

Raises:


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/httpx/plugins/digest_authentication.rb', line 35

def request(*args, **options)
  requests = build_requests(*args, options)
  probe_request = requests.first
  digest = probe_request.options.digest

  return super unless digest

  prev_response = wrap { send_requests(*probe_request, options).first }

  raise Error, "request doesn't require authentication (status: #{prev_response.status})" unless prev_response.status == 401

  probe_request.transition(:idle)

  responses = []

  while (request = requests.shift)
    token = digest.generate_header(request, prev_response)
    request.headers["authorization"] = "Digest #{token}"
    response = if requests.empty?
      send_requests(*request, options).first
    else
      wrap { send_requests(*request, options).first }
    end
    responses << response
    prev_response = response
  end

  return responses.first if responses.size == 1

  responses
end