-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AbstractLoader.php
287 lines (235 loc) · 7.49 KB
/
AbstractLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Jose\Easy;
use function in_array;
use InvalidArgumentException;
use function is_array;
use function is_callable;
use function is_int;
use function is_string;
use Jose\Component\Checker;
use Jose\Component\Core\Algorithm;
use Jose\Component\Core\JWK;
use Jose\Component\Core\JWKSet;
abstract class AbstractLoader
{
/**
* @var string
*/
protected $token;
/**
* @var JWKSet
*/
protected $jwkset;
/**
* @var Checker\HeaderChecker[]
*/
protected $headerCheckers = [];
/**
* @var Checker\ClaimChecker[]
*/
protected $claimCheckers = [];
/**
* @var string[]
*/
protected $allowedAlgorithms = [];
/**
* @var Algorithm[]
*/
protected $algorithms = [];
/**
* @var string[]
*/
protected $mandatoryClaims = [];
protected function __construct(string $token)
{
$this->token = $token;
$this->jwkset = new JWKSet([]);
$this->claimCheckers = [];
$this->algorithms = (new AlgorithmProvider($this->getAlgorithmMap()))
->getAvailableAlgorithms()
;
}
/**
* @param string[] $mandatoryClaims
*/
public function mandatory(array $mandatoryClaims): self
{
$clone = clone $this;
$clone->mandatoryClaims = $mandatoryClaims;
return $clone;
}
public function aud(string $aud, bool $inHeader = false): self
{
return $this->claim('aud', new Checker\AudienceChecker($aud, true), $inHeader);
}
public function iss(string $iss, bool $inHeader = false): self
{
return $this->claim('iss', new Checker\IssuerChecker([$iss], true), $inHeader);
}
public function jti(string $jti, bool $inHeader = false): self
{
return $this->claim('jti', $jti, $inHeader);
}
public function sub(string $sub, bool $inHeader = false): self
{
return $this->claim('sub', $sub, $inHeader);
}
/**
* @param null|array|callable|Checker\ClaimChecker $checker
*/
public function claim(string $key, $checker, bool $inHeader = false): self
{
$clone = clone $this;
if (false === $checker) {
unset($clone->claimCheckers[$key]);
return $clone;
}
switch (true) {
case $checker instanceof Checker\ClaimChecker:
break;
case is_callable($checker):
$checker = new CallableChecker($key, $checker);
break;
case is_array($checker):
$checker = new CallableChecker($key, static function ($value) use ($checker) {return in_array($value, $checker, true); });
break;
default:
$checker = new CallableChecker($key, static function ($value) use ($checker) {return $value === $checker; });
}
$clone->claimCheckers[$key] = $checker;
if ($inHeader) {
return $clone->header($key, $checker);
}
return $clone;
}
/**
* @param false|int $leeway
*
* @throws InvalidArgumentException if the leeway is negative, not an integer or not false
*/
public function exp($leeway = 0, bool $inHeader = false): self
{
if (false === $leeway) {
$clone = clone $this;
unset($clone->claimCheckers['exp']);
return $clone;
}
if (!is_int($leeway) or $leeway < 0) {
throw new InvalidArgumentException('First parameter for "exp" claim is invalid. Set false to disable or a positive integer.');
}
return $this->claim('exp', new Checker\ExpirationTimeChecker($leeway), $inHeader);
}
/**
* @param false|int $leeway
*
* @throws InvalidArgumentException if the leeway is negative, not an integer or not false
*/
public function nbf($leeway = 0, bool $inHeader = false): self
{
if (false === $leeway) {
$clone = clone $this;
unset($clone->claimCheckers['nbf']);
return $clone;
}
if (!is_int($leeway) or $leeway < 0) {
throw new InvalidArgumentException('First parameter for "nbf" claim is invalid. Set false to disable or a positive integer.');
}
return $this->claim('nbf', new Checker\NotBeforeChecker($leeway, true), $inHeader);
}
/**
* @param false|int $leeway
*
* @throws InvalidArgumentException if the leeway is negative, not an integer or not false
*/
public function iat($leeway = 0, bool $inHeader = false): self
{
if (false === $leeway) {
$clone = clone $this;
unset($clone->claimCheckers['iat']);
return $clone;
}
if (!is_int($leeway) or $leeway < 0) {
throw new InvalidArgumentException('First parameter for "iat" claim is invalid. Set false to disable or a positive integer.');
}
return $this->claim('iat', new Checker\IssuedAtChecker($leeway, true), $inHeader);
}
/**
* @param Algorithm|string $alg
*
* @throws InvalidArgumentException if the algorithm is not a string or an instance of Jose\Component\Core\Algorithm
*/
public function alg($alg): self
{
$clone = clone $this;
switch (true) {
case is_string($alg):
$clone->allowedAlgorithms[] = $alg;
return $clone;
case $alg instanceof Algorithm:
$clone->algorithms[$alg->name()] = $alg;
$clone->allowedAlgorithms[] = $alg->name();
return $clone;
default:
throw new InvalidArgumentException('Invalid parameter "alg". Shall be a string or an algorithm instance.');
}
}
/**
* @param Algorithm[]|string[] $algs
*/
public function algs($algs): self
{
$clone = clone $this;
foreach ($algs as $alg) {
$clone = $clone->alg($alg);
}
return $clone;
}
/**
* @param array|callable|Checker\HeaderChecker|false|mixed $checker
*/
public function header(string $key, $checker): self
{
$clone = clone $this;
if (false === $checker) {
unset($clone->headerCheckers[$key]);
return $clone;
}
switch (true) {
case $checker instanceof Checker\HeaderChecker:
break;
case is_callable($checker):
$checker = new CallableChecker($key, $checker);
break;
case is_array($checker):
$checker = new CallableChecker($key, static function ($value) use ($checker) {return in_array($value, $checker, true); });
break;
default:
$checker = new CallableChecker($key, static function ($value) use ($checker) {return $value === $checker; });
}
$clone->headerCheckers[$key] = $checker;
return $clone;
}
public function key(JWK $jwk): self
{
$clone = clone $this;
$jwkset = $this->jwkset->with($jwk);
$clone->jwkset = $jwkset;
return $clone;
}
public function keyset(JWKSet $jwkset): self
{
$clone = clone $this;
$clone->jwkset = $jwkset;
return $clone;
}
abstract protected function getAlgorithmMap(): array;
}