Search code examples
rubyauthenticationwebrick

Ruby Webrick HTTP Authentication


How can I do the same authentication stuff in this page using a subclass like this:

        class Configuration < HTTPServlet::AbstractServlet
        def do_GET (request, response)
            SOMETHING....
        end
        end

    server = HTTPServer.new(:Port => 666)
    server.mount "/conf", Configuration
    trap "INT" do server.shutdown end
    server.start

Solution

  • Seems to work OK for me if you do it in pretty much the same style e.g.

    class Configuration < HTTPServlet::AbstractServlet
        def do_GET(req, res)
            HTTPAuth.basic_auth(req, res, "My Realm") {|user, pass|
              # block should return true if
              # authentication token is valid
              user == 'user' && pass == 'topsecret'
            }
            res.body = 
              "Authenticated OK\n"
        end
    end
    

    What is the problem you're having?