It is very common to need to protect a part of a web site, which is an administration section or simply a private space. Many authentication solutions by login / password are given to us.
The principle of authentication by form is simple:

When navigating on the private space, it must be determine if the user is already logged on. The most common techniques are the use of cookies and sessions. Reminder:
In our case, it is preferable to identify the user with sessions in term of security (only the id forwards on the network) and practice (stocking of variables often used).
The advantage of the authentication by form resides in its administration facility: the list of authorized users can be stocked in a database, LDAP directory or in a file. However, it is important to note that it does not permit to protect html files, images and all documents accessible directly by URL.
The risks of using sessions at this stage are at two levels:
These problems can be solved by using HTTPS protocols, explained in the next chapter.
Concretely, an htaccess is an http server configuration file. It permits, among other things, to limit the rights of access to a site directory. This is useful to reserve an administration zone or to offer a member space.
It is useful to know that htaccess can be written directly in the Apache configuration file. All that is necessary is to put directives in a <Directory> with directory path to protect or a <Location> with the URL.
<Directory /usr/www/administrateur> ... </Directory>
Or :
<Location / > ... </ Location>
There are two authentication methods described in RFC 2617 of http protocol: basic and digest
The basic mechanism is usually used because of its compatibility and simplicity when installing.
HTTP/1.1 401 Authorization Required WWW-Authenticate: Basic realm="private Access"

Once seized, this information is sent to the server. The browser reiterates the preceding request with authorization header. The username and password are concatenated (separated by « : ») and encoded in base64. For example, for a login / password user:password, the request is as follow:
GET / HTTP/1.0 Authorization: Basic dXNlcjpwYXNzd29yZA==
Accreditations are then saved, thus avoiding re-seizing them for each request.
This method is very sensitive to any traffic listening because information is not crypted and sent in each request. Just like form authentication, a SSL transport solution is considerable.
AuthType Basic AuthName "private access" AuthUserFile /etc/httpd/conf/htpasswd Require valid-user
Users file can be generated with the htpasswd utility.
In the opposite of basic mode, this method offers a confidentiality service for accreditations, introducing a mechanism against replay and protecting the client against a spiteful server.
The mechanism is known under 'challenge/response' name: the server sends a challenge to the client, this one answers by a derived value of this challenge and by a secret that he chares with the server. The server makes sure that the client has the secret by calculating the response.
HTTP/1.1 401 Authorization Required WWW-Authenticate: Digest realm="private Access", nonce="vR54Tk==a965d986fa716e4d4592943b5762c73958f0328C", algorithm=MD5, domain="/", qop="auth"
For each request, the client will have to calculate his response with these parameters. Because the challenge is not renewed each time, this limits the non-replay.
It is useful to know that the server has no knowledge of the password. In the users file, the login is saved, realm and a hash of 'login:realm:password'.
We obtain a response like this:
GET / HTTP/1.1 Authorization: Digest username="user", realm="private Access", nonce="vR54Tk==a965d986fa716e4d4592943b5762c73958f0328c", uri="/", algorithm=MD5, response="411d4c35a806746ab353c67ab4e9cf5a", qop=auth, nc=00000001, cnonce="af08d67c532a3a01"
HTTP/1.1 200 OK Authentication-Info: rspauth="1d4d5a9d920fb22197471146c613e767", cnonce="be09d67c532a3a02", nc=00000001, qop=auth
rspauth allows a mutual authentication, proving that the server knows indeed the client's password.
It is important to note that the non-replay is placed at the level of 'http session' (time during which the nonce is usable) and not at the level of http request. The time to live is defined in the configuration of the module mod_auth_digest with the parameter AuthDigestNonceLifeTime. Beyond this duration, the nonce will be renewed and the client will have to authenticate once more. We can thus realize that this module offers a real confidentiality system of accreditations and not truly a non-replay service.
Besides its many advantages, the Digest module is also known for its incompatibility in between Apache and Internet Explorer and so between IIS and all others browsers (except MSIE of course). This incompatibility is due to a bad interpretation of the RFC 2616 by Microsoft.
The problem is located at the calculation level of the response. As we saw, the client generates his response with the URI (among other things). The RFC defines that the QUERY_STRING (string located after '?') must be taken in account, but MSIE and IIS don't do it. Consequently when the server re-calculates the client response, this one does not match and the client is rejected with a 404 Bad Request error.
The solution to this problem is not to pass variables by GET but in the request: by POST.
AuthType Digest AuthName private Access AuthDigestFile /etc/httpd/conf/htpasswd Require valid-user
The users file can be generated with the htdigest tool.
Caution: in Digest, AuthName allows to identify the user in addition of his login checking. This allows gathering users in a section. It must match with the realm in the users file.