Skip to content

Commit

Permalink
Add Client::getClientSecret() endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fschmtt committed Aug 20, 2024
1 parent 7690e40 commit e402e69
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ $myCustomRepresentation = $myCustomResource->myCustomEndpoint();

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

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

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

Expand Down
6 changes: 6 additions & 0 deletions examples/users-all.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@

foreach ($users as $user) {
echo sprintf('-> User "%s"%s', $user->getUsername(), PHP_EOL);

echo sprintf('--> Required actions:%s', PHP_EOL);

foreach ($user->getRequiredActions() ?? [] as $requiredAction) {
echo sprintf('---> %s%s', $requiredAction, PHP_EOL);
}
}
15 changes: 15 additions & 0 deletions src/Resource/Clients.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Fschmtt\Keycloak\Http\Method;
use Fschmtt\Keycloak\Http\Query;
use Fschmtt\Keycloak\Representation\Client as ClientRepresentation;
use Fschmtt\Keycloak\Representation\Credential;

/**
* @phpstan-type UserSession array<mixed>
Expand Down Expand Up @@ -108,4 +109,18 @@ public function getUserSessions(string $realm, string $clientUuid, ?Criteria $cr
)
);
}

public function getClientSecret(string $realm, string $clientUuid): Credential
{
return $this->queryExecutor->executeQuery(
new Query(
'/admin/realms/{realm}/clients/{clientUuid}/client-secret',
Credential::class,
[
'realm' => $realm,
'clientUuid' => $clientUuid,
]
)
);
}
}
13 changes: 13 additions & 0 deletions tests/Integration/Resource/ClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,17 @@ public function testGetUserSessions(): void

$resource->getUserSessions('master', $client->getId());
}

public function testGetClientSecret(): void
{
$resource = $this->getKeycloak()->clients();

$client = $resource->all('master')->first();
static::assertInstanceOf(Client::class, $client);

$clientUuid = $client->getId();
static::assertIsString($clientUuid);

$credential = $resource->getClientSecret('master', $clientUuid);
}
}
36 changes: 36 additions & 0 deletions tests/Unit/Resource/ClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Fschmtt\Keycloak\Http\Query;
use Fschmtt\Keycloak\Http\QueryExecutor;
use Fschmtt\Keycloak\Representation\Client as ClientRepresentation;
use Fschmtt\Keycloak\Representation\Credential;
use Fschmtt\Keycloak\Resource\Clients;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -242,4 +243,39 @@ public function testGetUserSessions(): void
$clients->getUserSessions('test-realm', $clientId)
);
}

public function testGetClientSecret(): void
{
$client = new ClientRepresentation(id: 'test-client');
$clientUuid = $client->getId();

static::assertIsString($clientUuid);

$credential = new Credential();

$query = new Query(
'/admin/realms/{realm}/clients/{clientUuid}/client-secret',
Credential::class,
[
'realm' => 'test-realm',
'clientUuid' => $clientUuid,
],
);

$queryExecutor = $this->createMock(QueryExecutor::class);
$queryExecutor->expects(static::once())
->method('executeQuery')
->with($query)
->willReturn($credential);

$clients = new Clients(
$this->createMock(CommandExecutor::class),
$queryExecutor,
);

static::assertSame(
$credential,
$clients->getClientSecret('test-realm', $clientUuid)
);
}
}

0 comments on commit e402e69

Please sign in to comment.