AbstractOAuth2Controller: Store provider and token in class properties

This way, they are available for subclasses to access them in one of
the template methods.

Refs #673.
This commit is contained in:
Franz Liedke 2016-03-18 22:22:35 +09:00
parent 588dd7b213
commit 4c03f13fef

View File

@ -23,6 +23,18 @@ abstract class AbstractOAuth2Controller implements ControllerInterface
*/
protected $authResponse;
/**
* @var \League\OAuth2\Client\Provider\AbstractProvider
*/
protected $provider;
/**
* The access token, once obtained.
*
* @var string
*/
protected $token;
/**
* @param AuthenticationResponseFactory $authResponse
*/
@ -39,7 +51,7 @@ abstract class AbstractOAuth2Controller implements ControllerInterface
{
$redirectUri = (string) $request->getAttribute('originalUri', $request->getUri())->withQuery('');
$provider = $this->getProvider($redirectUri);
$this->provider = $this->getProvider($redirectUri);
$session = $request->getAttribute('session');
@ -48,8 +60,8 @@ abstract class AbstractOAuth2Controller implements ControllerInterface
$state = array_get($queryParams, 'state');
if (! $code) {
$authUrl = $provider->getAuthorizationUrl($this->getAuthorizationUrlOptions());
$session->set('oauth2state', $provider->getState());
$authUrl = $this->provider->getAuthorizationUrl($this->getAuthorizationUrlOptions());
$session->set('oauth2state', $this->provider->getState());
return new RedirectResponse($authUrl.'&display=popup');
} elseif (! $state || $state !== $session->get('oauth2state')) {
@ -58,9 +70,9 @@ abstract class AbstractOAuth2Controller implements ControllerInterface
exit;
}
$token = $provider->getAccessToken('authorization_code', compact('code'));
$this->token = $this->provider->getAccessToken('authorization_code', compact('code'));
$owner = $provider->getResourceOwner($token);
$owner = $this->provider->getResourceOwner($this->token);
$identification = $this->getIdentification($owner);
$suggestions = $this->getSuggestions($owner);