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
|
67 |
|
public function __construct(string $name, $value, ?Validator $validator = null) |
47
|
|
|
{ |
48
|
67 |
|
$this->_name = $name; |
49
|
67 |
|
$this->_value = $value; |
50
|
67 |
|
$this->_validator = $validator; |
51
|
67 |
|
} |
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
|
8 |
|
public static function fromNameAndValue(string $name, $value): Claim |
62
|
|
|
{ |
63
|
8 |
|
if (array_key_exists($name, RegisteredClaim::MAP_NAME_TO_CLASS)) { |
64
|
7 |
|
$cls = RegisteredClaim::MAP_NAME_TO_CLASS[$name]; |
65
|
7 |
|
return $cls::fromJSONValue($value); |
66
|
|
|
} |
67
|
1 |
|
return new self($name, $value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the claim name. |
72
|
|
|
*/ |
73
|
44 |
|
public function name(): string |
74
|
|
|
{ |
75
|
44 |
|
return $this->_name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the claim value. |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
13 |
|
public function value() |
84
|
|
|
{ |
85
|
13 |
|
return $this->_value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Validate the claim against a given constraint. |
90
|
|
|
* |
91
|
|
|
* @param mixed $constraint Constraint value |
92
|
|
|
* |
93
|
|
|
* @return bool True if the claim is valid |
94
|
|
|
*/ |
95
|
44 |
|
public function validate($constraint): bool |
96
|
|
|
{ |
97
|
|
|
// if claim has no validator, consider validation failed |
98
|
44 |
|
if (!isset($this->_validator)) { |
99
|
2 |
|
return false; |
100
|
|
|
} |
101
|
42 |
|
return $this->_validator->validate($this->_value, $constraint); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validate the claim in a given context. |
106
|
|
|
* |
107
|
|
|
* Overridden in specific claims that provide default validation. |
108
|
|
|
* |
109
|
|
|
* @return bool True if claim is valid |
110
|
|
|
*/ |
111
|
30 |
|
public function validateWithContext(ValidationContext $ctx): bool |
112
|
|
|
{ |
113
|
|
|
// if validator has no constraint for the claim |
114
|
30 |
|
if (!$ctx->hasConstraint($this->_name)) { |
115
|
18 |
|
return true; |
116
|
|
|
} |
117
|
|
|
// if validation context has an explicitly defined validator for the claim |
118
|
17 |
|
if ($ctx->hasValidator($this->_name)) { |
119
|
2 |
|
return $ctx->validator($this->_name) |
120
|
2 |
|
->validate($this->_value, $ctx->constraint($this->_name)); |
121
|
|
|
} |
122
|
|
|
// validate using claim's default validator |
123
|
15 |
|
return $this->validate($ctx->constraint($this->_name)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|