10000 Add Digest HTTP Authentication with Basic Auth Fallback by derhelge · Pull Request #3074 · ytti/oxidized · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add Digest HTTP Authentication with Basic Auth Fallback #3074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions lib/oxidized/input/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,41 @@ def cmd_str(string)
def get_http(path)
schema = @secure ? "https://" : "http://"
uri = URI("#{schema}#{@node.ip}#{path}")
req = Net::HTTP::Get.new(uri)
req.basic_auth @username, @password unless @username.nil?
@headers.each do |header, value|
req.add_field(header, value)
end

Oxidized.logger.debug "Making request to: #{uri}"

ssl_verify = Oxidized.config.input.http.ssl_verify? ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https", verify_mode: ssl_verify) do |http|
http.request(req)

res = make_request(uri, ssl_verify)

if res.code == '401' && res['www-authenticate'].include?('Digest')
uri.user = @username
uri.password = @password
Oxidized.logger.debug "Server requires Digest authentication"
auth = Net::HTTP::DigestAuth.new.auth_header(uri, res['www-authenticate'], 'GET')

res = make_request(uri, ssl_verify, 'Authorization' => auth)
elsif @username && @password
Oxidized.logger.debug "Falling back to Basic authentication"
res = make_request(uri, ssl_verify, 'Authorization' => basic_auth_header)
end

Oxidized.logger.debug "Response code: #{res.code}"
res.body
end

def make_request(uri, ssl_verify, extra_headers = {})
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https", verify_mode: ssl_verify) do |http|
req = Net::HTTP::Get.new(uri)
@headers.merge(extra_headers).each { |header, value| req.add_field(header, value) }
Oxidized.logger.debug "Sending request with headers: #{@headers.merge(extra_headers)}"
http.request(req)
end
end

def basic_auth_header
"Basic " + ["#{@username}:#{@password}"].pack('m').delete("\r\n")
end

def log(str)
@log&.write(str)
Expand Down
0