Skip to content

Commit

Permalink
feat(Requests): Added keycloak security service & set request service…
Browse files Browse the repository at this point in the history
… type as an option
  • Loading branch information
Samir Comprés committed Oct 5, 2020
1 parent fcd3866 commit 5c996d7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
37 changes: 37 additions & 0 deletions Service/KeycloakSecurityService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace NTI\KeycloakSecurityBundle\Service;

use NTI\KeycloakSecurityBundle\Service\RequestService;
use Symfony\Component\DependencyInjection\ContainerInterface;

class KeycloakSecurityService extends RequestService {

protected $basePath = "/auth/realms/{realm}";

const GET_TOKEN_URL = "/protocol/openid-connect/token";

const PASSWORD_GRANT_TYPE = 'password';

public function __construct(ContainerInterface $container) {
parent::__construct($container);
$this->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;
}

}
16 changes: 8 additions & 8 deletions Service/RequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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()) {
Expand Down

0 comments on commit 5c996d7

Please sign in to comment.