1 | <?php |
||
38 | class AuthorizeController extends Controller |
||
39 | { |
||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | 9 | public function init() |
|
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | 9 | public function behaviors() |
|
78 | |||
79 | /** |
||
80 | * Send back an oauth token |
||
81 | * @return Response |
||
82 | * @since XXX |
||
83 | */ |
||
84 | 8 | public function actionIndex() |
|
85 | { |
||
86 | 8 | Yii::$app->response->headers->add('Content-Security-Policy', 'frame-ancestors \'none\';'); |
|
87 | 8 | $oauthServer = Yii::createObject('OAuth2\Server'); |
|
88 | /* @var \Oauth2\Server $oauthServer */ |
||
89 | 8 | $status = false; |
|
90 | 8 | $oauthRequest = OAuth2Request::createFromGlobals(); |
|
91 | 8 | $oauthResponse = new OAuth2Response(); |
|
92 | 8 | $grantType = Yii::$app->request->getQueryParam('response_type'); |
|
93 | switch ($grantType) { |
||
94 | // Authorization Code |
||
95 | 8 | case 'code': |
|
96 | 4 | if (Module::getInstance()->allowAuthorizationCode === true) { |
|
97 | 3 | $oauthGrantType = Yii::createObject('OAuth2\GrantType\AuthorizationCode'); |
|
98 | /* @var \OAuth2\GrantType\AuthorizationCode $oauthGrantType */ |
||
99 | 3 | $oauthServer->addGrantType($oauthGrantType); |
|
100 | 3 | $status = $oauthServer->validateAuthorizeRequest($oauthRequest, $oauthResponse); |
|
101 | 3 | $error = $oauthResponse->getParameters(); |
|
102 | 3 | if (($status === false) && (empty($error) === false)) { |
|
103 | 1 | Yii::$app->session->setFlash('error', $error, false); |
|
104 | // return $this->redirect(['error']); |
||
105 | 1 | } |
|
106 | 3 | } else { |
|
107 | 1 | $status = false; |
|
108 | 1 | Yii::$app->session->setFlash('error', ['error' => 'invalid_grant', 'error_description' => 'authorization code grant is not supported'], false); |
|
109 | } |
||
110 | 4 | break; |
|
111 | // Implicit |
||
112 | 4 | case 'token': |
|
113 | 4 | $status = $oauthServer->validateAuthorizeRequest($oauthRequest, $oauthResponse); |
|
114 | 4 | $error = $oauthResponse->getParameters(); |
|
115 | 4 | if (($status === false) && (empty($error) === false)) { |
|
116 | 2 | Yii::$app->session->setFlash('error', $error, false); |
|
117 | // return $this->redirect(['error']); |
||
118 | 2 | } |
|
119 | 4 | break; |
|
120 | } |
||
121 | |||
122 | 8 | if ($status === true) { |
|
123 | 4 | Yii::$app->session->set('oauthServer', $oauthServer); |
|
124 | 4 | if (isset($oauthRequest) === true) { |
|
125 | 4 | Yii::$app->session->set('oauthRequest', $oauthRequest); |
|
126 | 4 | } |
|
127 | 4 | if (Yii::$app->user->isGuest === true) { |
|
128 | 4 | $response = $this->redirect(['login']); |
|
129 | 4 | } else { |
|
130 | 1 | $response = $this->redirect(['authorize']); |
|
131 | } |
||
132 | 4 | } else { |
|
133 | //TODO: check if we should redirect to specific url with an error |
||
134 | 4 | $response = $this->redirect(['error']); |
|
135 | } |
||
136 | 8 | return $response; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Display login page |
||
141 | * @return Response|string |
||
142 | * @since XXX |
||
143 | */ |
||
144 | 5 | public function actionLogin() |
|
145 | { |
||
146 | 5 | Yii::$app->response->headers->add('Content-Security-Policy', 'frame-ancestors \'none\';'); |
|
147 | |||
148 | 5 | $oauthServer = Yii::$app->session->get('oauthServer'); |
|
149 | /* @var \Oauth2\Server $oauthServer */ |
||
150 | 5 | if ($oauthServer === null) { |
|
151 | 1 | Yii::$app->session->setFlash('error', [ |
|
152 | 1 | 'error' => 'request_invalid', |
|
153 | 1 | 'error_description' => 'The request was not performed as expected.', |
|
154 | 1 | ], false); |
|
155 | 1 | return $this->redirect(['error']); |
|
156 | } |
||
157 | |||
158 | 4 | $userForm = Yii::createObject('sweelix\oauth2\server\forms\User'); |
|
159 | 4 | $response = null; |
|
160 | /* @var \sweelix\oauth2\server\forms\User $userForm */ |
||
161 | 4 | if (Yii::$app->request->isPost === true) { |
|
162 | //TODO: handle case when user decline the grants |
||
163 | 4 | $userForm->load(Yii::$app->request->bodyParams); |
|
164 | 4 | if ($userForm->validate() === true) { |
|
165 | 4 | $userClass = $this->getUserClass(); |
|
166 | 4 | $realUser = $userClass::findByUsernameAndPassword($userForm->username, $userForm->password); |
|
167 | /* @var \sweelix\oauth2\server\interfaces\UserModelInterface $realUser */ |
||
168 | 4 | if ($realUser !== null) { |
|
169 | 4 | Yii::$app->user->login($realUser, Module::getInstance()->loginDuration); |
|
170 | 4 | $response = $this->redirect(['authorize']); |
|
171 | 4 | } else { |
|
172 | 2 | $userForm->addError('username'); |
|
173 | } |
||
174 | 4 | } |
|
175 | 4 | } |
|
176 | 4 | if ($response === null) { |
|
177 | // force empty password |
||
178 | 4 | $userForm->password = ''; |
|
179 | 4 | $response = $this->render('login', [ |
|
180 | 4 | 'user' => $userForm, |
|
181 | 4 | ]); |
|
182 | 4 | } |
|
183 | 4 | return $response; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Display authorize page |
||
188 | * @return string|Response |
||
189 | * @since XXX |
||
190 | */ |
||
191 | 4 | public function actionAuthorize() |
|
274 | |||
275 | /** |
||
276 | * Display an error page |
||
277 | * @return Response|string |
||
278 | * @since XXX |
||
279 | */ |
||
280 | 5 | public function actionError() |
|
289 | |||
290 | /** |
||
291 | * @var string |
||
292 | */ |
||
293 | private $userClass; |
||
294 | |||
295 | /** |
||
296 | * @return string classname for selected interface |
||
297 | * @since XXX |
||
298 | */ |
||
299 | 4 | public function getUserClass() |
|
307 | |||
308 | |||
309 | |||
310 | } |
||
311 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.