File: webrick/httpauth/basicauth.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: WEBrick#14
  module: HTTPAuth#15
  class: BasicAuth#16
includes
  Authenticator ( WEBrick::HTTPAuth )
inherits from
  Object ( Builtin-Module )
has properties
constant: AuthScheme #19
class method: make_passwd / 3 #21
attribute: realm [R] #26
attribute: userdb [R] #26
attribute: logger [R] #26
method: initialize / 2 #28
method: authenticate / 2 #33
method: challenge / 2 #55
  class: ProxyBasicAuth#61
includes
  ProxyAuthenticator ( WEBrick::HTTPAuth )
inherits from
  BasicAuth ( WEBrick::HTTPAuth )

Class Hierarchy

Object ( Builtin-Module )
BasicAuth ( WEBrick::HTTPAuth ) — #16
  ProxyBasicAuth    #61

Code

   1  #
   2  # httpauth/basicauth.rb -- HTTP basic access authentication
   3  #
   4  # Author: IPR -- Internet Programming with Ruby -- writers
   5  # Copyright (c) 2003 Internet Programming with Ruby writers. All rights
   6  # reserved.
   7  #
   8  # $IPR: basicauth.rb,v 1.5 2003/02/20 07:15:47 gotoyuzo Exp $
   9 
  10  require 'webrick/config'
  11  require 'webrick/httpstatus'
  12  require 'webrick/httpauth/authenticator'
  13 
  14  module WEBrick
  15    module HTTPAuth
  16      class BasicAuth
  17        include Authenticator
  18 
  19        AuthScheme = "Basic"
  20 
  21        def self.make_passwd(realm, user, pass)
  22          pass ||= ""
  23          pass.crypt(Utils::random_string(2))
  24        end
  25 
  26        attr_reader :realm, :userdb, :logger
  27 
  28        def initialize(config, default=Config::BasicAuth)
  29          check_init(config)
  30          @config = default.dup.update(config)
  31        end
  32 
  33        def authenticate(req, res)
  34          unless basic_credentials = check_scheme(req)
  35            challenge(req, res)
  36          end
  37          userid, password = basic_credentials.unpack("m*")[0].split(":", 2) 
  38          password ||= ""
  39          if userid.empty?
  40            error("user id was not given.")
  41            challenge(req, res)
  42          end
  43          unless encpass = @userdb.get_passwd(@realm, userid, @reload_db)
  44            error("%s: the user is not allowed.", userid)
  45            challenge(req, res)
  46          end
  47          if password.crypt(encpass) != encpass
  48            error("%s: password unmatch.", userid)
  49            challenge(req, res)
  50          end
  51          info("%s: authentication succeeded.", userid)
  52          req.user = userid
  53        end
  54 
  55        def challenge(req, res)
  56          res[@response_field] = "#{@auth_scheme} realm=\"#{@realm}\""
  57          raise @auth_exception
  58        end
  59      end
  60 
  61      class ProxyBasicAuth < BasicAuth
  62        include ProxyAuthenticator
  63      end
  64    end
  65  end