require 'webrick/config' require 'webrick/httpstatus' require 'webrick/httpauth/authenticator' require 'base64' module WEBrick module HTTPAuth class NotAuthentication include WEBrick::HTTPAuth::Authenticator AuthScheme = "Basic" def initialize( realm = "editing" ) config = { :UserDB => "nodb" , :Realm => realm } check_init(config) @config = Config::BasicAuth.dup.update(config) end def authenticate(req, res) unless basic_credentials = check_scheme(req) challenge(req, res) end userid, password = decode64(basic_credentials).split(":", 2) if userid.empty? error("user id was not given.") challenge(req, res) end info("%s: authentication succeeded.", userid) req.user = userid end def challenge(req, res) res[@response_field] = "#{@auth_scheme} realm=\"#{@realm}\"" raise @auth_exception end end class OnePasswordAuthentication include Authenticator AuthScheme = "Basic" attr_reader :realm, :userdb, :logger def initialize( password = "", realm = "editing" ) config = { :UserDB => "nodb" , :Realm => realm } check_init(config) @config = Config::BasicAuth.dup.update(config) @password = password end def authenticate(req, res) unless basic_credentials = check_scheme(req) challenge(req, res) end userid, password = decode64(basic_credentials).split(":", 2) password ||= "" if userid.empty? error("user id was not given.") challenge(req, res) end if password != @password error("%s: password unmatch.", userid) challenge(req, res) end info("%s: authentication succeeded.", userid) req.user = userid end def challenge(req, res) res[@response_field] = "#{@auth_scheme} realm=\"#{@realm}\"" raise @auth_exception end end end end