diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5070a67 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +/debug/ +composer.lock \ No newline at end of file diff --git a/IAuth.php b/IAuth.php new file mode 100644 index 0000000..4c50592 --- /dev/null +++ b/IAuth.php @@ -0,0 +1,16 @@ + + */ +interface IAuth { + + /** + * get User Info + * @return [] + */ + public function getUserInfo(); +} diff --git a/QqAuth.php b/QqAuth.php new file mode 100644 index 0000000..2398d53 --- /dev/null +++ b/QqAuth.php @@ -0,0 +1,79 @@ + + */ +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); + } + +} diff --git a/README.md b/README.md index 042b0e0..1b213c8 100644 --- a/README.md +++ b/README.md @@ -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 + ['site/auth'], + 'popupMode' => false, +]) +?> +``` diff --git a/WeiboAuth.php b/WeiboAuth.php new file mode 100644 index 0000000..f13d8ef --- /dev/null +++ b/WeiboAuth.php @@ -0,0 +1,52 @@ + + */ +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, + ]; + } + +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ea14697 --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "xj/yii2-oauth", + "description": "yii2-oauth", + "license": "BSD-3-Clause", + "authors": [ + { + "name": "xjflyttp", + "email": "xjflyttp@gmail.com" + } + ], + "require": { + "yiisoft/yii2": "*", + "yiisoft/yii2-authclient": "*" + }, + "autoload": { + "psr-4": { + "xj\\oauth\\": "" + } + } +}