|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Openidconnect\Specification; |
|
4
|
|
|
|
|
5
|
|
|
use Lcobucci\JWT\Token; |
|
6
|
|
|
|
|
7
|
|
|
class ValidatorChain |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $validators = []; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $messages = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param AbstractSpecification[] $validators |
|
21
|
|
|
* |
|
22
|
|
|
* @return $this |
|
23
|
|
|
*/ |
|
24
|
|
|
public function setValidators(array $validators) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->validators = []; |
|
27
|
|
|
|
|
28
|
|
|
foreach ($validators as $validator) { |
|
29
|
|
|
$this->addValidator($validator); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param AbstractSpecification $validator |
|
37
|
|
|
* |
|
38
|
|
|
* @return $this |
|
39
|
|
|
* @internal param string $claim |
|
40
|
|
|
*/ |
|
41
|
|
|
public function addValidator(AbstractSpecification $validator) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->validators[$validator->getName()] = $validator; |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array $data |
|
50
|
|
|
* @param Token $token |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
* @throws \OutOfBoundsException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function validate(array $data, Token $token) |
|
56
|
|
|
{ |
|
57
|
|
|
$valid = true; |
|
58
|
|
|
foreach ($this->validators as $claim => $validator) { |
|
59
|
|
|
if ($token->hasClaim($claim) === false) { |
|
60
|
|
|
if ($validator->isRequired()) { |
|
61
|
|
|
$valid = false; |
|
62
|
|
|
$this->messages[$claim] = sprintf('Missing required value for claim %s', $claim); |
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if (empty($data[$claim])) { |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
} else { |
|
70
|
|
|
if (isset($data[$claim]) && !$validator->isSatisfiedBy($data[$claim], $token->getClaim($claim))) { |
|
71
|
|
|
$valid = false; |
|
72
|
|
|
$this->messages[$claim] = $validator->getMessage(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $valid; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $name |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function hasValidator($name) |
|
86
|
|
|
{ |
|
87
|
|
|
return array_key_exists($name, $this->validators); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $name |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getValidator($name) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->validators[$name]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getMessages() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->messages; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|