Advertisement
  1. Code
  2. Coding Fundamentals
  3. Security

How to Do User Authentication With the Symfony Security Component

Scroll to top

In this article, you'll learn how to set up user authentication in PHP using the Symfony Security component. As well as authentication, I'll show you how to use its role-based authorization, which you can extend according to your needs.

The Symfony Security Component

The Symfony Security Component allows you to set up security features like authentication, role-based authorization, CSRF tokens and more very easily. In fact, it's further divided into four sub-components which you can choose from according to your needs.

The Security component has the following sub-components:

  • symfony/security-core
  • symfony/security-http
  • symfony/security-csrf
  • symfony/security-acl

In this article, we are going to explore the authentication feature provided by the symfony/security-core component.

As usual, we'll start with the installation and configuration instructions, and then we'll explore a few real-world examples to demonstrate the key concepts.

Installation and Configuration

In this section, we are going to install the Symfony Security component. I assume that you have already installed Composer on your system—we'll need it to install the Security component available at Packagist.

So go ahead and install the Security component using the following command.

1
$composer require symfony/security

We are going to load users from the MySQL database in our example, so we'll also need a database abstraction layer. Let's install one of the most popular database abstraction layers: Doctrine DBAL.

1
$composer require doctrine/dbal

That should have created the composer.json file, which should look like this:

1
{
2
    "require": {
3
        "symfony/security": "^4.1",
4
        "doctrine/dbal": "^2.7"
5
    }
6
}

Let's modify the composer.json file to look like the following one.

1
{
2
    "require": {
3
        "symfony/security": "^4.1",
4
        "doctrine/dbal": "^2.7"
5
    },
6
    "autoload": {
7
         "psr-4": {
8
             "Sfauth\\": "src"
9
         },
10
         "classmap": ["src"]
11
    }
12
}

As we have added a new classmap entry, let's go ahead and update the composer autoloader by running the following command.

1
$composer dump -o

Now, you can use the Sfauth namespace to autoload classes under the src directory.

So that's the installation part, but how are you supposed to use it? In fact, it's just a matter of including the autoload.php file created by Composer in your application, as shown in the following snippet.

1
<?php
2
require_once './vendor/autoload.php';
3
4
// application code

5
?>

A Real-World Example

Firstly, let's go through the usual authentication flow provided by the Symfony Security component.

  • The first thing is to retrieve the user credentials and create an unauthenticated token.
  • Next, we'll pass an unauthenticated token to the authentication manager for validation.
  • The authentication manager may contain different authentication providers, and one of them will be used to authenticate the current user request. The logic of how the user is authenticated is defined in the authentication provider.
  • The authentication provider contacts the user provider to retrieve the user. It's the responsibility of the user provider to load users from the respective back-end.
  • The user provider tries to load the user using the credentials provided by the authentication provider. In most cases, the user provider returns the user object that implements the UserInterface interface.
  • If the user is found, the authentication provider returns an unauthenticated token, and you can store this token for the subsequent requests.

In our example, we are going to match the user credentials against the MySQL database, thus we'll need to create the database user provider. We'll also create the database authentication provider that handles the authentication logic. And finally, we'll create the User class, which implements the UserInterface interface.

The User Class

In this section, we'll create the User class which represents the user entity in the authentication process.

Go ahead and create the src/User/User.php file with the following contents.

1
<?php
2
namespace Sfauth\User;
3
4
use Symfony\Component\Security\Core\User\UserInterface;
5
6
class User implements UserInterface
7
{
8
    private $username;
9
    private $password;
10
    private $roles;
11
12
    public function __construct(string $username, string $password, string $roles)
13
    {
14
        if (empty($username))
15
        {
16
            throw new \InvalidArgumentException('No username provided.');
17
        }
18
19
        $this->username = $username;
20
        $this->password = $password;
21
        $this->roles = $roles;
22
    }
23
24
    public function getUsername()
25
    {
26
        return $this->username;
27
    }
28
29
    public function getPassword()
30
    {
31
        return $this->password;
32
    }
33
34
    public function getRoles()
35
    {
36
        return explode(",", $this->roles);
37
    }
38
39
    public function getSalt()
40
    {
41
        return '';
42
    }
43
44
    public function eraseCredentials() {}
45
}

The important thing is that the User class must implement the Symfony Security UserInterface interface. Apart from that, there's nothing out of the ordinary here.

The Database Provider Class

It's the responsibility of the user provider to load users from the back-end. In this section, we'll create the database user provider, which loads the user from the MySQL database.

Let's create the src/User/DatabaseUserProvider.php file with the following contents.

