Skip to content

Commit

Permalink
Merge pull request #120 from fschmtt/credential-properties
Browse files Browse the repository at this point in the history
Credential properties
  • Loading branch information
fschmtt authored Sep 3, 2024
2 parents d92c734 + e402e69 commit c137df5
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 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);
}
}
16 changes: 10 additions & 6 deletions src/Representation/Credential.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Fschmtt\Keycloak\Representation;

use Fschmtt\Keycloak\Type\Map;

/**
* @method int|null getCreatedDate()
* @method string|null getCredentialData()
Expand Down Expand Up @@ -43,22 +45,24 @@
class Credential extends Representation
{
public function __construct(
protected ?int $createdDate = null,
protected ?string $credentialData = null,
protected ?string $id = null,
protected ?int $priority = null,
protected ?string $secretData = null,
protected ?bool $temporary = null,
protected ?string $type = null,
protected ?string $userLabel = null,
protected ?int $createdDate = null,
protected ?string $secretData = null,
protected ?string $credentialData = null,
protected ?int $priority = null,
protected ?string $value = null,
protected ?bool $temporary = null,
protected ?string $device = null,
protected ?string $hashedSaltedValue = null,
protected ?string $salt = null,
protected ?int $hashIterations = null,
protected ?string $algorithm = null,
protected ?int $counter = null,
protected ?string $algorithm = null,
protected ?int $digits = null,
protected ?int $period = null,
protected ?Map $config = null,
) {
}
}
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 c137df5

Please sign in to comment.