The Basic-Auth plugin provides HTTP Basic Authentication for Rusty Beam, authenticating users against credentials stored in an HTML configuration file. It supports both plaintext and bcrypt-encrypted passwords.
The basic-auth plugin is typically the first authentication layer in your plugin pipeline. It validates user credentials from HTTP Basic Authentication headers and sets user metadata for downstream plugins like the authorization plugin.
The basic-auth plugin is configured as part of the plugin pipeline in your host configuration:
<li itemprop="plugin" itemscope itemtype="https://rustybeam.net/schema/Plugin">
<span itemprop="library">file://./plugins/basic-auth.so</span>
<meta itemprop="realm" content="My Protected Site">
<meta itemprop="authfile" content="file://./auth/users.html">
</li>
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
authfile |
String (URL) | Yes | - | Path to the HTML file containing user credentials. Supports file:// URLs. |
realm |
String | No | "Restricted Area" | Authentication realm displayed in browser authentication dialogs. |
Credentials are defined in an HTML file using the Credential schema. Each credential entry includes:
<!DOCTYPE html>
<html>
<head>
<title>Authentication Credentials</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Roles</th>
<th>Encryption</th>
</tr>
</thead>
<tbody>
<!-- Plaintext password example -->
<tr itemscope itemtype="https://rustybeam.net/schema/Credential">
<td itemprop="username">alice</td>
<td itemprop="password">secret123</td>
<td>
<ul>
<li itemprop="role">users</li>
<li itemprop="role">editors</li>
</ul>
</td>
<td itemprop="encryption">plaintext</td>
</tr>
<!-- Bcrypt password example -->
<tr itemscope itemtype="https://rustybeam.net/schema/Credential">
<td itemprop="username">bob</td>
<td itemprop="password">$2b$10$x5BXBKwhjo.VCl8A/ieIJuqJxASQzArnVaQFcqwJrh5yrJbfHtwTC</td>
<td>
<ul>
<li itemprop="role">administrators</li>
<li itemprop="role">users</li>
</ul>
</td>
<td itemprop="encryption">bcrypt</td>
</tr>
</tbody>
</table>
</body>
</html>
Authorization: Basic <credentials>
headerauthenticated_user
metadataWhen authentication succeeds, the plugin sets the following metadata for use by downstream plugins:
Key | Value | Description |
---|---|---|
authenticated_user |
Username | The authenticated user's username |
user_roles |
Comma-separated roles | User's assigned roles (e.g., "users,editors") |
Typical pipeline order:
1. basic-auth.so → Authenticates user
2. authorization.so → Checks permissions
3. selector-handler.so → Processes requests
4. file-handler.so → Serves files
5. access-log.so → Logs requests
By default, the basic-auth plugin requires authentication for all requests. To allow anonymous access to certain resources:
Simple but insecure. Only use for development or testing:
<td itemprop="password">mypassword</td>
<td itemprop="encryption">plaintext</td>
Recommended for production. Generate bcrypt hashes using:
# Using Python
python -c "import bcrypt; print(bcrypt.hashpw(b'mypassword', bcrypt.gensalt()).decode())"
# Using htpasswd (Apache tools)
htpasswd -bnBC 10 "" mypassword | tr -d ':\n'
# Using online tools (for testing only!)
# Various bcrypt generators are available online
Then use in configuration:
<td itemprop="password">$2b$10$x5BXBKwhjo.VCl8A/ieIJuqJxASQzArnVaQFcqwJrh5yrJbfHtwTC</td>
<td itemprop="encryption">bcrypt</td>
When accessing a protected resource, browsers display a dialog:
Browsers cache Basic Authentication credentials for the session. Users typically need to close all browser windows to "log out".
Issue | Cause | Solution |
---|---|---|
Always getting 401 | Incorrect credentials or auth file path | Check auth file path and user credentials |
Can't load auth file | File path or permissions issue | Verify file:// URL and file permissions |
Bcrypt validation fails | Incorrect hash or format | Regenerate hash and ensure proper format |
No authentication prompt | Plugin not in pipeline or wrong order | Check plugin configuration and order |
Authorization not working | Metadata not being set | Ensure basic-auth comes before authorization |
Run the server with -v
flag to see authentication details:
./rusty-beam -v config.html
This will show:
# Using -u flag
curl -u alice:secret123 http://localhost:3000/protected/
# Using Authorization header
curl -H "Authorization: Basic YWxpY2U6c2VjcmV0MTIz" http://localhost:3000/protected/
# Encoding credentials
echo -n "alice:secret123" | base64
# Output: YWxpY2U6c2VjcmV0MTIz
// Using headers
fetch('http://localhost:3000/protected/', {
headers: {
'Authorization': 'Basic ' + btoa('alice:secret123')
}
})
// Using credentials in URL (not recommended)
fetch('http://alice:secret123@localhost:3000/protected/')
The authorization plugin depends on the authenticated_user
metadata set by basic-auth. Always place basic-auth before authorization in the pipeline.
The access-log plugin can log authenticated usernames. It reads the authenticated_user
metadata set by basic-auth.
Use the directory plugin to create different authentication requirements for different paths. For example, public areas without basic-auth and private areas with it.