[TIPS] WEBrick proxy server with Digest Auth Post date: 2011-07-16T21:30:00+09:00Modified at: 2011-07-16T21:30:11+09:00Text by: cat_in_136Tags: Rubyチラシの裏 Github source: _posts/2011-07-16-tips-webrick-proxy-server-with-digest.html (History) ↓Comments 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