Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
xjflyttp committed Aug 8, 2014
1 parent 2858202 commit 65e5ef9
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
/debug/
composer.lock
16 changes: 16 additions & 0 deletions IAuth.php
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();
}
79 changes: 79 additions & 0 deletions QqAuth.php
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);
}

}
75 changes: 74 additions & 1 deletion README.md
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,
])
?>
```
52 changes: 52 additions & 0 deletions WeiboAuth.php
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,
];
}

}
20 changes: 20 additions & 0 deletions composer.json
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\\": ""
}
}
}

0 comments on commit 65e5ef9

Please sign in to comment.