1
<?php
2
namespace Sfauth\User;
3
4
use Symfony\Component\Security\Core\User\UserProviderInterface;
5
use Symfony\Component\Security\Core\User\UserInterface;
6
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
7
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
8
use Doctrine\DBAL\Connection;
9
use Sfauth\User\User;
10
11
class DatabaseUserProvider implements UserProviderInterface
12
{
13
    private $connection;
14
15
    public function __construct(Connection $connection)
16
    {
17
        $this->connection = $connection;
18
    }
19
20
    public function loadUserByUsername($username)
21
    {
22
        return $this->getUser($username);
23
    }
24
25
    private function getUser($username)
26
    {
27
        $sql = "SELECT * FROM sf_users WHERE username = :name";
28
        $stmt = $this->connection->prepare($sql);
29
        $stmt->bindValue("name", $username);
30
        $stmt->execute();
31
        $row = $stmt->fetch();
32
33
        if (!$row['username'])
34
        {
35
            $exception = new UsernameNotFoundException(sprintf('Username "%s" not found in the database.', $row['username']));
36
            $exception->setUsername($username);
37
            throw $exception;
38
        }
39
        else
40
        {
41
            return new User($row['username'], $row['password'], $row['roles']);
42
        }
43
    }
44
45
    public function refreshUser(UserInterface $user)
46
    {
47
        if (!$user instanceof User)
48
        {
49
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
50
        }
51
52
        return $this->getUser($user->getUsername());
53
    }
54
55
    public function supportsClass($class)
56
    {
57
        return 'Sfauth\User\User' === $class;
58
    }
59
}

The user provider must implement the UserProviderInterface interface. We are using the doctrine DBAL to perform the database-related operations. As we have implemented the UserProviderInterface interface, we must implement the loadUserByUsername, refreshUser, and supportsClass methods.

The loadUserByUsername method should load the user by the username, and that's done in the getUser method. If the user is found, we return the corresponding Sfauth\User\User object, which implements the UserInterface interface.

On the other hand, the refreshUser method refreshes the supplied User object by fetching the latest information from the database.

And finally, the supportsClass method checks if the DatabaseUserProvider provider supports the supplied user class.

The Database Authentication Provider Class

Finally, we need to implement the user authentication provider, which defines the authentication logic—how a user is authenticated. In our case, we need to match the user credentials against the MySQL database, and thus we need to define the authentication logic accordingly.

Go ahead and create the src/User/DatabaseAuthenticationProvider.php file with the following contents.

1
<?php
2
namespace Sfauth\User;
3
4
use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
5
use Symfony\Component\Security\Core\User\UserProviderInterface;
6
use Symfony\Component\Security\Core\User\UserCheckerInterface;
7
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
8
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
9
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
use Symfony\Component\Security\Core\Exception\AuthenticationException;
12
13
class DatabaseAuthenticationProvider extends UserAuthenticationProvider
14
{
15
    private $userProvider;
16
17
    public function __construct(UserProviderInterface $userProvider, UserCheckerInterface $userChecker, string $providerKey, bool $hideUserNotFoundExceptions = true)
18
    {
19
        parent::__construct($userChecker, $providerKey, $hideUserNotFoundExceptions);
20
21
        $this->userProvider = $userProvider;
22
    }
23
24
    protected function retrieveUser($username, UsernamePasswordToken $token)
25
    {
26
        $user = $token->getUser();
27
28
        if ($user instanceof UserInterface)
29
        {
30
            return $user;
31
        }
32
33
        try {
34
            $user = $this->userProvider->loadUserByUsername($username);
35
36
            if (!$user instanceof UserInterface)
37
            {
38
                throw new AuthenticationServiceException('The user provider must return a UserInterface object.');
39
            }
40
41
            return $user;
42
        } catch (UsernameNotFoundException $e) {
43
            $e->setUsername($username);
44
45
            throw $e;
46
        } catch (\Exception $e) {
47
            $e = new AuthenticationServiceException($e->getMessage(), 0, $e);
48
            $e->setToken($token);
49
            throw $e;
50
        }
51
    }
52
53
    protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
54
    {
55
        $currentUser = $token->getUser();
56
        
57
        if ($currentUser instanceof UserInterface)
58
        {
59
            if ($currentUser->getPassword() !== $user->getPassword())
60
            {
61
                throw new AuthenticationException('Credentials were changed from another session.');
62
            }
63
        }
64
        else
65
        {
66
            $password = $token->getCredentials();
67
68
            if (empty($password))
69
            {
70
                throw new AuthenticationException('Password can not be empty.');
71
            }
72
73
            if ($user->getPassword() != md5($password))
74
            {
75
                throw new AuthenticationException('Password is invalid.');
76
            }
77
        }
78
    }
79
}

The DatabaseAuthenticationProvider authentication provider extends the UserAuthenticationProvider abstract class. Hence, we need to implement the retrieveUser and checkAuthentication abstract methods.

