Created
May 21, 2015 03:19
-
-
Save yplam/9f75a6999cf4fe2dad47 to your computer and use it in GitHub Desktop.
HWIOAuthBundle提供微信登录服务
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hwi_oauth: | |
... | |
resource_owners: | |
weixin: | |
service: app.oauth_weixin | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
app.oauth_weixin: | |
class: AppBundle\OAuth\ResourceOwner\WeiXinResourceOwner | |
arguments: | |
- @hwi_oauth.http_client | |
- @security.http_utils | |
- | |
client_id: %weixin_client_id% | |
client_secret: %weixin_client_secret% | |
- weixin | |
- @hwi_oauth.storage.session | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Connect weixin with HWIOAuthBundle package. | |
* | |
* Yplam <[email protected]> | |
* | |
*/ | |
namespace AppBundle\OAuth\ResourceOwner; | |
use Buzz\Message\MessageInterface as HttpMessageInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
use Symfony\Component\Security\Core\Exception\AuthenticationException; | |
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner; | |
class WeiXinResourceOwner extends GenericOAuth2ResourceOwner | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
protected $paths = array( | |
'identifier' => 'openid', | |
'nickname' => 'nickname', | |
'realname' => 'nickname', | |
'unionid' => 'unionid', | |
'profilepicture' => 'headimgurl', | |
); | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getUserInformation(array $accessToken = null, array $extraParameters = array()) | |
{ | |
$url = $this->normalizeUrl($this->options['infos_url'], array( | |
'access_token' => $accessToken['access_token'], | |
'openid' => isset($accessToken['uid']) ? $accessToken['uid'] : $accessToken['openid'], | |
)); | |
$response = $this->doGetUserInformationRequest($url); | |
$content = $this->getResponseContent($response); | |
$response = $this->getUserResponse(); | |
$response->setResponse($content); | |
$response->setResourceOwner($this); | |
$response->setOAuthToken(new OAuthToken($accessToken)); | |
return $response; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public function getAuthorizationUrl($redirectUri, array $extraParameters = array()) | |
{ | |
if ($this->options['csrf']) { | |
if (null === $this->state) { | |
$this->state = $this->generateNonce(); | |
} | |
$this->storage->save($this, $this->state, 'csrf_state'); | |
} | |
$parameters = array_merge(array( | |
'response_type' => 'code', | |
'appid' => $this->options['client_id'], | |
'scope' => $this->options['scope'], | |
'state' => $this->state ? urlencode($this->state) : null, | |
'redirect_uri' => $redirectUri, | |
), $extraParameters); | |
return $this->normalizeUrl($this->options['authorization_url'], $parameters); | |
} | |
/** | |
* Retrieve an access token for a given code. | |
* | |
* @param Request $request The request object from where the code is going to extracted | |
* @param mixed $redirectUri The uri to redirect the client back to | |
* @param array $extraParameters An array of parameters to add to the url | |
* | |
* @return array Array containing the access token and it's 'expires_in' value, | |
* along with any other parameters returned from the authentication | |
* provider. | |
* | |
* @throws AuthenticationException If an OAuth error occurred or no access token is found | |
*/ | |
public function getAccessToken(Request $request, $redirectUri, array $extraParameters = array()) | |
{ | |
$parameters = array_merge(array( | |
'code' => $request->query->get('code'), | |
'grant_type' => 'authorization_code', | |
'appid' => $this->options['client_id'], | |
'secret' => $this->options['client_secret'], | |
'redirect_uri' => $redirectUri, | |
), $extraParameters); | |
$response = $this->doGetTokenRequest($this->options['access_token_url'], $parameters); | |
$response = $this->getResponseContent($response); | |
$this->validateResponseContent($response); | |
return $response; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
protected function configureOptions(OptionsResolverInterface $resolver) | |
{ | |
parent::configureOptions($resolver); | |
$resolver->setDefaults(array( | |
'authorization_url' => 'https://open.weixin.qq.com/connect/qrconnect', | |
'access_token_url' => 'https://api.weixin.qq.com/sns/oauth2/access_token', | |
'infos_url' => 'https://api.weixin.qq.com/sns/userinfo', | |
'scope' => 'snsapi_login', | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment