From e875e3c0d550733bea366208a38f4278de79f645 Mon Sep 17 00:00:00 2001 From: a-sansara Date: Wed, 15 Mar 2017 18:04:31 +0100 Subject: [PATCH] version 1.0.1 - manage ws Authentication via userProvider --- config/db.yml | 7 ++++ config/security.yml | 12 +++++++ schema/170315-init-users.sql | 13 +++++++ src/MetaTech/PwsServer/Application.php | 8 ++++- src/MetaTech/PwsServer/Ws/Authentication.php | 38 ++++++++++++++------ src/MetaTech/PwsServer/Ws/Controller.php | 2 +- 6 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 config/db.yml create mode 100644 config/security.yml create mode 100644 schema/170315-init-users.sql diff --git a/config/db.yml b/config/db.yml new file mode 100644 index 0000000..1c5507b --- /dev/null +++ b/config/db.yml @@ -0,0 +1,7 @@ +default : + driver : pdo_mysql + host : db + dbname : pwsserver + user : dev + password : mysql + charset : utf8 diff --git a/config/security.yml b/config/security.yml new file mode 100644 index 0000000..063f8a3 --- /dev/null +++ b/config/security.yml @@ -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/schema/170315-init-users.sql b/schema/170315-init-users.sql new file mode 100644 index 0000000..545e391 --- /dev/null +++ b/schema/170315-init-users.sql @@ -0,0 +1,13 @@ +CREATE TABLE `users` ( + `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(30) NOT NULL, + `username` varchar(32) NOT NULL, + `password` varchar(255) NOT NULL, + `key` varchar(255) DEFAULT NULL, + `roles` varchar(255) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `UNIQ_6E736E72F85E0677` (`username`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; + +-- user dev : foo +INSERT INTO `users` (`id`, `name`, `username`, `password`, `key`, `roles`) VALUES ('', 'Meta-Tech', 'dev', 'EZJ4em8bQ409UiPU+LpfJ5IWpiTkT2lSzMkVEl3IP5A0TDRV+RZS1Q==', 'ed830045da9861d29c46f36b4f4b1a4d4b223408667c52428370e51b615e8769', 'ROLE_ADMIN'); diff --git a/src/MetaTech/PwsServer/Application.php b/src/MetaTech/PwsServer/Application.php index 051fbad..6857972 100644 --- a/src/MetaTech/PwsServer/Application.php +++ b/src/MetaTech/PwsServer/Application.php @@ -11,6 +11,9 @@ namespace MetaTech\PwsServer; use MetaTech\Silex\Application as App; use MetaTech\Silex\Provider\ControllerServiceProvider as CtrlProvider; +use MetaTech\Silex\Provider\UserProvider; +use MetaTech\Db\PdoWrapper; +use MetaTech\Db\Profile; use MetaTech\PwsAuth\Authenticator; use MetaTech\PwsServer\Ctrl\Test; use MetaTech\PwsServer\Ctrl\WebService; @@ -35,8 +38,11 @@ class Application extends App $app['ws.authenticator'] = function ($app) { return new Authenticator($app['config']['pwsauth']); }; + $app['pdo'] = function ($app) { + return new PdoWrapper(new Profile($app['config']['db']['default'])); + }; $app['user.provider'] = function ($app) { - return null; + return new UserProvider($app['pdo']); }; } diff --git a/src/MetaTech/PwsServer/Ws/Authentication.php b/src/MetaTech/PwsServer/Ws/Authentication.php index 4c9b064..87c9a82 100644 --- a/src/MetaTech/PwsServer/Ws/Authentication.php +++ b/src/MetaTech/PwsServer/Ws/Authentication.php @@ -10,8 +10,11 @@ namespace MetaTech\PwsServer\Ws; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; use MetaTech\PwsAuth\Authenticator; use MetaTech\Silex\Ws\Authentication as BaseAuthentication; +use MetaTech\Silex\Provider\UserProvider; /*! * @package MetaTech\PwsServer\Ws @@ -27,26 +30,39 @@ class Authentication extends BaseAuthentication /*! * @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 + * @param MetaTech\Silex\Provider\UserProvider $userProvider */ - public function __construct(Session $session, Authenticator $authenticator, $userProvider) + public function __construct(Session $session, Authenticator $authenticator, PasswordEncoderInterface $passEncoder = null, UserProvider $userProvider) { - parent::__construct($session, $authenticator); - $this->userOrovider = $userProvider; + parent::__construct($session, $authenticator, $passEncoder); + $this->userProvider = $userProvider; } /*! * @method checkUser * @public - * @param str $login - * @param str $password - * @param str $key + * @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) { - // @todo implements with userProvider - return true; + $done = false; + try { + if (!is_null($passEncoder)) { + $user = $this->userProvider->loadUserByUsername($login); + $salt = $this->authenticator->getUserSalt($login); + $done = $user->key == $key && $passEncoder->encodePassword($password, $salt) == $user->getPassword(); + } + } + catch(\Exception $e) { + //~ var_dump($e->getTraceAsString()); + } + return $done; } } diff --git a/src/MetaTech/PwsServer/Ws/Controller.php b/src/MetaTech/PwsServer/Ws/Controller.php index 517dfe4..ee188b0 100644 --- a/src/MetaTech/PwsServer/Ws/Controller.php +++ b/src/MetaTech/PwsServer/Ws/Controller.php @@ -32,6 +32,6 @@ class Controller extends BaseController public function __construct(Application $app = null) { $this->session = $app['session']; - $this->handler = new Authentication($this->session, $app['ws.authenticator'], $app['user.provider']); + $this->handler = new Authentication($this->session, $app['ws.authenticator'], $app['security.encoder.pbkdf2'], $app['user.provider']); } }