1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TH\OAuth2\Pimple; |
4
|
|
|
|
5
|
|
|
use OAuth2\Server; |
6
|
|
|
use OAuth2\Storage; |
7
|
|
|
use Pimple\Container; |
8
|
|
|
use Pimple\ServiceProviderInterface; |
9
|
|
|
use Silex\Api\ControllerProviderInterface; |
10
|
|
|
use Silex\Application; |
11
|
|
|
use TH\OAuth2\Controllers; |
12
|
|
|
use TH\OAuth2\HTMLAuthorizeRenderer; |
13
|
|
|
use TH\OAuth2\OAuth2AuthenticationListener; |
14
|
|
|
use TH\OAuth2\OAuth2AuthentificationProvider; |
15
|
|
|
use TH\OAuth2\OAuth2EntryPoint; |
16
|
|
|
|
17
|
|
|
class OAuth2ServerProvider implements ServiceProviderInterface, ControllerProviderInterface |
18
|
|
|
{ |
19
|
|
|
private $storagesTypes = [ |
20
|
|
|
'access_token' => Storage\AccessTokenInterface::class, |
21
|
|
|
'authorization_code' => Storage\AuthorizationCodeInterface::class, |
22
|
|
|
'client_credentials' => Storage\ClientCredentialsInterface::class, |
23
|
|
|
'client' => Storage\ClientInterface::class, |
24
|
|
|
'refresh_token' => Storage\RefreshTokenInterface::class, |
25
|
|
|
'user_credentials' => Storage\UserCredentialsInterface::class, |
26
|
|
|
'user_claims' => Storage\UserClaimsInterface::class, |
27
|
|
|
'public_key' => Storage\PublicKeyInterface::class, |
28
|
|
|
'jwt_bearer' => Storage\JwtBearerInterface::class, |
29
|
|
|
'scope' => Storage\ScopeInterface::class, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inherit |
34
|
|
|
*/ |
35
|
|
|
public function register(Container $container) |
36
|
|
|
{ |
37
|
|
|
$container['security.authentication_listener.factory.oauth2'] = $container->protect( |
38
|
|
|
$this->factory($container) |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$container['oauth2_server'] = $this->OAuth2Server($container); |
42
|
|
|
|
43
|
|
|
$this->setupControllers($container); |
44
|
|
|
|
45
|
|
|
$container['oauth2_server.authorize_renderer.view'] = __DIR__ . '/../../views/authorize.php'; |
46
|
|
|
|
47
|
|
|
$container['oauth2_server.authorize_renderer'] = function (Container $container) { |
48
|
|
|
return new HTMLAuthorizeRenderer($container['oauth2_server.authorize_renderer.view']); |
49
|
|
|
}; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function factory(Container $container) |
53
|
|
|
{ |
54
|
|
|
return function ($name) use ($container) { |
55
|
|
|
$this->registerFactoryDeps($container, $name); |
56
|
|
|
|
57
|
|
|
return [ |
58
|
|
|
'security.authentication_provider.'.$name.'.dao', |
59
|
|
|
'security.authentication_listener.'.$name.'.oauth2', |
60
|
|
|
'security.entry_point.'.$name.'.oauth2', |
61
|
|
|
'pre_auth' |
62
|
|
|
]; |
63
|
|
|
}; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function registerFactoryDeps(Container $container, $name) |
67
|
|
|
{ |
68
|
|
|
if (!isset($container['security.entry_point.'.$name.'.oauth2.realm'])) { |
69
|
|
|
$container['security.entry_point.'.$name.'.oauth2'] = 'AppName'; |
70
|
|
|
} |
71
|
|
|
if (!isset($container['security.entry_point.'.$name.'.oauth2'])) { |
72
|
|
|
$realm = $container['security.entry_point.'.$name.'.oauth2.realm']; |
73
|
|
|
$container['security.entry_point.'.$name.'.oauth2'] = new OAuth2EntryPoint($realm); |
74
|
|
|
} |
75
|
|
|
$this->registerAuthenticationListener($container, $name); |
76
|
|
|
if (!isset($container['security.authentication_provider.'.$name.'.dao'])) { |
77
|
|
|
$container['security.authentication_provider.'.$name.'.dao'] = function () use ($container, $name) { |
78
|
|
|
return new OAuth2AuthentificationProvider( |
79
|
|
|
$container['security.user_provider.'.$name], |
80
|
|
|
$container['security.user_checker'], |
81
|
|
|
$name |
82
|
|
|
); |
83
|
|
|
}; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function registerAuthenticationListener(Container $container, $name) |
88
|
|
|
{ |
89
|
|
|
if (!isset($container['security.authentication_listener.'.$name.'.oauth2'])) { |
90
|
|
|
$authListener = function () use ($container, $name) { |
91
|
|
|
return new OAuth2AuthenticationListener( |
92
|
|
|
$container['oauth2_server'], |
93
|
|
|
$container['security.token_storage'], |
94
|
|
|
$container['security.authentication_manager'], |
95
|
|
|
$name, |
96
|
|
|
$container['security.entry_point.'.$name.'.oauth2'], |
97
|
|
|
$container['logger'] |
98
|
|
|
); |
99
|
|
|
}; |
100
|
|
|
$container['security.authentication_listener.'.$name.'.oauth2'] = $authListener; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function OAuth2Server(Container $container) |
105
|
|
|
{ |
106
|
|
|
$container['oauth2_server.parameters'] = []; |
107
|
|
|
|
108
|
|
|
$container['oauth2_server.storage.default'] = function (Container $container) { |
109
|
|
|
return new Storage\Pdo($container['oauth2_server.storage.pdo_connection']); |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
$container['oauth2_server.storage.types'] = ['client', 'access_token']; |
113
|
|
|
|
114
|
|
|
$container['oauth2_server.storage'] = function (Container $container) { |
115
|
|
|
$storages = []; |
116
|
|
|
foreach ($container['oauth2_server.storage.types'] as $storageType) { |
117
|
|
|
$storages[$storageType] = $container['oauth2_server.storage.'.$storageType]; |
118
|
|
|
} |
119
|
|
|
return $storages; |
120
|
|
|
}; |
121
|
|
|
|
122
|
|
|
foreach ($this->storagesTypes as $storageType => $storageInterface) { |
123
|
|
|
$container['oauth2_server.storage.'.$storageType] = function (Container $container) use ($storageInterface) { |
124
|
|
|
if ($container['oauth2_server.storage.default'] instanceof $storageInterface) { |
125
|
|
|
return $container['oauth2_server.storage.default']; |
126
|
|
|
} |
127
|
|
|
}; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$container['oauth2_server.config'] = function () { |
131
|
|
|
return ['allow_implicit' => true, 'enforce_state' => false,]; |
132
|
|
|
}; |
133
|
|
|
|
134
|
|
|
$container['oauth2_server.grant_types'] = function () { |
135
|
|
|
return []; |
136
|
|
|
}; |
137
|
|
|
|
138
|
|
|
$container['oauth2_server.response_types'] = function () { |
139
|
|
|
return []; |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
$container['oauth2_server.token_type'] = function () { |
143
|
|
|
return null; |
144
|
|
|
}; |
145
|
|
|
|
146
|
|
|
$container['oauth2_server.scope_util'] = function () { |
147
|
|
|
return null; |
148
|
|
|
}; |
149
|
|
|
|
150
|
|
|
$container['oauth2_server.client_assertion_type'] = function () { |
151
|
|
|
return null; |
152
|
|
|
}; |
153
|
|
|
|
154
|
|
|
return function (Container $container) { |
155
|
|
|
return new Server( |
156
|
|
|
$container['oauth2_server.storage'], |
157
|
|
|
$container['oauth2_server.config'], |
158
|
|
|
$container['oauth2_server.grant_types'], |
159
|
|
|
$container['oauth2_server.response_types'], |
160
|
|
|
$container['oauth2_server.token_type'], |
161
|
|
|
$container['oauth2_server.scope_util'], |
162
|
|
|
$container['oauth2_server.client_assertion_type'] |
163
|
|
|
); |
164
|
|
|
}; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
private function setupControllers(Container $container) { |
168
|
|
|
$container['oauth2_server.controllers_as_service'] = false; |
169
|
|
|
$container['oauth2_server.controllers.authorize'] = function (Container $container) { |
170
|
|
|
return new Controllers\AuthorizeHandler( |
171
|
|
|
$container['oauth2_server']->getAuthorizeController(), |
172
|
|
|
$container['oauth2_server.authorize_renderer'] |
173
|
|
|
); |
174
|
|
|
}; |
175
|
|
|
$container['oauth2_server.controllers.authorize_validator'] = function (Container $container) { |
176
|
|
|
return new Controllers\AuthorizeValidator( |
177
|
|
|
$container['url_generator'], |
178
|
|
|
$container['oauth2_server']->getAuthorizeController(), |
179
|
|
|
$container['oauth2_server.authorize_renderer'] |
180
|
|
|
); |
181
|
|
|
}; |
182
|
|
|
$container['oauth2_server.controllers.authorize_handler'] = function (Container $container) { |
183
|
|
|
return new Controllers\AuthorizeHandler( |
184
|
|
|
$container['oauth2_server']->getAuthorizeController(), |
185
|
|
|
$container['oauth2_server.authorize_renderer'] |
186
|
|
|
); |
187
|
|
|
}; |
188
|
|
|
$container['oauth2_server.controllers.token'] = function (Container $container) { |
189
|
|
|
return new Controllers\TokenHandler($container['oauth2_server']->getTokenController()); |
190
|
|
|
}; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inherit |
195
|
|
|
*/ |
196
|
|
|
public function connect(Application $app) |
197
|
|
|
{ |
198
|
|
|
$controllers = $app['controllers_factory']; |
199
|
|
|
|
200
|
|
|
if ($app['oauth2_server.controllers_as_service']) { |
201
|
|
|
$controllers->post('/authorize', 'oauth2_server.controllers.authorize_handler:__invoke') |
202
|
|
|
->bind('oauth2_authorize_handler'); |
203
|
|
|
$controllers->get('/authorize', 'oauth2_server.controllers.authorize_validator:__invoke') |
204
|
|
|
->bind('oauth2_authorize_validator'); |
205
|
|
|
$controllers->post('/token', 'oauth2_server.controllers.token:__invoke')->bind('oauth2_token_handler'); |
206
|
|
|
} else { |
207
|
|
|
$controllers->post('/authorize', $app['oauth2_server.controllers.authorize_handler']) |
208
|
|
|
->bind('oauth2_authorize_handler'); |
209
|
|
|
$controllers->get('/authorize', $app['oauth2_server.controllers.authorize_validator']) |
210
|
|
|
->bind('oauth2_authorize_validator'); |
211
|
|
|
$controllers->post('/token', $app['oauth2_server.controllers.token'])->bind('oauth2_token_handler'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $controllers; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|