From 115e64a93e34f61b7d36235610e224f2b58057b7 Mon Sep 17 00:00:00 2001 From: Yansell Rivas Diaz Date: Thu, 6 May 2021 16:01:04 +0000 Subject: [PATCH] feat(IDCIKeycloakSecurityBundle) Fix error on RequestService * Validating 302 response on keycloak request service. --- Service/RequestService.php | 83 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) mode change 100644 => 100755 Service/RequestService.php diff --git a/Service/RequestService.php b/Service/RequestService.php old mode 100644 new mode 100755 index c4686a4..6f3b170 --- a/Service/RequestService.php +++ b/Service/RequestService.php @@ -2,6 +2,7 @@ namespace NTI\KeycloakSecurityBundle\Service; +use AppBundle\Util\StringUtils; use Doctrine\ORM\EntityManager; use Exception; use GuzzleHttp\Exception\GuzzleException; @@ -79,6 +80,10 @@ public function __construct(ContainerInterface $container) { ); } + /** + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ protected function refreshToken(){ $configuration = $this->em->getRepository('KeycloakSecurityBundle:KeycloakApiConfiguration')->findOneBy(array("environment" => $this->environment)); if(!$configuration) { @@ -110,7 +115,18 @@ protected function refreshToken(){ protected function restGet($path){ try { $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); + //Check if cookies exists + self::_checkCookie(); $response = $client->request('GET', $path, $this->headers); + //Make request and verify if response with 302 + if($response->code === 302){ + $this->container->get('session')->set('keycloak-cookie',$response->headers["set-cookie"]); + $reponse_header = array_merge($this->options,[ + "cookie" => $response->headers["set-cookie"], + 'allow_redirects' => false + ]); + $response = $client->request('GET', $path, $this->headers); + } return $response->getBody()->getContents(); } catch (RequestException $e) { if($e->getResponse()->getStatusCode() === 401 || $e->getResponse()->getStatusCode() === 403){ @@ -133,7 +149,18 @@ protected function restGet($path){ protected function restPost($path, $data, $type = "json"){ try { $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); + //Check if cookies exists + self::_checkCookie(); $response = $client->request('POST', $path, array_merge($this->headers, array($type => $data))); + //Make request and verify if response with 302 + if($response->code === 302){ + $this->container->get('session')->set('keycloak-cookie',$response->headers["set-cookie"]); + $reponse_header = array_merge($this->options,[ + "cookie" => $response->headers["set-cookie"], + 'allow_redirects' => false + ]); + $response = $client->request('POST', $path, array_merge($this->headers, array($type => $data))); + } return $response->getBody()->getContents(); } catch (RequestException $e) { if($e->getResponse()->getStatusCode() === 401 || $e->getResponse()->getStatusCode() === 403){ @@ -147,10 +174,28 @@ protected function restPost($path, $data, $type = "json"){ } } + /** + * @param $path + * @param $data + * @param string $type + * @return string|Response + * @throws GuzzleException + */ protected function restPut($path, $data, $type = "json"){ try { $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); + //Check if cookies exists + self::_checkCookie(); $response = $client->request('PUT', $path, array_merge($this->headers, array($type => $data))); + //Make request and verify if response with 302 + if($response->code === 302){ + $this->container->get('session')->set('keycloak-cookie',$response->headers["set-cookie"]); + $reponse_header = array_merge($this->options,[ + "cookie" => $response->headers["set-cookie"], + 'allow_redirects' => false + ]); + $response = $client->request('PUT', $path, array_merge($this->headers, array($type => $data))); + } return $response->getBody()->getContents(); } catch (RequestException $e) { if($e->getResponse()->getStatusCode() === 401 || $e->getResponse()->getStatusCode() === 403){ @@ -173,7 +218,18 @@ protected function restPut($path, $data, $type = "json"){ protected function restPatch($path, $data, $type = "json"){ try { $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); + //Check if cookies exists + self::_checkCookie(); $response = $client->request('PATCH', $path, array_merge($this->headers, array($type => $data))); + //Make request and verify if response with 302 + if($response->code === 302){ + $this->container->get('session')->set('keycloak-cookie',$response->headers["set-cookie"]); + $reponse_header = array_merge($this->options,[ + "cookie" => $response->headers["set-cookie"], + 'allow_redirects' => false + ]); + $response = $client->request('PATCH', $path, array_merge($this->headers, array($type => $data))); + } return $response->getBody()->getContents(); } catch (RequestException $e) { if($e->getResponse()->getStatusCode() === 401 || $e->getResponse()->getStatusCode() === 403){ @@ -194,7 +250,18 @@ protected function restPatch($path, $data, $type = "json"){ protected function restDelete($path, $data = null, $type = "json"){ try { $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); + //Check if cookies exists + self::_checkCookie(); $response = $client->request('DELETE', $path, array_merge($this->headers, array($type => $data))); + //Make request and verify if response with 302 + if($response->code === 302){ + $this->container->get('session')->set('keycloak-cookie',$response->headers["set-cookie"]); + $reponse_header = array_merge($this->options,[ + "cookie" => $response->headers["set-cookie"], + 'allow_redirects' => false + ]); + $response = $client->request('DELETE', $path, array_merge($this->headers, array($type => $data))); + } return $response->getBody()->getContents(); } catch (RequestException $e) { if($e->getResponse()->getStatusCode() === 401 || $e->getResponse()->getStatusCode() === 403){ @@ -208,4 +275,20 @@ protected function restDelete($path, $data = null, $type = "json"){ } } + /** + * @throws Exception + */ + public function _checkCookie(){ + $cookie = $this->container->get('session')->get('keycloak-cookie') ?? null; + if(null !== $cookie){ + $cookieObj = StringUtils::CreateCookieFromString($cookie); + $now = new \DateTime(); + if($cookieObj["expires"] > $now->getTimestamp()){ + $this->headers = array_merge($this->headers,[ + "cookie" => $cookie, + 'allow_redirects' => false + ]); + } + } + } } \ No newline at end of file