-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 03962e2
Showing
6 changed files
with
172 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,16 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 2 |
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,3 @@ | ||
.idea | ||
vendor/ | ||
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Socialite | ||
|
||
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,16 @@ | ||
# Weibo Provider | ||
|
||
## Installation | ||
|
||
``` | ||
composer require socialite-manager/weibo-provider | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
use Socialite\Provider\WeiboProvider; | ||
use Socialite\Socialite; | ||
|
||
Socialite::driver(WeiboProvider::class, $config); | ||
``` |
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,26 @@ | ||
{ | ||
"name": "socialite-manager/weibo-provider", | ||
"description": "Weibo provider", | ||
"license": "MIT", | ||
"require": { | ||
"socialite-manager/socialite": "^1.0" | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^0.9.2", | ||
"squizlabs/php_codesniffer": "^3.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Socialite\\Provider\\": "src/" | ||
} | ||
}, | ||
"scripts": { | ||
"check": [ | ||
"@cs-check", | ||
"@analyse" | ||
], | ||
"cs-check": "phpcs --standard=PSR2 --colors -p ./src", | ||
"cs-fix": "phpcbf --standard=PSR2 --colors ./src", | ||
"analyse": "phpstan analyse -l max 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,90 @@ | ||
<?php | ||
|
||
namespace Socialite\Provider; | ||
|
||
use Socialite\Two\AbstractProvider; | ||
use Socialite\Two\User; | ||
|
||
class WeiboProvider extends AbstractProvider | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAuthUrl(string $state) | ||
{ | ||
return $this->buildAuthUrlFromBase('https://api.weibo.com/oauth2/authorize', $state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenUrl() | ||
{ | ||
return 'https://api.weibo.com/oauth2/access_token'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getUserByToken(string $token) | ||
{ | ||
$response = $this->getHttpClient()->get('https://api.weibo.com/2/users/show.json', [ | ||
'query' => [ | ||
'access_token' => $token, | ||
'uid' => $this->getUid($token), | ||
], | ||
]); | ||
return json_decode($this->removeCallback($response->getBody()->getContents()), true); | ||
} | ||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function mapUserToObject(array $user) | ||
{ | ||
return (new User())->setRaw($user)->map([ | ||
'id' => $user['idstr'], | ||
'nickname' => $user['name'], | ||
'avatar' => $user['avatar_large'], | ||
'name' => null, | ||
'email' => null, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenFields(string $code) | ||
{ | ||
return array_merge(parent::getTokenFields($code), [ | ||
'grant_type' => 'authorization_code', | ||
]); | ||
} | ||
|
||
/** | ||
* @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; | ||
} | ||
|
||
/** | ||
* @param $token | ||
* @return string | ||
*/ | ||
protected function getUid($token) | ||
{ | ||
$response = $this->getHttpClient()->get('https://api.weibo.com/2/account/get_uid.json', [ | ||
'query' => ['access_token' => $token], | ||
]); | ||
return json_decode($response->getBody(), true)['uid']; | ||
} | ||
} |