Skip to content

Commit

Permalink
开放平台基础版 - OAuthDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
tianye committed Jul 25, 2016
0 parents commit c56dbe3
Show file tree
Hide file tree
Showing 31 changed files with 1,861 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
1 change: 1 addition & 0 deletions cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#文件序列化存储 缓存数据 可以增加 Core CacheDriver
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "tianye/open-oauth",
"description": "open-oauth",
"authors": [
{
"name": "tianye",
"email": "tianye@iwork365.com"
}
],
"require": {
"php": ">=5.4",
"guzzlehttp/guzzle": "^5.3"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"OpenOauth\\": "src/"
}
}
}
1 change: 1 addition & 0 deletions database/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#文件序列化存储 永久数据 可以增加 Core DatabaseDriver
15 changes: 15 additions & 0 deletions demo/authorized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
//生成 授权页面

//http://applist-test-open.weflame.com/authorized.php

include '../vendor/autoload.php';

use OpenOauth\Authorized;
use OpenOauth\Core\Config;

$config = new Config();
$config->init(['component_app_id' => '第三方平台appId', 'component_app_secret' => '第三方平台appSecret', 'component_app_token' => '第三方平台appToken', 'component_app_key' => '第三方平台appKey']);

$authorized = new Authorized();
$authorized->getAuthHTML('index.php');
12 changes: 12 additions & 0 deletions demo/callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
//接受服务号回调信息 用于把第三方开放平台测试版 转换为 正式版

include '../vendor/autoload.php';

use OpenOauth\core\Tools;

$record = new Tools();
$record->dataRecodes('callback_server', $_SERVER, 'callback');
$record->dataRecodes('callback_file_get_contents', file_get_contents('php://input'), 'callback');
$record->dataRecodes('callback_get', $_GET, 'callback');
$record->dataRecodes('callback_post', $_POST, 'callback');
43 changes: 43 additions & 0 deletions demo/notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
//接受第三方平台信息

include '../vendor/autoload.php';

use OpenOauth\Core\Config;
use OpenOauth\Decryption;
use OpenOauth\Core\Tools;
use OpenOauth\NotifyProcessing;

$config = new Config();
$config->init(['component_app_id' => '第三方平台appId', 'component_app_secret' => '第三方平台appSecret', 'component_app_token' => '第三方平台appToken', 'component_app_key' => '第三方平台appKey']);

$decryption = new Decryption();
$xml_array = $decryption->decryptionNoticeXML();

Tools::dataRecodes('xml_array', $xml_array, 'notice');

$notify_processing = new NotifyProcessing();
switch ($xml_array['InfoType']) {
case 'component_verify_ticket':
//每10分钟 接收一次微信推送过来 当前 第三方平台的 ticket 并且缓存
$notify_processing->componentVerifyTicket($xml_array);
exit('SUCCESS');
break;
case 'authorized':
//服务号授权
$notify_processing->Authorized($xml_array);
exit('SUCCESS');
break;
case 'unauthorized':
//服务号取消授权
$notify_processing->UnAuthorized($xml_array);
exit('SUCCESS');
break;
case 'updateauthorized':
//服务号更新授权
$notify_processing->UpdateAuthorized($xml_array);
exit('SUCCESS');
break;
}

exit('FAIL');
21 changes: 21 additions & 0 deletions demo/oauth_demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

include '../vendor/autoload.php';

use OpenOauth\Core\Config;
use OpenOauth\OpenAuth;

$config = new Config();
$config->init(['component_app_id' => '第三方平台appId', 'component_app_secret' => '第三方平台appSecret', 'component_app_token' => '第三方平台appToken', 'component_app_key' => '第三方平台appKey']);

$open_auth = new OpenAuth('授权的服务号appId');

//snsapi_base 小授权 snsapi_userinfo 大授权
$user = $open_auth->authorize(null, 'snsapi_userinfo');

var_dump($user);

//获取大授权 用户信息
$user_info = $open_auth->getUserInfo($user['access_token'], $user['openid']);

var_dump($user_info);
149 changes: 149 additions & 0 deletions src/Authorized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace OpenOauth;

use OpenOauth\Core\Core;
use OpenOauth\Core\Http\Http;

