-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DraperStudio
committed
Jul 13, 2015
0 parents
commit 158d705
Showing
7 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Created by https://www.gitignore.io | ||
|
||
### Composer ### | ||
composer.phar | ||
vendor/ | ||
|
||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preset: symfony |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 xyxu <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Laravel Socialite weixin.qq.com(WeChat) OAuth2 Provider |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "socialiteproviders/weixin", | ||
"description": "Weixin OAuth2 Provider for Laravel Socialite", | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "xyxu", | ||
"email": "[email protected]" | ||
}], | ||
"require": { | ||
"php": ">=5.5.0", | ||
"socialiteproviders/manager": "~1.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SocialiteProviders\\Weixin\\": "src/" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\Weixin; | ||
|
||
use Laravel\Socialite\Two\AbstractProvider; | ||
use Laravel\Socialite\Two\ProviderInterface; | ||
use Laravel\Socialite\Two\User; | ||
|
||
class Provider extends AbstractProvider implements ProviderInterface | ||
{ | ||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected $openId; | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected $scopes = ['snsapi_login']; | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function getAuthUrl($state) | ||
{ | ||
return $this->buildAuthUrlFromBase('https://open.weixin.qq.com/connect/oauth2/authorize', $state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function buildAuthUrlFromBase($url, $state) | ||
{ | ||
$session = $this->request->getSession(); | ||
|
||
$query = http_build_query($this->getCodeFields($state), '', '&', $this->encodingType); | ||
|
||
return $url.'?'.$query.'#wechat_redirect'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function getCodeFields($state = null) | ||
{ | ||
return [ | ||
'appid' => $this->clientId, 'redirect_uri' => $this->redirectUrl, | ||
'response_type' => 'code', | ||
'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator), | ||
'state' => $state, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function getTokenUrl() | ||
{ | ||
return 'https://api.weixin.qq.com/sns/oauth2/access_token'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function getUserByToken($token) | ||
{ | ||
$response = $this->getHttpClient()->get('https://api.weixin.qq.com/sns/userinfo', [ | ||
'query' => [ | ||
'access_token' => $token, | ||
'openid' => $this->openId, | ||
'lang' => 'zh_CN', | ||
], | ||
]); | ||
|
||
return json_decode($response->getBody(), true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function mapUserToObject(array $user) | ||
{ | ||
return (new User())->setRaw($user)->map([ | ||
'id' => $user['openid'], 'nickname' => $user['nickname'], | ||
'avatar' => $user['headimgurl'], 'name' => null, 'email' => null, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function getTokenFields($code) | ||
{ | ||
return [ | ||
'appid' => $this->clientId, 'secret' => $this->clientSecret, | ||
'code' => $code, 'grant_type' => 'authorization_code', | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
public function getAccessToken($code) | ||
{ | ||
$response = $this->getHttpClient()->get($this->getTokenUrl(), [ | ||
'query' => $this->getTokenFields($code), | ||
]); | ||
|
||
return $this->parseAccessToken($response->getBody()->getContents()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function parseAccessToken($body) | ||
{ | ||
$jsonArray = json_decode($body, true); | ||
$this->openId = $jsonArray['openid']; | ||
|
||
return $jsonArray['access_token']; | ||
} | ||
|
||
/** | ||
* @param mixed $response | ||
* | ||
* @return string | ||
*/ | ||
protected function removeCallback($response) | ||
{ | ||
if (strpos($response, 'callback') !== false) { | ||
$lpos = strpos($response, '('); | ||
$rpos = strrpos($response, ')'); | ||
$response = substr($response, $lpos + 1, $rpos - $lpos - 1); | ||
} | ||
|
||
return $response; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\Weixin; | ||
|
||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class WeixinExtendSocialite | ||
{ | ||
/** | ||
* Register the provider. | ||
* | ||
* @param \SocialiteProviders\Manager\SocialiteWasCalled $socialiteWasCalled | ||
*/ | ||
public function handle(SocialiteWasCalled $socialiteWasCalled) | ||
{ | ||
$socialiteWasCalled->extendSocialite( | ||
'weixin', __NAMESPACE__.'\Provider' | ||
); | ||
} | ||
} |