Get a Pentest and security assessment of your IT network.

Cyber Security

Secure Basic Auth

TL;DR

Basic Authentication is simple but insecure on its own. Always use it over HTTPS (SSL/TLS) and consider stronger authentication methods like API keys, OAuth 2.0, or multi-factor authentication for better cyber security.

1. Understand the Risks of Basic Auth

Basic Authentication sends usernames and passwords encoded in Base64 with every request. Base64 is easily decoded, meaning anyone intercepting the traffic can read your credentials. This is why it’s crucial to use HTTPS.

2. Always Use HTTPS (SSL/TLS)

HTTPS encrypts all communication between the client and server, protecting usernames and passwords from eavesdropping. If you don’t have an SSL certificate, get one! Most hosting providers offer free options (e.g., Let’s Encrypt).

3. Configure Your Web Server

Here’s how to enable Basic Auth on common web servers:

Apache

# .htaccess file example
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /path/to/.htpasswd
Require valid-user

Create the .htpasswd file using a tool like htpasswd`:

htpasswd -c /path/to/.htpasswd username

(The `-c` flag creates a new file; omit it to add users to an existing one.)

Nginx

# nginx.conf example (inside the server block)
auth_basic "Restricted Area";
auth_basic_user_file /path/to/.htpasswd;

Create the .htpasswd file using a tool like htpasswd`:

htpasswd -c /path/to/.htpasswd username

Node.js (Express)

const basicAuth = require('basic-auth');
const express = require('express');
const app = express();

app.get('/protected', (req, res) => {
  const user = basicAuth(req);

  if (!user || user.name !== 'username' || user.pass !== 'password') {
    res.setHeader('WWW-Authenticate', 'Basic realm="Restricted Area"');
    return res.status(401).send('Authentication required.');
  }

  res.send('Access granted!');
});

app.listen(3000, () => console.log('Server listening on port 3000'));

4. Limit Access to Specific Areas

Don't protect your entire website with Basic Auth. Only use it for sensitive administration panels or areas that don’t require public access.

5. Consider Stronger Authentication Methods

  • API Keys: Suitable for machine-to-machine communication.
  • OAuth 2.0: Allows users to grant limited access to their data without sharing passwords (e.g., logging in with Google).
  • Multi-Factor Authentication (MFA): Adds an extra layer of security by requiring a second verification method (e.g., a code from a mobile app).

6. Regularly Review and Update

Periodically review your Basic Auth configuration to ensure it's still secure. Change passwords regularly, especially for administrative accounts.

7. Logging & Monitoring

Enable logging on your web server to track authentication attempts (successful and failed). Monitor these logs for suspicious activity. This is a key part of cyber security best practice.

Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation