Merge pull request #173 from jildertmiedema/redirect-framework

Inserting a redirect callback
1.0
Ben Ramsey 2014-12-02 07:47:07 -06:00
commit 129f9c4a32
2 changed files with 31 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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