Skip to content

Commit 6be397b

Browse files
committed
Analyzers corrected
1 parent e9696eb commit 6be397b

File tree

22 files changed

+143
-42
lines changed

22 files changed

+143
-42
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
},
5353
"require": {
5454
"php": "^7.2",
55-
"ext-gmp": "*",
5655
"ext-json": "*",
5756
"ext-mbstring": "*",
5857
"ext-openssl": "*",
@@ -71,6 +70,7 @@
7170
"require-dev": {
7271
"ext-curl": "*",
7372
"ext-gmp": "*",
73+
"ext-sodium": "*",
7474
"bjeavons/zxcvbn-php": "^0.4.0",
7575
"blackfire/php-sdk": "^1.14",
7676
"nyholm/psr7": "^1.0",

src/Bundle/JoseFramework/Resources/config/analyzers.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* of the MIT license. See the LICENSE file for details.
1212
*/
1313

14+
use Jose\Component\Core\Util\Ecc\NistCurve;
1415
use Jose\Component\KeyManagement\Analyzer;
1516
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1617
use ZxcvbnPhp\Zxcvbn;
@@ -37,13 +38,16 @@
3738
$container->set(Analyzer\OctAnalyzer::class);
3839
$container->set(Analyzer\MixedKeyTypes::class);
3940
$container->set(Analyzer\MixedPublicAndPrivateKeys::class);
40-
$container->set(Analyzer\ES256KeyAnalyzer::class);
41-
$container->set(Analyzer\ES384KeyAnalyzer::class);
42-
$container->set(Analyzer\ES512KeyAnalyzer::class);
4341
$container->set(Analyzer\HS256KeyAnalyzer::class);
4442
$container->set(Analyzer\HS384KeyAnalyzer::class);
4543
$container->set(Analyzer\HS512KeyAnalyzer::class);
4644

45+
if (class_exists(NistCurve::class)) {
46+
$container->set(Analyzer\ES256KeyAnalyzer::class);
47+
$container->set(Analyzer\ES384KeyAnalyzer::class);
48+
$container->set(Analyzer\ES512KeyAnalyzer::class);
49+
}
50+
4751
if (class_exists(Zxcvbn::class)) {
4852
$container->set(Analyzer\ZxcvbnKeyAnalyzer::class);
4953
}

src/Bundle/JoseFramework/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"v1.1": "1.1.x-dev",
5353
"v1.2": "1.2.x-dev",
5454
"v1.3": "1.3.x-dev",
55-
"v2.0": "2.0.x-dev"
55+
"v2.0": "2.0.x-dev",
56+
"v2.1": "2.1.x-dev"
5657
}
5758
},
5859
"config": {

src/Component/Checker/ExpirationTimeChecker.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717
* This class is a claim checker.
1818
* When the "exp" is present, it will compare the value with the current timestamp.
1919
*/
20-
final class ExpirationTimeChecker implements ClaimChecker
20+
final class ExpirationTimeChecker implements ClaimChecker, HeaderChecker
2121
{
22-
private const CLAIM_NAME = 'exp';
22+
private const NAME = 'exp';
2323

2424
/**
2525
* @var int
2626
*/
2727
private $allowedTimeDrift;
28+
/**
29+
* @var bool
30+
*/
31+
private $protectedHeaderOnly;
2832

29-
public function __construct(int $allowedTimeDrift = 0)
33+
public function __construct(int $allowedTimeDrift = 0, bool $protectedHeaderOnly = false)
3034
{
3135
$this->allowedTimeDrift = $allowedTimeDrift;
36+
$this->protectedHeaderOnly = $protectedHeaderOnly;
3237
}
3338

3439
/**
@@ -37,15 +42,35 @@ public function __construct(int $allowedTimeDrift = 0)
3742
public function checkClaim($value): void
3843
{
3944
if (!\is_int($value)) {
40-
throw new InvalidClaimException('"exp" must be an integer.', self::CLAIM_NAME, $value);
45+
throw new InvalidClaimException('"exp" must be an integer.', self::NAME, $value);
4146
}
4247
if (time() > $value + $this->allowedTimeDrift) {
43-
throw new InvalidClaimException('The token expired.', self::CLAIM_NAME, $value);
48+
throw new InvalidClaimException('The token expired.', self::NAME, $value);
4449
}
4550
}
4651

4752
public function supportedClaim(): string
4853
{
49-
return self::CLAIM_NAME;
54+
return self::NAME;
55+
}
56+
57+
public function checkHeader($value): void
58+
{
59+
if (!\is_int($value)) {
60+
throw new InvalidHeaderException('"exp" must be an integer.', self::NAME, $value);
61+
}
62+
if (time() > $value + $this->allowedTimeDrift) {
63+
throw new InvalidHeaderException('The token expired.', self::NAME, $value);
64+
}
65+
}
66+
67+
public function supportedHeader(): string
68+
{
69+
return self::NAME;
70+
}
71+
72+
public function protectedHeaderOnly(): bool
73+
{
74+
return $this->protectedHeaderOnly;
5075
}
5176
}

src/Component/Checker/IssuedAtChecker.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717
* This class is a claim checker.
1818
* When the "iat" is present, it will compare the value with the current timestamp.
1919
*/
20-
final class IssuedAtChecker implements ClaimChecker
20+
final class IssuedAtChecker implements ClaimChecker, HeaderChecker
2121
{
22-
private const CLAIM_NAME = 'iat';
22+
private const NAME = 'iat';
2323

2424
/**
2525
* @var int
2626
*/
2727
private $allowedTimeDrift;
28+
/**
29+
* @var bool
30+
*/
31+
private $protectedHeaderOnly;
2832

29-
public function __construct(int $allowedTimeDrift = 0)
33+
public function __construct(int $allowedTimeDrift = 0, bool $protectedHeaderOnly = false)
3034
{
3135
$this->allowedTimeDrift = $allowedTimeDrift;
36+
$this->protectedHeaderOnly = $protectedHeaderOnly;
3237
}
3338

3439
/**
@@ -37,15 +42,35 @@ public function __construct(int $allowedTimeDrift = 0)
3742
public function checkClaim($value): void
3843
{
3944
if (!\is_int($value)) {
40-
throw new InvalidClaimException('The claim "iat" must be an integer.', self::CLAIM_NAME, $value);
45+
throw new InvalidClaimException('"iat" must be an integer.', self::NAME, $value);
4146
}
4247
if (time() < $value - $this->allowedTimeDrift) {
43-
throw new InvalidClaimException('The JWT is issued in the future.', self::CLAIM_NAME, $value);
48+
throw new InvalidClaimException('The JWT is issued in the future.', self::NAME, $value);
4449
}
4550
}
4651

4752
public function supportedClaim(): string
4853
{
49-
return self::CLAIM_NAME;
54+
return self::NAME;
55+
}
56+
57+
public function checkHeader($value): void
58+
{
59+
if (!\is_int($value)) {
60+
throw new InvalidHeaderException('The header "iat" must be an integer.', self::NAME, $value);
61+
}
62+
if (time() < $value - $this->allowedTimeDrift) {
63+
throw new InvalidHeaderException('The JWT is issued in the future.', self::NAME, $value);
64+
}
65+
}
66+
67+
public function supportedHeader(): string
68+
{
69+
return self::NAME;
70+
}
71+
72+
public function protectedHeaderOnly(): bool
73+
{
74+
return $this->protectedHeaderOnly;
5075
}
5176
}

src/Component/Checker/NotBeforeChecker.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717
* This class is a claim checker.
1818
* When the "nbf" is present, it will compare the value with the current timestamp.
1919
*/
20-
final class NotBeforeChecker implements ClaimChecker
20+
final class NotBeforeChecker implements ClaimChecker, HeaderChecker
2121
{
22-
private const CLAIM_NAME = 'nbf';
22+
private const NAME = 'nbf';
2323

2424
/**
2525
* @var int
2626
*/
2727
private $allowedTimeDrift;
28+
/**
29+
* @var bool
30+
*/
31+
private $protectedHeaderOnly;
2832

29-
public function __construct(int $allowedTimeDrift = 0)
33+
public function __construct(int $allowedTimeDrift = 0, bool $protectedHeaderOnly = false)
3034
{
3135
$this->allowedTimeDrift = $allowedTimeDrift;
36+
$this->protectedHeaderOnly = $protectedHeaderOnly;
3237
}
3338

3439
/**
@@ -37,15 +42,35 @@ public function __construct(int $allowedTimeDrift = 0)
3742
public function checkClaim($value): void
3843
{
3944
if (!\is_int($value)) {
40-
throw new InvalidClaimException('"nbf" must be an integer.', self::CLAIM_NAME, $value);
45+
throw new InvalidClaimException('"nbf" must be an integer.', self::NAME, $value);
4146
}
4247
if (time() < $value - $this->allowedTimeDrift) {
43-
throw new InvalidClaimException('The JWT can not be used yet.', self::CLAIM_NAME, $value);
48+
throw new InvalidClaimException('The JWT can not be used yet.', self::NAME, $value);
4449
}
4550
}
4651

4752
public function supportedClaim(): string
4853
{
49-
return self::CLAIM_NAME;
54+
return self::NAME;
55+
}
56+
57+
public function checkHeader($value): void
58+
{
59+
if (!\is_int($value)) {
60+
throw new InvalidHeaderException('"nbf" must be an integer.', self::NAME, $value);
61+
}
62+
if (time() < $value - $this->allowedTimeDrift) {
63+
throw new InvalidHeaderException('The JWT can not be used yet.', self::NAME, $value);
64+
}
65+
}
66+
67+
public function supportedHeader(): string
68+
{
69+
return self::NAME;
70+
}
71+
72+
public function protectedHeaderOnly(): bool
73+
{
74+
return $this->protectedHeaderOnly;
5075
}
5176
}

src/Component/Encryption/Serializer/JSONFlattenedSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public function unserialize(string $input): JWE
8888
);
8989
}
9090

91-
private function checkData(array $data): void
91+
private function checkData(?array $data): void
9292
{
93-
if (!isset($data['ciphertext']) || isset($data['recipients'])) {
93+
if (null === $data || !isset($data['ciphertext']) || isset($data['recipients'])) {
9494
throw new InvalidArgumentException('Unsupported input.');
9595
}
9696
}

src/Component/Encryption/Serializer/JSONGeneralSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public function unserialize(string $input): JWE
9797
);
9898
}
9999

100-
private function checkData(array $data): void
100+
private function checkData(?array $data): void
101101
{
102-
if (!isset($data['ciphertext']) || !isset($data['recipients'])) {
102+
if (null === $data || !isset($data['ciphertext']) || !isset($data['recipients'])) {
103103
throw new InvalidArgumentException('Unsupported input.');
104104
}
105105
}

src/Component/KeyManagement/Analyzer/ES256KeyAnalyzer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616
use Base64Url\Base64Url;
1717
use Jose\Component\Core\JWK;
1818
use Jose\Component\Core\Util\Ecc\NistCurve;
19+
use RuntimeException;
1920

2021
final class ES256KeyAnalyzer implements KeyAnalyzer
2122
{
23+
public function __construct()
24+
{
25+
if (!class_exists(NistCurve::class)) {
26+
throw new RuntimeException('Please install web-token/jwt-util-ecc to use this key analyzer');
27+
}
28+
}
29+
2230
public function analyze(JWK $jwk, MessageBag $bag): void
2331
{
2432
if ('EC' !== $jwk->get('kty')) {

src/Component/KeyManagement/Analyzer/ES384KeyAnalyzer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616
use Base64Url\Base64Url;
1717
use Jose\Component\Core\JWK;
1818
use Jose\Component\Core\Util\Ecc\NistCurve;
19+
use RuntimeException;
1920

2021
final class ES384KeyAnalyzer implements KeyAnalyzer
2122
{
23+
public function __construct()
24+
{
25+
if (!class_exists(NistCurve::class)) {
26+
throw new RuntimeException('Please install web-token/jwt-util-ecc to use this key analyzer');
27+
}
28+
}
29+
2230
public function analyze(JWK $jwk, MessageBag $bag): void
2331
{
2432
if ('EC' !== $jwk->get('kty')) {

0 commit comments

Comments
 (0)