Permalink
Browse files

Add auth_server for testing

  • Loading branch information...
1 parent 9fdf6f0 commit ae03253f659ce2d58a9ae2cf19f04e11b264e9dc @drbrain committed Sep 11, 2010
Showing with 48 additions and 0 deletions.
  1. +48 −0 sample/auth_server.rb
View
@@ -0,0 +1,48 @@
+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
+ config[:Algorithm] = 'MD5'
+
+ 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
+

0 comments on commit ae03253

Please sign in to comment.