|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Parroauth2\Client\Claim; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use BadMethodCallException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Read-only container for store claims |
|
10
|
|
|
* |
|
11
|
|
|
* @psalm-immutable |
|
12
|
|
|
*/ |
|
13
|
|
|
class Claims implements ArrayAccess |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array<string, mixed> |
|
17
|
|
|
*/ |
|
18
|
|
|
private $claims; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Claims constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array<string, mixed> $claims |
|
25
|
|
|
*/ |
|
26
|
52 |
|
public function __construct(array $claims) |
|
27
|
|
|
{ |
|
28
|
52 |
|
$this->claims = $claims; |
|
29
|
52 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $offset |
|
35
|
|
|
*/ |
|
36
|
5 |
|
final public function offsetExists($offset): bool |
|
37
|
|
|
{ |
|
38
|
5 |
|
return isset($this->claims[$offset]); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $offset |
|
45
|
|
|
*/ |
|
46
|
|
|
#[\ReturnTypeWillChange] |
|
47
|
27 |
|
final public function offsetGet($offset) |
|
48
|
|
|
{ |
|
49
|
27 |
|
return $this->claims[$offset]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
1 |
|
final public function offsetSet($offset, $value): void |
|
56
|
|
|
{ |
|
57
|
1 |
|
throw new BadMethodCallException(static::class . ' is read-only'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
1 |
|
final public function offsetUnset($offset): void |
|
64
|
|
|
{ |
|
65
|
1 |
|
throw new BadMethodCallException(static::class . ' is read-only'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get a claim value |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $name The claim name |
|
72
|
|
|
* @param mixed $default The default value to use when the claim is not defined |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed The claim value |
|
75
|
|
|
*/ |
|
76
|
20 |
|
final public function claim(string $name, $default = null) |
|
77
|
|
|
{ |
|
78
|
20 |
|
return $this->claims[$name] ?? $default; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get all claims |
|
83
|
|
|
* |
|
84
|
|
|
* @return array<string, mixed> |
|
85
|
|
|
*/ |
|
86
|
15 |
|
final public function claims(): array |
|
87
|
|
|
{ |
|
88
|
15 |
|
return $this->claims; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check if the claim exists |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $claim |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
1 |
|
final public function has(string $claim): bool |
|
99
|
|
|
{ |
|
100
|
1 |
|
return isset($this->claims[$claim]); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Check a claim value |
|
105
|
|
|
* Note: the check perform a strict comparison |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $name The claim name |
|
108
|
|
|
* @param mixed $expected The expected value |
|
109
|
|
|
* |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
7 |
|
final public function check(string $name, $expected): bool |
|
113
|
|
|
{ |
|
114
|
7 |
|
if (!isset($this->claims[$name])) { |
|
115
|
1 |
|
return $expected === null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
7 |
|
if (is_string($expected)) { |
|
119
|
7 |
|
return hash_equals((string) $this->claims[$name], $expected); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
return $this->claims[$name] === $expected; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Check all claims |
|
127
|
|
|
* |
|
128
|
|
|
* @param array<string, mixed> $claims A key / value array for check claims |
|
129
|
|
|
* |
|
130
|
|
|
* @return bool |
|
131
|
|
|
*/ |
|
132
|
1 |
|
final public function checkAll(array $claims): bool |
|
133
|
|
|
{ |
|
134
|
1 |
|
foreach ($claims as $claim => $value) { |
|
135
|
1 |
|
if (!$this->check($claim, $value)) { |
|
136
|
1 |
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
return true; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|