From 47f3b3121e7f9e2bdff6d2dda47b02c5024e0718 Mon Sep 17 00:00:00 2001 From: Olivier Philippon Date: Fri, 14 Apr 2017 13:56:19 +0100 Subject: [PATCH] Fix: Move `is_numeric` check from build time to runtime This will allow us to use the Symfony 3.2+ "%env(JWT_TOKEN_TTL)%" syntax --- DependencyInjection/Configuration.php | 6 ------ Services/JWSProvider/DefaultJWSProvider.php | 4 ++++ Services/JWSProvider/LcobucciJWSProvider.php | 4 ++++ Tests/Services/JWSProvider/AbstractJWSProviderTest.php | 9 +++++++++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 94203e17..c907a79e 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -34,12 +34,6 @@ public function getConfigTreeBuilder() ->end() ->scalarNode('token_ttl') ->defaultValue(3600) - ->validate() - ->ifTrue(function ($ttl) { - return !is_numeric($ttl); - }) - ->thenInvalid('The token_ttl must be a numeric value.') - ->end() ->end() ->arrayNode('encoder') ->addDefaultsIfNotSet() diff --git a/Services/JWSProvider/DefaultJWSProvider.php b/Services/JWSProvider/DefaultJWSProvider.php index 018b4bc6..10e55952 100644 --- a/Services/JWSProvider/DefaultJWSProvider.php +++ b/Services/JWSProvider/DefaultJWSProvider.php @@ -47,6 +47,10 @@ class DefaultJWSProvider implements JWSProviderInterface */ public function __construct(KeyLoaderInterface $keyLoader, $cryptoEngine, $signatureAlgorithm, $ttl) { + if (!is_numeric($ttl)) { + throw new \InvalidArgumentException(sprintf('The TTL should be a numeric value, got %s instead.', $ttl)); + } + $cryptoEngine = $cryptoEngine == 'openssl' ? 'OpenSSL' : 'SecLib'; if (!$this->isAlgorithmSupportedForEngine($cryptoEngine, $signatureAlgorithm)) { diff --git a/Services/JWSProvider/LcobucciJWSProvider.php b/Services/JWSProvider/LcobucciJWSProvider.php index a32dd636..d9946849 100644 --- a/Services/JWSProvider/LcobucciJWSProvider.php +++ b/Services/JWSProvider/LcobucciJWSProvider.php @@ -38,6 +38,10 @@ class LcobucciJWSProvider implements JWSProviderInterface */ public function __construct(RawKeyLoader $keyLoader, $cryptoEngine, $signatureAlgorithm, $ttl) { + if (!is_numeric($ttl)) { + throw new \InvalidArgumentException(sprintf('The TTL should be a numeric value, got %s instead.', $ttl)); + } + $this->keyLoader = $keyLoader; $this->signer = $this->getSignerForAlgorithm($signatureAlgorithm); $this->ttl = $ttl; diff --git a/Tests/Services/JWSProvider/AbstractJWSProviderTest.php b/Tests/Services/JWSProvider/AbstractJWSProviderTest.php index 1d039dc1..501c7ed4 100644 --- a/Tests/Services/JWSProvider/AbstractJWSProviderTest.php +++ b/Tests/Services/JWSProvider/AbstractJWSProviderTest.php @@ -117,6 +117,15 @@ public function testInvalidsignatureAlgorithm() new static::$providerClass($this->getKeyLoaderMock(), 'openssl', 'wrongAlgorithm', 3600); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The TTL should be a numeric value + */ + public function testInvalidTtl() + { + new static::$providerClass($this->getKeyLoaderMock(), 'openssl', 'wrongAlgorithm', 'invalid_ttl'); + } + private function getKeyLoaderMock() { return $this