|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @package andreoli/api-proxy-laravel |
| 5 | + * @author Michele Andreoli <michi.andreoli[at]gmail.com> |
| 6 | + * @copyright Copyright (c) Michele Andreoli |
| 7 | + * @license http://mit-license.org/ |
| 8 | + * @link https://github.com/mandreoli/api-proxy-laravel |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Andreoli\ApiProxy\Managers; |
| 12 | + |
| 13 | +use Andreoli\ApiProxy\ProxyAux; |
| 14 | +use Andreoli\ApiProxy\Models\ProxyResponse; |
| 15 | +use GuzzleHttp\Client; |
| 16 | +use GuzzleHttp\Exception\ClientException; |
| 17 | +use Andreoli\ApiProxy\Exceptions\MissingClientSecretException; |
| 18 | + |
| 19 | +class RequestManager { |
| 20 | + |
| 21 | + private $uri = null; |
| 22 | + private $method = null; |
| 23 | + private $callMode = null; |
| 24 | + private $clientSecrets = null; |
| 25 | + private $cookieManager = null; |
| 26 | + |
| 27 | + public function __construct($uri, $method, $clientSecrets, $callMode, $cookieManager) { |
| 28 | + $this->uri = $uri; |
| 29 | + $this->method = $method; |
| 30 | + $this->clientSecrets = $clientSecrets; |
| 31 | + $this->callMode = $callMode; |
| 32 | + $this->cookieManager = $cookieManager; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param $inputs |
| 37 | + * @param $parsedCookie |
| 38 | + * @return array |
| 39 | + */ |
| 40 | + public function executeRequest($inputs, $parsedCookie) { |
| 41 | + $cookie = null; |
| 42 | + switch ($this->callMode) { |
| 43 | + case ProxyAux::MODE_LOGIN: |
| 44 | + $inputs = $this->addLoginExtraParams($inputs); |
| 45 | + $proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs); |
| 46 | + |
| 47 | + $clientId = (array_key_exists(ProxyAux::CLIENT_ID, $inputs)) ? $inputs[ProxyAux::CLIENT_ID] : null; |
| 48 | + $content = $proxyResponse->getContent(); |
| 49 | + $content = ProxyAux::addQueryValue($content, ProxyAux::COOKIE_URI, $this->uri); |
| 50 | + $content = ProxyAux::addQueryValue($content, ProxyAux::COOKIE_METHOD, $this->method); |
| 51 | + $content = ProxyAux::addQueryValue($content, ProxyAux::CLIENT_ID, $clientId); |
| 52 | + |
| 53 | + $cookie = $this->cookieManager->createCookie($content); |
| 54 | + break; |
| 55 | + case ProxyAux::MODE_TOKEN: |
| 56 | + $inputs = $this->addTokenExtraParams($inputs, $parsedCookie); |
| 57 | + $proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs); |
| 58 | + |
| 59 | + //Get a new access token from refresh token if exists |
| 60 | + $ret = $this->refreshToken($proxyResponse, $inputs, $parsedCookie); |
| 61 | + $proxyResponse = $ret['response']; |
| 62 | + $cookie = $ret['cookie']; |
| 63 | + break; |
| 64 | + default: |
| 65 | + $proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs); |
| 66 | + } |
| 67 | + |
| 68 | + return array( |
| 69 | + 'response' => $proxyResponse, |
| 70 | + 'cookie' => $cookie |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param $proxyResponse |
| 76 | + * @param $inputs |
| 77 | + * @param $parsedCookie |
| 78 | + * @return array |
| 79 | + */ |
| 80 | + private function refreshToken($proxyResponse, $inputs, $parsedCookie) { |
| 81 | + $cookie = null; |
| 82 | + if ($proxyResponse->getStatusCode() != 200 && array_key_exists(ProxyAux::REFRESH_TOKEN, $parsedCookie)) { |
| 83 | + //Get a new access token from refresh token |
| 84 | + $inputs = $this->removeTokenExtraParams($inputs); |
| 85 | + $inputs = $this->addRefreshExtraParams($inputs, $parsedCookie); |
| 86 | + $proxyResponse = $this->replicateRequest($parsedCookie[ProxyAux::COOKIE_METHOD], $parsedCookie[ProxyAux::COOKIE_URI], $inputs); |
| 87 | + |
| 88 | + $content = $proxyResponse->getContent(); |
| 89 | + if ($proxyResponse->getStatusCode() === 200 && array_key_exists(ProxyAux::ACCESS_TOKEN, $content)) { |
| 90 | + $parsedCookie[ProxyAux::ACCESS_TOKEN] = $content[ProxyAux::ACCESS_TOKEN]; |
| 91 | + $parsedCookie[ProxyAux::REFRESH_TOKEN] = $content[ProxyAux::REFRESH_TOKEN]; |
| 92 | + |
| 93 | + $inputs = $this->removeRefreshTokenExtraParams($inputs); |
| 94 | + $inputs = $this->addTokenExtraParams($inputs, $parsedCookie); |
| 95 | + $proxyResponse = $this->replicateRequest($this->method, $this->uri, $inputs); |
| 96 | + |
| 97 | + //Set a new cookie with updated access token and refresh token |
| 98 | + $cookie = $this->cookieManager->createCookie($parsedCookie); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return array( |
| 103 | + 'response' => $proxyResponse, |
| 104 | + 'cookie' => $cookie |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param $method |
| 110 | + * @param $uri |
| 111 | + * @param $inputs |
| 112 | + * @return ProxyResponse |
| 113 | + */ |
| 114 | + private function replicateRequest($method, $uri, $inputs) { |
| 115 | + $guzzleResponse = $this->sendGuzzleRequest($method, $uri, $inputs); |
| 116 | + $proxyResponse = new ProxyResponse(null, $guzzleResponse->getStatusCode(), $guzzleResponse->getReasonPhrase(), $guzzleResponse->getProtocolVersion(), $this->getResponseContent($guzzleResponse)); |
| 117 | + |
| 118 | + return $proxyResponse; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param $response |
| 123 | + * @return mixed |
| 124 | + */ |
| 125 | + private function getResponseContent($response) { |
| 126 | + switch ($response->getHeader('content-type')) { |
| 127 | + case 'application/json': |
| 128 | + return $response->json(); |
| 129 | + case 'text/xml': |
| 130 | + case 'application/xml': |
| 131 | + return $response->xml(); |
| 132 | + default: |
| 133 | + return $response->getBody(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @param $method |
| 139 | + * @param $uriVal |
| 140 | + * @param $inputs |
| 141 | + * @return \GuzzleHttp\Message\FutureResponse|\GuzzleHttp\Message\ResponseInterface|\GuzzleHttp\Ring\Future\FutureInterface|mixed|null |
| 142 | + */ |
| 143 | + private function sendGuzzleRequest($method, $uriVal, $inputs) { |
| 144 | + $options = array(); |
| 145 | + $client = new Client(); |
| 146 | + if ($method === 'GET') { |
| 147 | + $options = array_add($options, 'query', $inputs); |
| 148 | + } |
| 149 | + else { |
| 150 | + $options = array_add($options, 'body', $inputs); |
| 151 | + } |
| 152 | + $request = $client->createRequest($method, $uriVal, $options); |
| 153 | + |
| 154 | + try { |
| 155 | + $response = $client->send($request); |
| 156 | + } |
| 157 | + catch (ClientException $ex) { |
| 158 | + $response = $ex->getResponse(); |
| 159 | + } |
| 160 | + |
| 161 | + return $response; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param $clientId |
| 166 | + * @return array |
| 167 | + * @throws MissingClientSecretException |
| 168 | + */ |
| 169 | + private function getClientInfo($clientId) { |
| 170 | + $info = ['id' => null, 'secret' => null]; |
| 171 | + |
| 172 | + if (isset($clientId)) { |
| 173 | + if (!array_key_exists($clientId, $this->clientSecrets)) { |
| 174 | + throw new MissingClientSecretException($clientId); |
| 175 | + } |
| 176 | + $info['id'] = $clientId; |
| 177 | + $info['secret'] = $this->clientSecrets[$clientId]; |
| 178 | + } |
| 179 | + else if (count($this->clientSecrets) >= 1) { |
| 180 | + $firstKey = key($this->clientSecrets); |
| 181 | + $info['id'] = $firstKey; |
| 182 | + $info['secret'] = $this->clientSecrets[$firstKey]; |
| 183 | + } |
| 184 | + |
| 185 | + return $info; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @param $inputs |
| 190 | + * @return array |
| 191 | + */ |
| 192 | + private function addLoginExtraParams($inputs) { |
| 193 | + //Get client secret key |
| 194 | + $clientId = (array_key_exists(ProxyAux::CLIENT_ID, $inputs)) ? $inputs[ProxyAux::CLIENT_ID] : null; |
| 195 | + $clientInfo = $this->getClientInfo($clientId); |
| 196 | + |
| 197 | + if (isset($clientInfo['id'])) { |
| 198 | + $inputs = ProxyAux::addQueryValue($inputs, ProxyAux::CLIENT_ID, $clientInfo['id']); |
| 199 | + } |
| 200 | + if (isset($clientInfo['secret'])) { |
| 201 | + $inputs = ProxyAux::addQueryValue($inputs, ProxyAux::CLIENT_SECRET, $clientInfo['secret']); |
| 202 | + } |
| 203 | + |
| 204 | + return $inputs; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @param $inputs |
| 209 | + * @param $parsedCookie |
| 210 | + * @return array |
| 211 | + */ |
| 212 | + private function addTokenExtraParams($inputs, $parsedCookie) { |
| 213 | + if (isset($parsedCookie[ProxyAux::ACCESS_TOKEN])) { |
| 214 | + $inputs = ProxyAux::addQueryValue($inputs, ProxyAux::ACCESS_TOKEN, $parsedCookie[ProxyAux::ACCESS_TOKEN]); |
| 215 | + } |
| 216 | + |
| 217 | + return $inputs; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * @param $inputs |
| 222 | + * @param $parsedCookie |
| 223 | + * @return array |
| 224 | + */ |
| 225 | + private function addRefreshExtraParams($inputs, $parsedCookie) { |
| 226 | + $inputs = ProxyAux::addQueryValue($inputs, ProxyAux::GRANT_TYPE, ProxyAux::REFRESH_TOKEN); |
| 227 | + $inputs = ProxyAux::addQueryValue($inputs, ProxyAux::REFRESH_TOKEN, $parsedCookie[ProxyAux::REFRESH_TOKEN]); |
| 228 | + if (isset($parsedCookie[ProxyAux::CLIENT_ID])) { |
| 229 | + $clientInfo = $this->getClientInfo($parsedCookie[ProxyAux::CLIENT_ID]); |
| 230 | + if (isset($clientInfo['id'])) { |
| 231 | + $inputs = ProxyAux::addQueryValue($inputs, ProxyAux::CLIENT_ID, $clientInfo['id']); |
| 232 | + } |
| 233 | + if (isset($clientInfo['secret'])) { |
| 234 | + $inputs = ProxyAux::addQueryValue($inputs, ProxyAux::CLIENT_SECRET, $clientInfo['secret']); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + return $inputs; |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * @param $inputs |
| 243 | + * @return array |
| 244 | + */ |
| 245 | + private function removeTokenExtraParams($inputs) { |
| 246 | + $inputs = ProxyAux::removeQueryValue($inputs, ProxyAux::ACCESS_TOKEN); |
| 247 | + |
| 248 | + return $inputs; |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * @param $inputs |
| 253 | + * @return array |
| 254 | + */ |
| 255 | + private function removeRefreshTokenExtraParams($inputs) { |
| 256 | + $inputs = ProxyAux::removeQueryValue($inputs, ProxyAux::GRANT_TYPE); |
| 257 | + $inputs = ProxyAux::removeQueryValue($inputs, ProxyAux::REFRESH_TOKEN); |
| 258 | + $inputs = ProxyAux::removeQueryValue($inputs, ProxyAux::CLIENT_ID); |
| 259 | + $inputs = ProxyAux::removeQueryValue($inputs, ProxyAux::CLIENT_SECRET); |
| 260 | + |
| 261 | + return $inputs; |
| 262 | + } |
| 263 | + |
| 264 | +} |
0 commit comments