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\JWKSet; |
10
|
|
|
use Jose\Component\KeyManagement\JKUFactory; |
11
|
|
|
use Jose\Component\KeyManagement\X5UFactory; |
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 KeySetAbstractFactory extends AbstractServiceFactory |
18
|
|
|
{ |
19
|
|
|
private const SERVICE_TYPE_KEY = 'key_sets'; |
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 JWKSet |
34
|
|
|
*/ |
35
|
4 |
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): JWKSet |
36
|
|
|
{ |
37
|
|
|
try { |
38
|
4 |
|
$keyConfig = $this->getServiceConfig($container, $requestedName); |
39
|
|
|
} catch (InvalidArgumentException $e) { |
40
|
|
|
throw new ServiceNotCreatedException('Unable to find service for ' . $requestedName, 0, $e); |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
$type = $keyConfig['type'] ?? null; |
44
|
4 |
|
$keyOptions = $keyConfig['options'] ?? []; |
45
|
|
|
|
46
|
4 |
|
switch ($type) { |
47
|
4 |
|
case 'jwkset': |
48
|
2 |
|
return JWKSet::createFromJson($keyOptions['value'] ?? '{}'); |
49
|
2 |
|
case 'jku': |
50
|
1 |
|
return $container->get(JKUFactory::class)->loadFromUrl( |
51
|
1 |
|
$keyOptions['url'] ?? '', |
52
|
1 |
|
$keyOptions['headers'] ?? [] |
53
|
|
|
); |
54
|
1 |
|
case 'x5u': |
55
|
1 |
|
return $container->get(X5UFactory::class)->loadFromUrl( |
56
|
1 |
|
$keyOptions['url'] ?? '', |
57
|
1 |
|
$keyOptions['headers'] ?? [] |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new ServiceNotCreatedException('Invalid jwk set type ' . $type); |
62
|
|
|
} |
63
|
|
|
|
64
|
14 |
|
protected function getServiceTypeName(): string |
65
|
|
|
{ |
66
|
14 |
|
return static::SERVICE_TYPE_KEY; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|