|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tymon\JWTAuth; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Tymon\JWTAuth\Claims\Factory; |
|
7
|
|
|
use Tymon\JWTAuth\Validators\PayloadValidator; |
|
8
|
|
|
|
|
9
|
|
|
class PayloadFactory |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Tymon\JWTAuth\Claims\Factory |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $claimFactory; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Illuminate\Http\Request |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var \Tymon\JWTAuth\Validators\PayloadValidator |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $validator; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $ttl = 60; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var boolean |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $refreshFlow = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $defaultClaims = ['iss', 'iat', 'exp', 'nbf', 'jti']; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $claims = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param \Tymon\JWTAuth\Claims\Factory $claimFactory |
|
48
|
|
|
* @param \Illuminate\Http\Request $request |
|
49
|
|
|
* @param \Tymon\JWTAuth\Validators\PayloadValidator $validator |
|
50
|
|
|
*/ |
|
51
|
12 |
|
public function __construct(Factory $claimFactory, Request $request, PayloadValidator $validator) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
12 |
|
$this->claimFactory = $claimFactory; |
|
54
|
12 |
|
$this->request = $request; |
|
55
|
12 |
|
$this->validator = $validator; |
|
56
|
12 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create the Payload instance |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $customClaims |
|
62
|
|
|
* @return \Tymon\JWTAuth\Payload |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function make(array $customClaims = []) |
|
65
|
|
|
{ |
|
66
|
9 |
|
$claims = $this->buildClaims($customClaims)->resolveClaims(); |
|
67
|
|
|
|
|
68
|
9 |
|
return new Payload($claims, $this->validator, $this->refreshFlow); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Add an array of claims to the Payload |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $claims |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
9 |
|
public function addClaims(array $claims) |
|
78
|
|
|
{ |
|
79
|
9 |
|
foreach ($claims as $name => $value) { |
|
80
|
3 |
|
$this->addClaim($name, $value); |
|
81
|
6 |
|
} |
|
82
|
|
|
|
|
83
|
9 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Add a claim to the Payload |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $name |
|
90
|
|
|
* @param mixed $value |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
9 |
|
public function addClaim($name, $value) |
|
94
|
|
|
{ |
|
95
|
9 |
|
$this->claims[$name] = $value; |
|
96
|
|
|
|
|
97
|
9 |
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Build the default claims |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $customClaims |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
9 |
|
protected function buildClaims(array $customClaims) |
|
107
|
|
|
{ |
|
108
|
|
|
// add any custom claims first |
|
109
|
9 |
|
$this->addClaims(array_diff_key($customClaims, $this->defaultClaims)); |
|
110
|
|
|
|
|
111
|
9 |
|
foreach ($this->defaultClaims as $claim) { |
|
112
|
9 |
|
if (! array_key_exists($claim, $customClaims)) { |
|
113
|
9 |
|
$this->addClaim($claim, $this->$claim()); |
|
114
|
6 |
|
} |
|
115
|
6 |
|
} |
|
116
|
|
|
|
|
117
|
9 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Build out the Claim DTO's |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
9 |
|
public function resolveClaims() |
|
126
|
|
|
{ |
|
127
|
9 |
|
$resolved = []; |
|
128
|
9 |
|
foreach ($this->claims as $name => $value) { |
|
129
|
9 |
|
$resolved[] = $this->claimFactory->get($name, $value); |
|
130
|
6 |
|
} |
|
131
|
|
|
|
|
132
|
9 |
|
return $resolved; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Set the Issuer (iss) claim |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
9 |
|
public function iss() |
|
141
|
|
|
{ |
|
142
|
9 |
|
return $this->request->url(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Set the Issued At (iat) claim |
|
147
|
|
|
* |
|
148
|
|
|
* @return int |
|
149
|
|
|
*/ |
|
150
|
6 |
|
public function iat() |
|
151
|
|
|
{ |
|
152
|
6 |
|
return Utils::now()->format('U'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Set the Expiration (exp) claim |
|
157
|
|
|
* |
|
158
|
|
|
* @return int |
|
159
|
|
|
*/ |
|
160
|
9 |
|
public function exp() |
|
161
|
|
|
{ |
|
162
|
9 |
|
return Utils::now()->addMinutes($this->ttl)->format('U'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Set the Not Before (nbf) claim |
|
167
|
|
|
* |
|
168
|
|
|
* @return int |
|
169
|
|
|
*/ |
|
170
|
9 |
|
public function nbf() |
|
171
|
|
|
{ |
|
172
|
9 |
|
return Utils::now()->format('U'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Set a unique id (jti) for the token |
|
177
|
|
|
* |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
6 |
|
protected function jti() |
|
181
|
|
|
{ |
|
182
|
6 |
|
$sub = array_get($this->claims, 'sub', ''); |
|
183
|
6 |
|
$nbf = array_get($this->claims, 'nbf', ''); |
|
184
|
|
|
|
|
185
|
6 |
|
return md5(sprintf('jti.%s.%s', $sub, $nbf)); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Set the token ttl (in minutes) |
|
190
|
|
|
* |
|
191
|
|
|
* @param int $ttl |
|
192
|
|
|
* @return $this |
|
193
|
|
|
*/ |
|
194
|
3 |
|
public function setTTL($ttl) |
|
195
|
|
|
{ |
|
196
|
3 |
|
$this->ttl = $ttl; |
|
197
|
|
|
|
|
198
|
3 |
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get the token ttl |
|
203
|
|
|
* |
|
204
|
|
|
* @return int |
|
205
|
|
|
*/ |
|
206
|
3 |
|
public function getTTL() |
|
207
|
|
|
{ |
|
208
|
3 |
|
return $this->ttl; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Set the refresh flow |
|
213
|
|
|
* |
|
214
|
|
|
* @param boolean $refreshFlow |
|
215
|
|
|
* @return $this |
|
216
|
|
|
*/ |
|
217
|
|
|
public function setRefreshFlow($refreshFlow = true) |
|
218
|
|
|
{ |
|
219
|
|
|
$this->refreshFlow = $refreshFlow; |
|
220
|
|
|
|
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Magically add a claim |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $method |
|
228
|
|
|
* @param array $parameters |
|
229
|
|
|
* @return PayloadFactory |
|
230
|
|
|
* @throws \BadMethodCallException |
|
231
|
|
|
*/ |
|
232
|
6 |
|
public function __call($method, $parameters) |
|
233
|
|
|
{ |
|
234
|
6 |
|
$this->addClaim($method, $parameters[0]); |
|
235
|
|
|
|
|
236
|
6 |
|
return $this; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|