14
14
use FOS \OAuthServerBundle \Event \OAuthEvent ;
15
15
use FOS \OAuthServerBundle \Form \Handler \AuthorizeFormHandler ;
16
16
use FOS \OAuthServerBundle \Model \ClientInterface ;
17
+ use FOS \OAuthServerBundle \Model \ClientManagerInterface ;
18
+ use OAuth2 \OAuth2 ;
17
19
use OAuth2 \OAuth2ServerException ;
18
- use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
19
- use Symfony \Component \DependencyInjection \ContainerInterface ;
20
+ use Symfony \Bundle \FrameworkBundle \Templating \EngineInterface ;
21
+ use Symfony \Component \EventDispatcher \EventDispatcher ;
22
+ use Symfony \Component \Form \Form ;
20
23
use Symfony \Component \HttpFoundation \Request ;
24
+ use Symfony \Component \HttpFoundation \RequestStack ;
21
25
use Symfony \Component \HttpFoundation \Response ;
26
+ use Symfony \Component \HttpFoundation \Session \SessionInterface ;
22
27
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
28
+ use Symfony \Component \Routing \Router ;
29
+ use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
23
30
use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
24
31
use Symfony \Component \Security \Core \User \UserInterface ;
25
32
28
35
*
29
36
* @author Chris Jones <[email protected] >
30
37
*/
31
- class AuthorizeController implements ContainerAwareInterface
38
+ class AuthorizeController
32
39
{
33
40
/**
34
41
* @var ClientInterface
35
42
*/
36
43
private $ client ;
37
44
38
45
/**
39
- * @var ContainerInterface
46
+ * @var SessionInterface
40
47
*/
41
- protected $ container ;
48
+ private $ session ;
42
49
43
50
/**
44
- * Sets the container.
51
+ * @var Form
52
+ */
53
+ private $ authorizeForm ;
54
+
55
+ /**
56
+ * @var AuthorizeFormHandler
57
+ */
58
+ private $ authorizeFormHandler ;
59
+
60
+ /**
61
+ * @var OAuth2
62
+ */
63
+ private $ oAuth2Server ;
64
+
65
+ /**
66
+ * @var EngineInterface
67
+ */
68
+ private $ templating ;
69
+
70
+ /**
71
+ * @var RequestStack
72
+ */
73
+ private $ requestStack ;
74
+
75
+ /**
76
+ * @var TokenStorageInterface
77
+ */
78
+ private $ tokenStorage ;
79
+
80
+ /**
81
+ * @var Router
82
+ */
83
+ private $ router ;
84
+
85
+ /**
86
+ * @var ClientManagerInterface
87
+ */
88
+ private $ clientManager ;
89
+
90
+ /**
91
+ * @var string
92
+ */
93
+ private $ templateEngineType ;
94
+
95
+ /**
96
+ * @var EventDispatcher
97
+ */
98
+ private $ eventDispatcher ;
99
+
100
+ /**
101
+ * This controller had been made as a service due to support symfony 4 where all* services are private by default.
102
+ * Thus, there is considered a bad practice to fetch services directly from container.
103
+ * @todo This controller could be refactored to do not rely on so many dependencies
45
104
*
46
- * @param ContainerInterface|null $container A ContainerInterface instance or null
105
+ * @param RequestStack $requestStack
106
+ * @param SessionInterface $session
107
+ * @param Form $authorizeForm
108
+ * @param AuthorizeFormHandler $authorizeFormHandler
109
+ * @param OAuth2 $oAuth2Server
110
+ * @param EngineInterface $templating
111
+ * @param TokenStorageInterface $tokenStorage
112
+ * @param Router $router
113
+ * @param ClientManagerInterface $clientManager
114
+ * @param EventDispatcher $eventDispatcher
115
+ * @param string $templateEngineType
47
116
*/
48
- public function setContainer (ContainerInterface $ container = null )
49
- {
50
- $ this ->container = $ container ;
117
+ public function __construct (
118
+ RequestStack $ requestStack ,
119
+ SessionInterface $ session ,
120
+ Form $ authorizeForm ,
121
+ AuthorizeFormHandler $ authorizeFormHandler ,
122
+ OAuth2 $ oAuth2Server ,
123
+ EngineInterface $ templating ,
124
+ TokenStorageInterface $ tokenStorage ,
125
+ Router $ router ,
126
+ ClientManagerInterface $ clientManager ,
127
+ EventDispatcher $ eventDispatcher ,
128
+ $ templateEngineType = 'twig '
129
+ ) {
130
+ $ this ->requestStack = $ requestStack ;
131
+ $ this ->session = $ session ;
132
+ $ this ->authorizeForm = $ authorizeForm ;
133
+ $ this ->authorizeFormHandler = $ authorizeFormHandler ;
134
+ $ this ->oAuth2Server = $ oAuth2Server ;
135
+ $ this ->templating = $ templating ;
136
+ $ this ->tokenStorage = $ tokenStorage ;
137
+ $ this ->router = $ router ;
138
+ $ this ->clientManager = $ clientManager ;
139
+ $ this ->templateEngineType = $ templateEngineType ;
140
+ $ this ->eventDispatcher = $ eventDispatcher ;
51
141
}
52
142
53
143
/**
54
144
* Authorize.
55
145
*/
56
146
public function authorizeAction (Request $ request )
57
147
{
58
- $ user = $ this ->getTokenStorage () ->getToken ()->getUser ();
148
+ $ user = $ this ->tokenStorage ->getToken ()->getUser ();
59
149
60
150
if (!$ user instanceof UserInterface) {
61
151
throw new AccessDeniedException ('This user does not have access to this section. ' );
62
152
}
63
153
64
- if (true === $ this ->container -> get ( ' session ' ) ->get ('_fos_oauth_server.ensure_logout ' )) {
65
- $ this ->container -> get ( ' session ' ) ->invalidate (600 );
66
- $ this ->container -> get ( ' session ' ) ->set ('_fos_oauth_server.ensure_logout ' , true );
154
+ if (true === $ this ->session ->get ('_fos_oauth_server.ensure_logout ' )) {
155
+ $ this ->session ->invalidate (600 );
156
+ $ this ->session ->set ('_fos_oauth_server.ensure_logout ' , true );
67
157
}
68
158
69
- $ form = $ this ->container -> get ( ' fos_oauth_server.authorize.form ' ) ;
70
- $ formHandler = $ this ->container -> get ( ' fos_oauth_server.authorize.form.handler ' ) ;
159
+ $ form = $ this ->authorizeForm ;
160
+ $ formHandler = $ this ->authorizeFormHandler ;
71
161
72
- $ event = $ this ->container -> get ( ' event_dispatcher ' ) ->dispatch (
162
+ $ event = $ this ->eventDispatcher ->dispatch (
73
163
OAuthEvent::PRE_AUTHORIZATION_PROCESS ,
74
164
new OAuthEvent ($ user , $ this ->getClient ())
75
165
);
76
166
77
167
if ($ event ->isAuthorizedClient ()) {
78
168
$ scope = $ request ->get ('scope ' , null );
79
169
80
- return $ this ->container
81
- ->get ('fos_oauth_server.server ' )
82
- ->finishClientAuthorization (true , $ user , $ request , $ scope );
170
+ return $ this ->oAuth2Server ->finishClientAuthorization (true , $ user , $ request , $ scope );
83
171
}
84
172
85
173
if (true === $ formHandler ->process ()) {
86
174
return $ this ->processSuccess ($ user , $ formHandler , $ request );
87
175
}
88
176
89
- return $ this ->container -> get ( ' templating ' ) ->renderResponse (
90
- 'FOSOAuthServerBundle:Authorize:authorize.html. ' .$ this ->container -> getParameter ( ' fos_oauth_server.template.engine ' ) ,
177
+ return $ this ->templating ->renderResponse (
178
+ 'FOSOAuthServerBundle:Authorize:authorize.html. ' .$ this ->templateEngineType ,
91
179
array (
92
180
'form ' => $ form ->createView (),
93
181
'client ' => $ this ->getClient (),
@@ -104,24 +192,23 @@ public function authorizeAction(Request $request)
104
192
*/
105
193
protected function processSuccess (UserInterface $ user , AuthorizeFormHandler $ formHandler , Request $ request )
106
194
{
107
- if (true === $ this ->container -> get ( ' session ' ) ->get ('_fos_oauth_server.ensure_logout ' )) {
108
- $ this ->getTokenStorage () ->setToken (null );
109
- $ this ->container -> get ( ' session ' ) ->invalidate ();
195
+ if (true === $ this ->session ->get ('_fos_oauth_server.ensure_logout ' )) {
196
+ $ this ->tokenStorage ->setToken (null );
197
+ $ this ->session ->invalidate ();
110
198
}
111
199
112
- $ this ->container -> get ( ' event_dispatcher ' ) ->dispatch (
200
+ $ this ->eventDispatcher ->dispatch (
113
201
OAuthEvent::POST_AUTHORIZATION_PROCESS ,
114
202
new OAuthEvent ($ user , $ this ->getClient (), $ formHandler ->isAccepted ())
115
203
);
116
204
117
- $ formName = $ this ->container -> get ( ' fos_oauth_server.authorize.form ' ) ->getName ();
205
+ $ formName = $ this ->authorizeForm ->getName ();
118
206
if (!$ request ->query ->all () && $ request ->request ->has ($ formName )) {
119
207
$ request ->query ->add ($ request ->request ->get ($ formName ));
120
208
}
121
209
122
210
try {
123
- return $ this ->container
124
- ->get ('fos_oauth_server.server ' )
211
+ return $ this ->oAuth2Server
125
212
->finishClientAuthorization ($ formHandler ->isAccepted (), $ user , $ request , $ formHandler ->getScope ());
126
213
} catch (OAuth2ServerException $ e ) {
127
214
return $ e ->getHttpResponse ();
@@ -137,7 +224,7 @@ protected function processSuccess(UserInterface $user, AuthorizeFormHandler $for
137
224
*/
138
225
protected function getRedirectionUrl (UserInterface $ user )
139
226
{
140
- return $ this ->container -> get ( ' router ' ) ->generate ('fos_oauth_server_profile_show ' );
227
+ return $ this ->router ->generate ('fos_oauth_server_profile_show ' );
141
228
}
142
229
143
230
/**
@@ -151,14 +238,12 @@ protected function getClient()
151
238
$ client = null ;
152
239
if (null !== $ request ) {
153
240
if (null === $ clientId = $ request ->get ('client_id ' )) {
154
- $ form = $ this ->container -> get ( ' fos_oauth_server.authorize.form ' ) ;
241
+ $ form = $ this ->authorizeForm ;
155
242
$ formData = $ request ->get ($ form ->getName (), array ());
156
243
$ clientId = isset ($ formData ['client_id ' ]) ? $ formData ['client_id ' ] : null ;
157
244
}
158
245
159
- $ client = $ this ->container
160
- ->get ('fos_oauth_server.client_manager ' )
161
- ->findClientByPublicId ($ clientId );
246
+ $ client = $ this ->clientManager ->findClientByPublicId ($ clientId );
162
247
}
163
248
164
249
if (null === $ client ) {
@@ -171,26 +256,16 @@ protected function getClient()
171
256
return $ this ->client ;
172
257
}
173
258
259
+ /**
260
+ * @return null|Request
261
+ */
174
262
private function getCurrentRequest ()
175
263
{
176
- if ($ this ->container ->has ('request_stack ' )) {
177
- $ request = $ this ->container ->get ('request_stack ' )->getCurrentRequest ();
178
- if (null === $ request ) {
179
- throw new \RuntimeException ('No current request. ' );
180
- }
181
-
182
- return $ request ;
183
- } else {
184
- return $ this ->container ->get ('request ' );
185
- }
186
- }
187
-
188
- private function getTokenStorage ()
189
- {
190
- if ($ this ->container ->has ('security.token_storage ' )) {
191
- return $ this ->container ->get ('security.token_storage ' );
264
+ $ request = $ this ->requestStack ->getCurrentRequest ();
265
+ if (null === $ request ) {
266
+ throw new \RuntimeException ('No current request. ' );
192
267
}
193
268
194
- return $ this -> container -> get ( ' security.context ' ) ;
269
+ return $ request ;
195
270
}
196
271
}
0 commit comments