Htpasswd
The htpasswd provider authenticates the users against an .htpasswd file: the application has a login form, and the user logs in with a username and a password. It is the simplest way in: it needs no database and no server, only a file. It does not use the libraries of the Keycloak provider. Its login form is protected with a CSRF token and a captcha, so it needs a CSRF token manager and a session: derafu/csrf and derafu/session, which a site that uses derafu/foundation already has.
Choose it with AUTH_PROVIDER=htpasswd (see Configuration). The login is /auth/login: the page, and the POST of the form. The variables are in Configuration.
AUTH_HTPASSWD_PATH=%kernel.project_dir%/var/.htpasswd
The file
A line of the file is username:hash. Create it with htpasswd of Apache, with bcrypt (-B):
htpasswd -B -c var/.htpasswd ana
htpasswd -B var/.htpasswd beto
- Only bcrypt. The hashes that start with
$2a$,$2b$or$2y$are the ones accepted. A line with any other hash (apr1, which is the default ofhtpasswd,{SHA},crypt, MD5) is ignored, as if the user was not in the file: that user can not log in. They are the formats that are weak or that the package does not implement. - The blank lines and the ones that start with
#are ignored. %kernel.project_dir%in the path is replaced by the directory of the project. Keep the file outside of the public directory of the web server.- The file is read every time it is needed, so a change in it is what the next login (or the next check of a session) sees.
- A file that does not exist, or that can not be read, is a
ConfigurationException.
The users
The .htpasswd file says who the user is: the user has an identity and no details. Its roles come from the .htgroup file; without it the user has no roles, and the application is for whoever is in the file: protect paths without roles (Route Protection) and every user that logs in gets in. The users are made by the UserFactoryInterface of the application, as in the other providers (Users).
The login
- The same error for a wrong password and for a user that is not in the file, and a user that does not exist takes as long as one that does (the password is verified against a hash anyway).
- The failed attempts are limited by identity and network of the client, as in the database provider (
AUTH_LOGIN_MAX_ATTEMPTSandAUTH_LOGIN_LOCK_SECONDS), with the PSR-6 pool of the application. See Database. - The session id is renewed when the user logs in, and the logout is a POST (Security).
The user of the session
The session keeps the identity. The file is read again every AUTH_WEB_REFRESH_INTERVAL_SECONDS (300 by default), and a user that is not in the file anymore loses the session at that moment. A change of the password does not close the session: the user is still in the file. See The user of the session is a copy.
The roles
The roles of the users come from an .htgroup file, the format of Apache: role: user1 user2. Set its path with AUTH_HTPASSWD_GROUP_PATH; empty (the default) means that the users have no roles. The file is read every time, like the .htpasswd, so a change in it is what the next login (or the next check of a session) sees. A user that is in no group has no roles.
AUTH_HTPASSWD_GROUP_PATH=%kernel.project_dir%/var/.htgroup
admin: ana
billing: ana beto