From 15510537f9844edd9a0d5d0776085041cb2f322d Mon Sep 17 00:00:00 2001 From: Martin Reinhardt Date: Fri, 2 Jun 2023 09:06:27 +0200 Subject: [PATCH] Allow configuration of keycloak client and realm (#87) Co-authored-by: Santiago --- config-template.json | 2 ++ src/Services/KeycloakService.php | 13 ++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/config-template.json b/config-template.json index af2961d..337d653 100644 --- a/config-template.json +++ b/config-template.json @@ -1,6 +1,8 @@ { "keycloak": { "baseUri": "http://keycloak:8080", + "realmName": "reconmap", + "clientId": "admin-cli", "clientSecret": "xxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyy" }, "jwt": { diff --git a/src/Services/KeycloakService.php b/src/Services/KeycloakService.php index eb3c0ca..876f1d2 100644 --- a/src/Services/KeycloakService.php +++ b/src/Services/KeycloakService.php @@ -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-----"; @@ -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()); @@ -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, @@ -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() ] @@ -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() ] ]); } } -