Skip to content

Commit

Permalink
Allow adding custom resources
Browse files Browse the repository at this point in the history
Fixes #70
  • Loading branch information
fschmtt committed Jan 12, 2024
1 parent ba2dd5b commit cac1fa7
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,51 @@ Keycloak 23.0.0 is running on Linux/5.10.25-linuxkit (amd64) with OpenJDK 64-Bit

More examples can be found in the [examples](examples) directory.

## Customization

### Custom representations & resources

You can register and use custom resources by providing your own representations and resources, e.g.:
```php
class MyCustomRepresentation extends \Fschmtt\Keycloak\Representation\Representation
{
public function __construct(
protected ?string $id = null,
protected ?string $name = null,
) {
}
}

class MyCustomResource extends \Fschmtt\Keycloak\Resource\Resource
{
public function myCustomEndpoint(): MyCustomRepresentation
{
return $this->queryExecutor->executeQuery(
new \Fschmtt\Keycloak\Http\Query(
'/my-custom-endpoint',
MyCustomRepresentation::class,
)
);
}
}
```

By extending the `Resource` class, you have access to both the `QueryExecutor` and `CommandExecutor`.
The `CommandExecutor` is designed to run state-changing commands against the server (without returning a response);
the `QueryExecutor` allows fetching resources and representations from the server.

To use your custom resource, pass the fully-qualified class name (FQCN) to the `Keycloak::resource()` method. It provides you with an instance of your resource you can then work with:
```php
$keycloak = new Keycloak(
$_SERVER['KEYCLOAK_BASE_URL'] ?? 'http://keycloak:8080',
'admin',
'admin',
);

$myCustomResource = $keycloak->resource(MyCustomResource::class);
$myCustomRepresentation = $myCustomResource->myCustomEndpoint();
```

## Available Resources
### [Attack Detection](https://www.keycloak.org/docs-api/23.0.0/rest-api/index.html#_attack_detection_resource)
| Endpoint | Response | API |
Expand Down
43 changes: 43 additions & 0 deletions examples/custom-resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

use Fschmtt\Keycloak\Keycloak;

/**
* @method string|null getId()
* @method self withId(?string $id)
* @method string getName()
* @method self|null withName(?string $name)
*/
class MyCustomRepresentation extends \Fschmtt\Keycloak\Representation\Representation
{
public function __construct(
protected ?string $id = null,
protected ?string $name = null,
) {
}
}

class MyCustomResource extends \Fschmtt\Keycloak\Resource\Resource
{
public function myCustomEndpoint(): MyCustomRepresentation
{
return $this->queryExecutor->executeQuery(
new \Fschmtt\Keycloak\Http\Query(
'/my-custom-endpoint',
MyCustomRepresentation::class,
)
);
}
}

$keycloak = new Keycloak(
$_SERVER['KEYCLOAK_BASE_URL'] ?? 'http://keycloak:8080',
'admin',
'admin',
);

/** @var MyCustomResource $myCustomResource */
$myCustomResource = $keycloak->resource(MyCustomResource::class);
$myCustomRepresentation = $myCustomResource->myCustomEndpoint();
12 changes: 12 additions & 0 deletions src/Keycloak.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Fschmtt\Keycloak\Resource\Clients;
use Fschmtt\Keycloak\Resource\Groups;
use Fschmtt\Keycloak\Resource\Realms;
use Fschmtt\Keycloak\Resource\Resource;
use Fschmtt\Keycloak\Resource\Roles;
use Fschmtt\Keycloak\Resource\ServerInfo;
use Fschmtt\Keycloak\Resource\Users;
Expand Down Expand Up @@ -102,6 +103,17 @@ public function roles(): Roles
return new Roles($this->commandExecutor, $this->queryExecutor);
}

/**
* @param class-string<Resource> $resource
* @return Resource

Check warning on line 108 in src/Keycloak.php

View check run for this annotation

Codecov / codecov/patch

src/Keycloak.php#L108

Added line #L108 was not covered by tests
*/
public function resource(string $resource): Resource

Check warning on line 110 in src/Keycloak.php

View check run for this annotation

Codecov / codecov/patch

src/Keycloak.php#L110

Added line #L110 was not covered by tests
{
$this->fetchVersion();

return new $resource($this->commandExecutor, $this->queryExecutor);
}

private function fetchVersion(): void
{
if ($this->version) {
Expand Down

0 comments on commit cac1fa7

Please sign in to comment.