I've been bringing myself up to speed on HTTP Basic Authentication.
I understand that this is fundamentally an insecure access mechanism (even when used over HTTPS, which it always should be) but I recognise that HTTP Basic Auth is not entirely without utility and I would like to be familiar with it, even if situations seldom arise in which I might deploy it.
My understanding so far:
After some reading, I understand that:
- a server may request authorisation for a resource by returning
401 (unauthorised) - a
WWW-Authenticateresponse header determines that the authentication to be used to access this resource will beBasic HTTP Basic Authenticationrequires either that:- a) the user submit username and password via a browser-generated console; or that
- b) once successfully submitted manually, the same username and password combination will be automatically submitted via an
Authorizationrequest header prepended to each HTTP request
So far, so good.
Issues to be aware of:
I also understand that there are some issues with HTTP Basic Auth which have evolved over time, like:
- some browsers no longer accept URL syntax such as
https://mylogin:mypassword@example.com/my-resource.html - where PHP is being run through CGI or FastCGI then the submitted Authorization Credentials will not be passed to
$_SERVER['HTTP_AUTHORIZATION']unless a hack is deployed - the most common recommendation being a URL rewrite via.htaccess mod_rewrite
and other issues which have persisted from the very beginning, like:
- it's a non-trivial problem to "log out" of HTTP Basic Auth since it was never intended or designed to have a log-out mechanism
The missing piece of the puzzle:
However, I'm still confused, because even where the user (or the Authorization request header) has submitted valid authentication credentials... how does the server know they are valid?
In every document I have come across discussing the mechanics of HTTP Basic Authentication the discussion stops short of the point at which the credentials are actually authenticated.
Question:
How are the submitted credentials actually authenticated?
Where is the server comparing the submitted credentials to... anything?
Bonus Question:
N.B. This is related to my main question immediately above because my use of .htaccess and queryString parameters to convey credentials (see below) renders deployment of HTTP Basic Auth entirely redundant - if I go down this route, I can convey credentials using .htaccess and queryString parameters alone and I don't need to deploy HTTP Basic Auth at all.
As a way to circumvent the CGI/FastCGI issue, I often see variations of these .htaccess lines cited:
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
or
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
or
SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
though my attempts to get any of these methods to populate PHP Environment Variables with the credentials have all proven unsuccessful.
Instead, I have deployed the following (using queryString parameters instead of Environment variables), successfully:
# WRITE HTTP BASIC AUTHENTICATION CREDENTIALS TO QUERY STRING
RewriteCond %{HTTP:Authorization} [NC]
RewriteCond %{QUERY_STRING} ^basicauth=login$ [NC]
RewriteRule ^my-document.php https://example.com/my-document.php?basicauth=login-submitted&credentials=%{HTTP:Authorization} [NC,L]
which appends the credentials as queryString parameters.
I am not unhappy with my own mod_rewrite solution above, but I am stumped that I cannot get Environment Variables working at all.
I'm idly wondering if there is something obvious I'm missing when it comes to the latter - like... might they be switched off in my PHP Configuration?
(And, if so, which entries would I need to check in PHPInfo to confirm that they were actually switched on and receptive to values transferred to them via mod_rewrite?)