Skip to content

Commit 7176e13

Browse files
committed
fixed cookie if refresh token not supplied
1 parent f992dd9 commit 7176e13

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"API",
77
"ajax",
88
"proxy",
9-
"javascript"
9+
"javascript",
10+
"oauth"
1011
],
1112
"authors": [
1213
{

src/Andreoli/ApiProxy/ApiProxyServiceProvider.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,22 @@ public function provides() {
7373
*/
7474
private function registerErrorHandlers() {
7575
$this->app->error(function(ProxyException $ex) {
76-
if (\Request::ajax() && \Request::wantsJson()) {
76+
//if (\Request::ajax() && \Request::wantsJson()) {
7777
return new JsonResponse([
7878
'error' => $ex->errorType,
7979
'error_description' => $ex->getMessage()
8080
], $ex->httpStatusCode, $ex->getHttpHeaders()
8181
);
82-
}
82+
//}
8383

84+
/*
8485
return \View::make('api-proxy-laravel::proxy_error', array(
8586
'header' => $ex->getHttpHeaders()[0],
8687
'code' => $ex->httpStatusCode,
8788
'error' => $ex->errorType,
8889
'message' => $ex->getMessage()
8990
));
91+
*/
9092
});
9193
}
9294

src/Andreoli/ApiProxy/Managers/CookieManager.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ public function validateCookie($parsedCookie) {
8484
if (!array_key_exists(ProxyAux::TOKEN_EXPIRES, $parsedCookie)) {
8585
throw new CookieInvalidException(ProxyAux::TOKEN_EXPIRES);
8686
}
87-
if (!array_key_exists(ProxyAux::REFRESH_TOKEN, $parsedCookie)) {
88-
throw new CookieInvalidException(ProxyAux::REFRESH_TOKEN);
89-
}
9087
if (!array_key_exists(ProxyAux::COOKIE_URI, $parsedCookie)) {
9188
throw new CookieInvalidException(ProxyAux::COOKIE_URI);
9289
}

src/Andreoli/ApiProxy/Managers/RequestManager.php

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,18 @@ public function executeRequest($inputs, $parsedCookie) {
6262
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs);
6363

6464
//Get a new access token from refresh token if exists
65-
$ret = $this->refreshToken($proxyResponse, $inputs, $parsedCookie);
66-
$proxyResponse = $ret['response'];
67-
$cookie = $ret['cookie'];
65+
$cookie = null;
66+
if ($proxyResponse->getStatusCode() != 200) {
67+
if (array_key_exists(ProxyAux::REFRESH_TOKEN, $parsedCookie)) {
68+
$ret = $this->tryRefreshToken($inputs, $parsedCookie);
69+
}
70+
else {
71+
$cookie = $this->cookieManager->destroyCookie();
72+
}
73+
}
74+
75+
$proxyResponse = (isset($ret)) ? $ret['response'] : $proxyResponse;
76+
$cookie = (isset($ret)) ? $ret['cookie'] : $cookie;
6877
break;
6978
default:
7079
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs);
@@ -77,41 +86,37 @@ public function executeRequest($inputs, $parsedCookie) {
7786
}
7887

7988
/**
80-
* @param $proxyResponse
8189
* @param $inputs
8290
* @param $parsedCookie
8391
* @return array
8492
*/
85-
private function refreshToken($proxyResponse, $inputs, $parsedCookie) {
86-
$cookie = null;
87-
if ($proxyResponse->getStatusCode() != 200 && array_key_exists(ProxyAux::REFRESH_TOKEN, $parsedCookie)) {
88-
$this->callMode = ProxyAux::MODE_REFRESH;
93+
private function tryRefreshToken($inputs, $parsedCookie) {
94+
$this->callMode = ProxyAux::MODE_REFRESH;
8995

90-
//TODO: remove and save additional params
96+
//TODO: remove and save additional params
9197

92-
//Get a new access token from refresh token
93-
$inputs = $this->removeTokenExtraParams($inputs);
94-
$inputs = $this->addRefreshExtraParams($inputs, $parsedCookie);
95-
$proxyResponse = $this->replicateRequest($parsedCookie[ProxyAux::COOKIE_METHOD], $parsedCookie[ProxyAux::COOKIE_URI], $inputs);
98+
//Get a new access token from refresh token
99+
$inputs = $this->removeTokenExtraParams($inputs);
100+
$inputs = $this->addRefreshExtraParams($inputs, $parsedCookie);
101+
$proxyResponse = $this->replicateRequest($parsedCookie[ProxyAux::COOKIE_METHOD], $parsedCookie[ProxyAux::COOKIE_URI], $inputs);
96102

97-
$content = $proxyResponse->getContent();
98-
if ($proxyResponse->getStatusCode() === 200 && array_key_exists(ProxyAux::ACCESS_TOKEN, $content)) {
99-
$this->callMode = ProxyAux::MODE_TOKEN;
100-
$parsedCookie[ProxyAux::ACCESS_TOKEN] = $content[ProxyAux::ACCESS_TOKEN];
101-
$parsedCookie[ProxyAux::REFRESH_TOKEN] = $content[ProxyAux::REFRESH_TOKEN];
103+
$content = $proxyResponse->getContent();
104+
if ($proxyResponse->getStatusCode() === 200 && array_key_exists(ProxyAux::ACCESS_TOKEN, $content)) {
105+
$this->callMode = ProxyAux::MODE_TOKEN;
106+
$parsedCookie[ProxyAux::ACCESS_TOKEN] = $content[ProxyAux::ACCESS_TOKEN];
107+
$parsedCookie[ProxyAux::REFRESH_TOKEN] = $content[ProxyAux::REFRESH_TOKEN];
102108

103-
//TODO: add additional saved params
109+
//TODO: add additional saved params
104110

105-
$inputs = $this->removeRefreshTokenExtraParams($inputs);
106-
$inputs = $this->addTokenExtraParams($inputs, $parsedCookie);
107-
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs);
111+
$inputs = $this->removeRefreshTokenExtraParams($inputs);
112+
$inputs = $this->addTokenExtraParams($inputs, $parsedCookie);
113+
$proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs);
108114

109-
//Set a new cookie with updated access token and refresh token
110-
$cookie = $this->cookieManager->createCookie($parsedCookie);
111-
}
112-
else {
113-
$cookie = $this->cookieManager->destroyCookie();
114-
}
115+
//Set a new cookie with updated access token and refresh token
116+
$cookie = $this->cookieManager->createCookie($parsedCookie);
117+
}
118+
else {
119+
$cookie = $this->cookieManager->destroyCookie();
115120
}
116121

117122
return array(

src/Andreoli/ApiProxy/Proxy.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Proxy {
2727
private $cookieManager = null;
2828
private $useHeader = false;
2929

30+
/**
31+
* @param $params
32+
*/
3033
public function __construct($params) {
3134
$this->uriParam = $params['uri_param'];
3235
$this->skipParam = $params['skip_param'];
@@ -40,7 +43,9 @@ public function __construct($params) {
4043
* @param $method
4144
* @param array $inputs
4245
* @return Response
46+
* @throws CookieExpiredException
4347
* @throws ProxyMissingParamException
48+
* @throws \Exception
4449
*/
4550
public function makeRequest($method, Array $inputs) {
4651
$this->checkMandatoriesInputParams($inputs);
@@ -49,7 +54,7 @@ public function makeRequest($method, Array $inputs) {
4954
//Retrieve the call mode from input parameters
5055
$this->callMode = $this->getRequestMode($inputs);
5156

52-
//Remove unuseful parameters from inputs
57+
//Remove parameters from inputs
5358
$inputs = ProxyAux::removeQueryValue($inputs, $this->uriParam);
5459
$inputs = ProxyAux::removeQueryValue($inputs, $this->skipParam);
5560

@@ -62,7 +67,7 @@ public function makeRequest($method, Array $inputs) {
6267
if (isset($this->redirectUri) && !empty($this->redirectUri)) {
6368
return \Redirect::to($this->redirectUri);
6469
}
65-
throw new $ex;
70+
throw $ex;
6671
}
6772
}
6873

0 commit comments

Comments
 (0)