DEV Community

Cover image for Understanding Basic Authentication
Ryoichi Homma
Ryoichi Homma

Posted on • Edited on

Understanding Basic Authentication

I recently had an opportunity to use Basic Authentication and Digest Authentication, so I'd like to share what I learned about these two authentication methods. This article will focus on Basic Authentication, especially how it works and how to implement it.

Basic Authentication

Basic Authentication is a simple authentication mechanism where the client sends credentials like username and password encoded in Base64 with each HTTP request. While Base64 encoding is not encryption, it allows the credentials to be included in a standard format. Since it lacks encryption, it is recommended to use HTTPS to secure credentials in transit.

Implementation

Follow these steps to implement Basic Authentication on the Apache server:

  • Install Apache using yum:
sudo yum install httpd -y
Enter fullscreen mode Exit fullscreen mode
  • Create a protected directory:
sudo mkdir -p /var/www/html/basic
Enter fullscreen mode Exit fullscreen mode

/basic can be anything.

  • Create the .htpasswd file:
sudo htpasswd -c /etc/httpd/.htpasswd username
Enter fullscreen mode Exit fullscreen mode

You'll be asked to type and confirm the password.

  • Edit the Apache configuration file:
sudo vi /etc/httpd/conf/httpd.conf
Enter fullscreen mode Exit fullscreen mode

Add the following inside the <Directory "/var/www/html"> section (usually from 160 lines):

<Directory "var/www/html/basic">
    AuthType Basic
    AuthName "Basic Auth"
    AuthUserFile /etc/httpd/.htpasswd
    Require user username
</Directory>
Enter fullscreen mode Exit fullscreen mode
  • Create an index.html file in the protected directory:
sudo vi /var/www/html/basic/index.html
Enter fullscreen mode Exit fullscreen mode

Add something you want to display when the user is authorized:

You're successfully authorized
Enter fullscreen mode Exit fullscreen mode
  1. Press i to start typing in the INSERT MODE.
  2. To exit the INSERT MODE, press esc, type :wq, and press Enter.
  • Restart the Apache server:
sudo systemctl restart httpd
Enter fullscreen mode Exit fullscreen mode
  • Test the authentication using curl command:
curl -L -u username:password http://xx.xx.xx.xx/basic
Enter fullscreen mode Exit fullscreen mode

If you see You're successfully authorized, authentication is working correctly.

In the next article, Understanding Digest Authentication, I will explore more about Digest Authentication.

Reference

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs