Authenticating against an LDAP server

Authenticating against an LDAP server

Symfony provides different means to work with an LDAP server.

The Security component offers:

  • The ldap user provider, using the Symfony\Component\Ldap\Security\LdapUserProvider class. Like all other user providers, it can be used with any authentication provider.
  • The form_login_ldap authentication provider, for authenticating against an LDAP server using a login form. Like all other authentication providers, it can be used with any user provider.
  • The http_basic_ldap authentication provider, for authenticating against an LDAP server using HTTP Basic. Like all other authentication providers, it can be used with any user provider.

This means that the following scenarios will work:

  • Checking a user’s password and fetching user information against an LDAP server. This can be done using both the LDAP user provider and either the LDAP form login or LDAP HTTP Basic authentication providers.
  • Checking a user’s password against an LDAP server while fetching user information from another source (database using FOSUserBundle, for example).
  • Loading user information from an LDAP server, while using another authentication strategy (token-based pre-authentication, for example).

Deprecated since version 4.4: The class Symfony\Component\Security\Core\User\LdapUserProvider has been deprecated in Symfony 4.4. Use the class Symfony\Component\Ldap\Security\LdapUserProvider instead.

Installation

In applications using Symfony Flex, run this command to install the Ldap component before using it:

  1. $ composer require symfony/ldap

Ldap Configuration Reference

See Security Configuration Reference (SecurityBundle) for the full LDAP configuration reference (form_login_ldap, http_basic_ldap, ldap). Some of the more interesting options are explained below.

Configuring the LDAP client

All mechanisms actually need an LDAP client previously configured. The providers are configured to use a default service named ldap, but you can override this setting in the security component’s configuration.

An LDAP client can be configured using the built-in LDAP PHP extension with the following service definition:

  • YAML

    1. # config/services.yaml
    2. services:
    3. Symfony\Component\Ldap\Ldap:
    4. arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
    5. Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
    6. arguments:
    7. - host: my-server
    8. port: 389
    9. encryption: tls
    10. options:
    11. protocol_version: 3
    12. referrals: false
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <service id="Symfony\Component\Ldap\Ldap">
    8. <argument type="service" id="Symfony\Component\Ldap\Adapter\ExtLdap\Adapter"/>
    9. </service>
    10. <service id="Symfony\Component\Ldap\Adapter\ExtLdap\Adapter">
    11. <argument type="collection">
    12. <argument key="host">my-server</argument>
    13. <argument key="port">389</argument>
    14. <argument key="encryption">tls</argument>
    15. <argument key="options" type="collection">
    16. <argument key="protocol_version">3</argument>
    17. <argument key="referrals">false</argument>
    18. </argument>
    19. </argument>
    20. </service>
    21. </services>
    22. </container>
  • PHP

    1. // config/services.php
    2. use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter;
    3. use Symfony\Component\Ldap\Ldap;
    4. $container->register(Ldap::class)
    5. ->addArgument(new Reference(Adapter::class));
    6. $container
    7. ->register(Adapter::class)
    8. ->setArguments([
    9. 'host' => 'my-server',
    10. 'port' => 389,
    11. 'encryption' => 'tls',
    12. 'options' => [
    13. 'protocol_version' => 3,
    14. 'referrals' => false
    15. ],
    16. ]);

Fetching Users Using the LDAP User Provider

