# ri > WEBrick::HTTPAuth::BasicAuth

---
type: CommandReference
command: WEBrick::HTTPAuth::BasicAuth
mode: ri
section: 
source: ri
---

## Quick Reference

- `WEBrick::HTTPAuth::BasicAuth.new(config)` — create a new BasicAuth instance with `:Realm` and `:UserDB` keys in the config hash.
- `basic_auth.authenticate(req, res)` — authenticate the request; returns true on success.
- `basic_auth.challenge(req, res)` — send a WWW-Authenticate challenge header.
- `basic_auth.logger` — returns the Logger object.
- `basic_auth.realm` — returns the authentication realm string.
- `basic_auth.userdb` — returns the user database (e.g., `Htpasswd` instance).

## Name

Basic Authentication for WEBrick — add HTTP Basic Authentication to a WEBrick servlet.

## Synopsis

ruby
require 'webrick'
require 'webrick/httpauth/basicauth'
require 'webrick/httpauth/htpasswd'

config = { :Realm => 'BasicAuth example realm' }
htpasswd = WEBrick::HTTPAuth::Htpasswd.new('my_password_file', password_hash: :bcrypt)
htpasswd.set_passwd(config[:Realm], 'username', 'password')
htpasswd.flush
config[:UserDB] = htpasswd

basic_auth = WEBrick::HTTPAuth::BasicAuth.new(config)
## Options (Methods & Attributes)

### Class Methods

- `make_passwd(realm, user, pass)` — generate a password hash compatible with the user database.

### Instance Methods

- `authenticate(req, res)` — verify the credentials in the request; returns `true` on success, otherwise raises an exception.
- `challenge(req, res)` — add a `WWW-Authenticate` header to the response, prompting the client for credentials.
- `logger` — returns the Logger instance (read-only).
- `realm` — returns the authentication realm (read-only).
- `userdb` — returns the user database object (read-only).

### Attributes

- `attr_reader :logger`
- `attr_reader :realm`
- `attr_reader :userdb`

### Includes

- `Authenticator` — module providing common authentication utilities.

## Examples

ruby
# Setup with bcrypt password file
config = { :Realm => 'Example Realm' }
htpasswd = WEBrick::HTTPAuth::Htpasswd.new('users.htpasswd', password_hash: :bcrypt)
htpasswd.set_passwd(config[:Realm], 'alice', 'secret')
htpasswd.flush
config[:UserDB] = htpasswd
auth = WEBrick::HTTPAuth::BasicAuth.new(config)

# Use in a servlet
class MyServlet < WEBrick::HTTPServlet::AbstractServlet
  def do_GET(req, res)
    @auth.authenticate(req, res)
    res.body = 'Hello, authenticated user!'
  end
end
## See Also

- [WEBrick::HTTPAuth::Htpasswd](http://localhost/phpMan.php/perldoc/WEBrick%3A%3AHTTPAuth%3A%3AHtpasswd/markdown)
- [WEBrick::HTTPAuth::DigestAuth](http://localhost/phpMan.php/perldoc/WEBrick%3A%3AHTTPAuth%3A%3ADigestAuth/markdown)
- [WEBrick::HTTPServlet::AbstractServlet](http://localhost/phpMan.php/perldoc/WEBrick%3A%3AHTTPServlet%3A%3AAbstractServlet/markdown)