From 9df4ea03a861b514c12da239f54a45d0cea49a8e Mon Sep 17 00:00:00 2001 From: Nodge Date: Tue, 15 Jul 2014 00:19:31 +0600 Subject: [PATCH] Fixed wrong redirect_uri when popup is used --- CHANGELOG.md | 3 +++ src/assets/js/eauth.js | 2 +- src/oauth2/Service.php | 22 ++++++++++++++-------- src/services/FacebookOAuth2Service.php | 24 +++++++++++++++++++++--- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31a20e4..f0df66c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Yii2 EAuth Change Log ===================== +### 2.2.2 (15.07.2014) +* Fixed wrong redirect_uri when popup is used + ### 2.2.1 (25.04.2014) * Fix missing query params in callback urls (#26) * Follow Yii2 code style diff --git a/src/assets/js/eauth.js b/src/assets/js/eauth.js index 81d353f..52dfe22 100644 --- a/src/assets/js/eauth.js +++ b/src/assets/js/eauth.js @@ -23,7 +23,7 @@ url += url.indexOf('?') >= 0 ? '&' : '?'; if (url.indexOf('redirect_uri=') === -1) url += 'redirect_uri=' + encodeURIComponent(redirect_uri) + '&'; - url += 'js'; + url += 'js='; var centerWidth = (window.screen.width - options.popup.width) / 2, centerHeight = (window.screen.height - options.popup.height) / 2; diff --git a/src/oauth2/Service.php b/src/oauth2/Service.php index 9c30f0d..1b0063a 100644 --- a/src/oauth2/Service.php +++ b/src/oauth2/Service.php @@ -174,17 +174,23 @@ protected function getProxy() */ protected function getCallbackUrl() { - $route = Yii::$app->getRequest()->getQueryParams(); - array_unshift($route, ''); - - // Can not use these params in OAuth2 callbacks - foreach (['code', 'state'] as $param) { - if (isset($route[$param])) { - unset($route[$param]); + if (isset($_GET['redirect_uri'])) { + $url = $_GET['redirect_uri']; + } + else { + $route = Yii::$app->getRequest()->getQueryParams(); + array_unshift($route, ''); + + // Can not use these params in OAuth2 callbacks + foreach (['code', 'state', 'redirect_uri'] as $param) { + if (isset($route[$param])) { + unset($route[$param]); + } } + + $url = Url::to($route, true); } - $url = Url::to($route, true); return $url; } diff --git a/src/services/FacebookOAuth2Service.php b/src/services/FacebookOAuth2Service.php index 617d491..986f1c2 100644 --- a/src/services/FacebookOAuth2Service.php +++ b/src/services/FacebookOAuth2Service.php @@ -73,9 +73,14 @@ public function getAccessTokenArgumentNames() */ public function parseAccessTokenResponse($response) { - // Facebook gives us a query string... - parse_str($response, $data); - return $data; + // Facebook gives us a query string or json + if ($response[0] === '{') { + return json_decode($response, true); + } + else { + parse_str($response, $data); + return $data; + } } /** @@ -95,4 +100,17 @@ protected function fetchResponseError($response) return null; } } + + /** + * @param array $data + * @return string|null + */ + public function getAccessTokenResponseError($data) + { + $error = $this->fetchResponseError($data); + if (!$error) { + return null; + } + return $error['code'].': '.$error['message']; + } }