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

PHP_CS removed in favour of ECS #1038

Merged
merged 4 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Coding Standards

on: [push]

jobs:
tests:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['8.1']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}

steps:
- name: Checkout
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: json, mbstring, openssl, sqlite3
coverage: xdebug

- name: Install Composer dependencies
run: |
composer install
composer require rector/rector symplify/easy-coding-standard --dev

- name: CONDING STANDARDS (ECS)
run: |
vendor/bin/ecs check

- name: CONDING STANDARDS (RECTOR)
run: |
vendor/bin/rector process --ansi --dry-run --xdebug

14 changes: 0 additions & 14 deletions .php_cs

This file was deleted.

16 changes: 11 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ maintainers accept and merge your work.
Coding standards
----------------

You MUST follow the [PSR-1](http://www.php-fig.org/psr/1/) and
[PSR-2](http://www.php-fig.org/psr/2/). If you don't know about any of them, you
should really read the recommendations. Can't wait? Use the [PHP-CS-Fixer
tool](http://cs.sensiolabs.org/):
You MUST follow the [PSR-12](http://www.php-fig.org/psr/12/).
If you don't know about any of them, you should really read the recommendations.
Can't wait? Use the [symplify/easy-coding-standard](https://github.com/symplify/easy-coding-standard):

```
$ vendor/bin/php-cs-fixer fix --config-file=.php_cs
$ vendor/bin/ecs --fix
```

In addition, we try to use the baseline code as close as the current PHP version features.
For that, we use [Rector](https://github.com/rectorphp/rector-src).

```
$ vendor/bin/rector process
```

__Note:__ Never fix coding standards in some existing code as it makes code review more difficult.
Expand Down
2 changes: 1 addition & 1 deletion Command/CheckConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->keyLoader->loadKey(KeyLoaderInterface::TYPE_PUBLIC);
}
} catch (\RuntimeException $e) {
$output->writeln('<error>'.$e->getMessage().'</error>');
$output->writeln('<error>' . $e->getMessage() . '</error>');

return 1;
}
Expand Down
7 changes: 3 additions & 4 deletions Command/GenerateTokenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$payload = [];

if(null !== $input->getOption('ttl') && ((int) $input->getOption('ttl')) == 0) {
if (null !== $input->getOption('ttl') && ((int) $input->getOption('ttl')) == 0) {
$payload['exp'] = 0;
}
elseif(null !== $input->getOption('ttl') && ((int) $input->getOption('ttl')) > 0) {
} elseif (null !== $input->getOption('ttl') && ((int) $input->getOption('ttl')) > 0) {
$payload['exp'] = time() + $input->getOption('ttl');
}

$token = $this->tokenManager->createFromPayload($user, $payload);

$output->writeln([
'',
'<info>'.$token.'</info>',
'<info>' . $token . '</info>',
'',
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ public function process(ContainerBuilder $container)
->setDeprecated(...$deprecationArgs);
}
}

2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->scalarNode('user_identity_field')
->setDeprecated(...$this->getDeprecationParameters('The "%path%.%node%" configuration key is deprecated since version 2.15, implement "'.UserInterface::class.'::getUserIdentifier()" instead.', '2.5'))
->setDeprecated(...$this->getDeprecationParameters('The "%path%.%node%" configuration key is deprecated since version 2.15, implement "' . UserInterface::class . '::getUserIdentifier()" instead.', '2.5'))
->defaultValue('username')
->cannotBeEmpty()
->end()
Expand Down
5 changes: 2 additions & 3 deletions DependencyInjection/LexikJWTAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

if (method_exists(Alias::class, 'getDeprecation')) {
$loader->load('deprecated_51.xml');
Expand Down Expand Up @@ -80,7 +80,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setAlias(JWTEncoderInterface::class, 'lexik_jwt_authentication.encoder');
$container->setAlias(
'lexik_jwt_authentication.key_loader',
new Alias('lexik_jwt_authentication.key_loader.'.('openssl' === $encoderConfig['crypto_engine'] && 'lexik_jwt_authentication.encoder.default' === $encoderConfig['service'] ? $encoderConfig['crypto_engine'] : 'raw'), true)
new Alias('lexik_jwt_authentication.key_loader.' . ('openssl' === $encoderConfig['crypto_engine'] && 'lexik_jwt_authentication.encoder.default' === $encoderConfig['service'] ? $encoderConfig['crypto_engine'] : 'raw'), true)
);

$container
Expand Down Expand Up @@ -141,7 +141,6 @@ public function load(array $configs, ContainerBuilder $container)
->replaceArgument(3, $config['pass_phrase'])
->replaceArgument(4, $encoderConfig['signature_algorithm']);
}

}

private static function createTokenExtractors(ContainerBuilder $container, array $tokenExtractorsConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function addConfiguration(NodeDefinition $node)

public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string
{
$authenticatorId = 'security.authenticator.jwt.'.$firewallName;
$authenticatorId = 'security.authenticator.jwt.' . $firewallName;

$userProviderId = empty($config['provider']) ? $userProviderId : 'security.user.provider.concrete.' . $config['provider'];

Expand Down
12 changes: 6 additions & 6 deletions DependencyInjection/Security/Factory/JWTFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public function __construct($triggerDeprecation = true)
*/
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
{
$providerId = 'security.authentication.provider.jwt.'.$id;
$providerId = 'security.authentication.provider.jwt.' . $id;
$container
->setDefinition($providerId, new ChildDefinition($config['authentication_provider']))
->replaceArgument(0, new Reference($userProvider));

$listenerId = 'security.authentication.listener.jwt.'.$id;
$listenerId = 'security.authentication.listener.jwt.' . $id;
$container
->setDefinition($listenerId, new ChildDefinition($config['authentication_listener']))
->replaceArgument(2, $config);
Expand All @@ -50,7 +50,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
}

if ($config['authorization_header']['enabled']) {
$authorizationHeaderExtractorId = 'lexik_jwt_authentication.extractor.authorization_header_extractor.'.$id;
$authorizationHeaderExtractorId = 'lexik_jwt_authentication.extractor.authorization_header_extractor.' . $id;
$container
->setDefinition($authorizationHeaderExtractorId, new ChildDefinition('lexik_jwt_authentication.extractor.authorization_header_extractor'))
->replaceArgument(0, $config['authorization_header']['prefix'])
Expand All @@ -62,7 +62,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
}

if ($config['query_parameter']['enabled']) {
$queryParameterExtractorId = 'lexik_jwt_authentication.extractor.query_parameter_extractor.'.$id;
$queryParameterExtractorId = 'lexik_jwt_authentication.extractor.query_parameter_extractor.' . $id;
$container
->setDefinition($queryParameterExtractorId, new ChildDefinition('lexik_jwt_authentication.extractor.query_parameter_extractor'))
->replaceArgument(0, $config['query_parameter']['name']);
Expand All @@ -73,7 +73,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
}

if ($config['cookie']['enabled']) {
$cookieExtractorId = 'lexik_jwt_authentication.extractor.cookie_extractor.'.$id;
$cookieExtractorId = 'lexik_jwt_authentication.extractor.cookie_extractor.' . $id;
$container
->setDefinition($cookieExtractorId, new ChildDefinition('lexik_jwt_authentication.extractor.cookie_extractor'))
->replaceArgument(0, $config['cookie']['name']);
Expand Down Expand Up @@ -174,7 +174,7 @@ public function addConfiguration(NodeDefinition $node)
*/
protected function createEntryPoint(ContainerBuilder $container, $id, $defaultEntryPoint)
{
$entryPointId = 'lexik_jwt_authentication.security.authentication.entry_point.'.$id;
$entryPointId = 'lexik_jwt_authentication.security.authentication.entry_point.' . $id;
$container->setDefinition($entryPointId, new ChildDefinition('lexik_jwt_authentication.security.authentication.entry_point'));

return $entryPointId;
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Security/Factory/JWTUserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function addConfiguration(NodeDefinition $node)
->ifTrue(function ($class) {
return !(new \ReflectionClass($class))->implementsInterface(JWTUserInterface::class);
})
->thenInvalid('The %s class must implement '.JWTUserInterface::class.' for using the "lexik_jwt" user provider.')
->thenInvalid('The %s class must implement ' . JWTUserInterface::class . ' for using the "lexik_jwt" user provider.')
->end()
->end()
->end()
Expand Down
6 changes: 4 additions & 2 deletions Event/AuthenticationFailureEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ public function setResponse(Response $response): void
$this->response = $response;
}

public function getRequest(): ?Request {
public function getRequest(): ?Request
{
return $this->request;
}

public function setRequest(Request $request) {
public function setRequest(Request $request)
{
$this->request = $request;
}
}
3 changes: 2 additions & 1 deletion Event/JWTNotFoundEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Lexik\Bundle\JWTAuthenticationBundle\Event;

use Symfony\Component\HttpFoundation\{Response, Request};
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

/**
Expand Down
2 changes: 1 addition & 1 deletion Response/JWTCompatAuthenticationFailureResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* https://github.com/lexik/LexikJWTAuthenticationBundle/issues/944
* https://github.com/vimeo/psalm/issues/7923
*/
if (80000 <= \PHP_VERSION_ID AND (new \ReflectionMethod(JsonResponse::class, 'setData'))->hasReturnType()) {
if (80000 <= \PHP_VERSION_ID and (new \ReflectionMethod(JsonResponse::class, 'setData'))->hasReturnType()) {
eval('
namespace Lexik\Bundle\JWTAuthenticationBundle\Response;

Expand Down
2 changes: 1 addition & 1 deletion Security/Authentication/Provider/JWTProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Provider;

use Symfony\Component\Security\Core\User\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTDecodeFailureException;
Expand All @@ -12,6 +11,7 @@
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

Expand Down
8 changes: 6 additions & 2 deletions Security/Authentication/Token/JWTUserToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
*
* @internal
*/
abstract class JWTCompatUserToken extends AbstractToken implements GuardTokenInterface {}
abstract class JWTCompatUserToken extends AbstractToken implements GuardTokenInterface
{
}
} else {
/**
* @internal
*/
abstract class JWTCompatUserToken extends AbstractToken {}
abstract class JWTCompatUserToken extends AbstractToken
{
}
}

/**
Expand Down
16 changes: 9 additions & 7 deletions Security/Authenticator/JWTAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class JWTAuthenticator extends AbstractAuthenticator implements AuthenticationEn
/**
* @var TokenExtractorInterface
*/
private $tokenExtractor;
private $tokenExtractor;

/**
* @var JWTTokenManagerInterface
Expand Down Expand Up @@ -123,10 +123,12 @@ public function doAuthenticate(Request $request) /*: Passport */
}

$passport = new SelfValidatingPassport(
new UserBadge((string)$payload[$idClaim],
function ($userIdentifier) use($payload) {
return $this->loadUser($payload, $userIdentifier);
})
new UserBadge(
(string)$payload[$idClaim],
function ($userIdentifier) use ($payload) {
return $this->loadUser($payload, $userIdentifier);
}
)
);

$passport->setAttribute('payload', $payload);
Expand Down Expand Up @@ -225,13 +227,13 @@ protected function loadUser(array $payload, string $identity): UserInterface
}

return $provider->loadUserByIdentifier($identity);
// More generic call to catch both UsernameNotFoundException for SF<5.3 and new UserNotFoundException
// More generic call to catch both UsernameNotFoundException for SF<5.3 and new UserNotFoundException
} catch (AuthenticationException $e) {
// try next one
}
}

if(!class_exists(UserNotFoundException::class)) {
if (!class_exists(UserNotFoundException::class)) {
$ex = new UsernameNotFoundException(sprintf('There is no user with username "%s".', $identity));
$ex->setUsername($identity);
} else {
Expand Down
2 changes: 1 addition & 1 deletion Security/Guard/JWTTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException as SecurityUserNotFoundException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException as SecurityUserNotFoundException;
use Symfony\Component\Security\Core\User\ChainUserProvider;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
Expand Down
1 change: 1 addition & 0 deletions Services/JWSProvider/JWSProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lexik\Bundle\JWTAuthenticationBundle\Signature\CreatedJWS;
use Lexik\Bundle\JWTAuthenticationBundle\Signature\LoadedJWS;

/**
* Interface for classes that are able to create and load JSON web signatures (JWS).
*
Expand Down
10 changes: 5 additions & 5 deletions Services/JWSProvider/LcobucciJWSProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Lexik\Bundle\JWTAuthenticationBundle\Services\JWSProvider;

use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Hmac\Sha384;
use Lcobucci\JWT\Signer\Hmac\Sha512;
use Lcobucci\JWT\Signer\Ecdsa;
use Lcobucci\Clock\SystemClock;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Ecdsa;
use Lcobucci\JWT\Signer\Hmac;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Hmac\Sha384;
use Lcobucci\JWT\Signer\Hmac\Sha512;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token;
Expand Down Expand Up @@ -109,7 +109,7 @@ public function create(array $payload, array $header = [])
$exp = $payload['exp'] ?? $now + $this->ttl;
unset($payload['exp']);

if($exp) {
if ($exp) {
$jws->expiresAt($exp instanceof \DateTimeImmutable ? $exp : ($this->useDateObjects ? new \DateTimeImmutable("@$exp") : $exp));
}
}
Expand Down
Loading