session = $app['session']; $this->handler = new Authentication($this->session, $app['ws.authenticator'], $app['security.default_encoder']); } /*! * @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; } }