manage responseHeader

This commit is contained in:
a-sansara 2017-03-21 01:35:31 +01:00
parent 3f484c6481
commit e20bef3b9a
2 changed files with 33 additions and 10 deletions

View File

@ -95,11 +95,13 @@ class Authentication
public function auth(Request $request) public function auth(Request $request)
{ {
$this->sessionInvalidate(); $this->sessionInvalidate();
$done = false; $done = false;
$msg = 'authentication require'; $msg = 'authentication require';
$token = $this->authenticator->getToken(); $token = $this->authenticator->getToken();
$login = $request->get('login');
$responseToken = $this->authenticator->generateResponseHeader($token, $login);
$headers = $this->getResponseHeaders($responseToken);
if ($this->authenticator->isValid($token)) { if ($this->authenticator->isValid($token)) {
$login = $request->get('login');
$password = $request->get('password'); $password = $request->get('password');
if ($this->authenticator->check($token, $login)) { if ($this->authenticator->check($token, $login)) {
try { try {
@ -117,7 +119,21 @@ class Authentication
if (!$done) { if (!$done) {
sleep(3); sleep(3);
} }
return new JsonResponse(compact('done', 'msg', 'data'), $done ? 200 : 401); return new JsonResponse(compact('done', 'msg', 'data'), $done ? 200 : 401, $headers);
}
/*!
* @method getResponseHeaders
* @private
* @param [assoc] $headers
* @return [assoc]
*/
private function getResponseHeaders($headers=[], $tokenResponse=null)
{
if (!empty($tokenResponse) || !empty($tokenResponse = $this->session->get('pwsauth.response'))) {
$headers['Pws-Response'] = $tokenResponse;
}
return $headers
} }
/*! /*!
@ -134,6 +150,7 @@ class Authentication
$user->key = $token->getIdent(); $user->key = $token->getIdent();
$user->login = $login; $user->login = $login;
$this->session->set('user', $user); $this->session->set('user', $user);
$this->session->set('pwsauth.response', $this->authenticator->generateResponseHeader($token, $login));
$this->session->save(); $this->session->save();
return $sid; return $sid;
} }
@ -147,8 +164,9 @@ class Authentication
public function check(Request $request) public function check(Request $request)
{ {
if (!$this->isAllowedRoute($request->getPathInfo())) { if (!$this->isAllowedRoute($request->getPathInfo())) {
$done = false; $done = false;
$msg = "authentication require"; $msg = "authentication require";
$headers = [];
try { try {
$token = $this->authenticator->getToken(); $token = $this->authenticator->getToken();
@ -158,9 +176,10 @@ class Authentication
$this->session->setId($sid); $this->session->setId($sid);
$this->session->start(); $this->session->start();
$user = $this->session->get('user'); $user = $this->session->get('user');
$data = compact('user');
// done : lets controller takes hand // done : lets controller takes hand
if (!is_null($user) && $user->key == $token->getIdent()) { if (!is_null($user) && $user->key == $token->getIdent()) {
$tokenResponse = $this->authenticator->generateResponseHeader($token, $user->login)
$this->session->set('pwsauth.response', $tokenResponse);
return; return;
} }
else { else {
@ -173,7 +192,7 @@ class Authentication
$done = false; $done = false;
$msg = $e->getMessage(); $msg = $e->getMessage();
} }
return new JsonResponse(compact('done', 'msg', 'data'), 401); return new JsonResponse(compact('done', 'msg', 'data'), 401, $headers);
} }
} }
} }

View File

@ -54,7 +54,11 @@ class Controller extends Base
if (is_null($data)) { if (is_null($data)) {
unset($data); unset($data);
} }
$response = new JsonResponse(compact('done', 'msg', 'data'), 200); $headers = [];
if (!empty($tokenResponse = $this->session->get('pwsauth.response'))) {
$headers['Pws-Response'] = $tokenResponse;
}
$response = new JsonResponse(compact('done', 'msg', 'data'), 200, $headers);
return $response; return $response;
} }