| 1 |
PHP (Symfony) |
| 2 |
============= |
| 3 |
|
| 4 |
|image0| |
| 5 |
|
| 6 |
Presentation |
| 7 |
------------ |
| 8 |
|
| 9 |
`Symfony <https://symfony.com/>`__ is the well-known PHP framework. It |
| 10 |
is intended to ease the development of PHP applications. |
| 11 |
|
| 12 |
Symfony provides many methods conventions to authenticate users (basic, |
| 13 |
ldap,...) and to load external user sources (ldap, database). The method |
| 14 |
presented here relies on the "remote_user" method. (in security |
| 15 |
firewall) |
| 16 |
|
| 17 |
Configuration |
| 18 |
------------- |
| 19 |
|
| 20 |
Follow these step to protect your application using the "REMOTE_USER" |
| 21 |
HTTP header. |
| 22 |
|
| 23 |
1. Adapt the app/config/security.yml configuration file as below: |
| 24 |
|
| 25 |
.. code-block:: yaml |
| 26 |
|
| 27 |
security: |
| 28 |
|
| 29 |
encoders: |
| 30 |
AppBundle\Security\User\HeaderUser: plaintext |
| 31 |
|
| 32 |
providers: |
| 33 |
header: |
| 34 |
id: AppBundle\Security\User\HeaderUserProvider |
| 35 |
|
| 36 |
firewalls: |
| 37 |
dev: |
| 38 |
pattern: ^/(_(profiler|wdt)|css|images|js)/ |
| 39 |
security: false |
| 40 |
|
| 41 |
main: |
| 42 |
pattern: ^/ |
| 43 |
remote_user: |
| 44 |
user: HTTP_REMOTE_USER |
| 45 |
provider: header |
| 46 |
|
| 47 |
- encoders : define a password hashing scheme (useless in our case, but |
| 48 |
the parameter is mandatory) |
| 49 |
- providers : define the user providers (even virtual) |
| 50 |
- remote_user : define the authentication method to "assume the user is |
| 51 |
already authenticated and get an http variable to know his username" |
| 52 |
- user : define the HTTP header containing the username |
| 53 |
- provider : references the previously defined provider owning the user |
| 54 |
data (in our scenario, a virtual) |
| 55 |
|
| 56 |
2. Define a "header user" class |
| 57 |
|
| 58 |
Create the file src/AppBundle/Security/User/HeaderUser.php : |
| 59 |
|
| 60 |
.. code-block:: php |
| 61 |
|
| 62 |
<?php |
| 63 |
|
| 64 |
// src/Security/User/HeaderUser.php |
| 65 |
namespace AppBundle\Security\User; |
| 66 |
|
| 67 |
use Symfony\Component\Security\Core\User\UserInterface; |
| 68 |
use Symfony\Component\Security\Core\User\EquatableInterface; |
| 69 |
|
| 70 |
class HeaderUser implements UserInterface, EquatableInterface |
| 71 |
{ |
| 72 |
private $username; |
| 73 |
private $password; |
| 74 |
private $salt; |
| 75 |
private $roles; |
| 76 |
|
| 77 |
public function __construct($username, $password, $salt, array $roles) |
| 78 |
{ |
| 79 |
$this->username = $username; |
| 80 |
$this->password = $password; |
| 81 |
$this->salt = $salt; |
| 82 |
$this->roles = $roles; |
| 83 |
} |
| 84 |
|
| 85 |
public function getRoles() |
| 86 |
{ |
| 87 |
return $this->roles; |
| 88 |
} |
| 89 |
|
| 90 |
public function getPassword() |
| 91 |
{ |
| 92 |
return $this->password; |
| 93 |
} |
| 94 |
|
| 95 |
public function getSalt() |
| 96 |
{ |
| 97 |
return $this->salt; |
| 98 |
} |
| 99 |
public function getUsername() |
| 100 |
{ |
| 101 |
return $this->username; |
| 102 |
} |
| 103 |
|
| 104 |
public function eraseCredentials() |
| 105 |
{ |
| 106 |
} |
| 107 |
|
| 108 |
public function isEqualTo(UserInterface $user) |
| 109 |
{ |
| 110 |
if (!$user instanceof HeaderUser) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
if ($this->username !== $user->getUsername()) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
//if ($this->password !== $user->getPassword()) { |
| 119 |
// return false; |
| 120 |
//} |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
} |
| 125 |
?> |
| 126 |
|
| 127 |
3. Define a "header user provider" class relying on the previous class |
| 128 |
|
| 129 |
Create the file src/AppBundle/Security/User/HeaderUserProvider.php : |
| 130 |
|
| 131 |
.. code-block:: php |
| 132 |
|
| 133 |
<?php |
| 134 |
|
| 135 |
// src/Security/User/HeaderUserProvider.php |
| 136 |
namespace AppBundle\Security\User; |
| 137 |
|
| 138 |
use AppBundle\Security\User\HeaderUser; |
| 139 |
use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 140 |
use Symfony\Component\Security\Core\User\UserInterface; |
| 141 |
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
| 142 |
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
| 143 |
|
| 144 |
class HeaderUserProvider implements UserProviderInterface |
| 145 |
{ |
| 146 |
public function loadUserByUsername($username) |
| 147 |
{ |
| 148 |
|
| 149 |
if ($username) { |
| 150 |
|
| 151 |
$password = "dummy"; |
| 152 |
$salt = ""; |
| 153 |
$roles = array('ROLE_USER'); |
| 154 |
|
| 155 |
return new HeaderUser($username, $password, $salt, $roles); |
| 156 |
} |
| 157 |
|
| 158 |
throw new UsernameNotFoundException( |
| 159 |
sprintf('Username "%s" does not exist.', $username) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
public function refreshUser(UserInterface $user) |
| 164 |
{ |
| 165 |
if (!$user instanceof HeaderUser) { |
| 166 |
throw new UnsupportedUserException( |
| 167 |
sprintf('Instances of "%s" are not supported.', get_class($user)) |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
return $this->loadUserByUsername($user->getUsername()); |
| 172 |
} |
| 173 |
|
| 174 |
public function supportsClass($class) |
| 175 |
{ |
| 176 |
return HeaderUser::class === $class; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
?> |
| 181 |
|
| 182 |
References |
| 183 |
---------- |
| 184 |
|
| 185 |
- http://symfony.com/doc/current/security/pre_authenticated.html#remote-user-based-authentication |
| 186 |
- https://symfony.com/doc/current/security/custom_provider.html |
| 187 |
|
| 188 |
.. |image0| image:: /applications/symfony_logo.png |
| 189 |
:class: align-center |
| 190 |
|