forked from xjflyttp/yii2-oauth
-
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
Showing
6 changed files
with
244 additions
and
1 deletion.
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,3 @@ | ||
/vendor/ | ||
/debug/ | ||
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,16 @@ | ||
<?php | ||
|
||
namespace xj\oauth; | ||
|
||
/** | ||
* Sina Weibo OAuth | ||
* @author xjflyttp <[email protected]> | ||
*/ | ||
interface IAuth { | ||
|
||
/** | ||
* get User Info | ||
* @return [] | ||
*/ | ||
public function getUserInfo(); | ||
} |
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,79 @@ | ||
<?php | ||
|
||
namespace xj\oauth; | ||
|
||
use yii\authclient\OAuth2; | ||
|
||
/** | ||
* QQ OAuth | ||
* @author xjflyttp <[email protected]> | ||
*/ | ||
class QqAuth extends OAuth2 implements IAuth { | ||
|
||
public $authUrl = 'https://graph.qq.com/oauth2.0/authorize'; | ||
public $tokenUrl = 'https://graph.qq.com/oauth2.0/token'; | ||
public $apiBaseUrl = 'https://graph.qq.com'; | ||
|
||
public function init() { | ||
parent::init(); | ||
if ($this->scope === null) { | ||
$this->scope = implode(',', [ | ||
'get_user_info', | ||
]); | ||
} | ||
} | ||
|
||
protected function initUserAttributes() { | ||
return $this->api('oauth2.0/me', 'GET'); | ||
} | ||
|
||
/** | ||
* get UserInfo | ||
* @return [] | ||
* @see http://wiki.connect.qq.com/get_user_info | ||
*/ | ||
public function getUserInfo() { | ||
$openid = $this->getUserAttributes(); | ||
return $this->api("user/get_user_info", 'GET', [ | ||
'oauth_consumer_key' => $openid['client_id'], | ||
'openid' => $openid['openid' | ||
]]); | ||
} | ||
|
||
protected function defaultName() { | ||
return 'QQ'; | ||
} | ||
|
||
protected function defaultTitle() { | ||
return 'QQ'; | ||
} | ||
|
||
protected function defaultViewOptions() { | ||
return [ | ||
'popupWidth' => 800, | ||
'popupHeight' => 500, | ||
]; | ||
} | ||
|
||
/** | ||
* Processes raw response converting it to actual data. | ||
* @param string $rawResponse raw response. | ||
* @param string $contentType response content type. | ||
* @throws Exception on failure. | ||
* @return array actual response. | ||
*/ | ||
protected function processResponse($rawResponse, $contentType = self::CONTENT_TYPE_AUTO) { | ||
if ($contentType == self::CONTENT_TYPE_AUTO) { | ||
//jsonp to json | ||
if (strpos($rawResponse, "callback") === 0) { | ||
$lpos = strpos($rawResponse, "("); | ||
$rpos = strrpos($rawResponse, ")"); | ||
$rawResponse = substr($rawResponse, $lpos + 1, $rpos - $lpos - 1); | ||
$rawResponse = trim($rawResponse); | ||
$contentType = self::CONTENT_TYPE_JSON; | ||
} | ||
} | ||
return parent::processResponse($rawResponse, $contentType); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,4 +1,77 @@ | ||
yii2-oauth | ||
========== | ||
|
||
OAUTH QQ WEIBO | ||
OAUTH QQ|WEIBO | ||
|
||
composer.json | ||
----- | ||
```json | ||
"require": { | ||
"xj/yii2-oauth": "*" | ||
}, | ||
``` | ||
|
||
components configure | ||
------ | ||
```php | ||
'components' => [ | ||
'authClientCollection' => [ | ||
'class' => 'yii\authclient\Collection', | ||
'clients' => [ | ||
'qq' => [ | ||
'class' => 'xj\oauth\QqAuth', | ||
'clientId' => '111', | ||
'clientSecret' => '111', | ||
|
||
], | ||
'sina' => [ | ||
'class' => 'xj\oauth\SinaAuth', | ||
'clientId' => '111', | ||
'clientSecret' => '111', | ||
] | ||
] | ||
] | ||
... | ||
] | ||
``` | ||
|
||
Controller | ||
---------- | ||
```php | ||
class SiteController extends Controller | ||
{ | ||
public function actions() | ||
{ | ||
return [ | ||
'auth' => [ | ||
'class' => 'yii\authclient\AuthAction', | ||
'successCallback' => [$this, 'successCallback'], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Success Callback | ||
* @param QqAuth|WeiboAuth $client | ||
* @see http://wiki.connect.qq.com/get_user_info | ||
* @see http://stuff.cebe.cc/yii2docs/yii-authclient-authaction.html | ||
*/ | ||
public function successCallback($client) { | ||
$id = $client->getId(); // qq | sina | ||
$attributes = $client->getUserAttributes(); // basic info | ||
$userInfo = $client->getUserInfo(); // user extend info | ||
var_dump($id, $attributes, $userInfo); | ||
} | ||
} | ||
``` | ||
|
||
View | ||
----------- | ||
```php | ||
<?= | ||
yii\authclient\widgets\AuthChoice::widget([ | ||
'baseAuthUrl' => ['site/auth'], | ||
'popupMode' => false, | ||
]) | ||
?> | ||
``` |
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,52 @@ | ||
<?php | ||
|
||
namespace xj\oauth; | ||
|
||
use yii\authclient\OAuth2; | ||
|
||
/** | ||
* Sina Weibo OAuth | ||
* @author xjflyttp <[email protected]> | ||
*/ | ||
class WeiboAuth extends OAuth2 implements IAuth { | ||
|
||
public $authUrl = 'https://api.weibo.com/oauth2/authorize'; | ||
public $tokenUrl = 'https://api.weibo.com/oauth2/access_token'; | ||
public $apiBaseUrl = 'https://api.weibo.com'; | ||
|
||
/** | ||
* | ||
* @return [] | ||
* @see http://open.weibo.com/wiki/Oauth2/get_token_info | ||
* @see http://open.weibo.com/wiki/2/users/show | ||
*/ | ||
protected function initUserAttributes() { | ||
return $this->api('oauth2/get_token_info', 'POST'); | ||
} | ||
|
||
/** | ||
* get UserInfo | ||
* @return [] | ||
* @see http://open.weibo.com/wiki/2/users/show | ||
*/ | ||
public function getUserInfo() { | ||
$openid = $this->getUserAttributes(); | ||
return $this->api("2/users/show.json", 'GET', ['uid' => $openid['uid']]); | ||
} | ||
|
||
protected function defaultName() { | ||
return 'Weibo'; | ||
} | ||
|
||
protected function defaultTitle() { | ||
return 'Weibo'; | ||
} | ||
|
||
protected function defaultViewOptions() { | ||
return [ | ||
'popupWidth' => 800, | ||
'popupHeight' => 500, | ||
]; | ||
} | ||
|
||
} |
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 @@ | ||
{ | ||
"name": "xj/yii2-oauth", | ||
"description": "yii2-oauth", | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "xjflyttp", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "*", | ||
"yiisoft/yii2-authclient": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"xj\\oauth\\": "" | ||
} | ||
} | ||
} |