Cookie Authentication in Apache
Solution 1:
Sure. I do the same thing.
When a user logs in, I give them a cookie and create a token in /t/
tokenid, and put it in a cookie: S=
tokenid;PATH=/
Then, I can use RewriteCond
to check for the file's existence:
RewriteEngine on
# check for no cookie being set
RewriteCond %{HTTP:Cookie} !S=([a-zA-Z0-9]+)
RewriteRule ^/*protected/ /login.html [L,R]
# check for an invalid cookie being set
RewriteCond %{HTTP:Cookie} S=([a-zA-Z0-9]+)
RewriteCond /t/%1 !-f
RewriteRule ^/*protected/ /login.html [L,R]
Finally, a garbage collector runs periodically and deletes old tokens:
find /t -type f \! -atime +1 -delete
To make the atime automatically update, I have /t
mounted without noatime
, and I have it web-accessible (but not indexed) and part of the stylesheet references /loggedin.txt
which is rewritten as:
RewriteCond %{HTTP:Cookie} S=([a-zA-Z0-9]+)
RewriteRule ^/*loggedin\.txt$ /t/%1 [L]