|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Jose\Component\Checker; |
|
15
|
|
|
|
|
16
|
|
|
use Jose\Component\Core\Converter\JsonConverterInterface; |
|
17
|
|
|
use Jose\Component\Core\JWTInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class ClaimCheckerManager. |
|
21
|
|
|
*/ |
|
22
|
|
|
final class ClaimCheckerManager |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var JsonConverterInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $payloadEncoder; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ClaimCheckerInterface[] |
|
31
|
|
|
*/ |
|
32
|
|
|
private $checkers = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* ClaimCheckerManager constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param JsonConverterInterface $payloadEncoder |
|
38
|
|
|
* @param ClaimCheckerInterface[] $checkers |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(JsonConverterInterface $payloadEncoder, array $checkers) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->payloadEncoder = $payloadEncoder; |
|
43
|
|
|
foreach ($checkers as $checker) { |
|
44
|
|
|
$this->add($checker); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param ClaimCheckerInterface $checker |
|
50
|
|
|
*/ |
|
51
|
|
|
private function add(ClaimCheckerInterface $checker) |
|
52
|
|
|
{ |
|
53
|
|
|
$claim = $checker->supportedClaim(); |
|
54
|
|
|
if (array_key_exists($claim, $this->checkers)) { |
|
55
|
|
|
throw new \InvalidArgumentException(sprintf('The claim checker "%s" is already supported.', $claim)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->checkers[$claim] = $checker; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param JWTInterface $jwt |
|
63
|
|
|
*/ |
|
64
|
|
|
public function check(JWTInterface $jwt) |
|
65
|
|
|
{ |
|
66
|
|
|
$claims = $this->payloadEncoder->decode($jwt->getPayload()); |
|
67
|
|
|
if (!is_array($claims)) { |
|
68
|
|
|
throw new \InvalidArgumentException('The payload is does not contain claims.'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
foreach ($this->checkers as $claim => $checker) { |
|
72
|
|
|
if (array_key_exists($claim, $claims)) { |
|
73
|
|
|
$checker->checkClaim($claims[$claim]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|