inital commit

This commit is contained in:
a-sansara 2017-03-14 23:24:20 +01:00
commit 499efe73d6
10 changed files with 539 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vendor/
composer.lock

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 meta-tech.academy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# MetaTech Silex Core Package
Core package for silex2 applications

35
composer.json Normal file
View File

@ -0,0 +1,35 @@
{
"name" : "meta-tech/silex-core",
"type" : "library",
"homepage" : "https://github.com/meta-tech/silex-core",
"description" : "meta-tech silex-core package for silex2 applications",
"license" : "MIT",
"authors" : [
{
"name" : "a-Sansara",
"homepage" : "https://github.com/a-sansara/"
}
],
"keywords" : ["Silex", "Core", "WebService"],
"autoload" : {
"psr-4" : {
"" : "src/"
}
},
"require": {
"meta-tech/pws-auth" : "@dev",
"meta-tech/silex-controller-service" : "@dev",
"silex/silex": "~2.0",
"gecko-packages/gecko-silex-config-service": "^2.0"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/meta-tech/pws-auth.git"
},
{
"type": "git",
"url": "https://github.com/meta-tech/silex-controller-service.git"
}
]
}

10
config/main.yml.dist Normal file
View File

@ -0,0 +1,10 @@
env :
name : local
prod : 0
debug : 1
url : pwsserver.docker
protocol : http
info :
app_name : pwsserver
version : 1.0.2

22
config/pwsauth.yml.dist Normal file
View File

@ -0,0 +1,22 @@
type : PwsAuth2
header :
auth : Pws-Authorization
ident : Pws-Ident
salt :
common : jK5#p9Mh5.Zv}
# used for generating user specific salt
user.index : 10
user.length : 12
hash :
sep : /
algo : sha256
# effective token length size. out of bound data is simply noise
length : 52
# session index (or obfuscate length)
session.index : 58
# ending noise data length)
noise.length : 12

View File

@ -0,0 +1,83 @@
<?php
/*
* This file is part of the silex-core package.
*
* (c) meta-tech.academy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MetaTech\Silex;
use Silex\Application as BaseApplication;
use Silex\Provider\SessionServiceProvider;
use Silex\Provider\ServiceControllerServiceProvider;
use GeckoPackages\Silex\Services\Config\ConfigServiceProvider;
/*!
* @package MetaTech\Silex
* @class Application
* @extends Silex\Application
* @author a-Sansara
* @date 2017-03-12 21:46:43 CET
*/
class Application extends BaseApplication
{
/*!
* @@constrcutor
* @public
* @param [] $values
*/
public function __construct(array $values = array())
{
parent::__construct();
foreach ($values as $k => $v) {
$this[$k] = $v;
}
$this->setProviders();
$this->setServices();
$this->setGlobals();
$this->routingDefinition();
}
/*!
* @method setProviders
* @protected
*/
protected function setProviders()
{
$this->register(new ConfigServiceProvider('config'), [
'config.dir' => $this['path'].'/config/',
'config.format' => '%key%.yml'
]);
$this->register(new SessionServiceProvider());
$this->register(new ServiceControllerServiceProvider());
}
/*!
* @method setServices
* @protected
*/
protected function setServices()
{
}
/*!
* @method setGlobals
* @protected
*/
protected function setGlobals()
{
$this['debug'] = boolval($this['config']['main']['env']['debug']);
}
/*!
* @method routingDefinition
* @protected
*/
protected function routingDefinition()
{
}
}

View File

@ -0,0 +1,70 @@
<?php
/*
* This file is part of the silex-core package.
*
* (c) meta-tech.academy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MetaTech\Silex\Ctrl;
use Silex\Application;
use Silex\Api\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
/*!
* @package MetaTech\silex\Ctrl
* @class Base
* @abstract
* @implements Silex\Api\ControllerProviderInterface
* @author a-Sansara
* @date 2017-03-12 15:34:26 CET
*/
abstract class Base implements ControllerProviderInterface
{
const PRIORITY = Application::EARLY_EVENT;
const NS = 'ctrl.';
/*!
* @constrcutor
* @param Silex\Application $silex
*/
public function __construct(Application $app = null)
{
}
public function ns()
{
return static::NS . (new \ReflectionClass(static::class))->getShortName();
}
/*!
* @method init
* @public
* @param Silex\Application $app
*/
public function before(Request $request, Application $app)
{
}
/*!
* @method connect
* @public
* @param Silex\Application $app
* @return Silex\ControllerCollection
*/
public function connect(Application $app)
{
$collection = $app['controllers_factory'];
$ctrl = $this;
$collection->before(function(Request $request, Application $app) use ($ctrl) {
return $ctrl->before($request, $app);
}, static::PRIORITY);
//~ var_dump($collection);
return $this->routing($collection);
}
}

View File

