From 5c996d7d16618c4af3a690458cf3a243eddb5800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samir=20Compr=C3=A9s?= Date: Mon, 5 Oct 2020 11:22:02 -0400 Subject: [PATCH] feat(Requests): Added keycloak security service & set request service type as an option --- Resources/config/services.yaml | 5 ++++ Service/KeycloakSecurityService.php | 37 +++++++++++++++++++++++++++++ Service/RequestService.php | 16 ++++++------- 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 Service/KeycloakSecurityService.php diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 592f82d..00af591 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -39,4 +39,9 @@ services: nti.keycloak.admin.role.service: public: true class: NTI\KeycloakSecurityBundle\Service\KeycloakAdminRoleService + arguments: ["@service_container"] + + nti.keycloak.security.service: + public: true + class: NTI\KeycloakSecurityBundle\Service\KeycloakSecurityService arguments: ["@service_container"] \ No newline at end of file diff --git a/Service/KeycloakSecurityService.php b/Service/KeycloakSecurityService.php new file mode 100644 index 0000000..fb4735c --- /dev/null +++ b/Service/KeycloakSecurityService.php @@ -0,0 +1,37 @@ +basePath = str_replace("{realm}", $this->container->getParameter(self::KEYCLOAK_REALM), $this->basePath); + } + + public function getToken($username, $password) { + $url = $this->basePath.self::GET_TOKEN_URL; + + $options = array( + 'client_id' => $this->container->getParameter(self::KEYCLOAK_CLIENT_ID), + 'client_secret' => $this->container->getParameter(self::KEYCLOAK_CLIENT_SECRET), + 'username' => $username, + 'password' => $password, + 'grant_type' => $this::PASSWORD_GRANT_TYPE + ); + + $result = $this->restPost($url, $options, 'form_params'); + $response = json_decode($result, true); + return $response; + } + +} \ No newline at end of file diff --git a/Service/RequestService.php b/Service/RequestService.php index 4d7aa84..4d06a12 100644 --- a/Service/RequestService.php +++ b/Service/RequestService.php @@ -76,20 +76,20 @@ protected function restGet($path){ * @return string * @throws \GuzzleHttp\Exception\GuzzleException */ - protected function restPost($path, $data){ + protected function restPost($path, $data, $type = "json"){ $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); $response = $client->request('POST', $path, array_merge($this->headers, array( - "json" => $data + $type => $data ))); return $response->getBody()->getContents(); } - protected function restPut($path, $data){ + protected function restPut($path, $data, $type = "json"){ $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); $response = $client->request('PUT', $path, array_merge($this->headers, array( - "json" => $data + $type => $data ))); return $response->getBody()->getContents(); } @@ -100,10 +100,10 @@ protected function restPut($path, $data){ * @return string * @throws \GuzzleHttp\Exception\GuzzleException */ - protected function restPatch($path, $data){ + protected function restPatch($path, $data, $type = "json"){ $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); $response = $client->request('PATCH', $path, array_merge($this->headers, array( - "json" => $data + $type => $data ))); return $response->getBody()->getContents(); } @@ -112,12 +112,12 @@ protected function restPatch($path, $data){ * @param $path * @return Response */ - protected function restDelete($path, $data = null){ + protected function restDelete($path, $data = null, $type = "json"){ $client = new \GuzzleHttp\Client(array('base_uri' => $this->baseUrl)); $response = null; try { $response = $client->request('DELETE', $path, array_merge($this->headers, array( - "json" => $data + $type => $data ))); } catch (RequestException $e) { if ($e->hasResponse()) {