|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Soluble\Wallit\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Soluble\Wallit\Config\ConfigProvider; |
|
9
|
|
|
use Soluble\Wallit\Exception\ConfigException; |
|
10
|
|
|
use Soluble\Wallit\Service\JwtService; |
|
11
|
|
|
|
|
12
|
|
|
class JwtAuthMiddlewareFactory |
|
13
|
|
|
{ |
|
14
|
|
|
public const CONFIG_KEY = 'token_auth_middleware'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param ContainerInterface $container |
|
18
|
|
|
* |
|
19
|
|
|
* @return JwtAuthMiddleware |
|
20
|
|
|
*/ |
|
21
|
3 |
|
public function __invoke(ContainerInterface $container): JwtAuthMiddleware |
|
22
|
|
|
{ |
|
23
|
3 |
|
$config = $container->has('config') ? $container->get('config') : []; |
|
24
|
3 |
|
$options = $config[ConfigProvider::CONFIG_PREFIX][self::CONFIG_KEY] ?? false; |
|
25
|
|
|
|
|
26
|
3 |
|
if (!is_array($options)) { |
|
27
|
1 |
|
throw new ConfigException( |
|
28
|
1 |
|
sprintf( |
|
29
|
1 |
|
"Missing or invalid entry ['%s']['%s'] in container configuration.", |
|
30
|
1 |
|
ConfigProvider::CONFIG_PREFIX, |
|
31
|
1 |
|
self::CONFIG_KEY |
|
32
|
|
|
) |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
2 |
|
if (!isset($options['token_providers']) || !is_array($options['token_providers'])) { |
|
36
|
1 |
|
throw new ConfigException( |
|
37
|
1 |
|
sprintf( |
|
38
|
1 |
|
"Missing or invalid entry ['%s']['%s']['%s'] in container configuration.", |
|
39
|
1 |
|
ConfigProvider::CONFIG_PREFIX, |
|
40
|
1 |
|
self::CONFIG_KEY, |
|
41
|
1 |
|
'token_providers' |
|
42
|
|
|
) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
if (!$container->has(JwtService::class)) { |
|
47
|
1 |
|
throw new ConfigException( |
|
48
|
1 |
|
sprintf( |
|
49
|
1 |
|
"Cannot locate required '%s' from container, was it provided ?", |
|
50
|
1 |
|
JwtService::class |
|
51
|
|
|
) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return new JwtAuthMiddleware( |
|
56
|
|
|
$options['token_providers'], |
|
57
|
|
|
$container->get(JwtService::class), |
|
58
|
|
|
array_intersect_key($options, JwtAuthMiddleware::DEFAULT_OPTIONS) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|