1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
require 'webrick'
require 'tempfile'
class AuthServlet < WEBrick::HTTPServlet::AbstractServlet
@instance = nil
def self.get_instance server, *options
@instance ||= new(server, *options)
end
def initialize server
super server
config = {}
config[:Realm] = 'net-http-digest_auth'
config[:UseOpaque] = false
config[:AutoReloadUserDB] = false
passwd_file = Tempfile.new 'net-http-digest_auth'
passwd_file.close
htpasswd = WEBrick::HTTPAuth::Htpasswd.new passwd_file.path
htpasswd.auth_type = WEBrick::HTTPAuth::DigestAuth
htpasswd.set_passwd config[:Realm], 'username', 'password'
htpasswd.flush
config[:UserDB] = htpasswd
@digest_auth = WEBrick::HTTPAuth::DigestAuth.new config
end
def do_GET req, res
@digest_auth.authenticate req, res
res.body = 'worked!'
end
end
s = WEBrick::HTTPServer.new :Port => 8000
s.mount '/', AuthServlet
trap 'INT' do s.shutdown end
s.start
|