1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Extractor\ChainExtractor; |
8
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Claims; |
9
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Claims\ClaimsCollector; |
10
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Claims\OrganizationClaims; |
11
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Claims\SecurityClaims; |
12
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Claims\TimestampClaims; |
13
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Lcobucci\Builder; |
14
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Lcobucci\Parser; |
15
|
|
|
use Damax\Bundle\ApiAuthBundle\Listener\ExceptionListener; |
16
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\Authenticator as ApiKeyAuthenticator; |
17
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\ApiKey\TokenUserProvider; |
18
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\Jwt\AuthenticationHandler; |
19
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\Jwt\Authenticator as JwtAuthenticator; |
20
|
|
|
use Lcobucci\Clock\SystemClock; |
21
|
|
|
use Lcobucci\JWT\Configuration as JwtConfiguration; |
22
|
|
|
use Lcobucci\JWT\Signer\Key; |
23
|
|
|
use Symfony\Component\Config\FileLocator; |
24
|
|
|
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
25
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
26
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
27
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
28
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
29
|
|
|
|
30
|
|
|
class DamaxApiAuthExtension extends ConfigurableExtension |
31
|
|
|
{ |
32
|
|
|
protected function loadInternal(array $config, ContainerBuilder $container) |
33
|
|
|
{ |
34
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
35
|
|
|
$loader->load('services.xml'); |
36
|
|
|
|
37
|
|
|
if ($config['api_key']['enabled']) { |
38
|
|
|
$this->configureApiKey($config['api_key'], $container); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($config['jwt']['enabled']) { |
42
|
|
|
$this->configureJwt($config['jwt'], $container); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($config['format_exceptions']) { |
46
|
|
|
$this->configureExceptions($container); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function configureApiKey(array $config, ContainerBuilder $container): self |
51
|
|
|
{ |
52
|
|
|
$extractors = $this->configureExtractors($config['extractors']); |
53
|
|
|
|
54
|
|
|
// User provider. |
55
|
|
|
$container |
56
|
|
|
->register('damax.api_auth.api_key.user_provider', TokenUserProvider::class) |
57
|
|
|
->addArgument($config['tokens']) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
// Authenticator. |
61
|
|
|
$container |
62
|
|
|
->register('damax.api_auth.api_key.authenticator', ApiKeyAuthenticator::class) |
63
|
|
|
->addArgument($extractors) |
64
|
|
|
; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function configureJwt(array $config, ContainerBuilder $container): self |
70
|
|
|
{ |
71
|
|
|
$signer = $this->configureJwtSigner($config['signer']); |
72
|
|
|
|
73
|
|
|
$clock = new Definition(SystemClock::class); |
74
|
|
|
|
75
|
|
|
$configuration = (new Definition(JwtConfiguration::class)) |
76
|
|
|
->setFactory(JwtConfiguration::class . '::forSymmetricSigner') |
77
|
|
|
->addArgument($signer) |
78
|
|
|
->addArgument(new Definition(Key::class, [ |
79
|
|
|
$config['signer']['signing_key'], |
80
|
|
|
$config['signer']['passphrase'], |
81
|
|
|
])) |
82
|
|
|
; |
83
|
|
|
|
84
|
|
|
if (Configuration::SIGNER_ASYMMETRIC === $config['signer']['type']) { |
85
|
|
|
$configuration |
86
|
|
|
->setFactory(JwtConfiguration::class . '::forAsymmetricSigner') |
87
|
|
|
->addArgument(new Definition(Key::class, [ |
88
|
|
|
$config['signer']['verification_key'], |
89
|
|
|
])) |
90
|
|
|
; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$parser = (new Definition(Parser::class)) |
94
|
|
|
->addArgument($configuration) |
95
|
|
|
->addArgument($clock) |
96
|
|
|
->addArgument($config['parser']['issuers'] ?? null) |
97
|
|
|
->addArgument($config['parser']['audience'] ?? null) |
98
|
|
|
; |
99
|
|
|
|
100
|
|
|
$claims = $this->configureJwtClaims($config['builder'], $clock, $container); |
101
|
|
|
|
102
|
|
|
$builder = (new Definition(Builder::class)) |
103
|
|
|
->addArgument($configuration) |
104
|
|
|
->addArgument($claims) |
105
|
|
|
; |
106
|
|
|
|
107
|
|
|
$extractors = $this->configureExtractors($config['extractors']); |
108
|
|
|
|
109
|
|
|
// Authenticator. |
110
|
|
|
$container |
111
|
|
|
->register('damax.api_auth.jwt.authenticator', JwtAuthenticator::class) |
112
|
|
|
->addArgument($extractors) |
113
|
|
|
->addArgument($parser) |
114
|
|
|
->addArgument($config['identity_claim'] ?? null) |
115
|
|
|
; |
116
|
|
|
|
117
|
|
|
// Handler. |
118
|
|
|
$container |
119
|
|
|
->register('damax.api_auth.jwt.handler', AuthenticationHandler::class) |
120
|
|
|
->addArgument($builder) |
121
|
|
|
; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function configureExceptions(ContainerBuilder $container): self |
127
|
|
|
{ |
128
|
|
|
$container |
129
|
|
|
->getDefinition(ExceptionListener::class) |
130
|
|
|
->addTag('kernel.event_listener', ['event' => 'kernel.exception', 'method' => 'onKernelException']) |
131
|
|
|
; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function configureJwtClaims(array $config, Definition $clock, ContainerBuilder $container): Definition |
137
|
|
|
{ |
138
|
|
|
// Default claims. |
139
|
|
|
$container |
140
|
|
|
->register(TimestampClaims::class) |
141
|
|
|
->addArgument($clock) |
142
|
|
|
->addArgument($config['ttl']) |
143
|
|
|
->addTag('damax.api_auth.jwt_claims') |
144
|
|
|
; |
145
|
|
|
$container |
146
|
|
|
->register(OrganizationClaims::class) |
147
|
|
|
->addArgument($config['issuer'] ?? null) |
148
|
|
|
->addArgument($config['audience'] ?? null) |
149
|
|
|
->addTag('damax.api_auth.jwt_claims') |
150
|
|
|
; |
151
|
|
|
$container |
152
|
|
|
->register(SecurityClaims::class) |
153
|
|
|
->addTag('damax.api_auth.jwt_claims') |
154
|
|
|
; |
155
|
|
|
|
156
|
|
|
$container->setAlias(Claims::class, ClaimsCollector::class); |
157
|
|
|
|
158
|
|
|
return $container |
159
|
|
|
->register(ClaimsCollector::class) |
160
|
|
|
->addArgument(new TaggedIteratorArgument('damax.api_auth.jwt_claims')) |
161
|
|
|
; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function configureJwtSigner(array $config): Definition |
165
|
|
|
{ |
166
|
|
|
$dirs = ['HS' => 'Hmac', 'RS' => 'Rsa', 'ES' => 'Ecdsa']; |
167
|
|
|
$algo = $config['algorithm']; |
168
|
|
|
|
169
|
|
|
return new Definition('Lcobucci\\JWT\\Signer\\' . $dirs[substr($algo, 0, 2)] . '\\Sha' . substr($algo, 2)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
private function configureExtractors(array $config): Definition |
173
|
|
|
{ |
174
|
|
|
$extractors = []; |
175
|
|
|
|
176
|
|
|
foreach ($config as $item) { |
177
|
|
|
$className = sprintf('Damax\\Bundle\\ApiAuthBundle\\Extractor\\%sExtractor', ucfirst($item['type'])); |
178
|
|
|
|
179
|
|
|
$extractors[] = (new Definition($className)) |
180
|
|
|
->setArgument(0, $item['name']) |
181
|
|
|
->setArgument(1, $item['prefix'] ?? null) |
182
|
|
|
; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return new Definition(ChainExtractor::class, [$extractors]); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|