-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1184 from cultuurnet/PPF-434/keycloak-integration
PPF-434 Keycloak integration
- Loading branch information
Showing
69 changed files
with
3,642 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Domain\Integrations; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* @extends Collection<int, Environment> | ||
*/ | ||
final class Environments extends Collection | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Keycloak; | ||
|
||
use App\Keycloak\Client\ApiClient; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class CachedKeycloakClientStatus | ||
{ | ||
private array $statuses = []; | ||
|
||
public function __construct(private readonly ApiClient $apiClient, private readonly LoggerInterface $logger) | ||
{ | ||
} | ||
|
||
public function isClientBlocked(Client $client): bool | ||
{ | ||
$uuid = $client->id->toString(); | ||
|
||
if(! isset($this->statuses[$uuid])) { | ||
$this->statuses[$uuid] = $this->apiClient->fetchIsClientActive($client); | ||
} else { | ||
$this->logger->info(self::class . ' - ' . $uuid . ': cache hit: ' . ($this->statuses[$uuid] ? 'Active' : 'Blocked')); | ||
} | ||
|
||
return ! $this->statuses[$uuid]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Keycloak; | ||
|
||
use App\Domain\Integrations\Environment; | ||
use Illuminate\Support\Facades\App; | ||
use InvalidArgumentException; | ||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
final readonly class Client | ||
{ | ||
public function __construct( | ||
public UuidInterface $id, | ||
public UuidInterface $integrationId, | ||
public string $clientId, | ||
public string $clientSecret, | ||
public Environment $environment, | ||
) { | ||
} | ||
|
||
public static function createFromJson( | ||
Realm $realm, | ||
UuidInterface $integrationId, | ||
array $data | ||
): self { | ||
if (empty($data['secret'])) { | ||
throw new InvalidArgumentException('Missing secret'); | ||
} | ||
|
||
return new self( | ||
Uuid::fromString($data['id']), | ||
$integrationId, | ||
$data['clientId'], | ||
$data['secret'], | ||
$realm->environment, | ||
); | ||
} | ||
|
||
public function getKeycloakUrl(): string | ||
{ | ||
$baseUrl = $this->getRealm()->baseUrl; | ||
|
||
return $baseUrl . 'admin/master/console/#/' . $this->getRealm()->internalName . '/clients/' . $this->id->toString() . '/settings'; | ||
} | ||
|
||
public function getRealm(): Realm | ||
{ | ||
/** @var Realms $realmCollection */ | ||
$realmCollection = App::get(Realms::class); | ||
|
||
foreach ($realmCollection as $realm) { | ||
if ($realm->environment === $this->environment) { | ||
return $realm; | ||
} | ||
} | ||
|
||
throw new InvalidArgumentException( | ||
sprintf('Could not convert environment %s to realm:', $this->environment->value) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Keycloak\Client; | ||
|
||
use App\Domain\Integrations\Integration; | ||
use App\Keycloak\Client; | ||
use App\Keycloak\ClientId\ClientIdFactory; | ||
use App\Keycloak\Realm; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
interface ApiClient | ||
{ | ||
public function createClient(Realm $realm, Integration $integration, ClientIdFactory $clientIdFactory): Client; | ||
|
||
public function addScopeToClient(Client $client, UuidInterface $scopeId): void; | ||
|
||
public function fetchIsClientActive(Client $client): bool; | ||
|
||
public function unblockClient(Client $client): void; | ||
|
||
public function blockClient(Client $client): void; | ||
|
||
public function updateClient(Client $client, array $body): void; | ||
|
||
public function deleteScopes(Client $client): void; | ||
} |
Oops, something went wrong.