|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWT\Claim; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWT\Claim\Validator\Validator; |
|
8
|
|
|
use Sop\JWX\JWT\ValidationContext; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents a JWT claim. |
|
12
|
|
|
* |
|
13
|
|
|
* @see https://tools.ietf.org/html/rfc7519#section-4 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Claim |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Claim name. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $_name; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Claim value. |
|
26
|
|
|
* |
|
27
|
|
|
* @var mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $_value; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Claim validator. |
|
33
|
|
|
* |
|
34
|
|
|
* @var null|Validator |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_validator; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name Claim name |
|
42
|
|
|
* @param mixed $value Claim value |
|
43
|
|
|
* @param null|Validator $validator Claim validator or null if claim doesn't |
|
44
|
|
|
* provide validation |
|
45
|
|
|
*/ |
|
46
|
66 |
|
public function __construct(string $name, $value, ?Validator $validator = null) |
|
47
|
|
|
{ |
|
48
|
66 |
|
$this->_name = $name; |
|
49
|
66 |
|
$this->_value = $value; |
|
50
|
66 |
|
$this->_validator = $validator; |
|
51
|
66 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Initialize from a name and a value. |
|
55
|
|
|
* |
|
56
|
|
|
* Returns a specific claim object if applicable. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $name Claim name |
|
59
|
|
|
* @param mixed $value Claim value |
|
60
|
|
|
* |
|
61
|
|
|
* @return Claim |
|
62
|
|
|
*/ |
|
63
|
8 |
|
public static function fromNameAndValue(string $name, $value): Claim |
|
64
|
|
|
{ |
|
65
|
8 |
|
if (array_key_exists($name, RegisteredClaim::MAP_NAME_TO_CLASS)) { |
|
66
|
7 |
|
$cls = RegisteredClaim::MAP_NAME_TO_CLASS[$name]; |
|
67
|
7 |
|
return $cls::fromJSONValue($value); |
|
68
|
|
|
} |
|
69
|
1 |
|
return new self($name, $value); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the claim name. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
43 |
|
public function name(): string |
|
78
|
|
|
{ |
|
79
|
43 |
|
return $this->_name; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the claim value. |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
12 |
|
public function value() |
|
88
|
|
|
{ |
|
89
|
12 |
|
return $this->_value; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Validate the claim against a given constraint. |
|
94
|
|
|
* |
|
95
|
|
|
* @param mixed $constraint Constraint value |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool True if the claim is valid |
|
98
|
|
|
*/ |
|
99
|
43 |
|
public function validate($constraint): bool |
|
100
|
|
|
{ |
|
101
|
|
|
// if claim has no validator, consider validation failed |
|
102
|
43 |
|
if (!isset($this->_validator)) { |
|
103
|
2 |
|
return false; |
|
104
|
|
|
} |
|
105
|
41 |
|
return $this->_validator->validate($this->_value, $constraint); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Validate the claim in a given context. |
|
110
|
|
|
* |
|
111
|
|
|
* @param ValidationContext $ctx |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool True if claim is valid |
|
114
|
|
|
*/ |
|
115
|
29 |
|
public function validateWithContext(ValidationContext $ctx): bool |
|
116
|
|
|
{ |
|
117
|
|
|
// if validator has no constraint for the claim |
|
118
|
29 |
|
if (!$ctx->hasConstraint($this->_name)) { |
|
119
|
21 |
|
return true; |
|
120
|
|
|
} |
|
121
|
16 |
|
$constraint = $ctx->constraint($this->_name); |
|
122
|
|
|
// if validation context has an explicitly |
|
123
|
|
|
// defined validator for the claim |
|
124
|
16 |
|
if ($ctx->hasValidator($this->_name)) { |
|
125
|
2 |
|
return $ctx->validator($this->_name) |
|
126
|
2 |
|
->validate($this->_value, $constraint); |
|
127
|
|
|
} |
|
128
|
|
|
// validate using claim's default validator |
|
129
|
14 |
|
return $this->validate($ctx->constraint($this->_name)); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|