2 # httpauth/basicauth.rb -- HTTP basic access authentication
4 # Author: IPR -- Internet Programming with Ruby -- writers
5 # Copyright (c) 2003 Internet Programming with Ruby writers. All rights
8 # $IPR: basicauth.rb,v 1.5 2003/02/20 07:15:47 gotoyuzo Exp $
10 require 'webrick/config'
11 require 'webrick/httpstatus'
12 require 'webrick/httpauth/authenticator'
21 def self.make_passwd(realm, user, pass)
23 pass.crypt(Utils::random_string(2))
26 attr_reader :realm, :userdb, :logger
28 def initialize(config, default=Config::BasicAuth)
30 @config = default.dup.update(config)
33 def authenticate(req, res)
34 unless basic_credentials = check_scheme(req)
37 userid, password = basic_credentials.unpack("m*")[0].split(":", 2)
40 error("user id was not given.")
43 unless encpass = @userdb.get_passwd(@realm, userid, @reload_db)
44 error("%s: the user is not allowed.", userid)
47 if password.crypt(encpass) != encpass
48 error("%s: password unmatch.", userid)
51 info("%s: authentication succeeded.", userid)
55 def challenge(req, res)
56 res[@response_field] = "#{@auth_scheme} realm=\"#{@realm}\""
61 class ProxyBasicAuth < BasicAuth
62 include ProxyAuthenticator