If you want to fetch user information from an LDAP server, you may want to use the ldap user provider.

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. providers:
    5. my_ldap:
    6. ldap:
    7. service: Symfony\Component\Ldap\Ldap
    8. base_dn: dc=example,dc=com
    9. search_dn: "cn=read-only-admin,dc=example,dc=com"
    10. search_password: password
    11. default_roles: ROLE_USER
    12. uid_key: uid
    13. extra_fields: ['email']
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <provider name="my_ldap">
    10. <ldap service="Symfony\Component\Ldap\Ldap"
    11. base-dn="dc=example,dc=com"
    12. search-dn="cn=read-only-admin,dc=example,dc=com"
    13. search-password="password"
    14. default-roles="ROLE_USER"
    15. uid-key="uid"/>
    16. </provider>
    17. </config>
    18. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. use Symfony\Component\Ldap\Ldap;
    3. $container->loadFromExtension('security', [
    4. 'providers' => [
    5. 'ldap_users' => [
    6. 'ldap' => [
    7. 'service' => Ldap::class,
    8. 'base_dn' => 'dc=example,dc=com',
    9. 'search_dn' => 'cn=read-only-admin,dc=example,dc=com',
    10. 'search_password' => 'password',
    11. 'default_roles' => 'ROLE_USER',
    12. 'uid_key' => 'uid',
    13. 'extra_fields' => ['email'],
    14. ],
    15. ],
    16. ],
    17. ]);

Caution

The Security component escapes provided input data when the LDAP user provider is used. However, the LDAP component itself does not provide any escaping yet. Thus, it’s your responsibility to prevent LDAP injection attacks when using the component directly.

Caution

The user configured above in the user provider is only used to retrieve data. It’s a static user defined by its username and password (for improved security, define the password as an environment variable).

If your LDAP server allows retrieval of information anonymously, you can set the search_dn and search_password options to null.

The ldap user provider supports many different configuration options:

service

type: string default: ldap

This is the name of your configured LDAP client. You can freely choose the name, but it must be unique in your application and it cannot start with a number or contain white spaces.

base_dn

type: string default: null

This is the base DN for the directory

search_dn

type: string default: null

This is your read-only user’s DN, which will be used to authenticate against the LDAP server to fetch the user’s information.

search_password

type: string default: null

This is your read-only user’s password, which will be used to authenticate against the LDAP server to fetch the user’s information.

default_roles

type: array default: []

This is the default role you wish to give to a user fetched from the LDAP server. If you do not configure this key, your users won’t have any roles, and will not be considered as authenticated fully.

uid_key

type: string default: null

This is the entry’s key to use as its UID. Depends on your LDAP server implementation. Commonly used values are:

  • sAMAccountName (default)
  • userPrincipalName
  • uid

If you pass null as the value of this option, the default UID key is used sAMAccountName.

extra_fields

type: array default: null

New in version 4.4: The extra_fields option was introduced in Symfony 4.4.

Defines the custom fields to pull from the LDAP server. If any field does not exist, an \InvalidArgumentException will be thrown.

filter

type: string default: null

This key lets you configure which LDAP query will be used. The {uid_key} string will be replaced by the value of the uid_key configuration value (by default, sAMAccountName), and the {username} string will be replaced by the username you are trying to load.

For example, with a uid_key of uid, and if you are trying to load the user fabpot, the final string will be: (uid=fabpot).

If you pass null as the value of this option, the default filter is used ({uid_key}={username}).

To prevent LDAP injection, the username will be escaped.

The syntax for the filter key is defined by RFC4515.

Authenticating against an LDAP server

Authenticating against an LDAP server can be done using either the form login or the HTTP Basic authentication providers.

They are configured exactly as their non-LDAP counterparts, with the addition of two configuration keys and one optional key:

service

type: string default: ldap

This is the name of your configured LDAP client. You can freely choose the name, but it must be unique in your application and it cannot start with a number or contain white spaces.

dn_string

type: string default: {username}

This key defines the form of the string used to compose the DN of the user, from the username. The {username} string is replaced by the actual username of the person trying to authenticate.

For example, if your users have DN strings in the form uid=einstein,dc=example,dc=com, then the dn_string will be uid={username},dc=example,dc=com.

query_string

type: string default: null

This (optional) key makes the user provider search for a user and then use the found DN for the bind process. This is useful when using multiple LDAP user providers with different base_dn. The value of this option must be a valid search string (e.g. uid="{username}"). The placeholder value will be replaced by the actual username.

When this option is used, query_string will search in the DN specified by dn_string and the DN resulted of the query_string will be used to authenticate the user with their password. Following the previous example, if your users have the following two DN: dc=companyA,dc=example,dc=com and dc=companyB,dc=example,dc=com, then dn_string should be dc=example,dc=com.

