diff --git a/src/Provider/AbstractProvider.php b/src/Provider/AbstractProvider.php index 8be82f3..602691a 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,18 @@ 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 +310,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