2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2005-01-02 17:31:13 -05:00
|
|
|
require "test/unit"
|
|
|
|
require "net/http"
|
|
|
|
require "tempfile"
|
|
|
|
require "webrick"
|
|
|
|
require "webrick/httpauth/basicauth"
|
2018-03-28 04:06:44 -04:00
|
|
|
require "stringio"
|
2010-02-02 08:58:56 -05:00
|
|
|
require_relative "utils"
|
2005-01-02 17:31:13 -05:00
|
|
|
|
|
|
|
class TestWEBrickHTTPAuth < Test::Unit::TestCase
|
2016-01-05 01:09:17 -05:00
|
|
|
def teardown
|
|
|
|
WEBrick::Utils::TimeoutHandler.terminate
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2005-01-02 17:31:13 -05:00
|
|
|
def test_basic_auth
|
2014-11-09 09:01:20 -05:00
|
|
|
log_tester = lambda {|log, access_log|
|
|
|
|
assert_equal(1, log.length)
|
|
|
|
assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, log[0])
|
|
|
|
}
|
|
|
|
TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log|
|
2005-01-02 17:31:13 -05:00
|
|
|
realm = "WEBrick's realm"
|
|
|
|
path = "/basic_auth"
|
|
|
|
|
|
|
|
server.mount_proc(path){|req, res|
|
|
|
|
WEBrick::HTTPAuth.basic_auth(req, res, realm){|user, pass|
|
|
|
|
user == "webrick" && pass == "supersecretpassword"
|
2009-03-05 22:56:38 -05:00
|
|
|
}
|
2005-01-02 17:31:13 -05:00
|
|
|
res.body = "hoge"
|
|
|
|
}
|
|
|
|
http = Net::HTTP.new(addr, port)
|
|
|
|
g = Net::HTTP::Get.new(path)
|
|
|
|
g.basic_auth("webrick", "supersecretpassword")
|
2008-10-29 07:48:35 -04:00
|
|
|
http.request(g){|res| assert_equal("hoge", res.body, log.call)}
|
2005-01-02 17:31:13 -05:00
|
|
|
g.basic_auth("webrick", "not super")
|
2008-10-29 07:48:35 -04:00
|
|
|
http.request(g){|res| assert_not_equal("hoge", res.body, log.call)}
|
2005-01-02 17:31:13 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-07-25 23:21:52 -04:00
|
|
|
def test_basic_auth_sha
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("test_webrick_auth") {|tmpfile|
|
|
|
|
tmpfile.puts("webrick:{SHA}GJYFRpBbdchp595jlh3Bhfmgp8k=")
|
|
|
|
tmpfile.flush
|
|
|
|
assert_raise(NotImplementedError){
|
|
|
|
WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path)
|
|
|
|
}
|
2005-01-02 17:31:13 -05:00
|
|
|
}
|
2018-07-25 23:21:52 -04:00
|
|
|
end
|
2005-01-02 17:31:13 -05:00
|
|
|
|
2018-07-25 23:21:52 -04:00
|
|
|
def test_basic_auth_md5
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("test_webrick_auth") {|tmpfile|
|
|
|
|
tmpfile.puts("webrick:$apr1$IOVMD/..$rmnOSPXr0.wwrLPZHBQZy0")
|
|
|
|
tmpfile.flush
|
|
|
|
assert_raise(NotImplementedError){
|
|
|
|
WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path)
|
|
|
|
}
|
2005-01-02 17:31:13 -05:00
|
|
|
}
|
|
|
|
end
|
2011-07-05 01:06:05 -04:00
|
|
|
|
2018-07-25 23:21:52 -04:00
|
|
|
[nil, :crypt, :bcrypt].each do |hash_algo|
|
|
|
|
begin
|
|
|
|
case hash_algo
|
|
|
|
when :crypt
|
|
|
|
# require 'string/crypt'
|
|
|
|
when :bcrypt
|
|
|
|
require 'bcrypt'
|
|
|
|
end
|
|
|
|
rescue LoadError
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
define_method(:"test_basic_auth_htpasswd_#{hash_algo}") do
|
|
|
|
log_tester = lambda {|log, access_log|
|
|
|
|
log.reject! {|line| /\A\s*\z/ =~ line }
|
|
|
|
pats = [
|
|
|
|
/ERROR Basic WEBrick's realm: webrick: password unmatch\./,
|
|
|
|
/ERROR WEBrick::HTTPStatus::Unauthorized/
|
|
|
|
]
|
|
|
|
pats.each {|pat|
|
|
|
|
assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}")
|
|
|
|
log.reject! {|line| pat =~ line }
|
|
|
|
}
|
|
|
|
assert_equal([], log)
|
|
|
|
}
|
|
|
|
TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log|
|
|
|
|
realm = "WEBrick's realm"
|
|
|
|
path = "/basic_auth2"
|
|
|
|
|
|
|
|
Tempfile.create("test_webrick_auth") {|tmpfile|
|
|
|
|
tmpfile.close
|
|
|
|
tmp_pass = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path, password_hash: hash_algo)
|
|
|
|
tmp_pass.set_passwd(realm, "webrick", "supersecretpassword")
|
|
|
|
tmp_pass.set_passwd(realm, "foo", "supersecretpassword")
|
|
|
|
tmp_pass.flush
|
2017-09-14 07:16:23 -04:00
|
|
|
|
2018-07-25 23:21:52 -04:00
|
|
|
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path, password_hash: hash_algo)
|
|
|
|
users = []
|
|
|
|
htpasswd.each{|user, pass| users << user }
|
|
|
|
assert_equal(2, users.size, log.call)
|
|
|
|
assert(users.member?("webrick"), log.call)
|
|
|
|
assert(users.member?("foo"), log.call)
|
2017-09-14 07:16:23 -04:00
|
|
|
|
2018-07-25 23:21:52 -04:00
|
|
|
server.mount_proc(path){|req, res|
|
|
|
|
auth = WEBrick::HTTPAuth::BasicAuth.new(
|
|
|
|
:Realm => realm, :UserDB => htpasswd,
|
|
|
|
:Logger => server.logger
|
|
|
|
)
|
|
|
|
auth.authenticate(req, res)
|
|
|
|
res.body = "hoge"
|
|
|
|
}
|
|
|
|
http = Net::HTTP.new(addr, port)
|
|
|
|
g = Net::HTTP::Get.new(path)
|
|
|
|
g.basic_auth("webrick", "supersecretpassword")
|
|
|
|
http.request(g){|res| assert_equal("hoge", res.body, log.call)}
|
|
|
|
g.basic_auth("webrick", "not super")
|
|
|
|
http.request(g){|res| assert_not_equal("hoge", res.body, log.call)}
|
2017-09-14 07:16:23 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-25 23:21:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
define_method(:"test_basic_auth_bad_username_htpasswd_#{hash_algo}") do
|
|
|
|
log_tester = lambda {|log, access_log|
|
|
|
|
assert_equal(2, log.length)
|
|
|
|
assert_match(/ERROR Basic WEBrick's realm: foo\\ebar: the user is not allowed./, log[0])
|
|
|
|
assert_match(/ERROR WEBrick::HTTPStatus::Unauthorized/, log[1])
|
|
|
|
}
|
|
|
|
TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log|
|
|
|
|
realm = "WEBrick's realm"
|
|
|
|
path = "/basic_auth"
|
|
|
|
|
|
|
|
Tempfile.create("test_webrick_auth") {|tmpfile|
|
|
|
|
tmpfile.close
|
|
|
|
tmp_pass = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path, password_hash: hash_algo)
|
|
|
|
tmp_pass.set_passwd(realm, "webrick", "supersecretpassword")
|
|
|
|
tmp_pass.set_passwd(realm, "foo", "supersecretpassword")
|
|
|
|
tmp_pass.flush
|
|
|
|
|
|
|
|
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path, password_hash: hash_algo)
|
|
|
|
users = []
|
|
|
|
htpasswd.each{|user, pass| users << user }
|
|
|
|
server.mount_proc(path){|req, res|
|
|
|
|
auth = WEBrick::HTTPAuth::BasicAuth.new(
|
|
|
|
:Realm => realm, :UserDB => htpasswd,
|
|
|
|
:Logger => server.logger
|
|
|
|
)
|
|
|
|
auth.authenticate(req, res)
|
|
|
|
res.body = "hoge"
|
|
|
|
}
|
|
|
|
http = Net::HTTP.new(addr, port)
|
|
|
|
g = Net::HTTP::Get.new(path)
|
|
|
|
g.basic_auth("foo\ebar", "passwd")
|
|
|
|
http.request(g){|res| assert_not_equal("hoge", res.body, log.call) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2017-09-14 07:16:23 -04:00
|
|
|
end
|
|
|
|
|
2011-07-05 01:06:05 -04:00
|
|
|
DIGESTRES_ = /
|
2013-04-02 02:28:13 -04:00
|
|
|
([a-zA-Z\-]+)
|
|
|
|
[ \t]*(?:\r\n[ \t]*)*
|
2011-07-05 01:06:05 -04:00
|
|
|
=
|
2013-04-02 02:28:13 -04:00
|
|
|
[ \t]*(?:\r\n[ \t]*)*
|
2011-07-05 01:06:05 -04:00
|
|
|
(?:
|
|
|
|
"((?:[^"]+|\\[\x00-\x7F])*)" |
|
|
|
|
([!\#$%&'*+\-.0-9A-Z^_`a-z|~]+)
|
|
|
|
)/x
|
|
|
|
|
|
|
|
def test_digest_auth
|
2014-11-09 09:01:20 -05:00
|
|
|
log_tester = lambda {|log, access_log|
|
|
|
|
log.reject! {|line| /\A\s*\z/ =~ line }
|
|
|
|
pats = [
|
|
|
|
/ERROR Digest WEBrick's realm: no credentials in the request\./,
|
|
|
|
/ERROR WEBrick::HTTPStatus::Unauthorized/,
|
|
|
|
/ERROR Digest WEBrick's realm: webrick: digest unmatch\./
|
|
|
|
]
|
|
|
|
pats.each {|pat|
|
|
|
|
assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}")
|
|
|
|
log.reject! {|line| pat =~ line }
|
|
|
|
}
|
|
|
|
assert_equal([], log)
|
|
|
|
}
|
|
|
|
TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log|
|
2011-07-05 01:06:05 -04:00
|
|
|
realm = "WEBrick's realm"
|
|
|
|
path = "/digest_auth"
|
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("test_webrick_auth") {|tmpfile|
|
|
|
|
tmpfile.close
|
|
|
|
tmp_pass = WEBrick::HTTPAuth::Htdigest.new(tmpfile.path)
|
|
|
|
tmp_pass.set_passwd(realm, "webrick", "supersecretpassword")
|
|
|
|
tmp_pass.set_passwd(realm, "foo", "supersecretpassword")
|
|
|
|
tmp_pass.flush
|
|
|
|
|
|
|
|
htdigest = WEBrick::HTTPAuth::Htdigest.new(tmpfile.path)
|
|
|
|
users = []
|
|
|
|
htdigest.each{|user, pass| users << user }
|
|
|
|
assert_equal(2, users.size, log.call)
|
|
|
|
assert(users.member?("webrick"), log.call)
|
|
|
|
assert(users.member?("foo"), log.call)
|
|
|
|
|
|
|
|
auth = WEBrick::HTTPAuth::DigestAuth.new(
|
|
|
|
:Realm => realm, :UserDB => htdigest,
|
|
|
|
:Algorithm => 'MD5',
|
|
|
|
:Logger => server.logger
|
|
|
|
)
|
|
|
|
server.mount_proc(path){|req, res|
|
|
|
|
auth.authenticate(req, res)
|
|
|
|
res.body = "hoge"
|
|
|
|
}
|
2011-07-05 01:06:05 -04:00
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Net::HTTP.start(addr, port) do |http|
|
|
|
|
g = Net::HTTP::Get.new(path)
|
|
|
|
params = {}
|
|
|
|
http.request(g) do |res|
|
|
|
|
assert_equal('401', res.code, log.call)
|
|
|
|
res["www-authenticate"].scan(DIGESTRES_) do |key, quoted, token|
|
|
|
|
params[key.downcase] = token || quoted.delete('\\')
|
|
|
|
end
|
|
|
|
params['uri'] = "http://#{addr}:#{port}#{path}"
|
2011-07-05 01:06:05 -04:00
|
|
|
end
|
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
g['Authorization'] = credentials_for_request('webrick', "supersecretpassword", params)
|
|
|
|
http.request(g){|res| assert_equal("hoge", res.body, log.call)}
|
2011-07-05 01:06:05 -04:00
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
params['algorithm'].downcase! #4936
|
|
|
|
g['Authorization'] = credentials_for_request('webrick', "supersecretpassword", params)
|
|
|
|
http.request(g){|res| assert_equal("hoge", res.body, log.call)}
|
2011-07-05 01:06:05 -04:00
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
g['Authorization'] = credentials_for_request('webrick', "not super", params)
|
|
|
|
http.request(g){|res| assert_not_equal("hoge", res.body, log.call)}
|
|
|
|
end
|
|
|
|
}
|
2011-07-05 01:06:05 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-03-28 04:06:44 -04:00
|
|
|
def test_digest_auth_int
|
|
|
|
log_tester = lambda {|log, access_log|
|
|
|
|
log.reject! {|line| /\A\s*\z/ =~ line }
|
|
|
|
pats = [
|
|
|
|
/ERROR Digest wb auth-int realm: no credentials in the request\./,
|
|
|
|
/ERROR WEBrick::HTTPStatus::Unauthorized/,
|
|
|
|
/ERROR Digest wb auth-int realm: foo: digest unmatch\./
|
|
|
|
]
|
|
|
|
pats.each {|pat|
|
|
|
|
assert(!log.grep(pat).empty?, "webrick log doesn't have expected error: #{pat.inspect}")
|
|
|
|
log.reject! {|line| pat =~ line }
|
|
|
|
}
|
|
|
|
assert_equal([], log)
|
2018-04-03 07:27:00 -04:00
|
|
|
}
|
2018-03-28 04:06:44 -04:00
|
|
|
TestWEBrick.start_httpserver({}, log_tester) {|server, addr, port, log|
|
|
|
|
realm = "wb auth-int realm"
|
|
|
|
path = "/digest_auth_int"
|
|
|
|
|
|
|
|
Tempfile.create("test_webrick_auth_int") {|tmpfile|
|
|
|
|
tmpfile.close
|
|
|
|
tmp_pass = WEBrick::HTTPAuth::Htdigest.new(tmpfile.path)
|
|
|
|
tmp_pass.set_passwd(realm, "foo", "Hunter2")
|
|
|
|
tmp_pass.flush
|
|
|
|
|
|
|
|
htdigest = WEBrick::HTTPAuth::Htdigest.new(tmpfile.path)
|
|
|
|
users = []
|
|
|
|
htdigest.each{|user, pass| users << user }
|
|
|
|
assert_equal %w(foo), users
|
|
|
|
|
|
|
|
auth = WEBrick::HTTPAuth::DigestAuth.new(
|
|
|
|
:Realm => realm, :UserDB => htdigest,
|
|
|
|
:Algorithm => 'MD5',
|
|
|
|
:Logger => server.logger,
|
|
|
|
:Qop => %w(auth-int),
|
|
|
|
)
|
|
|
|
server.mount_proc(path){|req, res|
|
|
|
|
auth.authenticate(req, res)
|
|
|
|
res.body = "bbb"
|
|
|
|
}
|
|
|
|
Net::HTTP.start(addr, port) do |http|
|
|
|
|
post = Net::HTTP::Post.new(path)
|
|
|
|
params = {}
|
|
|
|
data = 'hello=world'
|
|
|
|
body = StringIO.new(data)
|
|
|
|
post.content_length = data.bytesize
|
|
|
|
post['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
|
|
post.body_stream = body
|
|
|
|
|
|
|
|
http.request(post) do |res|
|
|
|
|
assert_equal('401', res.code, log.call)
|
|
|
|
res["www-authenticate"].scan(DIGESTRES_) do |key, quoted, token|
|
|
|
|
params[key.downcase] = token || quoted.delete('\\')
|
|
|
|
end
|
|
|
|
params['uri'] = "http://#{addr}:#{port}#{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
body.rewind
|
|
|
|
cred = credentials_for_request('foo', 'Hunter3', params, body)
|
|
|
|
post['Authorization'] = cred
|
|
|
|
post.body_stream = body
|
|
|
|
http.request(post){|res|
|
|
|
|
assert_equal('401', res.code, log.call)
|
|
|
|
assert_not_equal("bbb", res.body, log.call)
|
|
|
|
}
|
|
|
|
|
|
|
|
body.rewind
|
|
|
|
cred = credentials_for_request('foo', 'Hunter2', params, body)
|
|
|
|
post['Authorization'] = cred
|
|
|
|
post.body_stream = body
|
|
|
|
http.request(post){|res| assert_equal("bbb", res.body, log.call)}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-07-05 01:06:05 -04:00
|
|
|
private
|
2018-03-28 04:06:44 -04:00
|
|
|
def credentials_for_request(user, password, params, body = nil)
|
2011-07-05 01:06:05 -04:00
|
|
|
cnonce = "hoge"
|
|
|
|
nonce_count = 1
|
|
|
|
ha1 = "#{user}:#{params['realm']}:#{password}"
|
2018-03-28 04:06:44 -04:00
|
|
|
if body
|
|
|
|
dig = Digest::MD5.new
|
|
|
|
while buf = body.read(16384)
|
|
|
|
dig.update(buf)
|
|
|
|
end
|
|
|
|
body.rewind
|
|
|
|
ha2 = "POST:#{params['uri']}:#{dig.hexdigest}"
|
|
|
|
else
|
|
|
|
ha2 = "GET:#{params['uri']}"
|
|
|
|
end
|
|
|
|
|
2011-07-05 01:06:05 -04:00
|
|
|
request_digest =
|
|
|
|
"#{Digest::MD5.hexdigest(ha1)}:" \
|
|
|
|
"#{params['nonce']}:#{'%08x' % nonce_count}:#{cnonce}:#{params['qop']}:" \
|
|
|
|
"#{Digest::MD5.hexdigest(ha2)}"
|
2011-07-05 06:09:04 -04:00
|
|
|
"Digest username=\"#{user}\"" \
|
2011-07-05 01:06:05 -04:00
|
|
|
", realm=\"#{params['realm']}\"" \
|
|
|
|
", nonce=\"#{params['nonce']}\"" \
|
|
|
|
", uri=\"#{params['uri']}\"" \
|
|
|
|
", qop=#{params['qop']}" \
|
|
|
|
", nc=#{'%08x' % nonce_count}" \
|
|
|
|
", cnonce=\"#{cnonce}\"" \
|
|
|
|
", response=\"#{Digest::MD5.hexdigest(request_digest)}\"" \
|
|
|
|
", opaque=\"#{params['opaque']}\"" \
|
|
|
|
", algorithm=#{params['algorithm']}"
|
|
|
|
end
|
2005-01-02 17:31:13 -05:00
|
|
|
end
|