From e8573b1df4a0f6f111b56c19608aa4253385337c Mon Sep 17 00:00:00 2001 From: Jildert Miedema Date: Mon, 1 Dec 2014 23:10:06 +0100 Subject: [PATCH 1/2] inserting a redirect callback --- src/Provider/AbstractProvider.php | 20 +++++++++++++++++--- test/src/Provider/AbstractProviderTest.php | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 8be82f3..415e775 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -2,6 +2,7 @@ namespace League\OAuth2\Client\Provider; +use Closure; use Guzzle\Http\Exception\BadResponseException; use Guzzle\Service\Client as GuzzleClient; use League\OAuth2\Client\Exception\IDPException as IDPException; @@ -34,6 +35,8 @@ abstract class AbstractProvider implements ProviderInterface protected $httpClient; + protected $redirectHandler; + /** * @var int This represents: PHP_QUERY_RFC1738, which is the default value for php 5.4 * and the default encryption type for the http_build_query setup @@ -128,13 +131,19 @@ abstract class AbstractProvider implements ProviderInterface return $this->urlAuthorize().'?'.$this->httpBuildQuery($params, '', '&'); } - // @codeCoverageIgnoreStart public function authorize($options = []) { - header('Location: '.$this->getAuthorizationUrl($options)); + $url = $this->getAuthorizationUrl($options); + if ($this->redirectHandler) + { + $handler = $this->redirectHandler; + return $handler($url); + } + // @codeCoverageIgnoreStart + header('Location: ' . $url); exit; + // @codeCoverageIgnoreEnd } - // @codeCoverageIgnoreEnd public function getAccessToken($grant = 'authorization_code', $params = []) { @@ -302,4 +311,9 @@ abstract class AbstractProvider implements ProviderInterface return $response; } + + public function setRedirectHandler(Closure $handler) + { + $this->redirectHandler = $handler; + } } diff --git a/test/src/Provider/AbstractProviderTest.php b/test/src/Provider/AbstractProviderTest.php index d9657e7..29c7e57 100644 --- a/test/src/Provider/AbstractProviderTest.php +++ b/test/src/Provider/AbstractProviderTest.php @@ -65,6 +65,21 @@ class AbstractProviderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($value, $mockProvider->{$key}); } } + + public function testSetRedirectHandler() + { + $this->testFunction = false; + + $callback = function ($url) { + $this->testFunction = $url; + }; + + $this->provider->setRedirectHandler($callback); + + $this->provider->authorize('http://test.url/'); + + $this->assertNotFalse($this->testFunction); + } } class MockProvider extends \League\OAuth2\Client\Provider\AbstractProvider From 55de45401eaa21f53c0b2414091da6f3b0f3fcb7 Mon Sep 17 00:00:00 2001 From: Jildert Miedema Date: Mon, 1 Dec 2014 23:13:35 +0100 Subject: [PATCH 2/2] cs fix --- src/Provider/AbstractProvider.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 415e775..602691a 100644 --- a/src/Provider/AbstractProvider.php +++ b/src/Provider/AbstractProvider.php @@ -134,8 +134,7 @@ abstract class AbstractProvider implements ProviderInterface public function authorize($options = []) { $url = $this->getAuthorizationUrl($options); - if ($this->redirectHandler) - { + if ($this->redirectHandler) { $handler = $this->redirectHandler; return $handler($url); }