version 1.0.1 - manage ws Authentication via userProvider

This commit is contained in:
a-sansara 2017-03-15 18:04:31 +01:00 committed by a-sansara
parent dc0b7533b7
commit e875e3c0d5
6 changed files with 67 additions and 13 deletions

7
config/db.yml Normal file
View File

@ -0,0 +1,7 @@
default :
driver : pdo_mysql
host : db
dbname : pwsserver
user : dev
password : mysql
charset : utf8

12
config/security.yml Normal file
View File

@ -0,0 +1,12 @@
security.firewalls :
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/ws
anonymous: true
security.params :
sleep : 3

View File

@ -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');

View File

@ -11,6 +11,9 @@ namespace MetaTech\PwsServer;
use MetaTech\Silex\Application as App; use MetaTech\Silex\Application as App;
use MetaTech\Silex\Provider\ControllerServiceProvider as CtrlProvider; 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\PwsAuth\Authenticator;
use MetaTech\PwsServer\Ctrl\Test; use MetaTech\PwsServer\Ctrl\Test;
use MetaTech\PwsServer\Ctrl\WebService; use MetaTech\PwsServer\Ctrl\WebService;
@ -35,8 +38,11 @@ class Application extends App
$app['ws.authenticator'] = function ($app) { $app['ws.authenticator'] = function ($app) {
return new Authenticator($app['config']['pwsauth']); return new Authenticator($app['config']['pwsauth']);
}; };
$app['pdo'] = function ($app) {
return new PdoWrapper(new Profile($app['config']['db']['default']));
};
$app['user.provider'] = function ($app) { $app['user.provider'] = function ($app) {
return null; return new UserProvider($app['pdo']);
}; };
} }

View File

@ -10,8 +10,11 @@
namespace MetaTech\PwsServer\Ws; namespace MetaTech\PwsServer\Ws;
use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use MetaTech\PwsAuth\Authenticator; use MetaTech\PwsAuth\Authenticator;
use MetaTech\Silex\Ws\Authentication as BaseAuthentication; use MetaTech\Silex\Ws\Authentication as BaseAuthentication;
use MetaTech\Silex\Provider\UserProvider;
/*! /*!
* @package MetaTech\PwsServer\Ws * @package MetaTech\PwsServer\Ws
@ -29,11 +32,13 @@ class Authentication extends BaseAuthentication
* @public * @public
* @param Symfony\Component\HttpFoundation\Session\Session $session * @param Symfony\Component\HttpFoundation\Session\Session $session
* @param MetaTech\PwsAuth\Authenticator $authenticator * @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); parent::__construct($session, $authenticator, $passEncoder);
$this->userOrovider = $userProvider; $this->userProvider = $userProvider;
} }
/*! /*!
@ -42,11 +47,22 @@ class Authentication extends BaseAuthentication
* @param str $login * @param str $login
* @param str $password * @param str $password
* @param str $key * @param str $key
* @param Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface $passEncoder
* @return bool * @return bool
*/ */
public function checkUser($login, $password, $key) public function checkUser($login, $password, $key, PasswordEncoderInterface $passEncoder = null)
{ {
// @todo implements with userProvider $done = false;
return true; 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;
} }
} }

View File

@ -32,6 +32,6 @@ class Controller extends BaseController
public function __construct(Application $app = null) public function __construct(Application $app = null)
{ {
$this->session = $app['session']; $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']);
} }
} }