|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Soluble\Wallit\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Soluble\Wallit\Config\ConfigProvider; |
|
9
|
|
|
use Soluble\Wallit\Exception\ConfigException; |
|
10
|
|
|
use Soluble\Wallit\Token\Jwt\SignatureAlgos; |
|
11
|
|
|
|
|
12
|
|
|
class JwtServiceFactory |
|
13
|
|
|
{ |
|
14
|
|
|
public const CONFIG_KEY = 'token_service'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Map signature algorithms. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
public static $algosMap = [ |
|
22
|
|
|
SignatureAlgos::HS256 => \Lcobucci\JWT\Signer\Hmac\Sha256::class, |
|
23
|
|
|
SignatureAlgos::HS384 => \Lcobucci\JWT\Signer\Hmac\Sha384::class, |
|
24
|
|
|
SignatureAlgos::HS512 => \Lcobucci\JWT\Signer\Hmac\Sha512::class |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param ContainerInterface $container |
|
29
|
|
|
* |
|
30
|
|
|
* @return JwtService |
|
31
|
|
|
* |
|
32
|
|
|
* @throws ConfigException |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function __invoke(ContainerInterface $container): JwtService |
|
35
|
|
|
{ |
|
36
|
7 |
|
$config = $container->has('config') ? $container->get('config') : []; |
|
37
|
|
|
|
|
38
|
7 |
|
$options = $config[ConfigProvider::CONFIG_PREFIX][self::CONFIG_KEY] ?? null; |
|
39
|
|
|
|
|
40
|
7 |
|
if (!is_array($options)) { |
|
41
|
1 |
|
throw new ConfigException( |
|
42
|
1 |
|
sprintf( |
|
43
|
1 |
|
"Missing or invalid entry ['%s']['%s'] in container configuration.", |
|
44
|
1 |
|
ConfigProvider::CONFIG_PREFIX, |
|
45
|
1 |
|
self::CONFIG_KEY |
|
46
|
|
|
) |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
6 |
|
$verificationKey = $options['secret'] ?? null; |
|
51
|
6 |
|
if ($verificationKey === null) { |
|
52
|
1 |
|
throw new ConfigException(sprintf( |
|
53
|
1 |
|
"Missing secret key in config (['%s']['%s']['%s'] = '%s')", |
|
54
|
1 |
|
ConfigProvider::CONFIG_PREFIX, |
|
55
|
1 |
|
self::CONFIG_KEY, |
|
56
|
1 |
|
'secret', |
|
57
|
1 |
|
$verificationKey ?? 'NULL' |
|
58
|
|
|
)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
$algo = $options['algo'] ?? null; |
|
62
|
5 |
|
$algoClass = self::$algosMap[$algo] ?? null; |
|
63
|
|
|
|
|
64
|
5 |
|
if ($algoClass === null || !class_exists($algoClass)) { |
|
65
|
2 |
|
throw new ConfigException(sprintf( |
|
66
|
2 |
|
"Missing or invalid algorithm in config (['%s']['%s']['%s'] = '%s')", |
|
67
|
2 |
|
ConfigProvider::CONFIG_PREFIX, |
|
68
|
2 |
|
self::CONFIG_KEY, |
|
69
|
2 |
|
'algo', |
|
70
|
2 |
|
$algo ?? 'NULL' |
|
71
|
|
|
)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
$jwtService = new JwtService(new $algoClass(), $verificationKey); |
|
75
|
|
|
|
|
76
|
2 |
|
return $jwtService; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|