From 9b317826d538ad6de61b0d2f67ef1881a57ad077 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 15 Mar 2017 16:41:18 +0100 Subject: [PATCH] version 1.0.1 - add security password encoder in Authentication --- README.md | 1 + config/security.yml.dist | 12 ++++++++ src/MetaTech/Silex/Application.php | 4 +++ src/MetaTech/Silex/Provider/UserProvider.php | 1 + src/MetaTech/Silex/Ws/Authentication.php | 30 ++++++++++++-------- src/MetaTech/Silex/Ws/Controller.php | 2 +- 6 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 config/security.yml.dist diff --git a/README.md b/README.md index 652ab84..8f29cd6 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Core package for silex2 applications * meta-tech/pws-auth * meta-tech/silex-controller-service * silex/silex (v2) +* symfony/security (~3.2) * gecko-packages/gecko-silex-config-service diff --git a/config/security.yml.dist b/config/security.yml.dist new file mode 100644 index 0000000..063f8a3 --- /dev/null +++ b/config/security.yml.dist @@ -0,0 +1,12 @@ +security.firewalls : + dev: + pattern: ^/(_(profiler|wdt)|css|images|js)/ + security: false + + main: + pattern: ^/ws + anonymous: true + +security.params : + sleep : 3 + diff --git a/src/MetaTech/Silex/Application.php b/src/MetaTech/Silex/Application.php index bbfe3e6..9319cb8 100644 --- a/src/MetaTech/Silex/Application.php +++ b/src/MetaTech/Silex/Application.php @@ -12,6 +12,7 @@ namespace MetaTech\Silex; use Silex\Application as BaseApplication; use Silex\Provider\SessionServiceProvider; use Silex\Provider\ServiceControllerServiceProvider; +use Silex\Provider\SecurityServiceProvider; use GeckoPackages\Silex\Services\Config\ConfigServiceProvider; /*! @@ -52,6 +53,9 @@ class Application extends BaseApplication ]); $this->register(new SessionServiceProvider()); $this->register(new ServiceControllerServiceProvider()); + if (!empty($this['config']['security'])) { + $this->register(new SecurityServiceProvider(), $this['config']['security']); + } } /*! diff --git a/src/MetaTech/Silex/Provider/UserProvider.php b/src/MetaTech/Silex/Provider/UserProvider.php index 0578d58..2e2807f 100644 --- a/src/MetaTech/Silex/Provider/UserProvider.php +++ b/src/MetaTech/Silex/Provider/UserProvider.php @@ -105,6 +105,7 @@ class UserProvider implements UserProviderInterface $user = $this->loadUser($username); $u = new User($user->username, $user->password, explode(',', $user->roles), true, true, true, true); $u->labelName = $user->name; + $u->key = $user->key; return $u; } diff --git a/src/MetaTech/Silex/Ws/Authentication.php b/src/MetaTech/Silex/Ws/Authentication.php index 39fe2d8..dac0d54 100644 --- a/src/MetaTech/Silex/Ws/Authentication.php +++ b/src/MetaTech/Silex/Ws/Authentication.php @@ -13,6 +13,7 @@ use Silex\Application; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; use MetaTech\PwsAuth\Authenticator; use MetaTech\PwsAuth\Token; @@ -24,21 +25,25 @@ use MetaTech\PwsAuth\Token; */ class Authentication { - /*! @protected @®ar Symfony\Component\HttpFoundation\Session\Session $session */ + /*! @protected @var Symfony\Component\HttpFoundation\Session\Session $session */ protected $session; - /*! @protected @®ar MetaTech\PwsAuth\Authenticator $authenticator */ + /*! @protected @var MetaTech\PwsAuth\Authenticator $authenticator */ protected $authenticator; + /*! @protected @var Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface $passEncoder */ + protected $passEncoder; /*! * @constructor * @public - * @param Symfony\Component\HttpFoundation\Session\Session $session - * @param MetaTech\PwsAuth\Authenticator $authenticator + * @param Symfony\Component\HttpFoundation\Session\Session $session + * @param MetaTech\PwsAuth\Authenticator $authenticator + * @param Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface $passEncoder */ - public function __construct(Session $session, Authenticator $authenticator) + public function __construct(Session $session, Authenticator $authenticator, PasswordEncoderInterface $passEncoder = null) { - $this->session = $session; + $this->session = $session; $this->authenticator = $authenticator; + $this->passEncoder = $passEncoder; } /*! @@ -70,12 +75,13 @@ class Authentication /*! * @method checkUser * @public - * @param str $login - * @param str $password - * @param str $key - * @return bool + * @param str $login + * @param str $password + * @param str $key + * @param Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface $passEncoder + * @return bool */ - public function checkUser($login, $password, $key) + public function checkUser($login, $password, $key, PasswordEncoderInterface $passEncoder = null) { // implements on subclass return false; @@ -97,7 +103,7 @@ class Authentication $password = $request->get('password'); if ($this->authenticator->check($token, $login)) { try { - if ($done = $this->checkUser($login, $password, $token->getIdent())) { + if ($done = $this->checkUser($login, $password, $token->getIdent(), $this->passEncoder)) { $sid = $this->onSuccess($token, $login); $msg = "authentication sucessful ! logged as $login"; $data = compact('sid'); diff --git a/src/MetaTech/Silex/Ws/Controller.php b/src/MetaTech/Silex/Ws/Controller.php index c95e926..377b548 100644 --- a/src/MetaTech/Silex/Ws/Controller.php +++ b/src/MetaTech/Silex/Ws/Controller.php @@ -38,7 +38,7 @@ class Controller extends Base public function __construct(Application $app = null) { $this->session = $app['session']; - $this->handler = new Authentication($this->session, $app['ws.authenticator']); + $this->handler = new Authentication($this->session, $app['ws.authenticator'], $app['security.default_encoder']); } /*!