Skip to content

Commit

Permalink
Fixes #48 and #50
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed May 30, 2020
1 parent ee7a0dd commit 2984144
Show file tree
Hide file tree
Showing 27 changed files with 195 additions and 150 deletions.
8 changes: 6 additions & 2 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$header = 'The MIT License (MIT)
Copyright (c) 2014-2019 Spomky-Labs
Copyright (c) 2014-2020 Spomky-Labs
This software may be modified and distributed under the terms
of the MIT license. See the LICENSE file for details.';
Expand All @@ -14,7 +14,6 @@ $finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/Features')
->in(__DIR__.'/Resources')
->in(__DIR__.'/Tests')
->path(__DIR__.'/SpomkyLabsLexikJoseBundle.php')
;

return PhpCsFixer\Config::create()
Expand Down Expand Up @@ -57,6 +56,11 @@ return PhpCsFixer\Config::create()
'style' => 'annotation',
],
'php_unit_test_case_static_method_calls' => true,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
])
->setRiskyAllowed(true)
->setUsingCache(true)
Expand Down
5 changes: 3 additions & 2 deletions Checker/AlgHeaderChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace SpomkyLabs\LexikJoseBundle\Checker;

use function is_string;
use Jose\Component\Checker\HeaderChecker;
use Jose\Component\Checker\InvalidHeaderException;
use function Safe\sprintf;
Expand All @@ -37,7 +38,7 @@ public function __construct(string $algorithm)
*/
public function checkHeader($algorithm): void
{
if (!\is_string($algorithm)) {
if (!is_string($algorithm)) {
throw new InvalidHeaderException('The value of the header "alg" is not valid', 'alg', $algorithm);
}

Expand Down
5 changes: 3 additions & 2 deletions Checker/EncHeaderChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace SpomkyLabs\LexikJoseBundle\Checker;

use function is_string;
use Jose\Component\Checker\HeaderChecker;
use Jose\Component\Checker\InvalidHeaderException;
use function Safe\sprintf;
Expand All @@ -37,7 +38,7 @@ public function __construct(string $algorithm)
*/
public function checkHeader($algorithm): void
{
if (!\is_string($algorithm)) {
if (!is_string($algorithm)) {
throw new InvalidHeaderException('The value of the header "enc" is not valid', 'enc', $algorithm);
}

Expand Down
5 changes: 3 additions & 2 deletions Checker/IssuerChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace SpomkyLabs\LexikJoseBundle\Checker;

use Exception;
use Jose\Component\Checker\ClaimChecker;
use Jose\Component\Checker\HeaderChecker;
use function Safe\sprintf;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function supportedClaim(): string
public function checkClaim($issuer): void
{
if ($this->issuer !== $issuer) {
throw new \Exception(sprintf('The issuer "%s" is not allowed.', $issuer));
throw new Exception(sprintf('The issuer "%s" is not allowed.', $issuer));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand Down
8 changes: 5 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace SpomkyLabs\LexikJoseBundle\DependencyInjection;

use Assert\Assertion;
use RuntimeException;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand All @@ -27,7 +27,9 @@ public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('lexik_jose');
$rootNode = $treeBuilder->getRootNode();
Assertion::isInstanceOf($rootNode, ArrayNodeDefinition::class, 'Invalid root node');
if (!$rootNode instanceof ArrayNodeDefinition) {
throw new RuntimeException('Invalid root node');
}
$rootNode
->addDefaultsIfNotSet()
->children()
Expand Down
10 changes: 3 additions & 7 deletions DependencyInjection/SpomkyLabsLexikJoseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace SpomkyLabs\LexikJoseBundle\DependencyInjection;

use function array_key_exists;
use Jose\Bundle\JoseFramework\Helper\ConfigurationHelper;
use SpomkyLabs\LexikJoseBundle\Encoder\LexikJoseEncoder;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function prepend(ContainerBuilder $container): void
{
$isDebug = $container->getParameter('kernel.debug');
$bridgeConfig = current($container->getExtensionConfig($this->getAlias()));
if (!\array_key_exists('claim_checked', $bridgeConfig)) {
if (!array_key_exists('claim_checked', $bridgeConfig)) {
$bridgeConfig['claim_checked'] = [];
}
$claim_aliases = array_merge(
Expand All @@ -96,11 +97,6 @@ public function prepend(ContainerBuilder $container): void
$container->prependExtensionConfig('lexik_jwt_authentication', $lexikConfig);
}

/**
* @param ContainerBuilder $container
* @param array $bridgeConfig
* @param bool $isDebug
*/
private function enableEncryptionSupport(ContainerBuilder $container, array $bridgeConfig, bool $isDebug): void
{
ConfigurationHelper::addJWEBuilder($container, $this->getAlias(), [$bridgeConfig['encryption']['key_encryption_algorithm']], [$bridgeConfig['encryption']['content_encryption_algorithm']], ['DEF'], $isDebug);
Expand Down
51 changes: 5 additions & 46 deletions Encoder/LexikJoseEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand All @@ -15,6 +15,7 @@

use Base64Url\Base64Url;
use Exception;
use function is_string;
use Jose\Component\Checker\ClaimCheckerManager;
use Jose\Component\Checker\HeaderCheckerManager;
use Jose\Component\Checker\InvalidClaimException;
Expand Down Expand Up @@ -134,16 +135,7 @@ final class LexikJoseEncoder implements JWTEncoderInterface
/**
* LexikJoseEncoder constructor.
*
* @param JWSBuilder $jwsBuilder
* @param JWSVerifier $jwsLoader
* @param ClaimCheckerManager $claimCheckerManager
* @param HeaderCheckerManager $signatureHeaderCheckerManager
* @param JWKSet $signatureKeyset
* @param int|string $signatureKeyIndex
* @param string $signatureAlgorithm
* @param string $issuer
* @param int $ttl
* @param array $mandatoryClaims
* @param int|string $signatureKeyIndex
*/
public function __construct(JWSBuilder $jwsBuilder,
JWSVerifier $jwsLoader,
Expand Down Expand Up @@ -171,13 +163,7 @@ public function __construct(JWSBuilder $jwsBuilder,
}

/**
* @param JWEBuilder $jweBuilder
* @param JWEDecrypter $jweLoader
* @param HeaderCheckerManager $encryptionHeaderCheckerManager
* @param JWKSet $encryptionKeyset
* @param int|string $encryptionKeyIndex
* @param string $keyEncryptionAlgorithm
* @param string $contentEncryptionAlgorithm
* @param int|string $encryptionKeyIndex
*/
public function enableEncryptionSupport(JWEBuilder $jweBuilder, JWEDecrypter $jweLoader, HeaderCheckerManager $encryptionHeaderCheckerManager, JWKSet $encryptionKeyset, $encryptionKeyIndex, string $keyEncryptionAlgorithm, string $contentEncryptionAlgorithm): void
{
Expand Down Expand Up @@ -208,11 +194,6 @@ public function encode(array $payload): string
}
}

/**
* @param array $payload
*
* @return string
*/
private function sign(array $payload): string
{
$payload += $this->getAdditionalPayload();
Expand All @@ -233,11 +214,6 @@ private function sign(array $payload): string
return $serializer->serialize($jws, 0);
}

/**
* @param string $jws
*
* @return string
*/
public function encrypt(string $jws): string
{
$headers = $this->getEncryptionHeader();
Expand All @@ -260,11 +236,7 @@ public function encrypt(string $jws): string
}

/**
* @param string $token
*
* @throws JWTDecodeFailureException
*
* @return string
*/
private function decrypt(string $token): string
{
Expand All @@ -279,15 +251,11 @@ private function decrypt(string $token): string
}

/**
* @param string $token
*
* @throws InvalidClaimException
* @throws InvalidHeaderException
* @throws JWTDecodeFailureException
* @throws MissingMandatoryClaimException
* @throws MissingMandatoryHeaderParameterException
*
* @return array
*/
private function verify(string $token): array
{
Expand All @@ -298,7 +266,7 @@ private function verify(string $token): array
throw new JWTDecodeFailureException('decoding_error', 'An error occurred while trying to verify the JWT token.');
}
$jwt = $jws->getPayload();
if (!\is_string($jwt)) {
if (!is_string($jwt)) {
throw new JWTDecodeFailureException('decoding_error', 'An error occurred while trying to verify the JWT token.');
}

Expand Down Expand Up @@ -336,9 +304,6 @@ public function decode($token): array
}
}

/**
* @return array
*/
private function getAdditionalPayload(): array
{
return [
Expand All @@ -350,9 +315,6 @@ private function getAdditionalPayload(): array
];
}

/**
* @return array
*/
private function getSignatureHeader(): array
{
return [
Expand All @@ -362,9 +324,6 @@ private function getSignatureHeader(): array
];
}

/**
* @return array
*/
private function getEncryptionHeader(): array
{
return [
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/encryption_services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand All @@ -15,7 +15,7 @@
use SpomkyLabs\LexikJoseBundle\Checker\EncHeaderChecker;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $container) {
return static function (ContainerConfigurator $container): void {
$container = $container->services()->defaults()
->private()
->autoconfigure()
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2019 Spomky-Labs
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand All @@ -18,7 +18,7 @@
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;

return function (ContainerConfigurator $container) {
return static function (ContainerConfigurator $container): void {
$container = $container->services()->defaults()
->private()
->autoconfigure()
Expand Down
Loading

0 comments on commit 2984144

Please sign in to comment.