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