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
|
|
|
$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 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) { |
123
|
|
|
$container['oauth2_server.storage.'.$storageType] = function (Container $container) { |
124
|
|
|
return $container['oauth2_server.storage.default']; |
125
|
|
|
}; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$container['oauth2_server.config'] = function () { |
129
|
|
|
return ['allow_implicit' => true, 'enforce_state' => false,]; |
130
|
|
|
}; |
131
|
|
|
|
132
|
|
|
$container['oauth2_server.grant_types'] = function () { |
133
|
|
|
return []; |
134
|
|
|
}; |
135
|
|
|
|
136
|
|
|
$container['oauth2_server.response_types'] = function () { |
137
|
|
|
return []; |
138
|
|
|
}; |
139
|
|
|
|
140
|
|
|
$container['oauth2_server.token_type'] = function () { |
141
|
|
|
return null; |
142
|
|
|
}; |
143
|
|
|
|
144
|
|
|
$container['oauth2_server.scope_util'] = function () { |
145
|
|
|
return null; |
146
|
|
|
}; |
147
|
|
|
|
148
|
|
|
$container['oauth2_server.client_assertion_type'] = function () { |
149
|
|
|
return null; |
150
|
|
|
}; |
151
|
|
|
|
152
|
|
|
return function (Container $container) { |
153
|
|
|
return new Server( |
154
|
|
|
$container['oauth2_server.storage'], |
155
|
|
|
$container['oauth2_server.config'], |
156
|
|
|
$container['oauth2_server.grant_types'], |
157
|
|
|
$container['oauth2_server.response_types'], |
158
|
|
|
$container['oauth2_server.token_type'], |
159
|
|
|
$container['oauth2_server.scope_util'], |
160
|
|
|
$container['oauth2_server.client_assertion_type'] |
161
|
|
|
); |
162
|
|
|
}; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function setupControllers(Container $container) { |
166
|
|
|
$container['oauth2_server.controllers_as_service'] = false; |
167
|
|
|
|
168
|
|
|
$container['oauth2_server.controllers.authorize'] = function (Container $container) { |
169
|
|
|
return new Controllers\AuthorizeHandler( |
170
|
|
|
$container['oauth2_server']->getAuthorizeController(), |
171
|
|
|
$container['oauth2_server.authorize_renderer'] |
172
|
|
|
); |
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
|
|
|
|
183
|
|
|
$container['oauth2_server.controllers.authorize_handler'] = function (Container $container) { |
184
|
|
|
return new Controllers\AuthorizeHandler( |
185
|
|
|
$container['oauth2_server']->getAuthorizeController(), |
186
|
|
|
$container['oauth2_server.authorize_renderer'] |
187
|
|
|
); |
188
|
|
|
}; |
189
|
|
|
|
190
|
|
|
$container['oauth2_server.controllers.token'] = function (Container $container) { |
191
|
|
|
return new Controllers\TokenHandler($container['oauth2_server']->getTokenController()); |
192
|
|
|
}; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @inherit |
197
|
|
|
*/ |
198
|
|
|
public function connect(Application $app) |
199
|
|
|
{ |
200
|
|
|
$controllers = $app['controllers_factory']; |
201
|
|
|
|
202
|
|
|
if ($app['oauth2_server.controllers_as_service']) { |
203
|
|
|
$controllers->post('/authorize', 'oauth2_server.controllers.authorize_handler:__invoke') |
204
|
|
|
->bind('oauth2_authorize_handler'); |
205
|
|
|
$controllers->get('/authorize', 'oauth2_server.controllers.authorize_validator:__invoke') |
206
|
|
|
->bind('oauth2_authorize_validator'); |
207
|
|
|
$controllers->post('/token', 'oauth2_server.controllers.token:__invoke')->bind('oauth2_token_handler'); |
208
|
|
|
} else { |
209
|
|
|
$controllers->post('/authorize', $app['oauth2_server.controllers.authorize_handler']) |
210
|
|
|
->bind('oauth2_authorize_handler'); |
211
|
|
|
$controllers->get('/authorize', $app['oauth2_server.controllers.authorize_validator']) |
212
|
|
|
->bind('oauth2_authorize_validator'); |
213
|
|
|
$controllers->post('/token', $app['oauth2_server.controllers.token'])->bind('oauth2_token_handler'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $controllers; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: