1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TMV\JWTModule\DIFactory\KeyManagement; |
6
|
|
|
|
7
|
|
|
use Interop\Container\ContainerInterface; |
8
|
|
|
use Interop\Container\Exception\ContainerException; |
9
|
|
|
use Jose\Component\Core\JWK; |
10
|
|
|
use Jose\Component\Core\JWKSet; |
11
|
|
|
use Jose\Component\KeyManagement\JWKFactory; |
12
|
|
|
use TMV\JWTModule\DIFactory\AbstractServiceFactory; |
13
|
|
|
use TMV\JWTModule\Exception\InvalidArgumentException; |
14
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
15
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
16
|
|
|
|
17
|
|
|
class KeyAbstractFactory extends AbstractServiceFactory |
18
|
|
|
{ |
19
|
|
|
private const SERVICE_TYPE_KEY = 'keys'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create an object |
23
|
|
|
* |
24
|
|
|
* @param ContainerInterface $container |
25
|
|
|
* @param string $requestedName |
26
|
|
|
* @param null|array $options |
27
|
|
|
* |
28
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service |
29
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
30
|
|
|
* creating a service |
31
|
|
|
* @throws ContainerException if any other error occurs |
32
|
|
|
* |
33
|
|
|
* @return JWK |
34
|
|
|
*/ |
35
|
4 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): JWK |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
4 |
|
$keyConfig = $this->getServiceConfig($container, $requestedName); |
39
|
|
|
} catch (InvalidArgumentException $e) { |
40
|
|
|
throw new ServiceNotCreatedException('Unable to find service for ' . $requestedName); |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
$type = $keyConfig['type'] ?? null; |
44
|
4 |
|
$keyOptions = $keyConfig['options'] ?? []; |
45
|
|
|
|
46
|
4 |
|
switch ($type) { |
47
|
4 |
|
case 'secret': |
48
|
1 |
|
return JWKFactory::createFromSecret( |
49
|
1 |
|
$keyOptions['secret'] ?? '', |
50
|
1 |
|
$keyOptions['additional_values'] ?? [] |
51
|
|
|
); |
52
|
|
|
|
53
|
3 |
|
case 'jwk': |
54
|
2 |
|
$jwk = JWKFactory::createFromJsonObject($keyOptions['value'] ?? ''); |
55
|
|
|
|
56
|
2 |
|
if (! $jwk instanceof JWK) { |
57
|
|
|
throw new ServiceNotCreatedException('Invalid value key for service ' . $requestedName); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
return $jwk; |
61
|
|
|
|
62
|
1 |
|
case 'certificate': |
63
|
|
|
return JWKFactory::createFromCertificateFile( |
64
|
|
|
$keyOptions['path'] ?? '', |
65
|
|
|
$keyOptions['additional_values'] ?? [] |
66
|
|
|
); |
67
|
|
|
|
68
|
1 |
|
case 'x5c': |
69
|
|
|
return JWKFactory::createFromCertificate( |
70
|
|
|
$keyOptions['value'] ?? '', |
71
|
|
|
$keyOptions['additional_values'] ?? [] |
72
|
|
|
); |
73
|
|
|
|
74
|
1 |
|
case 'file': |
75
|
|
|
return JWKFactory::createFromKeyFile( |
76
|
|
|
$keyOptions['path'] ?? '', |
77
|
|
|
$keyOptions['password'] ?? null, |
78
|
|
|
$keyOptions['additional_values'] ?? [] |
79
|
|
|
); |
80
|
|
|
|
81
|
1 |
|
case 'jwkset': |
82
|
1 |
|
$keySetService = $keyOptions['key_set'] ?? null; |
83
|
1 |
|
$keySet = $container->get($keySetService); |
84
|
|
|
|
85
|
1 |
|
if (! $keySet instanceof JWKSet) { |
86
|
|
|
throw new ServiceNotCreatedException('Unable to get keyset ' . $keySetService); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return JWKFactory::createFromKeySet( |
90
|
1 |
|
$keySet, |
91
|
1 |
|
(int) ($keyOptions['index'] ?? 0) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
throw new ServiceNotCreatedException('Invalid jwk type ' . $type); |
96
|
|
|
} |
97
|
|
|
|
98
|
15 |
|
protected function getServiceTypeName(): string |
99
|
|
|
{ |
100
|
15 |
|
return static::SERVICE_TYPE_KEY; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|