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"
|
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
|
|
|
|
|
|
|
|
def test_basic_auth2
|
2014-11-09 09:01:20 -05:00
|
|
|
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|
|
2005-01-02 17:31:13 -05:00
|
|
|
realm = "WEBrick's realm"
|
|
|
|
path = "/basic_auth2"
|
|
|
|
|
* 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::Htpasswd.new(tmpfile.path)
|
|
|
|
tmp_pass.set_passwd(realm, "webrick", "supersecretpassword")
|
|
|
|
tmp_pass.set_passwd(realm, "foo", "supersecretpassword")
|
|
|
|
tmp_pass.flush
|
|
|
|
|
|
|
|
htpasswd = WEBrick::HTTPAuth::Htpasswd.new(tmpfile.path)
|
|
|
|
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)
|
|
|
|
|
|
|
|
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)}
|
2005-01-02 17:31:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_basic_auth3
|
* 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
|
|
|
}
|
|
|
|
|
* 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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
private
|
|
|
|
def credentials_for_request(user, password, params)
|
|
|
|
cnonce = "hoge"
|
|
|
|
nonce_count = 1
|
|
|
|
ha1 = "#{user}:#{params['realm']}:#{password}"
|
|
|
|
ha2 = "GET:#{params['uri']}"
|
|
|
|
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
|