class Authorized extends Core
{
const GET_API_QUERY_AUTH = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth'; //使用授权码换取公众号的接口调用凭据和授权信息
const GET_API_AUTHORIZER_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token'; //获取(刷新)授权公众号的接口调用凭据(令牌)

/**
* @param $redirect_path
*/
function getAuthHTML($redirect_path)
{
$component_app_id = $this->configs->component_app_id;
$pre_auth_code = $this->getComponentPreAuthCode();
$redirect_uri = $_SERVER['HTTP_HOST'] . '/' . $redirect_path;

$editorSrc = <<<HTML
<script language="JavaScript" type="text/javascript">
window.location.href="https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=$component_app_id&pre_auth_code=$pre_auth_code&redirect_uri=$redirect_uri";
</script>
HTML;
exit($editorSrc);
}

/**
* 使用授权码换取公众号的接口调用凭据和授权信息
*
* @param string $authorizer_app_id
*
* @return array|bool|null|string|void
*/
function getApiQueryAuth($authorizer_app_id = '')
{
$time = time();

$authorization_info_key = 'authorized:' . $this->configs->component_app_id . ':' . $authorizer_app_id;
$query_auth_key = 'query_auth:' . $this->configs->component_app_id . ':' . $authorizer_app_id;

$query_auth_info = parent::$databaseDriver->_get($query_auth_key);
//如果存在数据
if (!empty($query_auth_info)) {

//没超时 返回数据
if ($query_auth_info['expired_time'] >= ($time - 1000) && $query_auth_info['authorization_state'] == 'authorized') {
return $query_auth_info;
} else {
//如果超时了 获取新的 access_token 和 新的 刷新令牌 refresh_token
$api_authorizer_token = $this->getApiAuthorizerToken($query_auth_info['authorization_info']['authorizer_appid'], $query_auth_info['authorization_info']['authorizer_refresh_token']);
if (!empty($api_authorizer_token)) {
$query_auth_info['authorization_info']['authorizer_access_token'] = $api_authorizer_token['authorizer_access_token'];
$query_auth_info['authorization_info']['authorizer_refresh_token'] = $api_authorizer_token['authorizer_refresh_token'];
$query_auth_info['authorization_info']['expires_in'] = $api_authorizer_token['expires_in'];
$query_auth_info['expired_time'] = $time + $api_authorizer_token['expires_in'];
$query_auth_info['authorization_state'] = 'authorized';

parent::$databaseDriver->_set($query_auth_key, $query_auth_info);

return $query_auth_info;
}
}
}

$authorization_info = parent::$databaseDriver->_get($authorization_info_key);

$query_data = http_build_query(['component_access_token' => $this->getComponentAccessToken()]);

if ($authorization_info['AuthorizationCodeExpiredTime'] <= $time) {
$this->setError('授权Code超时');

return false;
}
$request_data = [
'component_appid' => $authorization_info['AppId'],
'authorization_code' => $authorization_info['AuthorizationCode'],
];

$response_data = Http::_post(self::GET_API_QUERY_AUTH . '?' . $query_data, $request_data);

if (!$response_data) {
$this->setError(Http::$error);

return false;
}

$response_data['authorization_state'] = 'authorized';
$response_data['expired_time'] = $time + $response_data['authorization_info']['expires_in'];
parent::$databaseDriver->_set($query_auth_key, $response_data);

return $response_data;
}

/**
* 获取(刷新)授权公众号的接口调用凭据(令牌)
*
* @param string $authorizer_app_id 公众号app_id
* @param string $authorizer_refresh_token 刷新TOKEN的 authorizer_refresh_token
*
* @return array authorization_info
*/
private function getApiAuthorizerToken($authorizer_app_id = '', $authorizer_refresh_token = '')
{
$query_data = http_build_query(['component_access_token' => $this->getComponentAccessToken()]);
$request_data = [
'component_appid' => $this->configs->component_app_id,
'authorizer_appid' => $authorizer_app_id,
'authorizer_refresh_token' => $authorizer_refresh_token,
];

$response_data = Http::_post(self::GET_API_AUTHORIZER_TOKEN_URL . '?' . $query_data, $request_data);

if (!$response_data) {
$this->setError(Http::$error);

return false;
}

return $response_data;
}

/**
* 获取授权服务号 AccessToken
*
* @param string $authorizer_app_id
*
* @return bool
*/
public function getAuthorizerAccessToken($authorizer_app_id = '')
{
$query_auth_info = $this->getApiQueryAuth($authorizer_app_id);

if (!empty($query_auth_info)) {

if ($query_auth_info['authorization_state'] == 'authorized') {
return $query_auth_info['authorization_info']['authorizer_access_token'];
} else {
$this->setError('已经取消授权的服务号:' . $query_auth_info['authorization_info']['authorizer_appid']);

return false;
}
}

return false;
}
}
40 changes: 40 additions & 0 deletions src/Core/CacheDriver/BaseDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace OpenOauth\Core\CacheDriver;

/**
* 缓存基类.
*
*/
abstract class BaseDriver
{
protected $cacheDir; // 缓存路径

/**
* 初始化时设置缓存路径.
*
* @param string $dir 路径信息
*/
public function __construct($dir)
{
$this->cacheDir = $dir;
}

/**
* 根据缓存名获取缓存内容.
*
* @param string $name 缓存名
*/
abstract public function _get($name);

/**
* 根据缓存名 设置缓存值和超时时间.
*
* @param string $name 缓存名
* @param string|void $value 缓存值
* @param int $expires 超时时间
*
* @return boolean;
*/
abstract public function _set($name, $value, $expires);
}
Loading

0 comments on commit c56dbe3

Please sign in to comment.