Skip to content

Instantly share code, notes, and snippets.

@polidog
Last active June 14, 2016 10:02
Show Gist options
  • Save polidog/8ef08aaddf865064130cedf0108c1c07 to your computer and use it in GitHub Desktop.
Save polidog/8ef08aaddf865064130cedf0108c1c07 to your computer and use it in GitHub Desktop.
YahooID連携用のResourceOwner
<?php
namespace AppBundle\OAuth\ResourceOwner
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
use Symfony\Component\HttpFoundation\Request;
use Buzz\Message\RequestInterface as HttpRequestInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class YahooJpResourceOwner extends GenericOAuth2ResourceOwner
{
/**
* {@inheritDoc}
*/
protected $paths = array(
'identifier' => 'user_id',
'nickname' => 'name',
'realname' => 'name',
'firstname' => 'given_name',
'lastname' => "family_name"
);
/**
* {@inheritDoc}
*/
public function getUserInformation(array $accessToken, array $extraParameters = array())
{
$content = $this->doGetUserInformationRequest($this->normalizeUrl($this->options['infos_url'], ['schema' => 'openid']),[
'access_token' => $accessToken['access_token']
]);
$response = $this->getUserResponse();
$response->setResponse($content->getContent());
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));
return $response;
}
public function getAccessToken(Request $request, $redirectUri, array $extraParameters = array())
{
$parameters = array_merge(array(
'code' => $request->query->get('code'),
'grant_type' => 'authorization_code',
'redirect_uri' => $redirectUri,
), $extraParameters);
$response = $this->doGetTokenRequest($this->options['access_token_url'], $parameters);
$response = $this->getResponseContent($response);
$this->validateResponseContent($response);
return $response;
}
/**
* {@inheritDoc}
*/
public function refreshAccessToken($refreshToken, array $extraParameters = array())
{
$parameters = array_merge( array(
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
), $extraParameters);
$response = $this->doGetTokenRequest($this->options['access_token_url'], $parameters);
$response = $this->getResponseContent($response);
$this->validateResponseContent($response);
return $response;
}
/**
* {@inheritDoc}
*/
protected function doGetTokenRequest($url, array $parameters = array())
{
$headers = array(
'Authorization: Basic ' . base64_encode($this->options['client_id'] . ':' . $this->options['client_secret']),
'Content-Type: application/x-www-form-urlencoded',
);
return $this->httpRequest($this->options['access_token_url'], http_build_query($parameters, '', '&'), $headers, HttpRequestInterface::METHOD_POST);
}
/**
* {@inheritDoc}
*/
protected function doGetUserInformationRequest($url, array $parameters = array())
{
$headers = array(
'Authorization: Bearer ' . $parameters['access_token'],
);
return $this->httpRequest($url, http_build_query($parameters, '', '&'), $headers);
}
protected function configureOptions(OptionsResolverInterface $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefaults(array(
'use_bearer_authorization' => false,
'use_commas_in_scope' => true
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment