1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TMV\JWTModule\DIFactory\Checker; |
6
|
|
|
|
7
|
|
|
use Jose\Component\Checker\HeaderChecker; |
8
|
|
|
use Jose\Component\Checker\HeaderCheckerManagerFactory; |
9
|
|
|
use Jose\Component\Checker\TokenTypeSupport; |
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use TMV\JWTModule\Exception\InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
class HeaderCheckerManagerFactoryFactory |
14
|
|
|
{ |
15
|
|
|
private const MODULE_KEY = 'jwt_module'; |
16
|
|
|
|
17
|
|
|
private const SERVICE_TYPE_KEY = 'header_checker_manager'; |
18
|
|
|
|
19
|
9 |
|
public function __invoke(ContainerInterface $container): HeaderCheckerManagerFactory |
20
|
|
|
{ |
21
|
9 |
|
$config = $container->get('config')[static::MODULE_KEY][static::SERVICE_TYPE_KEY] ?? []; |
22
|
|
|
|
23
|
9 |
|
$checkers = $this->getCheckers($container, $config['checkers'] ?? []); |
24
|
9 |
|
$tokenTypes = $this->getTokenType($container, $config['token_types'] ?? []); |
25
|
|
|
|
26
|
9 |
|
$factory = new HeaderCheckerManagerFactory(); |
27
|
|
|
|
28
|
9 |
|
foreach ($checkers as $alias => $checker) { |
29
|
1 |
|
if (! \is_string($alias)) { |
30
|
|
|
throw new InvalidArgumentException('Invalid alias for header checker'); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
$factory->add($alias, $checker); |
34
|
|
|
} |
35
|
|
|
|
36
|
9 |
|
foreach ($tokenTypes as $tokenType) { |
37
|
9 |
|
$factory->addTokenTypeSupport($tokenType); |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
return $factory; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ContainerInterface $container |
45
|
|
|
* @param string[]|HeaderChecker[] $checkers |
46
|
|
|
* |
47
|
|
|
* @return HeaderChecker[] |
48
|
|
|
*/ |
49
|
9 |
|
private function getCheckers(ContainerInterface $container, array $checkers): array |
50
|
|
|
{ |
51
|
|
|
return \array_map(static function ($checker) use ($container) { |
52
|
1 |
|
if ($checker instanceof HeaderChecker) { |
53
|
1 |
|
return $checker; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$checkerInstance = $container->get($checker); |
57
|
|
|
|
58
|
1 |
|
if (! $checkerInstance instanceof HeaderChecker) { |
59
|
|
|
throw new InvalidArgumentException('Invalid header checker'); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $checkerInstance; |
63
|
9 |
|
}, $checkers); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ContainerInterface $container |
68
|
|
|
* @param string[]|TokenTypeSupport[] $tokenTypes |
69
|
|
|
* |
70
|
|
|
* @return TokenTypeSupport[] |
71
|
|
|
*/ |
72
|
9 |
|
private function getTokenType(ContainerInterface $container, array $tokenTypes): array |
73
|
|
|
{ |
74
|
|
|
return \array_map(static function ($tokenType) use ($container) { |
75
|
9 |
|
if ($tokenType instanceof TokenTypeSupport) { |
76
|
1 |
|
return $tokenType; |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
$tokenType = $container->get($tokenType); |
80
|
|
|
|
81
|
9 |
|
if (! $tokenType instanceof TokenTypeSupport) { |
82
|
|
|
throw new InvalidArgumentException('Invalid token type'); |
83
|
|
|
} |
84
|
|
|
|
85
|
9 |
|
return $tokenType; |
86
|
9 |
|
}, $tokenTypes); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|