This symfony user provider reads user from the htpasswd file.

Installs: 748

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 1

Open Issues: 0


0.1 2021-01-04 15:19 UTC

This package is auto-updated.

Last update: 2021-10-04 17:04:37 UTC


Latest Stable Version Total Downloads License PHP v7.2 Symfony 4

This symfony user provider reads user from the htpasswd file.

To use the a htpasswd file you need to execute following three steps:

  1. Run composer to install the bundle,
  2. configure your security configuration and
  3. create a htpasswd password storage file.

All available password formats of the basic authentication are supported: apr1-md5, bcrypt, crypt, sha1 and plain text - see

Table of Contents


The usage of PHP v7.2 and Symfony 4 is obligatory.

Integration & Basic Usage


Please install via composer.

composer require prokki/htpasswd "^0.0"

The bundle will be automatically added to your bundles.yaml configuration.

Symfony Configuration

To enable the functionality you have to change your security configuration manually.

  1. Enable the provider HtpasswdUserProvider and the encoder HtpasswdEncoder in your security configuraton, i.e. in your %kernel.project_dir%/config/security.yaml file.
  2. To enable http basic authentication add the http_basic authentication method to the firewall configuration.
  3. Additionally be sure to enable access_control for at least one path.



  # 1.a) add the HtpasswdUserProvider as a new provider (the name does not matter)
      id: Htpasswd\Security\HtpasswdUserProvider

  # 1.b) and add the HtpasswdEncoder as a new encoder (the name does not matter)
      id: Htpasswd\Security\HtpasswdEncoder

  # 2. enable basic authentication
      http_basic: ~
  # 3. be sure to add one path
    - { path: ^/, roles: ROLE_USER }

Feel free to use the full flexibility of the Symfoniy security layer which means i.e. you can

  • chain firewalls,
  • add other providers and encoders,
  • restrict basic authentication to certain paths only
  • etc.

Add Htpasswd File

Create your custom htpasswd file using the htpasswd command in the project directory of your project.

Hint: Usually the command is installed automatically if you run an apache web server, i.e. if you use xampp or your package management system. On linux systems the command is mostly provided by the package apache2-utils.

Create a htpasswd file in your project directory. Call the following command and type in your password:

htpasswd -cB .htpasswd admin

A new file was created in your project:

  - config/
    - packages/
      - security.yaml   <-- do not forget to change your configuration
    - bundles.yaml
    - services.yaml
  - src/
    - Controller/
    - Entity/
    - Service/
    - [...]
  - tests/
  - composer.json
  - .htpasswd           <-- new htpasswd file in your project directory

⚠️ For safety reasons we suggest to not include the htpasswd file in your repository!

Advanced Usage & Configuration

You are not really able to change the configuration of the bundle, but at least you can customize the location of your htpasswd file and the default user roles.

In both cases you have to create a new configuratio file htpasswd.yaml for the bundle:

  - config/
    - packages/
      - security.yaml
      - htpasswd.yaml   <-- custom configuration file

The default content of the file should look like following. If you just insert these configuration, the bundle works with default settings.

  path: ~       # the path of your htpasswd file, ie. "%kernel.project_dir%/public/passwords.txt" 
  roles: ~      # the default roles of each basic auth user

Change Location of Your Htpasswd File

It is possible to change the default location by changing the path variable.

This is useful if you use your htpasswd file in other projects or if you set up a basic authentication additionally via your apache2 configuration.

  path: "/etc/apache2/.htpasswd" 
  roles: ~

Change Default User Roles

The default user role assigned to each user (included by the HtpasswdUserProvider) is ROLE_USER. To change the default user roles, adapt the roles variable in the configuration:

  path: ~ 
  roles: ["ROLE_ADMIN", "ROLE_ALLOW_WRITE", ...]

If you change the roles config parameter, be sure to include all roles which are necessary. There is no process to add another default user role.

Additionally please take care, that the roles

  1. follow the recommendations of symfony user roles and
  2. match the access_control settings in your security.yaml file.

User Roles Inside Htpasswd File

The implementation of the basic authentication allows you to add user roles at the end of each line in the htpasswd file.

Similar to overwriting the user roles by configuration, be sure to

  1. follow the recommendations of symfony user roles and
  2. match the access_control settings in your security.yaml file.

The user roles are a comma-separated list which are separated from the origin line by a colon. Example:


⚠️ This feature is probably not supported by all platforms!

Content of a well-structured htpasswd file:

# encoded by bcrypt, pass: admin1

# encoded by apr1-md5, pass: admin2

# encoded by crypt, pass: admin3

# encoded by sha1, pass: admin4

# not encoded / plain text

Content of a htpasswd file with additional user roles:

# encoded by bcrypt, pass: admin1, user roles ROLE_USER and ROLE_ADMIN

# encoded by apr1-md5, pass: admin2, user roles ROLE_USER and ROLE_SUPERVISOR

# encoded by crypt, pass: admin3, user roles ROLE_READ_ONLY

# encoded by sha1, pass: admin4, user roles ROLE_USER and ROLE_ADMIN

# not encoded / plain text


A big thank you to for the implementation of the apr1-md5 algorithm.