@ -0,0 +1,164 @@
<?php
/*
* This file is part of the silex-core package.
*
* (c) meta-tech.academy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MetaTech\Silex\Ws;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use MetaTech\PwsAuth\Authenticator;
use MetaTech\PwsAuth\Token;
/*!
* @package MetaTech\Silex\Ws
* @class Authentication
* @author a-Sansara
* @date 2017-03-12 16:04:40 CET
*/
class Authentication
{
/*! @protected @®ar Symfony\Component\HttpFoundation\Session\Session $session */
protected $session;
/*! @protected @®ar MetaTech\PwsAuth\Authenticator $authenticator */
protected $authenticator;
/*!
* @constructor
* @public
* @param Symfony\Component\HttpFoundation\Session\Session $session
* @param MetaTech\PwsAuth\Authenticator $authenticator
*/
public function __construct(Session $session, Authenticator $authenticator)
{
$this->session = $session;
$this->authenticator = $authenticator;
}
/*!
* @method isAllowedRoute
* @public
* @param str $route
* @return bool
*/
public function isAllowedRoute($route)
{
$allowed = false;
$p = '/ws/public/';
if (in_array($route, ['/ws/auth']) || substr($route, 0, strlen($p)) == $p) {
$allowed = true;
}
return $allowed;
}
/*!
* @method sessionInvalidate
* @public
*/
public function sessionInvalidate()
{
$this->session->invalidate(1);
$this->session->save();
}
/*!
* @method checkUser
* @public
* @param str $login
* @param str $password
* @param str $key
* @return bool
*/
public function checkUser($login, $password, $key)
{
return true;
}
/*!
* @method auth
* @param Symfony\Component\HttpFoundation\Request $request
* @public
*/
public function auth(Request $request)
{
$this->sessionInvalidate();
$done = false;
$msg = 'authentication require';
$token = $this->authenticator->getToken();
if ($this->authenticator->isValid($token)) {
$login = $request->get('login');
$password = $request->get('password');
if ($done = $this->authenticator->check($token, $login)) {
if ($this->checkUser($login, $password, $token->getIdent())) {
$sid = $this->onSuccess($token, $login);
$msg = "authentication sucessful ! logged as $login";
$data = compact('sid');
}
}
}
return new JsonResponse(compact('done', 'msg', 'data'), $done ? 200 : 401);
}
/*!
* @method onsuccess
* @public
* @param MetaTech\PwsAuth\Token $token
* @param str $login
*/
public function onsuccess(Token $token, $login)
{
$this->session->start();
$sid = $this->session->getId();
$user = new \stdclass();
$user->key = $token->getIdent();
$user->login = $login;
$this->session->set('user', $user);
$this->session->save();
return $sid;
}
/*!
* @method check
* @public
* @param Symfony\Component\HttpFoundation\Request $request
* @return void | Symfony\Component\HttpFoundation\JsonResponse
*/
public function check(Request $request)
{
if (!$this->isAllowedRoute($request->getPathInfo())) {
$this->sessionInvalidate();
$done = false;
$msg = "authentication require";
try {
$token = $this->authenticator->getToken();
if ($this->authenticator->isValid($token)) {
$sid = $this->authenticator->getSessionId($token);
$this->session->setId($sid);
$this->session->start();
$user = $this->session->get('user');
// done : lets controller takes hand
if (!is_null($user) && $user->key == $token->getIdent()) {
$user->wskey = $token->getValue();
$this->session->set('user', $user);
return;
}
else {
$this->sessionInvalidate();
}
}
}
catch(\Exception $e) {
$done = false;
$msg = $e->getMessage();
}
return new JsonResponse(compact('done', 'msg'), 401);
}
}
}

View File

@ -0,0 +1,129 @@
<?php
/*
* This file is part of the silex-core package.
*
* (c) meta-tech.academy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace MetaTech\Silex\Ws;
use Silex\Application;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use MetaTech\Silex\Ctrl\Base;
use MetaTech\Silex\Ws\Authentication;
/*!
* @package MetaTech\Silex\Ws
* @class Controller
* @extends MetaTech\Core\Ctrl\Base
* @author a-Sansara
* @date 2017-03-12 15:39:30 CET
*/
class Controller extends Base
{
/*! @protected @var MetaTech\Core\Ws\Authentication $handler */
protected $handler;
/*! @protected @var Symfony\Component\HttpFoundation\Session\Session $session */
protected $session;
/*!
* @constrcutor
* @public
* @param Silex\Application $app
*/
public function __construct(Application $app = null)
{
$this->session = $app['session'];
$this->handler = new Authentication($this->session, $app['ws.authenticator']);
}
/*!
* @method response
* @public
* @param bool $done
* @param str $msg
* @param [] $data
* @return Symfony\Component\HttpFoundation\JsonResponse
*/
public function response($done = false, $msg = "fail", $data = null)
{
if (is_null($data)) {
unset($data);
}
$response = new JsonResponse(compact('done', 'msg', 'data'), 200);
return $response;
}
/*!
* @method before
* @public
* @param Symfony\Component\HttpFoundation\Request $request
* @param Silex\Application $app
* @return
*/
public function before(Request $request, Application $app)
{
return $this->handler->check($request);
}
/*!
* @method auth
* @public
* @return Symfony\Component\HttpFoundation\JsonResponse
*/
public function auth(Request $request)
{
return $this->handler->auth($request);
}
/*!
* Authentication handler already check that user is authenticate.
* This is just the response
*
* @method isAuthenticate
* @public
* @return Symfony\Component\HttpFoundation\JsonResponse
*/
public function isAuthenticate()
{
$done = true;
$user = $this->session->get('user');
$msg = 'logged as '.$user->login;
return $this->response($done, $msg);
}
/*!
* @method logout
* @public
* @return Symfony\Component\HttpFoundation\JsonResponse
*/
public function logout()
{
$this->handler->sessionInvalidate();
$sessid = $this->session->getId();
$done = true;
$msg = 'session logout';
return $this->response($done, $msg);
}
/*!
* @method routing
* @public
* @param Silex\ControllerCollection $collection
* @return Silex\ControllerCollection
*/
public function routing(ControllerCollection $collection) : ControllerCollection
{
$_ = $this->ns();
$collection->match('/auth' , "$_:auth");
$collection->match('/logout' , "$_:logout");
$collection->match('/isauth' , "$_:isAuthenticate");
return $collection;
}
}