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