forked from IDCI-Consulting/IDCIKeycloakSecurityBundle
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Requests): Added keycloak security service & set request service…
… type as an option
- Loading branch information
Samir Comprés
committed
Oct 5, 2020
1 parent
fcd3866
commit 5c996d7
Showing
3 changed files
with
50 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters