|
28 | 28 | use yii\base\Exception; |
29 | 29 | use yii\helpers\Json; |
30 | 30 | use Yii; |
| 31 | +use yii\web\HttpException; |
31 | 32 | use yii\web\UnauthorizedHttpException; |
32 | 33 |
|
33 | 34 | class OAuth2 extends \yii\authclient\OAuth2 |
@@ -305,6 +306,45 @@ public function logout($globalLogout=true) |
305 | 306 | return true; |
306 | 307 | } |
307 | 308 |
|
| 309 | + /** |
| 310 | + * @inheritdoc |
| 311 | + */ |
| 312 | + public function fetchAccessToken($authCode, array $params = []) |
| 313 | + { |
| 314 | + if ($this->validateAuthState) { |
| 315 | + $authState = $this->getState('authState'); |
| 316 | + if (!isset($_REQUEST['state']) || empty($authState) || strcmp($_REQUEST['state'], $authState) !== 0) { |
| 317 | + throw new HttpException(400, 'Invalid auth state parameter.'); |
| 318 | + } else { |
| 319 | + $this->removeState('authState'); |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + $defaultParams = [ |
| 324 | + 'code' => $authCode, |
| 325 | + 'grant_type' => 'authorization_code', |
| 326 | + 'redirect_uri' => $this->getReturnUrl(), |
| 327 | + ]; |
| 328 | + |
| 329 | + $defaultHeaders = [ |
| 330 | + 'Content-Type' => 'application/x-www-form-urlencoded', |
| 331 | + 'Authorization' => 'Basic ' . base64_encode($this->clientId . ":" . $this->clientSecret), |
| 332 | + ]; |
| 333 | + |
| 334 | + $request = $this->createRequest() |
| 335 | + ->setMethod('POST') |
| 336 | + ->setUrl($this->tokenUrl) |
| 337 | + ->setHeaders($defaultHeaders) |
| 338 | + ->setData(array_merge($defaultParams, $params)); |
| 339 | + |
| 340 | + $response = $this->sendRequest($request); |
| 341 | + |
| 342 | + $token = $this->createToken(['params' => $response]); |
| 343 | + $this->setAccessToken($token); |
| 344 | + |
| 345 | + return $token; |
| 346 | + } |
| 347 | + |
308 | 348 | public function init() |
309 | 349 | { |
310 | 350 | parent::init(); |
|
0 commit comments