这几天一直在搞OAuth2.0的东西,写SDK啥的,为了更加深入的了解服务端的OAuth验证机制,就自己动手搭了个php下OAuth的环境,并且将它移植到了自己比较熟的tp框架里。
废话不少说,开动。
其实网上是有OAuth2.0的php版本的。
你可以在http://code.google.com/p/oauth2-php/ 找到源代码,上面实现了PDO和MongoDB的数据模式。这里我也是基于这些代码在TP中进行整合的。
好,这里我们可以把下载下来的包解压,把Lib下的OAuth.inc改名为OAuth2.class.php后放到tp核心包下的目录下:
Tree代码
/Extend/Library/ORG/OAuth/OAuth2.class.php
接下来我们要继承这个类;
在这个目录下新建一个ThinkOAuth2.class.php文件:
Php代码
/**
* @category ORG
* @package ORG
* @author Leyteris
* @version 2012.3.16
*/
// OAUTH2_DB_DSN 数据库连接DSN
// OAUTH2_CODES_TABLE 服务器表名称
// OAUTH2_CLIENTS_TABLE 客户端表名称
// OAUTH2_TOKEN_TABLE 验证码表名称
import("ORG.OAuth.OAuth2");
class ThinkOAuth2 extends OAuth2 {
private $db;
private $table;
/**
* 构造
*/
public function __construct() {
parent::__construct();
$this -> db = Db::getInstance(C('OAUTH2_DB_DSN'));
$this -> table = array(
'auth_codes'=>C('OAUTH2_CODES_TABLE'),
'clients'=>C('OAUTH2_CLIENTS_TABLE'),
'tokens'=>C('OAUTH2_TOKEN_TABLE')
);
}
/**
* 析构
*/
function __destruct() {
$this->db = NULL; // Release db connection
}
private function handleException($e) {
echo "Database error: " . $e->getMessage();
exit;
}
/**
*
* 增加client
* @param string $client_id
* @param string $client_secret
* @param string $redirect_uri
*/
public function addClient($client_id, $client_secret, $redirect_uri) {
$time = time();
$sql = "INSERT INTO {$this -> table['clients']} ".
"(client_id, client_secret, redirect_uri, create_time) VALUES (\"{$client_id}\", \"{$client_secret}\", \"{$redirect_uri}\",\"{$time}\")";
$this -> db -> execute($sql);
}