Adding authentication to your website
Custom Authentication
Apache is a very extensible system – so flexible that you don't even have to use it for http. By writing Apache modules, you can extend Apache. Modules can be written in C, like the modules discussed in this article, but the C language is time consuming and cumbersome for web admins who aren't experienced software developers.
Thanks to modules such as mod_perl and mod_python, you can build your own custom Apache modules in more agile languages.
Apache provides great flexibility when dealing with access to content, but one thing it doesn't take into account is the time of day. As an example of how to customize Apache authentication, assume your company has a first and second shift and you want to ensure that first-shift employees can only access pages or applications during their work hours. First, enable mod_perl with:
LoadModule perl_module modules/mod_perl.so
mod_perl lets you override a particular phase of the Apache life cycle by writing a Perl module. In this case, the module compares the username and password but also makes sure the employee is authorized to work at the current time (Listing 4). Also, reject everyone who tries to work on Saturdays and Sundays.
Listing 4
No Weekend Work!
This code can be placed anywhere in Perl's @INC path in a file named ByShiftAuth.pm. If you want to put it in another location, add the following to your Apache configuration:
<Perl> use lib qw( /path/to/directory ); </Perl>
After loading in the necessary mod_perl modules, DBI, and the MD5 libraries, Listing 4 defines the handler.
First, check to see whether this is the initial request or some sort of internal redirect. If it's not the initial request, the system has already authenticated this request and can just pass along the information.
Next, check to make sure the current day isn't a weekend, and if it is, simply reject everyone. The script then obtains the username and password from the user.
If both a username and password aren't available, the script bails out and tells the browser to re-prompt with the AUTH_REQUIRED return value.
Next, the script must connect to the database and look for the user's password. The query assumes the start and end times of the user's shift are in the users table.
If the user doesn't exist or is attempting to login outside of their shift hours, this query won't return any data. In production, you would probably want to tell the user that the reason they can't log in is that it isn't their shift so that they don't keep retrying.
Finally, compare the MD5 digest of the password with the password retrieved from the database.
If the test fails, the user is prompted again. If all goes well, return the OK constant, which tells Apache that the user is authenticated.
Now that you see how the code works, the next step is to tell Apache to use it, which you can do by overriding the authentication phase with the custom code:
<Location /admin> AuthType Basic AuthName "Admin Pages" PerlAuthenHandler ByShiftAuth Require valid-user </Location>
Conclusion
These short examples show some of the ways you can protect your web pages using more advanced Apache configurations. For more options, I encourage you to check the Apache website (http://httpd.apache.org).
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.