commit 499efe73d699de6751de3a3011a2a110bee3f4f0 Author: a-sansara Date: Tue Mar 14 23:24:20 2017 +0100 inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d1502b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/ +composer.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d9b7477 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f300e1 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# MetaTech Silex Core Package + +Core package for silex2 applications diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..7520d2a --- /dev/null +++ b/composer.json @@ -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" + } + ] +} diff --git a/config/main.yml.dist b/config/main.yml.dist new file mode 100644 index 0000000..6e0186b --- /dev/null +++ b/config/main.yml.dist @@ -0,0 +1,10 @@ +env : + name : local + prod : 0 + debug : 1 + url : pwsserver.docker + protocol : http + +info : + app_name : pwsserver + version : 1.0.2 diff --git a/config/pwsauth.yml.dist b/config/pwsauth.yml.dist new file mode 100644 index 0000000..e2b15d3 --- /dev/null +++ b/config/pwsauth.yml.dist @@ -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 + diff --git a/src/MetaTech/Silex/Application.php b/src/MetaTech/Silex/Application.php new file mode 100644 index 0000000..bbfe3e6 --- /dev/null +++ b/src/MetaTech/Silex/Application.php @@ -0,0 +1,83 @@ + $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() + { + + } +} diff --git a/src/MetaTech/Silex/Ctrl/Base.php b/src/MetaTech/Silex/Ctrl/Base.php new file mode 100644 index 0000000..44d8970 --- /dev/null +++ b/src/MetaTech/Silex/Ctrl/Base.php @@ -0,0 +1,70 @@ +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); + } +} diff --git a/src/MetaTech/Silex/Ws/Authentication.php b/src/MetaTech/Silex/Ws/Authentication.php new file mode 100644 index 0000000..bc38f81 --- /dev/null +++ b/src/MetaTech/Silex/Ws/Authentication.php @@ -0,0 +1,164 @@ +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); + } + } +} diff --git a/src/MetaTech/Silex/Ws/Controller.php b/src/MetaTech/Silex/Ws/Controller.php new file mode 100644 index 0000000..c95e926 --- /dev/null +++ b/src/MetaTech/Silex/Ws/Controller.php @@ -0,0 +1,129 @@ +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; + } +}