pws-server/README.md

174 lines
4.6 KiB
Markdown
Raw Normal View History

2017-03-14 23:49:44 +00:00
# MetaTech PwsServer
PwsServer is a web application skeleton in silex2 managing web services through PwsAuth protocol
### Requirements
2017-03-15 02:43:12 +00:00
* PHP 7.0
* meta-tech/silex 2
* meta-tech/silex-core
* meta-tech/pws-client (to test ws)
2017-03-14 23:49:44 +00:00
### Install
2017-03-15 02:03:22 +00:00
The package can be installed using [ Composer ](https://getcomposer.org/).
2017-03-14 23:49:44 +00:00
```
composer require meta-tech/pws-server
```
Or add the package to your `composer.json`.
```
"require": {
2017-03-16 00:58:14 +00:00
"meta-tech/pws-server" : "^1.0"
2017-03-14 23:49:44 +00:00
}
```
2017-03-16 00:58:14 +00:00
### Usage
2017-03-14 23:49:44 +00:00
2017-03-16 00:58:14 +00:00
see [ MetaTech\Silex\Provider\ControllerServiceProvider ](https://github.com/meta-tech/silex-controller-service)
to managing controllers & routing in application
2017-03-14 23:49:44 +00:00
```php
namespace MetaTech\PwsServer;
use MetaTech\Silex\Application as App;
use MetaTech\Silex\Provider\ControllerServiceProvider as CtrlProvider;
2017-03-16 00:58:14 +00:00
use MetaTech\Silex\Provider\UserProvider;
use MetaTech\Db\PdoWrapper;
use MetaTech\Db\Profile;
2017-03-14 23:49:44 +00:00
use MetaTech\PwsAuth\Authenticator;
use MetaTech\PwsServer\Ctrl\Test;
use MetaTech\PwsServer\Ctrl\WebService;
use MetaTech\PwsServer\Ctrl\OtherWebService;
class Application extends App
{
/*!
* @method setServices
* @protected
*/
protected function setServices()
{
$app = $this;
$app['ws.authenticator'] = function ($app) {
return new Authenticator($app['config']['pwsauth']);
};
2017-03-16 00:58:14 +00:00
$app['pdo'] = function ($app) {
return new PdoWrapper(new Profile($app['config']['db']['default']));
};
$app['user.provider'] = function ($app) {
return new UserProvider($app['pdo']);
};
2017-03-14 23:49:44 +00:00
}
/*!
* @method routingDefinition
* @protected
*/
protected function routingDefinition()
{
$this->register(new CtrlProvider(Test::class , [$this], '/'));
$this->register(new CtrlProvider(WebService::class , [$this], '/ws'));
$this->register(new CtrlProvider(OtherWebService::class, [$this], '/ws/deep'));
}
}
```
Controller example :
```php
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
2017-03-16 00:58:14 +00:00
use MetaTech\PwsServer\Ws\Controller;
2017-03-14 23:49:44 +00:00
class WebService extends Controller
{
public function index(Request $request)
{
$done = true;
$msg = 'this is index';
return $this->response($done, $msg);
}
public function routing(ControllerCollection $collection) : ControllerCollection
{
$collection = parent::routing($collection);
$_ = $this->ns();
$collection->match('/', "$_:index");
return $collection;
}
}
```
2017-03-16 00:58:14 +00:00
check `OtherWebService` to see another controller and deep routes inside rooting /ws entry point.
(the main différence consist in no calling the parent routing method)
2017-03-14 23:49:44 +00:00
2017-03-16 00:58:14 +00:00
`pwsAuth` Authentication mecanism is already provided by the `MetaTech\Silex\Ws\Controller` parent class
& the `MetaTech\Silex\Ws\Authentication` handler (in [ meta-tech/silex-core](https://github.com/meta-tech/silex-core) package)
The project now implement the `checkUser` method via a `userProvider`
It use a `MetaTech\Silex\Ws\Authentication` and `MetaTech\Silex\Ws\Controller` subclasses :
```php
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;
class Authentication extends BaseAuthentication
{
protected $userProvider;
public function __construct(Session $session, Authenticator $authenticator, PasswordEncoderInterface $passEncoder = null, UserProvider $userProvider)
{
parent::__construct($session, $authenticator, $passEncoder);
$this->userProvider = $userProvider;
}
public function checkUser($login, $password, $key, PasswordEncoderInterface $passEncoder = null)
{
$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;
}
}
```
2017-03-14 23:49:44 +00:00
### Test uris :
access through web browser :
* servername/
* servername/test
access through pws-client :
* servername/ws
* servername/ws/deep
* servername/ws/isauth
### License
The project is released under the MIT license, see the LICENSE file.