Class: WEBrick::HTTPAuth::BasicAuth

Inherits:
Object
  • Object
show all
Includes:
Authenticator
Defined in:
lib/webrick/httpauth/basicauth.rb

Constant Summary

AuthScheme =
"Basic"

Constants included from Authenticator

Authenticator::AuthException, Authenticator::RequestField, Authenticator::ResponseField, Authenticator::ResponseInfoField

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (BasicAuth) initialize(config, default = Config::BasicAuth)

A new instance of BasicAuth



28
29
30
31
# File 'lib/webrick/httpauth/basicauth.rb', line 28

def initialize(config, default=Config::BasicAuth)
  check_init(config)
  @config = default.dup.update(config)
end

Instance Attribute Details

- (Object) logger (readonly)

Returns the value of attribute logger



26
27
28
# File 'lib/webrick/httpauth/basicauth.rb', line 26

def logger
  @logger
end

- (Object) realm (readonly)

Returns the value of attribute realm



26
27
28
# File 'lib/webrick/httpauth/basicauth.rb', line 26

def realm
  @realm
end

- (Object) userdb (readonly)

Returns the value of attribute userdb



26
27
28
# File 'lib/webrick/httpauth/basicauth.rb', line 26

def userdb
  @userdb
end

Class Method Details

+ (Object) make_passwd(realm, user, pass)



21
22
23
24
# File 'lib/webrick/httpauth/basicauth.rb', line 21

def self.make_passwd(realm, user, pass)
  pass ||= ""
  pass.crypt(Utils::random_string(2))
end

Instance Method Details

- (Object) authenticate(req, res)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/webrick/httpauth/basicauth.rb', line 33

def authenticate(req, res)
  unless basic_credentials = check_scheme(req)
    challenge(req, res)
  end
  userid, password = basic_credentials.unpack("m*")[0].split(":", 2)
  password ||= ""
  if userid.empty?
    error("user id was not given.")
    challenge(req, res)
  end
  unless encpass = @userdb.get_passwd(@realm, userid, @reload_db)
    error("%s: the user is not allowed.", userid)
    challenge(req, res)
  end
  if password.crypt(encpass) != encpass
    error("%s: password unmatch.", userid)
    challenge(req, res)
  end
  info("%s: authentication succeeded.", userid)
  req.user = userid
end

- (Object) challenge(req, res)

Raises:

  • (@auth_exception)


55
56
57
58
# File 'lib/webrick/httpauth/basicauth.rb', line 55

def challenge(req, res)
  res[@response_field] = "#{@auth_scheme} realm=\"#{@realm}\""
  raise @auth_exception
end