Skip to content

rzougabenoma/keycloak-rest-api-client-php

 
 

Repository files navigation

PHP Analysis PHP Unit PHP Integration (Keycloak compatibility) PHP Legacy (Keycloak compatibility)

Keycloak Admin REST API Client

PHP client to interact with Keycloak's Admin REST API.

Inspired by keycloak/keycloak-nodejs-admin-client.

Note: This library is WIP and can not be considered stable yet - do not use this in production.

Installation

Install via Composer:

composer require fschmtt/keycloak-rest-api-client-php

Usage

Example:

$keycloak = new \Fschmtt\Keycloak\Keycloak(
    baseUrl: 'http://keycloak:8080',
    username: 'admin',
    password: 'admin'
    version: '20.0.1'
);

$serverInfo = $keycloak->serverInfo()->get();

echo sprintf(
    'Keycloak %s is running on %s/%s (%s) with %s/%s since %s and is currently using %s of %s (%s %%) memory.',
    $serverInfo->getSystemInfo()->getVersion(),
    $serverInfo->getSystemInfo()->getOsName(),
    $serverInfo->getSystemInfo()->getOsVersion(),
    $serverInfo->getSystemInfo()->getOsArchitecture(),
    $serverInfo->getSystemInfo()->getJavaVm(),
    $serverInfo->getSystemInfo()->getJavaVersion(),
    $serverInfo->getSystemInfo()->getUptime(),
    $serverInfo->getMemoryInfo()->getUsedFormated(),
    $serverInfo->getMemoryInfo()->getTotalFormated(),
    100 - $serverInfo->getMemoryInfo()->getFreePercentage(),
);

will print e.g.

Keycloak 20.0.1 is running on Linux/5.10.25-linuxkit (amd64) with OpenJDK 64-Bit Server VM/11.0.11 since 0 days, 2 hours, 37 minutes, 7 seconds and is currently using 139 MB of 512 MB (28 %) memory.

More examples can be found in the examples directory.

Available Resources

Endpoint Status Code Response API
DELETE /auth/admin/realms/{realm}/attack-detection/brute-force/users 204 n/a AttackDetection::clear()
GET /auth/admin/realms/{realm}/attack-detection/brute-force/users/{userId} 204 Map AttackDetection::user()
DELETE /auth/admin/realms/{realm}/attack-detection/brute-force/users/{userId} 204 n/a AttackDetection::clearUser()
Endpoint Status Code Response API
POST /auth/admin/realms 201 Realm Realms::import()
GET /auth/admin/realms 200 RealmCollection Realms::all()
PUT /auth/admin/realms/{realm} 204 Realm Realms::update()
DELETE /auth/admin/realms/{realm} 204 n/a Realms::delete()
GET /auth/admin/realms/{realm}/admin-events 200 array Realms::adminEvents()
DELETE /auth/admin/realms/{realm}/admin-events 204 n/a Realms::deleteAdminEvents()
POST /auth/admin/realms/{realm}/clear-keys-cache 204 n/a Realms::clearKeysCache()
POST /auth/admin/realms/{realm}/clear-realm-cache 204 n/a Realms::clearRealmCache()
POST /auth/admin/realms/{realm}/clear-user-cache 204 n/a Realms::clearUserCache()
GET /auth/admin/realms/{realm}/clients 200 ClientCollection Realms::clients()
GET /auth/admin/realms/{realm}/users 200 UserCollection Realms::users()
Endpoint Status Code Response API
GET /auth/admin/serverinfo 200 ServerInfo ServerInfo::get()

Local development and testing

Run docker compose up -d keycloak to start a local Keycloak instance listening on http://localhost:8080.

Run your script (e.g. examples/serverinfo.php) from within the php container:

docker compose run --rm php php examples/serverinfo.php

Composer scripts

  • analyze: Run phpstan and psalm analysis
  • ecs: Run Easy Coding Standard (ECS)
  • ecs:fix: Fix Easy Coding Standard (ECS) errors
  • test: Run unit and integration tests
  • test:unit: Run unit tests
  • test:integration: Run integration tests (requires a fresh and running Keycloak instance)

About

PHP client to interact with Keycloak's Admin REST API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 99.9%
  • Dockerfile 0.1%