Skip to content

Commit c5dcbe6

Browse files
authored
Merge pull request #89 from punktDeForks/feature/roles-keys
FEATURE: Representations and Collection for realm keys
2 parents 883499e + 92794fa commit c5dcbe6

File tree

7 files changed

+145
-0
lines changed

7 files changed

+145
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ $myCustomRepresentation = $myCustomResource->myCustomEndpoint();
128128
| `PUT /admin/realms/{realm}` | [Realm](src/Representation/Realm.php) | [Realms::update()](src/Resource/Realms.php) |
129129
| `DELETE /admin/realms/{realm}` | `n/a` | [Realms::delete()](src/Resource/Realms.php) |
130130
| `GET /admin/realms/{realm}/admin-events` | `array` | [Realms::adminEvents()](src/Resource/Realms.php) |
131+
| `GET /admin/realms/{realm}/keys` | [KeysMetadata](src/Representation/KeysMetadata.php) | [Realms::keys()](src/Resource/Realms.php) |
131132
| `DELETE /admin/realms/{realm}/admin-events` | `n/a` | [Realms::deleteAdminEvents()](src/Resource/Realms.php) |
132133
| `POST /admin/realms/{realm}/clear-keys-cache` | `n/a` | [Realms::clearKeysCache()](src/Resource/Realms.php) |
133134
| `POST /admin/realms/{realm}/clear-realm-cache` | `n/a` | [Realms::clearRealmCache()](src/Resource/Realms.php) |
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fschmtt\Keycloak\Collection;
6+
7+
use Fschmtt\Keycloak\Representation\KeyMetadata;
8+
9+
/**
10+
* @extends Collection<KeyMetadata>
11+
*
12+
* @codeCoverageIgnore
13+
*/
14+
class KeyMetadataCollection extends Collection
15+
{
16+
public static function getRepresentationClass(): string
17+
{
18+
return KeyMetadata::class;
19+
}
20+
}

src/Representation/KeyMetadata.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fschmtt\Keycloak\Representation;
6+
7+
use Fschmtt\Keycloak\Attribute\Since;
8+
9+
/**
10+
* @method string|null getProviderId()
11+
* @method int|null getProviderPriority()
12+
* @method string|null getKid()
13+
* @method string|null getStatus()
14+
* @method string|null getType()
15+
* @method string|null getAlgorithm()
16+
* @method string|null getPublicKey()
17+
* @method string|null getCertificate()
18+
* @method string|null getUse()
19+
* @method int|null getValidTo()
20+
* @method self withProviderId(?string $providerId)
21+
* @method self withProviderPriority(?int $providerPriority)
22+
* @method self withKid(?string $kid)
23+
* @method self withStatus(?string $status)
24+
* @method self withType(?string $type)
25+
* @method self withAlgorithm(?string $algorithm)
26+
* @method self withPublicKey(?string $publicKey)
27+
* @method self withCertificate(?string $certificate)
28+
* @method self withUse(?string $use)
29+
* @method self withValidTo(?int $validTo)
30+
*
31+
* @codeCoverageIgnore
32+
*/
33+
class KeyMetadata extends Representation
34+
{
35+
public function __construct(
36+
protected ?string $providerId = null,
37+
protected ?int $providerPriority = null,
38+
protected ?string $kid = null,
39+
protected ?string $status = null,
40+
protected ?string $type = null,
41+
protected ?string $algorithm = null,
42+
protected ?string $publicKey = null,
43+
protected ?string $certificate = null,
44+
protected ?string $use = null,
45+
#[Since('23.0.0')]
46+
protected ?int $validTo = null,
47+
) {
48+
}
49+
}

src/Representation/KeysMetadata.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Fschmtt\Keycloak\Representation;
6+
7+
use Fschmtt\Keycloak\Collection\KeyMetadataCollection;
8+
9+
/**
10+
* @method array|null getActive()
11+
* @method KeyMetadataCollection|null getKeys()
12+
* @method self withActive(?array $active)
13+
* @method self withKeyMetadataCollection(?KeyMetadataCollection $keys)
14+
*
15+
* @codeCoverageIgnore
16+
*/
17+
class KeysMetadata extends Representation
18+
{
19+
/**
20+
* @param string[]|null $active
21+
* @param KeyMetadataCollection|null $keys
22+
*/
23+
public function __construct(
24+
protected ?array $active = null,
25+
protected ?KeyMetadataCollection $keys = null,
26+
) {
27+
}
28+
}

src/Resource/Realms.php

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Fschmtt\Keycloak\Http\Criteria;
1010
use Fschmtt\Keycloak\Http\Method;
1111
use Fschmtt\Keycloak\Http\Query;
12+
use Fschmtt\Keycloak\Representation\KeysMetadata;
1213
use Fschmtt\Keycloak\Representation\Realm;
1314

1415
/**
@@ -99,6 +100,20 @@ public function adminEvents(string $realm, ?Criteria $criteria = null): array
99100
)
100101
);
101102
}
103+
104+
public function keys(string $realm, ?Criteria $criteria = null): KeysMetadata
105+
{
106+
return $this->queryExecutor->executeQuery(
107+
new Query(
108+
'/admin/realms/{realm}/keys',
109+
KeysMetadata::class,
110+
[
111+
'realm' => $realm,
112+
],
113+
$criteria,
114+
)
115+
);
116+
}
102117

103118
public function deleteAdminEvents(string $realm): void
104119
{

tests/Integration/Resource/RealmsTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public function testCanGetRealm(): void
2828
static::assertEquals('master', $realm->getRealm());
2929
}
3030

31+
public function testCanGetRealmKeys(): void
32+
{
33+
$realmkeys = $this->getKeycloak()->realms()->keys(realm: 'master');
34+
static::assertArrayHasKey('AES', $realmkeys->getActive());
35+
static::assertGreaterThan(1, $realmkeys->getKeys()->count());
36+
}
37+
3138
public function testCanUpdateRealm(): void
3239
{
3340
$realm = $this->getKeycloak()->realms()->get(realm: 'master');

tests/Unit/Resource/RealmsTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Fschmtt\Keycloak\Http\Method;
1111
use Fschmtt\Keycloak\Http\Query;
1212
use Fschmtt\Keycloak\Http\QueryExecutor;
13+
use Fschmtt\Keycloak\Representation\KeysMetadata;
1314
use Fschmtt\Keycloak\Representation\Realm;
1415
use Fschmtt\Keycloak\Resource\Realms;
1516
use PHPUnit\Framework\Attributes\CoversClass;
@@ -267,4 +268,28 @@ public function testClearUserCache(): void
267268
);
268269
$realms->clearUserCache('realm-with-cache');
269270
}
271+
272+
public function testGetKeys(): void
273+
{
274+
$query = new Query(
275+
'/admin/realms/{realm}/keys',
276+
KeysMetadata::class,
277+
[
278+
'realm' => 'realm-with-keys',
279+
],
280+
);
281+
282+
$queryExecutor = $this->createMock(QueryExecutor::class);
283+
$queryExecutor->expects(static::once())
284+
->method('executeQuery')
285+
->with($query)
286+
->willReturn(new KeysMetadata());
287+
288+
$realms = new Realms(
289+
$this->createMock(CommandExecutor::class),
290+
$queryExecutor,
291+
);
292+
293+
$realms->keys('realm-with-keys');
294+
}
270295
}

0 commit comments

Comments
 (0)