Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加腾讯QQ/微信/微博客户端 #102

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,9 @@ Yii Framework 2 authclient extension Change Log
- Bug #37: Fixed `\yii\authclient\widgets\AuthChoice` overrides any `<a>` tag click behavior between `begin()` and `end()` methods (klimov-paul)
- Enh #31: Allow to disable automatic 'refresh access token' requests (klimov-paul)
- Enh #58: Added support for user attribute request params setup for Twitter (umanamente, klimov-paul)
- Add : Added Tencent QQ client (xutongle)
- Add : Added WeChat client (xutongle)
- Add : Added Sina WeiBo client (xutongle)


2.0.5 September 23, 2015
Expand Down
9 changes: 9 additions & 0 deletions assets/authchoice.css
Expand Up @@ -46,6 +46,15 @@
.auth-icon.live {
background-position: 0 -372px;
}
.auth-icon.qq {
background-position: 0 -406px
}
.auth-icon.weibo {
background-position: 0 -438px
}
.auth-icon.wechat {
background-position: 0 -470px
}

.auth-link:hover .auth-icon i,
.auth-link:focus .auth-icon i {
Expand Down
Binary file modified assets/authchoice.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
194 changes: 194 additions & 0 deletions clients/QQ.php
@@ -0,0 +1,194 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

namespace yii\authclient;

use Yii;
use yii\authclient\OAuth2;
use yii\web\HttpException;


/**
* QQ allows authentication via QQ OAuth.
*
* In order to use QQ OAuth you must register your application at <http://connect.qq.com/>.
*
* Example application configuration:
*
* ~~~
* 'components' => [
* 'authClientCollection' => [
* 'class' => 'yii\authclient\Collection',
* 'clients' => [
* 'qq' => [
* 'class' => 'yii\authclient\QQ',
* 'clientId' => 'qq_appid',
* 'clientSecret' => 'qq_appkey',
* ],
* ],
* ]
* ...
* ]
* ~~~
*
* @see http://connect.qq.com/
* @see http://wiki.connect.qq.com/
*
* @author Xu Tongle <xutongle@gmail.com>
*/
class QQ extends OAuth2
{

/**
* @inheritdoc
*/
public $authUrl = 'https://graph.qq.com/oauth2.0/authorize';
/**
* @inheritdoc
*/
public $tokenUrl = 'https://graph.qq.com/oauth2.0/token';
/**
* @inheritdoc
*/
public $apiBaseUrl = 'https://graph.qq.com';

/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->scope === null) {
$this->scope = implode(',', [
'get_user_info',
]);
}
}

/**
* @inheritdoc
*/
public function buildAuthUrl(array $params = [])
{
$authState = $this->generateAuthState();
$this->setState('authState', $authState);
$params['state'] = $authState;
return parent::buildAuthUrl($params);
}

/**
* @inheritdoc
*/
public function fetchAccessToken($authCode, array $params = [])
{
$authState = $this->getState('authState');
if (!isset($_REQUEST['state']) || empty($authState) || strcmp($_REQUEST['state'], $authState) !== 0) {
throw new HttpException(400, 'Invalid auth state parameter.');
} else {
$this->removeState('authState');
}

return parent::fetchAccessToken($authCode, $params);

}

/**
* @inheritdoc
*/
protected function defaultNormalizeUserAttributeMap()
{
return [
'username' => 'nickname',
];
}

/**
* @inheritdoc
*/
protected function initUserAttributes()
{
$user = $this->api('oauth2.0/me', 'GET');
if ( isset($user['error']) ) {
throw new HttpException(400, $user['error']. ':'. $user['error_description']);
}
$userAttributes = $this->api(
"user/get_user_info",
'GET',
[
'oauth_consumer_key' => $user['client_id'],
'openid' => $user['openid'],
]
);
$userAttributes['id'] = $user['openid'];
return $userAttributes;
}

/**
* @inheritdoc
*/
protected function processResponse($rawResponse, $contentType = self::CONTENT_TYPE_AUTO)
{
if ($contentType === self::CONTENT_TYPE_AUTO && strpos($rawResponse, "callback(") === 0) {
$count = 0;
$jsonData = preg_replace('/^callback\(\s*(\\{.*\\})\s*\);$/is', '\1', $rawResponse, 1, $count);
if ($count === 1) {
$rawResponse = $jsonData;
$contentType = self::CONTENT_TYPE_JSON;
}
}
return parent::processResponse($rawResponse, $contentType);
}

/**
* Generates the auth state value.
* @return string auth state value.
*/
protected function generateAuthState()
{
return sha1(uniqid(get_class($this), true));
}

/**
* @inheritdoc
*/
protected function defaultReturnUrl()
{
$params = $_GET;
unset($params['code']);
unset($params['state']);
$params[0] = Yii::$app->controller->getRoute();

return Yii::$app->getUrlManager()->createAbsoluteUrl($params);
}

/**
* @inheritdoc
*/
protected function defaultName()
{
return 'qq';
}

/**
* @inheritdoc
*/
protected function defaultTitle()
{
return 'QQ';
}

/**
* @inheritdoc
*/
protected function defaultViewOptions()
{
return [
'popupWidth' => 800,
'popupHeight' => 500,
];
}
}