Skip to content

Commit 8d27499

Browse files
author
Phan Van Thanh
committed
Update fetchAccessToken request, add header params
1 parent da48774 commit 8d27499

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

fproject/authclient/OAuth2.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use yii\base\Exception;
2929
use yii\helpers\Json;
3030
use Yii;
31+
use yii\web\HttpException;
3132
use yii\web\UnauthorizedHttpException;
3233

3334
class OAuth2 extends \yii\authclient\OAuth2
@@ -305,6 +306,45 @@ public function logout($globalLogout=true)
305306
return true;
306307
}
307308

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+
308348
public function init()
309349
{
310350
parent::init();

0 commit comments

Comments
 (0)