Skip to content

Commit

Permalink
feat(IDCIKeycloakSecurityBundle) Fix error on RequestService
Browse files Browse the repository at this point in the history
 * Validating 302 response on keycloak request service.
  • Loading branch information
Yansell Rivas Diaz committed May 6, 2021
1 parent 656f5bd commit 115e64a
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Service/RequestService.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NTI\KeycloakSecurityBundle\Service;

use AppBundle\Util\StringUtils;
use Doctrine\ORM\EntityManager;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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){
Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -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){
Expand All @@ -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
]);
}
}
}
}

0 comments on commit 115e64a

Please sign in to comment.