1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine; |
5
|
|
|
|
6
|
|
|
use Kdyby\Doctrine\DI\IEntityProvider; |
7
|
|
|
use Kdyby\Events\DI\EventsExtension; |
8
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
9
|
|
|
use League\OAuth2\Server\CryptKey; |
10
|
|
|
use League\OAuth2\Server\Grant\AuthCodeGrant; |
11
|
|
|
use League\OAuth2\Server\Grant\ClientCredentialsGrant; |
12
|
|
|
use League\OAuth2\Server\Grant\ImplicitGrant; |
13
|
|
|
use League\OAuth2\Server\Grant\PasswordGrant; |
14
|
|
|
use League\OAuth2\Server\Grant\RefreshTokenGrant; |
15
|
|
|
use League\OAuth2\Server\ResourceServer; |
16
|
|
|
use Lookyman\NetteOAuth2Server\RedirectConfig; |
17
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\AccessToken\AccessTokenRepository; |
18
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\AuthCode\AuthCodeRepository; |
19
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\Client\ClientRepository; |
20
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\RefreshToken\RefreshTokenRepository; |
21
|
|
|
use Lookyman\NetteOAuth2Server\Storage\Doctrine\Scope\ScopeRepository; |
22
|
|
|
use Lookyman\NetteOAuth2Server\Storage\IAuthorizationRequestSerializer; |
23
|
|
|
use Lookyman\NetteOAuth2Server\UI\ApproveControlFactory; |
24
|
|
|
use Lookyman\NetteOAuth2Server\UI\OAuth2Presenter; |
25
|
|
|
use Lookyman\NetteOAuth2Server\User\LoginSubscriber; |
26
|
|
|
use Lookyman\NetteOAuth2Server\User\UserRepository; |
27
|
|
|
use Nette\Application\IPresenterFactory; |
28
|
|
|
use Nette\DI\CompilerExtension; |
29
|
|
|
use Nette\DI\Statement; |
30
|
|
|
use Nette\Utils\Validators; |
31
|
|
|
|
32
|
|
|
class NetteOAuth2ServerDoctrineExtension extends CompilerExtension implements IEntityProvider |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $defaults = [ |
38
|
|
|
'grants' => [ |
39
|
|
|
'authCode' => false, |
40
|
|
|
'clientCredentials' => false, |
41
|
|
|
'implicit' => false, |
42
|
|
|
'password' => false, |
43
|
|
|
'refreshToken' => false, |
44
|
|
|
], |
45
|
|
|
'privateKey' => null, |
46
|
|
|
'publicKey' => null, |
47
|
|
|
'approveDestination' => null, |
48
|
|
|
'loginDestination' => null, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
public function loadConfiguration() |
52
|
|
|
{ |
53
|
|
|
$builder = $this->getContainerBuilder(); |
54
|
|
|
$config = $this->validateConfig($this->defaults); |
55
|
|
|
|
56
|
|
|
// Table mapping & Login redirection |
57
|
|
|
$builder->addDefinition($this->prefix('tablePrefixSubscriber')) |
58
|
|
|
->setClass(TablePrefixSubscriber::class) |
59
|
|
|
->addTag(EventsExtension::TAG_SUBSCRIBER); |
60
|
|
|
$builder->addDefinition($this->prefix('loginSubscriber')) |
61
|
|
|
->setClass(LoginSubscriber::class) |
62
|
|
|
->addTag(EventsExtension::TAG_SUBSCRIBER); |
63
|
|
|
|
64
|
|
|
// Common repositories |
65
|
|
|
$builder->addDefinition($this->prefix('repository.accessToken')) |
66
|
|
|
->setClass(AccessTokenRepository::class); |
67
|
|
|
$builder->addDefinition($this->prefix('repository.authCode')) |
68
|
|
|
->setClass(AuthCodeRepository::class); |
69
|
|
|
$builder->addDefinition($this->prefix('repository.client')) |
70
|
|
|
->setClass(ClientRepository::class); |
71
|
|
|
$builder->addDefinition($this->prefix('repository.refreshToken')) |
72
|
|
|
->setClass(RefreshTokenRepository::class); |
73
|
|
|
$builder->addDefinition($this->prefix('repository.scope')) |
74
|
|
|
->setClass(ScopeRepository::class); |
75
|
|
|
$builder->addDefinition($this->prefix('repository.user')) |
76
|
|
|
->setClass(UserRepository::class); |
77
|
|
|
|
78
|
|
|
// Encryption keys |
79
|
|
|
Validators::assertField($config, 'publicKey', 'string'); |
80
|
|
|
Validators::assertField($config, 'privateKey', 'string|array'); |
81
|
|
|
if (is_array($config['privateKey'])) { |
82
|
|
|
Validators::assertField($config['privateKey'], 'keyPath', 'string'); |
83
|
|
|
Validators::assertField($config['privateKey'], 'passPhrase', 'string'); |
84
|
|
|
$privateKey = new Statement(CryptKey::class, [$config['privateKey']['keyPath'], $config['privateKey']['passPhrase']]); |
85
|
|
|
|
86
|
|
|
} else { |
87
|
|
|
$privateKey = $config['privateKey']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Authorization & resource server |
91
|
|
|
$authorizationServer = $builder->addDefinition($this->prefix('authorizationServer')) |
92
|
|
|
->setClass(AuthorizationServer::class, [ |
93
|
|
|
'privateKey' => $privateKey, |
94
|
|
|
'publicKey' => $config['publicKey'], |
95
|
|
|
]); |
96
|
|
|
$builder->addDefinition($this->prefix('resourceServer')) |
97
|
|
|
->setClass(ResourceServer::class, [ |
98
|
|
|
'publicKey' => $config['publicKey'], |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
// Grants |
102
|
|
|
Validators::assertField($config, 'grants', 'array'); |
103
|
|
|
foreach ($config['grants'] as $grant => $options) { |
104
|
|
|
Validators::assert($options, 'boolean|array'); |
105
|
|
|
if ($options === false) { |
106
|
|
|
continue; |
107
|
|
|
|
108
|
|
|
} else { |
109
|
|
|
$options = (array) $options; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$definition = $builder->addDefinition($this->prefix('grant.' . $grant)); |
113
|
|
|
|
114
|
|
|
switch ($grant) { |
115
|
|
|
case 'authCode': |
116
|
|
|
if (!array_key_exists('authCodeTtl', $options)) { |
117
|
|
|
$options['authCodeTtl'] = 'PT10M'; |
118
|
|
|
} |
119
|
|
|
$definition->setClass(AuthCodeGrant::class, ['authCodeTTL' => $this->createDateIntervalStatement($options['authCodeTtl'])]); |
120
|
|
|
break; |
121
|
|
|
case 'clientCredentials': |
122
|
|
|
$definition->setClass(ClientCredentialsGrant::class); |
123
|
|
|
break; |
124
|
|
|
case 'implicit': |
125
|
|
|
if (!array_key_exists('accessTokenTtl', $options)) { |
126
|
|
|
$options['accessTokenTtl'] = 'PT10M'; |
127
|
|
|
} |
128
|
|
|
$definition->setClass(ImplicitGrant::class, ['accessTokenTTL' => $this->createDateIntervalStatement($options['accessTokenTtl'])]); |
129
|
|
|
break; |
130
|
|
|
case 'password': |
131
|
|
|
$definition->setClass(PasswordGrant::class); |
132
|
|
|
break; |
133
|
|
|
case 'refreshToken': |
134
|
|
|
$definition->setClass(RefreshTokenGrant::class); |
135
|
|
|
break; |
136
|
|
|
default: |
137
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown grant %s', $grant)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$args = [$this->prefix('@grant.' . $grant)]; |
141
|
|
|
if (array_key_exists('ttl', $options)) { |
142
|
|
|
$args[] = $this->createDateIntervalStatement($options['ttl']); |
143
|
|
|
} |
144
|
|
|
$authorizationServer->addSetup('enableGrantType', $args); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Presenter, Control factory, Serializer |
148
|
|
|
$builder->addDefinition($this->prefix('presenter')) |
149
|
|
|
->setClass(OAuth2Presenter::class); |
150
|
|
|
$builder->addDefinition($this->prefix('approveControlFactory')) |
151
|
|
|
->setClass(ApproveControlFactory::class); |
152
|
|
|
$builder->addDefinition($this->prefix('serializer')) |
153
|
|
|
->setClass(IAuthorizationRequestSerializer::class) |
154
|
|
|
->setFactory(AuthorizationRequestSerializer::class); |
155
|
|
|
|
156
|
|
|
// Redirect config |
157
|
|
|
Validators::assertField($config, 'approveDestination', 'string|null'); |
158
|
|
|
Validators::assertField($config, 'loginDestination', 'string|null'); |
159
|
|
|
$builder->addDefinition($this->prefix('redirectConfig')) |
160
|
|
|
->setClass(RedirectConfig::class, [ |
161
|
|
|
'approveDestination' => $config['approveDestination'], |
162
|
|
|
'loginDestination' => $config['loginDestination'], |
163
|
|
|
]); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function beforeCompile() |
167
|
|
|
{ |
168
|
|
|
$builder = $this->getContainerBuilder(); |
169
|
|
|
|
170
|
|
|
// Mapping |
171
|
|
|
$presenterFactory = $builder->getDefinition($builder->getByType(IPresenterFactory::class)); |
172
|
|
|
$presenterFactory->addSetup('if (!? instanceof \Nette\Application\PresenterFactory) { throw new \RuntimeException(\'Cannot set OAuth2Server mapping\'); } else { ?->setMapping(?); }', [ |
173
|
|
|
'@self', '@self', ['NetteOAuth2Server' => 'Lookyman\NetteOAuth2Server\UI\*Presenter'] |
174
|
|
|
]); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
public function getEntityMappings() |
181
|
|
|
{ |
182
|
|
|
return ['Lookyman\NetteOAuth2Server\Storage\Doctrine' => __DIR__]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $interval |
187
|
|
|
* @return Statement |
188
|
|
|
* @throws \Exception |
189
|
|
|
*/ |
190
|
|
|
private function createDateIntervalStatement(string $interval): Statement |
191
|
|
|
{ |
192
|
|
|
new \DateInterval($interval); // throw early |
193
|
|
|
return new Statement(\DateInterval::class, [$interval]); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|