-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Class does not comply with psr-4 autoloading standard (#403)
- Loading branch information
0 parents
commit 3b16727
Showing
3 changed files
with
180 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,140 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\WeixinWeb; | ||
|
||
use Illuminate\Support\Arr; | ||
use SocialiteProviders\Manager\OAuth2\AbstractProvider; | ||
use SocialiteProviders\Manager\OAuth2\User; | ||
|
||
class Provider extends AbstractProvider | ||
{ | ||
/** | ||
* Unique Provider Identifier. | ||
*/ | ||
const IDENTIFIER = 'WEIXINWEB'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $openId; | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected $scopes = ['snsapi_login']; | ||
|
||
/** | ||
* set Open Id. | ||
* | ||
* @param string $openId | ||
*/ | ||
public function setOpenId($openId) | ||
{ | ||
$this->openId = $openId; | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function getAuthUrl($state) | ||
{ | ||
//return $this->buildAuthUrlFromBase('https://open.weixin.qq.com/connect/qrconnect', $state); | ||
return $this->buildAuthUrlFromBase($this->getConfig( | ||
'auth_base_uri', | ||
'https://open.weixin.qq.com/connect/qrconnect' | ||
), $state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected function buildAuthUrlFromBase($url, $state) | ||
{ | ||
$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' => Arr::get($user, 'openid'), | ||
'unionid' => Arr::get($user, 'unionid'), | ||
'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 getAccessTokenResponse($code) | ||
{ | ||
$response = $this->getHttpClient()->get($this->getTokenUrl(), [ | ||
'query' => $this->getTokenFields($code), | ||
]); | ||
|
||
$this->credentialsResponseBody = json_decode($response->getBody(), true); | ||
$this->openId = $this->credentialsResponseBody['openid']; | ||
|
||
//return $this->parseAccessToken($response->getBody()); | ||
return $this->credentialsResponseBody; | ||
} | ||
|
||
public static function additionalConfigKeys() | ||
{ | ||
return ['auth_base_uri']; | ||
} | ||
} |
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 @@ | ||
<?php | ||
|
||
namespace SocialiteProviders\WeixinWeb; | ||
|
||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class WeixinWebExtendSocialite | ||
{ | ||
/** | ||
* Register the provider. | ||
* | ||
* @param \SocialiteProviders\Manager\SocialiteWasCalled $socialiteWasCalled | ||
*/ | ||
public function handle(SocialiteWasCalled $socialiteWasCalled) | ||
{ | ||
$socialiteWasCalled->extendSocialite( | ||
'weixinweb', | ||
__NAMESPACE__.'\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,19 @@ | ||
{ | ||
"name": "socialiteproviders/weixin-web", | ||
"description": "Weixin-Web OAuth2 Provider for Laravel Socialite", | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "xyxu", | ||
"email": "[email protected]" | ||
}], | ||
"require": { | ||
"php": "^5.6 || ^7.0", | ||
"ext-json": "*", | ||
"socialiteproviders/manager": "~2.0 || ~3.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SocialiteProviders\\WeixinWeb\\": "" | ||
} | ||
} | ||
} |