alma-medical / keycloak-client
A PHP client for Keycloak REST API
Installs: 4 972
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 2
Open Issues: 2
Requires
- psr/simple-cache: ^1.0
- stevenmaguire/oauth2-keycloak: ^2.1
- symfony/cache: ^4.4.0|^5.0
- symfony/http-client: ^4.4.0|^5.0
Requires (Dev)
- phpunit/phpunit: ^8
This package is not auto-updated.
Last update: 2024-11-08 11:14:11 UTC
README
This package provides a PHP wrapper for Keycloak REST API.
Installation
Run this command to integrate the Keycloak REST API client to your existing project:
composer require alma-medical/keycloak-client
How to use it
First of all we need to configure a oauth2 provider for authenticating against Keycloak. For that purpose we use the stevenmaguire/oauth2-keycloak package, a league/oauth2-client Keycloak provider. This is an example of how to do that:
use Stevenmaguire\OAuth2\Client\Provider\Keycloak as KeycloakProvider; $provider = new KeycloakProvider([ 'authServerUrl' => 'https://my-keycloak.com/auth', 'clientId' => 'myClientId', 'clientSecret' => 'myCleintSecret', 'realm' => 'myRealm', ]);
Once we have our provider instance we need to create an API client:
use AlmaMedical\KeycloakClient\Keycloak; $client = new Keycloak($provider);
Now we can call any api method with callMedthod()
function:
$response = $client->callMethod('users');
To make easier the parse of the responses, some methods have been implemented:
Get users
This method gets all the users:
use AlmaMedical\KeycloakClient\Method\GetUsers; $getUsers = new GetUsers($client); $users = $getUsers->call();
Get user
This method get a user
use AlmaMedical\KeycloakClient\Method\GetUser; $getUsers = new GetUser($client, 'user_id'); $user = $getUsers->call();
Cache
You can use a Psr\Cache\CacheItemPoolInterface
to cache Keycloak tokens. To use it simply set your CacheItemPoolInterface
and the client will package will use it:
use Symfony\Component\Cache\Adapter\FilesystemAdapter; $client->setCachePool(new FilesystemAdapter());
Running tests
To run the tests run the following command:
./vendor/bin/phpunit