Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update bundle to symfony 7 #38

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
/.idea/
/composer.lock
8 changes: 4 additions & 4 deletions Controller/KeycloakController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

class KeycloakController extends AbstractController
{
public function connect(ClientRegistry $clientRegistry)
public function connect(ClientRegistry $clientRegistry): RedirectResponse
{
return $clientRegistry->getClient('keycloak')->redirect();
}

public function connectCheck(Request $request, string $defaultTargetRouteName)
public function connectCheck(Request $request, string $defaultTargetRouteName): RedirectResponse
{
$loginReferrer = null;
if ($request->hasSession()) {
Expand All @@ -24,12 +24,12 @@ public function connectCheck(Request $request, string $defaultTargetRouteName)
return $loginReferrer ? $this->redirect($loginReferrer) : $this->redirectToRoute($defaultTargetRouteName);
}

public function logout(Request $request, string $defaultTargetRouteName)
public function logout(Request $request, string $defaultTargetRouteName): RedirectResponse
{
return new RedirectResponse($this->generateUrl($defaultTargetRouteName));
}

public function account(ClientRegistry $clientRegistry)
public function account(ClientRegistry $clientRegistry): RedirectResponse
{
return $this->redirect($clientRegistry->getClient('keycloak')->getOAuth2Provider()->getResourceOwnerManageAccountUrl());
}
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('idci_keycloak_security');

Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/IDCIKeycloakSecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class IDCIKeycloakSecurityExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
Expand All @@ -23,7 +23,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('idci_keycloak_security.ssl_verification', $config['ssl_verification']);
}

public function prepend(ContainerBuilder $container)
public function prepend(ContainerBuilder $container): void
{
$bundles = $container->getParameter('kernel.bundles');

Expand All @@ -37,7 +37,7 @@ public function prepend(ContainerBuilder $container)
$container->prependExtensionConfig('knpu_oauth2_client', $this->generateKeycloakAuthConfiguration($config));
}

protected function generateKeycloakAuthConfiguration(array $config)
protected function generateKeycloakAuthConfiguration(array $config): array
{
return [
'clients' => [
Expand Down
4 changes: 2 additions & 2 deletions EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class ExceptionListener
/**
* @var UrlGeneratorInterface
*/
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is now useless

private $urlGenerator;
private UrlGeneratorInterface $urlGenerator;

public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}

public function onKernelException(ExceptionEvent $event)
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();

Expand Down
4 changes: 2 additions & 2 deletions EventListener/LogoutListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function __construct(
$this->defaultTargetRouteName = $defaultTargetRouteName;
}

public function onSymfonyComponentSecurityHttpEventLogoutEvent(LogoutEvent $event)
public function onSymfonyComponentSecurityHttpEventLogoutEvent(LogoutEvent $event): void
{
if (null === $event->getToken() || null === $event->getToken()->getUser()) {
if (null === $event->getToken()?->getUser()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a good idea to use the null safe operator (uncompatible with < php8)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm Symfony 6.4 requires >= PHP 8.1

return;
}

Expand Down
2 changes: 1 addition & 1 deletion IDCIKeycloakSecurityBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class IDCIKeycloakSecurityBundle extends Bundle
/**
* @return void
*/
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
parent::build($container);
}
Expand Down
20 changes: 10 additions & 10 deletions Provider/KeycloakProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class KeycloakProvider extends AbstractProvider
/**
* @var string use to identify the "public"" way to call the auth server
*/
const MODE_PUBLIC = 'public';
private const MODE_PUBLIC = 'public';

/**
* @var string use to identify the "private"" way to call the auth server
*/
const MODE_PRIVATE = 'private';
private const MODE_PRIVATE = 'private';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ This breaks the backward compatibility.


public ?string $authServerPublicUrl = null;

Expand Down Expand Up @@ -58,17 +58,17 @@ public function decryptResponse($response): array
*
* @return string
*/
public function getBaseUrl($mode = self::MODE_PUBLIC)
public function getBaseUrl(string $mode = self::MODE_PUBLIC): ?string
{
return self::MODE_PRIVATE === $mode ? $this->authServerPrivateUrl : $this->authServerPublicUrl;
}

public function getBaseUrlWithRealm($mode)
public function getBaseUrlWithRealm($mode): string
{
return sprintf('%s/realms/%s', $this->getBaseUrl($mode), $this->realm);
}

public function getResourceOwnerManageAccountUrl()
public function getResourceOwnerManageAccountUrl(): string
{
return sprintf('%s/account', $this->getBaseUrlWithRealm(self::MODE_PUBLIC));
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public function getBaseApiUrlWithRealm(): string
return sprintf('%s/admin/realms/%s', $this->getBaseUrl(self::MODE_PRIVATE), $this->realm);
}

public function getLogoutUrl(array $options = [])
public function getLogoutUrl(array $options = []): string
{
$base = $this->getBaseLogoutUrl();
$params = $this->getAuthorizationParameters($options);
Expand All @@ -128,12 +128,12 @@ public function getResourceOwner(AccessToken $token): KeycloakResourceOwner
return $this->createResourceOwner($response, $token);
}

public function getClientId()
public function getClientId(): string
{
return $this->clientId;
}

public function getClientSecret()
public function getClientSecret(): string
{
return $this->clientSecret;
}
Expand All @@ -148,7 +148,7 @@ protected function getScopeSeparator(): string
return ' ';
}

protected function checkResponse(ResponseInterface $response, $data)
protected function checkResponse(ResponseInterface $response, $data): void
{
if (!empty($data['error'])) {
$error = sprintf('%s: %s', $data['error'], $data['error_description']);
Expand All @@ -162,7 +162,7 @@ protected function createResourceOwner(array $response, AccessToken $token): Key
return new KeycloakResourceOwner($response, $token);
}

protected function getAllowedClientOptions(array $options)
protected function getAllowedClientOptions(array $options): array
{
return ['timeout', 'proxy', 'verify'];
}
Expand Down
16 changes: 8 additions & 8 deletions Security/User/KeycloakBearerUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@

class KeycloakBearerUser extends OAuthUser
{
private ?string $accessToken;
private ?string $accessToken = null;

private ?string $clientId;
private ?string $clientId = null;

private ?string $email;
private ?string $email = null;

private ?string $displayName;
private ?string $displayName = null;

private ?string $firstName;
private ?string $firstName = null;

private ?string $lastName;
private ?string $lastName = null;

private bool $emailVerified;
private bool $emailVerified = false;

public function __toString(): string
{
return $this->getUsername();
return $this->getUserIdentifier();
}

public function setAccessToken(string $accessToken): self
Expand Down
1 change: 1 addition & 0 deletions Security/User/KeycloakBearerUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class KeycloakBearerUserProvider extends OAuthUserProvider implements KeycloakBe

public function __construct(ClientRegistry $clientRegistry, HttpClientInterface $httpClient, mixed $sslVerification)
{
parent::__construct();
$this->clientRegistry = $clientRegistry;
$this->httpClient = $httpClient;
$this->sslVerification = $sslVerification;
Expand Down
8 changes: 4 additions & 4 deletions Security/User/KeycloakUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public function __construct(
array $roles,
AccessToken $accessToken,
string $id,
?string $email,
?string $displayName,
?string $firstName,
?string $lastName,
?string $email = null,
?string $displayName = null,
?string $firstName = null,
?string $lastName = null,
?string $preferredLanguage = 'en',
array $resources = []
) {
Expand Down
1 change: 1 addition & 0 deletions Security/User/KeycloakUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class KeycloakUserProvider extends OAuthUserProvider implements KeycloakUserProv

public function __construct(ClientRegistry $clientRegistry, LoggerInterface $logger)
{
parent::__construct();
$this->clientRegistry = $clientRegistry;
$this->logger = $logger;
}
Expand Down
29 changes: 15 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@
}
],
"require": {
"symfony/dependency-injection": "^5.3|^6.0",
"symfony/framework-bundle": "^5.3|^6.0",
"symfony/http-client": "^5.3|^6.0",
"symfony/routing": "^5.3|^6.0",
"symfony/security-bundle": "^5.3|^6.0",
"symfony/http-foundation": "^5.3|^6.0",
"php": ">=8.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://symfony.com/releases/6.4 => requirement php8.1.0 or higher

"symfony/dependency-injection": "^6.4|^7.0",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/http-client": "^6.4|^7.0",
"symfony/routing": "^6.4|^7.0",
"symfony/security-bundle": "^6.4|^7.0",
"symfony/http-foundation": "^6.4|^7.0",
"knpuniversity/oauth2-client-bundle": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
"symfony/expression-language": "^5.3|^6.0",
"symfony/finder": "^5.3|^6.0",
"symfony/form": "^5.3|^6.0",
"symfony/stopwatch": "^5.3|^6.0",
"symfony/twig-bundle": "^5.3|^6.0",
"symfony/validator": "^5.3|^6.0",
"symfony/yaml": "^5.3|^6.0"
"phpunit/phpunit": "^11",
"symfony/expression-language": "^6.4|^7.0",
"symfony/finder": "^6.4|^7.0",
"symfony/form": "^6.4|^7.0",
"symfony/stopwatch": "^6.4|^7.0",
"symfony/twig-bundle": "^6.4|^7.0",
"symfony/validator": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symfony 5.4 is still officially supported and for a while. I think using a BC Layer is not too expensive here. I have no idea what @konandrum thinks about this...

},
"autoload": {
"psr-4": {
Expand Down