Bear in mind that usernames must be unique across both DN, as the authentication provider won’t be able to select the correct user for the bind process if more than one is found.

Examples are provided below, for both form_login_ldap and http_basic_ldap.

Configuration example for form login

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. firewalls:
    5. main:
    6. # ...
    7. form_login_ldap:
    8. # ...
    9. service: Symfony\Component\Ldap\Ldap
    10. dn_string: 'uid={username},dc=example,dc=com'
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <firewall name="main">
    10. <form-login-ldap service="Symfony\Component\Ldap\Ldap"
    11. dn-string="uid={username},dc=example,dc=com"/>
    12. </firewall>
    13. </config>
    14. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. use Symfony\Component\Ldap\Ldap;
    3. $container->loadFromExtension('security', [
    4. 'firewalls' => [
    5. 'main' => [
    6. 'form_login_ldap' => [
    7. 'service' => Ldap::class,
    8. 'dn_string' => 'uid={username},dc=example,dc=com',
    9. // ...
    10. ],
    11. ],
    12. ]
    13. ]);

Configuration example for HTTP Basic

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. firewalls:
    5. main:
    6. stateless: true
    7. http_basic_ldap:
    8. service: Symfony\Component\Ldap\Ldap
    9. dn_string: 'uid={username},dc=example,dc=com'
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <!-- ... -->
    10. <firewall name="main" stateless="true">
    11. <http-basic-ldap service="Symfony\Component\Ldap\Ldap"
    12. dn-string="uid={username},dc=example,dc=com"/>
    13. </firewall>
    14. </config>
    15. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. use Symfony\Component\Ldap\Ldap;
    3. $container->loadFromExtension('security', [
    4. // ...
    5. 'firewalls' => [
    6. 'main' => [
    7. 'http_basic_ldap' => [
    8. 'service' => Ldap::class,
    9. 'dn_string' => 'uid={username},dc=example,dc=com',
    10. ],
    11. 'stateless' => true,
    12. ],
    13. ],
    14. ]);

Configuration example for form login and query_string

  • YAML

    1. # config/packages/security.yaml
    2. security:
    3. # ...
    4. firewalls:
    5. main:
    6. # ...
    7. form_login_ldap:
    8. service: Symfony\Component\Ldap\Ldap
    9. dn_string: 'dc=example,dc=com'
    10. query_string: '(&(uid={username})(memberOf=cn=users,ou=Services,dc=example,dc=com))'
    11. search_dn: '...'
    12. search_password: 'the-raw-password'
  • XML

    1. <!-- config/packages/security.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <srv:container xmlns="http://symfony.com/schema/dic/security"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:srv="http://symfony.com/schema/dic/services"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <config>
    9. <firewall name="main">
    10. <!-- ... -->
    11. <form-login-ldap service="Symfony\Component\Ldap\Ldap"
    12. dn-string="dc=example,dc=com"
    13. query-string="(&amp;(uid={username})(memberOf=cn=users,ou=Services,dc=example,dc=com))"
    14. search-dn="..."
    15. search-password="the-raw-password"/>
    16. </firewall>
    17. </config>
    18. </srv:container>
  • PHP

    1. // config/packages/security.php
    2. use Symfony\Component\Ldap\Ldap;
    3. $container->loadFromExtension('security', [
    4. 'firewalls' => [
    5. 'main' => [
    6. // ...
    7. 'form_login_ldap' => [
    8. 'service' => Ldap::class,
    9. 'dn_string' => 'dc=example,dc=com',
    10. 'query_string' => '(&(uid={username})(memberOf=cn=users,ou=Services,dc=example,dc=com))',
    11. 'search_dn' => '...',
    12. 'search_password' => 'the-raw-password',
    13. ],
    14. ],
    15. ]
    16. ]);

Deprecated since version 4.4: Using the query_string config option without defining search_dn and search_password is deprecated since Symfony 4.4.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.