1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tymon\JWTAuth; |
4
|
|
|
|
5
|
|
|
use Tymon\JWTAuth\Claims\Claim; |
6
|
|
|
use Tymon\JWTAuth\Exceptions\PayloadException; |
7
|
|
|
use Tymon\JWTAuth\Validators\PayloadValidator; |
8
|
|
|
|
9
|
|
|
class Payload implements \ArrayAccess |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The array of claims |
13
|
|
|
* |
14
|
|
|
* @var \Tymon\JWTAuth\Claims\Claim[] |
15
|
|
|
*/ |
16
|
|
|
private $claims = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Build the Payload |
20
|
|
|
* |
21
|
|
|
* @param array $claims |
22
|
|
|
* @param \Tymon\JWTAuth\Validators\PayloadValidator $validator |
23
|
|
|
* @param bool $refreshFlow |
24
|
|
|
*/ |
25
|
66 |
|
public function __construct(array $claims, PayloadValidator $validator, $refreshFlow = false) |
26
|
|
|
{ |
27
|
66 |
|
$this->claims = $claims; |
28
|
|
|
|
29
|
66 |
|
$validator->setRefreshFlow($refreshFlow)->check($this->toArray()); |
30
|
66 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the array of claim instances |
34
|
|
|
* |
35
|
|
|
* @return \Tymon\JWTAuth\Claims\Claim[] |
36
|
|
|
*/ |
37
|
3 |
|
public function getClaims() |
38
|
|
|
{ |
39
|
3 |
|
return $this->claims; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the array of claims |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
66 |
|
public function toArray() |
48
|
|
|
{ |
49
|
66 |
|
$results = []; |
50
|
66 |
|
foreach ($this->claims as $claim) { |
51
|
66 |
|
$results[$claim->getName()] = $claim->getValue(); |
52
|
44 |
|
} |
53
|
|
|
|
54
|
66 |
|
return $results; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the payload |
59
|
|
|
* |
60
|
|
|
* @param string $claim |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
24 |
|
public function get($claim = null) |
64
|
|
|
{ |
65
|
24 |
|
if (! is_null($claim)) { |
66
|
15 |
|
if (is_array($claim)) { |
67
|
3 |
|
return array_map([$this, 'get'], $claim); |
68
|
|
|
} |
69
|
|
|
|
70
|
15 |
|
return array_get($this->toArray(), $claim, false); |
71
|
|
|
} |
72
|
|
|
|
73
|
12 |
|
return $this->toArray(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Determine whether the payload has the claim |
78
|
|
|
* |
79
|
|
|
* @param \Tymon\JWTAuth\Claims\Claim $claim |
80
|
|
|
* @return boolean |
81
|
|
|
*/ |
82
|
3 |
|
public function has(Claim $claim) |
83
|
|
|
{ |
84
|
3 |
|
return in_array($claim, $this->claims); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the payload as a string |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
3 |
|
public function __toString() |
93
|
|
|
{ |
94
|
3 |
|
return json_encode($this->toArray()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Determine if an item exists at an offset. |
99
|
|
|
* |
100
|
|
|
* @param mixed $key |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
3 |
|
public function offsetExists($key) |
104
|
|
|
{ |
105
|
3 |
|
return array_key_exists($key, $this->toArray()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get an item at a given offset. |
110
|
|
|
* |
111
|
|
|
* @param mixed $key |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
24 |
|
public function offsetGet($key) |
115
|
|
|
{ |
116
|
24 |
|
return array_get($this->toArray(), $key, []); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Don't allow changing the payload as it should be immutable |
121
|
|
|
* |
122
|
|
|
* @param mixed $key |
123
|
|
|
* @param mixed $value |
124
|
|
|
* @throws Exceptions\PayloadException |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
3 |
|
public function offsetSet($key, $value) |
128
|
|
|
{ |
129
|
3 |
|
throw new PayloadException('The payload is immutable'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Don't allow changing the payload as it should be immutable |
134
|
|
|
* |
135
|
|
|
* @param string $key |
136
|
|
|
* @throws Exceptions\PayloadException |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
3 |
|
public function offsetUnset($key) |
140
|
|
|
{ |
141
|
3 |
|
throw new PayloadException('The payload is immutable'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Magically get a claim value |
146
|
|
|
* |
147
|
|
|
* @param string $method |
148
|
|
|
* @param array $parameters |
149
|
|
|
* @return mixed |
150
|
|
|
* @throws \BadMethodCallException |
151
|
|
|
*/ |
152
|
6 |
|
public function __call($method, $parameters) |
153
|
|
|
{ |
154
|
6 |
|
if (! method_exists($this, $method) && starts_with($method, 'get')) { |
155
|
6 |
|
$class = sprintf('Tymon\\JWTAuth\\Claims\\%s', substr($method, 3)); |
156
|
|
|
|
157
|
6 |
|
foreach ($this->claims as $claim) { |
158
|
6 |
|
if (get_class($claim) === $class) { |
159
|
4 |
|
return $claim->getValue(); |
160
|
|
|
} |
161
|
4 |
|
} |
162
|
2 |
|
} |
163
|
|
|
|
164
|
3 |
|
throw new \BadMethodCallException(sprintf('The claim [%s] does not exist on the payload.', $method)); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|