Skip to content

Commit c137df5

Browse files
authored
Merge pull request #120 from fschmtt/credential-properties
Credential properties
2 parents d92c734 + e402e69 commit c137df5

File tree

6 files changed

+87
-12
lines changed

6 files changed

+87
-12
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ $myCustomRepresentation = $myCustomResource->myCustomEndpoint();
114114

115115
### [Clients](https://www.keycloak.org/docs-api/25.0.0/rest-api/index.html#_clients)
116116

117-
| Endpoint | Response | API |
118-
|---------------------------------------------------|---------------------------------------------------------|-----------------------------------------------|
119-
| `GET /admin/realms/{realm}/clients` | [ClientCollection](src/Collection/ClientCollection.php) | [Clients::all()](src/Resource/Clients.php) |
120-
| `GET /admin/realms/{realm}/clients/{client-uuid}` | [Client](src/Representation/Client.php) | [Clients::get()](src/Resource/Clients.php) |
121-
| `PUT /admin/realms/{realm}/clients/{client-uuid}` | [Client](src/Representation/Client.php) | [Clients::update()](src/Resource/Clients.php) |
122-
| `POST /admin/realms/{realm}/clients` | [Client](src/Representation/Client.php) | [Clients::import()](src/Resource/Clients.php) |
117+
| Endpoint | Response | API |
118+
|----------------------------------------------------------------|---------------------------------------------------------|--------------------------------------------------------|
119+
| `GET /admin/realms/{realm}/clients` | [ClientCollection](src/Collection/ClientCollection.php) | [Clients::all()](src/Resource/Clients.php) |
120+
| `GET /admin/realms/{realm}/clients/{client-uuid}` | [Client](src/Representation/Client.php) | [Clients::get()](src/Resource/Clients.php) |
121+
| `PUT /admin/realms/{realm}/clients/{client-uuid}` | [Client](src/Representation/Client.php) | [Clients::update()](src/Resource/Clients.php) |
122+
| `POST /admin/realms/{realm}/clients` | [Client](src/Representation/Client.php) | [Clients::import()](src/Resource/Clients.php) |
123+
| `GET /admin/realms/{realm}/clients/{clientUuid}/client-secret` | [Client](src/Representation/Client.php) | [Clients::getClientSecret()](src/Resource/Clients.php) |
123124

124125
### [Groups](https://www.keycloak.org/docs-api/25.0.0/rest-api/index.html#_clients)
125126

examples/users-all.php

+6
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@
1919

2020
foreach ($users as $user) {
2121
echo sprintf('-> User "%s"%s', $user->getUsername(), PHP_EOL);
22+
23+
echo sprintf('--> Required actions:%s', PHP_EOL);
24+
25+
foreach ($user->getRequiredActions() ?? [] as $requiredAction) {
26+
echo sprintf('---> %s%s', $requiredAction, PHP_EOL);
27+
}
2228
}

src/Representation/Credential.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Fschmtt\Keycloak\Representation;
66

7+
use Fschmtt\Keycloak\Type\Map;
8+
79
/**
810
* @method int|null getCreatedDate()
911
* @method string|null getCredentialData()
@@ -43,22 +45,24 @@
4345
class Credential extends Representation
4446
{
4547
public function __construct(
46-
protected ?int $createdDate = null,
47-
protected ?string $credentialData = null,
4848
protected ?string $id = null,
49-
protected ?int $priority = null,
50-
protected ?string $secretData = null,
51-
protected ?bool $temporary = null,
5249
protected ?string $type = null,
5350
protected ?string $userLabel = null,
51+
protected ?int $createdDate = null,
52+
protected ?string $secretData = null,
53+
protected ?string $credentialData = null,
54+
protected ?int $priority = null,
5455
protected ?string $value = null,
56+
protected ?bool $temporary = null,
5557
protected ?string $device = null,
58+
protected ?string $hashedSaltedValue = null,
5659
protected ?string $salt = null,
5760
protected ?int $hashIterations = null,
58-
protected ?string $algorithm = null,
5961
protected ?int $counter = null,
62+
protected ?string $algorithm = null,
6063
protected ?int $digits = null,
6164
protected ?int $period = null,
65+
protected ?Map $config = null,
6266
) {
6367
}
6468
}

src/Resource/Clients.php

+15
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\Representation\Client as ClientRepresentation;
13+
use Fschmtt\Keycloak\Representation\Credential;
1314

1415
/**
1516
* @phpstan-type UserSession array<mixed>
@@ -108,4 +109,18 @@ public function getUserSessions(string $realm, string $clientUuid, ?Criteria $cr
108109
)
109110
);
110111
}
112+
113+
public function getClientSecret(string $realm, string $clientUuid): Credential
114+
{
115+
return $this->queryExecutor->executeQuery(
116+
new Query(
117+
'/admin/realms/{realm}/clients/{clientUuid}/client-secret',
118+
Credential::class,
119+
[
120+
'realm' => $realm,
121+
'clientUuid' => $clientUuid,
122+
]
123+
)
124+
);
125+
}
111126
}

tests/Integration/Resource/ClientsTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,17 @@ public function testGetUserSessions(): void
6060

6161
$resource->getUserSessions('master', $client->getId());
6262
}
63+
64+
public function testGetClientSecret(): void
65+
{
66+
$resource = $this->getKeycloak()->clients();
67+
68+
$client = $resource->all('master')->first();
69+
static::assertInstanceOf(Client::class, $client);
70+
71+
$clientUuid = $client->getId();
72+
static::assertIsString($clientUuid);
73+
74+
$credential = $resource->getClientSecret('master', $clientUuid);
75+
}
6376
}

tests/Unit/Resource/ClientsTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Fschmtt\Keycloak\Http\Query;
1212
use Fschmtt\Keycloak\Http\QueryExecutor;
1313
use Fschmtt\Keycloak\Representation\Client as ClientRepresentation;
14+
use Fschmtt\Keycloak\Representation\Credential;
1415
use Fschmtt\Keycloak\Resource\Clients;
1516
use PHPUnit\Framework\Attributes\CoversClass;
1617
use PHPUnit\Framework\TestCase;
@@ -242,4 +243,39 @@ public function testGetUserSessions(): void
242243
$clients->getUserSessions('test-realm', $clientId)
243244
);
244245
}
246+
247+
public function testGetClientSecret(): void
248+
{
249+
$client = new ClientRepresentation(id: 'test-client');
250+
$clientUuid = $client->getId();
251+
252+
static::assertIsString($clientUuid);
253+
254+
$credential = new Credential();
255+
256+
$query = new Query(
257+
'/admin/realms/{realm}/clients/{clientUuid}/client-secret',
258+
Credential::class,
259+
[
260+
'realm' => 'test-realm',
261+
'clientUuid' => $clientUuid,
262+
],
263+
);
264+
265+
$queryExecutor = $this->createMock(QueryExecutor::class);
266+
$queryExecutor->expects(static::once())
267+
->method('executeQuery')
268+
->with($query)
269+
->willReturn($credential);
270+
271+
$clients = new Clients(
272+
$this->createMock(CommandExecutor::class),
273+
$queryExecutor,
274+
);
275+
276+
static::assertSame(
277+
$credential,
278+
$clients->getClientSecret('test-realm', $clientUuid)
279+
);
280+
}
245281
}

0 commit comments

Comments
 (0)