The job of the retrieveUser method is to load the user from the corresponding user provider. In our case, it will use the DatabaseUserProvider user provider to load the user from the MySQL database.

On the other hand, the checkAuthentication method performs the necessary checks in order to authenticate the current user. Please note that I've used the MD5 method for password encryption. Of course, you should use more secure encryption methods to store user passwords.

How It Works Altogether

So far, we have created all the necessary elements for authentication. In this section, we'll see how to put it all together to set up the authentication functionality.

Go ahead and create the db_auth.php file and populate it with the following contents.

1
<?php
2
require_once './vendor/autoload.php';
3
4
use Sfauth\User\DatabaseUserProvider;
5
use Symfony\Component\Security\Core\User\UserChecker;
6
use Sfauth\User\DatabaseAuthenticationProvider;
7
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
8
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
9
use Symfony\Component\Security\Core\Exception\AuthenticationException;
10
11
// init doctrine db connection

12
$doctrineConnection = \Doctrine\DBAL\DriverManager::getConnection(
13
  array('url' => 'mysql://{USERNAME}:{PASSWORD}@{HOSTNAME}/{DATABASE_NAME}'), new \Doctrine\DBAL\Configuration()
14
);
15
16
// init our custom db user provider

17
$userProvider = new DatabaseUserProvider($doctrineConnection);
18
19
// we'll use default UserChecker, it's used to check additional checks like account lock/expired etc.

20
// you can implement your own by implementing UserCheckerInterface interface

21
$userChecker = new UserChecker();
22
23
// init our custom db authentication provider

24
$dbProvider = new DatabaseAuthenticationProvider(
25
    $userProvider,
26
    $userChecker,
27
    'frontend'
28
);
29
30
// init authentication provider manager

31
$authenticationManager = new AuthenticationProviderManager(array($dbProvider));
32
33
try {
34
    // init un/pw, usually you'll get these from the $_POST variable, submitted by the end user

35
    $username = 'admin';
36
    $password = 'admin';
37
38
    // get unauthenticated token

39
    $unauthenticatedToken = new UsernamePasswordToken(
40
        $username,
41
        $password,
42
        'frontend'
43
    );
44
45
    // authenticate user & get authenticated token

46
    $authenticatedToken = $authenticationManager->authenticate($unauthenticatedToken);
47
48
    // we have got the authenticated token (user is logged in now), it can be stored in a session for later use

49
    echo $authenticatedToken;
50
    echo "\n";
51
} catch (AuthenticationException $e) {
52
    echo $e->getMessage();
53
    echo "\n";
54
}

Recall the authentication flow which was discussed in the beginning of this article—the above code reflects that sequence.

The first thing was to retrieve the user credentials and create an unauthenticated token.

1
$unauthenticatedToken = new UsernamePasswordToken(
2
    $username,
3
    $password,
4
    'frontend'
5
);

Next, we have passed that token to the authentication manager for validation.

1
// authenticate user & get authenticated token

2
$authenticatedToken = $authenticationManager->authenticate($unauthenticatedToken);

When the authenticate method is called, a lot of things are happening behind the scenes.

Firstly, the authentication manager selects an appropriate authentication provider. In our case, it's the DatabaseAuthenticationProvider authentication provider, which will be selected for authentication.

Next, it retrieves the user by the username from the DatabaseUserProvider user provider. Finally, the checkAuthentication method performs the necessary checks to authenticate the current user request.

Should you wish to test the db_auth.php script, you'll need to create the sf_users table in your MySQL database.

1
CREATE TABLE `sf_users` (
2
  `id` int(11) NOT NULL AUTO_INCREMENT,
3
  `username` varchar(255) NOT NULL,
4
  `password` varchar(255) NOT NULL,
5
  `roles` enum('registered','moderator','admin') DEFAULT NULL,
6
  PRIMARY KEY (`id`)
7
) ENGINE=InnoDB;
8
9
INSERT INTO `sf_users` VALUES (1,'admin','21232f297a57a5a743894a0e4a801fc3','admin');

Go ahead and run the db_auth.php script to see how it goes. Upon successful completion, you should receive an authenticated token, as shown in the following snippet.

1
$php db_auth.php
2
UsernamePasswordToken(user="admin", authenticated=true, roles="admin")

Once the user is authenticated, you can store the authenticated token in the session for the subsequent requests.

And with that, we've completed our simple authentication demo!

Conclusion

Today, we looked at the Symfony Security component, which allows you to integrate security features in your PHP applications. Specifically, we discussed the authentication feature provided by the symfony/security-core sub-component, and I showed you an example of how this functionality can be implemented in your own app.

Feel free to post your thoughts using the feed below!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.