Skip to content

Commit

Permalink
Allow configuration of keycloak client and realm (#87)
Browse files Browse the repository at this point in the history
Co-authored-by: Santiago <santiagolizardo@users.noreply.github.com>
  • Loading branch information
hypery2k and santiagolizardo committed Jun 2, 2023
1 parent 061f0c9 commit 1551053
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 2 additions & 0 deletions config-template.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"keycloak": {
"baseUri": "http://keycloak:8080",
"realmName": "reconmap",
"clientId": "admin-cli",
"clientSecret": "xxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyy"
},
"jwt": {
Expand Down
13 changes: 6 additions & 7 deletions src/Services/KeycloakService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private function getClient(): Client

public function getPublicKey(): string
{
$realmInfoEncoded = file_get_contents($this->config['baseUri'] . '/realms/reconmap');
$realmInfoEncoded = file_get_contents($this->config['baseUri'] . '/realms/'. $this->config['realmName']);
$realmInfo = json_decode($realmInfoEncoded);
$publicKey = $realmInfo->public_key;
return "-----BEGIN PUBLIC KEY-----\n{$publicKey}\n-----END PUBLIC KEY-----";
Expand All @@ -34,10 +34,10 @@ public function getPublicKey(): string
public function getAccessToken(): string
{
$client = $this->getClient();
$response = $client->post('/realms/reconmap/protocol/openid-connect/token', [
$response = $client->post('/realms/' . $this->config['realmName'] . '/protocol/openid-connect/token', [
'form_params' => [
'grant_type' => 'client_credentials',
'client_id' => 'admin-cli',
'client_id' => $this->config['clientId'],
'client_secret' => $this->config['clientSecret']
]]);
$json = json_decode($response->getBody()->getContents());
Expand All @@ -55,7 +55,7 @@ public function createUser(User $user, string $password, string $accessToken): s
$client = $this->getClient();
list($firstName, $lastName) = explode(' ', $user->full_name);

$response = $client->post('/admin/realms/reconmap/users', [
$response = $client->post('/admin/realms/' . $this->config['realmName'] . '/users', [
'headers' => ['Authorization' => 'Bearer ' . $accessToken],
'json' => [
"firstName" => $firstName,
Expand All @@ -80,7 +80,7 @@ public function getUser(string $email)
{
$client = $this->getClient();

$client->get('/admin/realms/reconmap/users/?email=' . $email, [
$client->get('/admin/realms/' . $this->config['realmName'] . '/users/?email=' . $email, [
'headers' => [
'Authorization' => 'Bearer ' . $this->getAccessToken()
]
Expand All @@ -91,11 +91,10 @@ public function deleteUser(array $user)
{
$client = $this->getClient();

$client->delete('/admin/realms/reconmap/users/' . $user['subject_id'], [
$client->delete('/admin/realms/' . $this->config['realmName'] . '/users/' . $user['subject_id'], [
'headers' => [
'Authorization' => 'Bearer ' . $this->getAccessToken()
]
]);
}
}

0 comments on commit 1551053

Please sign in to comment.