|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Sop\JWX\JWT; |
|
6
|
|
|
|
|
7
|
|
|
use Sop\JWX\JWT\Claim\Claim; |
|
8
|
|
|
use Sop\JWX\JWT\Claim\TypedClaims; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents a set of Claim objects. |
|
12
|
|
|
* |
|
13
|
|
|
* @see https://tools.ietf.org/html/rfc7519#section-4 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Claims implements \Countable, \IteratorAggregate |
|
16
|
|
|
{ |
|
17
|
|
|
use TypedClaims; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Claims. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Claim[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $_claims; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Claim ...$claims Zero or more claims |
|
30
|
|
|
*/ |
|
31
|
30 |
|
public function __construct(Claim ...$claims) |
|
32
|
|
|
{ |
|
33
|
30 |
|
$this->_claims = []; |
|
34
|
30 |
|
foreach ($claims as $claim) { |
|
35
|
27 |
|
$this->_claims[$claim->name()] = $claim; |
|
36
|
|
|
} |
|
37
|
30 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Convert to string. |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function __toString(): string |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->toJSON(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Initialize from a JSON string. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $json JSON |
|
51
|
|
|
* |
|
52
|
|
|
* @throws \UnexpectedValueException If JSON is malformed |
|
53
|
|
|
*/ |
|
54
|
8 |
|
public static function fromJSON(string $json): self |
|
55
|
|
|
{ |
|
56
|
8 |
|
$claims = []; |
|
57
|
8 |
|
$fields = json_decode($json, true, 32, JSON_BIGINT_AS_STRING); |
|
58
|
8 |
|
if (!is_array($fields)) { |
|
59
|
1 |
|
throw new \UnexpectedValueException('Invalid JSON.'); |
|
60
|
|
|
} |
|
61
|
7 |
|
foreach ($fields as $name => $value) { |
|
62
|
7 |
|
$claims[] = Claim::fromNameAndValue($name, $value); |
|
63
|
|
|
} |
|
64
|
7 |
|
return new self(...$claims); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get self with Claim objects added. |
|
69
|
|
|
* |
|
70
|
|
|
* @param Claim ...$claims One or more Claim objects |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function withClaims(Claim ...$claims): self |
|
73
|
|
|
{ |
|
74
|
1 |
|
$obj = clone $this; |
|
75
|
1 |
|
foreach ($claims as $claim) { |
|
76
|
1 |
|
$obj->_claims[$claim->name()] = $claim; |
|
77
|
|
|
} |
|
78
|
1 |
|
return $obj; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get all claims. |
|
83
|
|
|
* |
|
84
|
|
|
* @return Claim[] |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function all(): array |
|
87
|
|
|
{ |
|
88
|
1 |
|
return array_values($this->_claims); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check whether claim is present. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $name Claim name |
|
95
|
|
|
*/ |
|
96
|
8 |
|
public function has(string $name): bool |
|
97
|
|
|
{ |
|
98
|
8 |
|
return isset($this->_claims[$name]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get claim by name. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $name Claim name |
|
105
|
|
|
* |
|
106
|
|
|
* @throws \LogicException If claim is not present |
|
107
|
|
|
*/ |
|
108
|
10 |
|
public function get(string $name): Claim |
|
109
|
|
|
{ |
|
110
|
10 |
|
if (!isset($this->_claims[$name])) { |
|
111
|
1 |
|
throw new \LogicException("Claim {$name} not set."); |
|
112
|
|
|
} |
|
113
|
9 |
|
return $this->_claims[$name]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Convert to a JSON. |
|
118
|
|
|
*/ |
|
119
|
10 |
|
public function toJSON(): string |
|
120
|
|
|
{ |
|
121
|
10 |
|
$data = []; |
|
122
|
10 |
|
foreach ($this->_claims as $claim) { |
|
123
|
8 |
|
$data[$claim->name()] = $claim->value(); |
|
124
|
|
|
} |
|
125
|
10 |
|
return json_encode((object) $data, JSON_UNESCAPED_SLASHES); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Check whether a claims set is valid in the given context. |
|
130
|
|
|
* |
|
131
|
|
|
* @param ValidationContext $ctx Validation context |
|
132
|
|
|
*/ |
|
133
|
4 |
|
public function isValid(ValidationContext $ctx): bool |
|
134
|
|
|
{ |
|
135
|
|
|
try { |
|
136
|
4 |
|
$ctx->validate($this); |
|
137
|
2 |
|
} catch (\RuntimeException $e) { |
|
138
|
2 |
|
return false; |
|
139
|
|
|
} |
|
140
|
2 |
|
return true; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get the number of claims. |
|
145
|
|
|
* |
|
146
|
|
|
* @see \Countable::count() |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function count(): int |
|
149
|
|
|
{ |
|
150
|
2 |
|
return count($this->_claims); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get iterator for Claim objects keyed by claim name. |
|
155
|
|
|
* |
|
156
|
|
|
* @see \IteratorAggregate::getIterator() |
|
157
|
|
|
*/ |
|
158
|
28 |
|
public function getIterator(): \ArrayIterator |
|
159
|
|
|
{ |
|
160
|
28 |
|
return new \ArrayIterator($this->_claims); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|