[TIPS] WEBrick proxy server with Digest Auth
Well-known WEBrick proxy server with BASIC auth is as following:
auth_proc = Proc.new do |req,res|
WEBrick::HTTPAuth.proxy_basic_auth(req,res,'proxy') do |user,pass|
user == "user" && pass == "pass"
end
end
server = WEBrick::HTTPProxyServer.new({
:Port => 8080,
:ProxyAuthProcess => auth_proc
})
Signal.trap("INT") do server.shutdown end
server.start
But There is not a DIGEST version of proxy_basic_auth()
method...
Solution: Use WEBrick::HTTPAuth::ProxyDigestAuth
:
require "webrick"
require "webrick/httpproxy"
require "webrick/httpauth/digestauth"
require "webrick/httpauth/basicauth"
require "webrick/httpauth/userdb"
userdb = Hash.new
userdb.extend(WEBrick::HTTPAuth::UserDB)
userdb.auth_type = WEBrick::HTTPAuth::ProxyDigestAuth # or WEBrick::HTTPAuth::ProxyBasicAuth
userdb.set_passwd("realm", "user", "pass")
authenticator = userdb.auth_type.new({
:Realm => "realm",
:UserDB => userdb,
:Algorithm => "MD5",
})
auth_proc = Proc.new do |req,res|
authenticator.authenticate(req, res)
end
server = WEBrick::HTTPProxyServer.new({
:Port => 8080,
:ProxyAuthProcess => auth_proc
})
Signal.trap("INT") do server.shutdown end
server.start