[官方demo](https://github.com/bshaffer/oauth2-demo-php)
[文檔地址](http://bshaffer.github.io/oauth2-server-php-docs/cookbook/)
```
git clone git@github.com:bshaffer/oauth2-server-php.git
```
~~~php
require_once('/path/to/oauth2-server-php/src/OAuth2/Autoloader.php');
OAuth2\Autoloader::register();
~~~
或者composer安裝
```
composer require bshaffer/oauth2-server-php "^1.10"
```
新建OAuth數據庫auth2,并執行如下建表sql
```
CREATE TABLE oauth_clients (
client_id VARCHAR(80) NOT NULL,
client_secret VARCHAR(80),
redirect_uri VARCHAR(2000),
grant_types VARCHAR(80),
scope VARCHAR(4000),
user_id VARCHAR(80),
PRIMARY KEY (client_id)
);
CREATE TABLE oauth_access_tokens (
access_token VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
PRIMARY KEY (access_token)
);
CREATE TABLE oauth_authorization_codes (
authorization_code VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
redirect_uri VARCHAR(2000),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
id_token VARCHAR(1000),
PRIMARY KEY (authorization_code)
);
CREATE TABLE oauth_refresh_tokens (
refresh_token VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
PRIMARY KEY (refresh_token)
);
CREATE TABLE oauth_users (
username VARCHAR(80),
password VARCHAR(80),
first_name VARCHAR(80),
last_name VARCHAR(80),
email VARCHAR(80),
email_verified BOOLEAN,
scope VARCHAR(4000)
);
CREATE TABLE oauth_scopes (
scope VARCHAR(80) NOT NULL,
is_default BOOLEAN,
PRIMARY KEY (scope)
);
CREATE TABLE oauth_jwt (
client_id VARCHAR(80) NOT NULL,
subject VARCHAR(80),
public_key VARCHAR(2000) NOT NULL
);
```
插入一條測試
~~~sql
INSERT INTO oauth_clients (client_id, client_secret, redirect_uri) VALUES ("testclient", "testpass", "http://fake/");
~~~
### 根目錄新建`authorize.php`授權文件(server.php)
```
require_once __DIR__ . '/server.php';
$request = OAuth2\Request::createFromGlobals();
$response = new OAuth2\Response();
// validate the authorize request
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
// display an authorization form
if (empty($_POST)) {
exit('Do You Authorize TestClient?');
}
// print the authorization code if the user has authorized your client
$is_authorized = ($_POST['authorized'] === 'yes');
$user_id = 1;
$server->handleAuthorizeRequest($request, $response, $is_authorized, $user_id);
if ($is_authorized) {
// this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=') + 5, 40);
//exit("SUCCESS AND DO redirect_uri! Authorization Code: $code");
}
$response->send();
```
在瀏覽器中打開如下連接,然后點擊yes按鈕
```
http://www.xxx.com/authorize.php?response_type=code&client_id=testclient&state=xyz
```
獲取的 authorization_code:243cd370e035881d0cc5bfb421ed7d5919f99d1f
### 新建OAuth 2.0服務加載及token生成文件index.php并配置好數據庫連接信息
```
require_once('oauth2-server-php/src/OAuth2/Autoloader.php');
OAuth2\Autoloader::register();
$dsn = 'mysql:dbname=auth2;host=192.168.200.7';
$username = 'root';
$password = 'admin';
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
$server = new OAuth2\Server($storage);
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage)); //or any grant type you like!
$server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
```
9、獲取access_token
```
curl -u testclient:testpass http://www.xxx.com/index.php -d 'grant_type=authorization_code&code=243cd370e035881d0cc5bfb421ed7d5919f99d1f'
```
返回結果:
```
{
"access_token": "fe2617e4034cb25044f6f22a7b2356ca6161c8a5",
"expires_in": 3600,
"token_type": "Bearer",
"scope": null,
"refresh_token": "8f9621f8fa9b6a857dffea7a67f4edc0fbdd8f8c"
}
```
- 目錄結構與基礎
- 修改數據后頁面無變化
- 防跨目錄設置
- input
- 系統目錄
- 自動生成的文件以及目錄
- 類自動加載
- url生成
- 數據增刪改查
- 增加數據
- 數據更新
- 數據刪除
- 數據查詢
- 架構
- 生命周期
- 入口文件
- URL訪問規則
- 配置
- 默認慣例配置配置
- 初始應用配置
- 路由
- 域名路由
- URL生成
- 數據庫操作
- 方法列表
- 連接數據庫
- 分布式數據庫
- 查詢構造器
- 查詢數據
- 添加數據
- 更新數據
- 刪除數據
- 查詢語法
- 聚合查詢(統計)
- 時間查詢
- 高級查詢
- 視圖查詢
- 子查詢
- 輔助查詢之鏈式操作
- where
- table
- alias
- field
- order
- limit
- page
- group
- having
- join
- union
- distinct
- lock
- cache
- comment
- fetchSql
- force
- bind
- partition
- strict
- failException
- sequence(pgsql專用)
- 查詢事件
- 事務操作
- 監聽SQL
- 存儲過程
- 數據集
- 控制器
- 跳轉和重定向
- 空控制器和空操作
- 分層控制器
- Rest控制器
- 資源控制器
- 自動定位控制器
- tp3的增刪改查
- 方法注入
- 模型
- 屬性方法一覽
- 類方法詳解
- Model
- 調用model不存在的屬性
- 調用model中不存在的方法
- 調用model中不存在的靜態方法
- hasOne
- belongsTo
- hasMany {Relation}
- belongsToMany
- hasManyThrough
- morphMany
- morphOne
- morphTo
- ::hasWhere {Query}
- ::has
- relationCount
- data 【model】
- setInc {integer|true}
- setDec {integer|true}
- save {integer | false}
- saveAll {array}
- delete {integer}
- ::get 查詢單條數據 {Model}
- ::all 查詢多條數據{Model [ ]}
- ::create 新增單條數據 {Model}
- ::update 更新單條數據 {Model}
- ::destroy {integer}
- ::scope {Query}
- getAttr {mixed}
- xxx
- append
- appendRelationAttr
- hidden
- visible
- except
- readonly
- auto
- together
- allowField
- isUpdate
- validate
- toCollection
- toJson
- toArray
- 定義
- 新增
- 更新
- 查詢
- 刪除
- 聚合
- 獲取器
- 修改器
- 時間戳
- 只讀字段
- 軟刪除
- 類型轉換
- 數據完成
- 查詢范圍
- 模型分層
- 數組訪問和轉換
- JSON序列化
- 事件
- 關聯
- 一對一關聯
- 主表一對一關聯
- 從表一對一關聯(相對關聯)
- 一對多關聯
- 主表定義一對多關聯
- 從表定義一對多關聯
- 遠程一對多
- 多對多關聯
- 多態關聯
- 動態屬性
- 關聯預載入with()
- 關聯統計
- N+1查詢
- 聚合模型
- Model方法集合
- 表單驗證
- 驗證器
- 驗證規則
- 錯誤信息
- 驗證場景
- 控制器驗證
- 模型驗證
- 內置規則
- 靜態調用
- 表單令牌
- Token身份令牌
- 視圖
- 模版
- 變量輸出
- 函數輸出
- Request請求參數
- 模板注釋及原樣輸出
- 三元運算
- 內置標簽
- 模板繼承
- 模板布局
- 日志
- 日志初始化
- 日志驅動
- 日志寫入
- 獨立日志
- 日志清空
- 寫入授權
- 自定義日志
- 錯誤和調試
- 異常
- php系統異常及thinkphp5異常機制
- 異常處理
- 拋出異常
- 異常封裝
- resful
- 404頁面
- 調試模式
- Trace調試
- SQL調試
- 變量調試
- 性能調試
- 遠程調試
- 安全
- 輸入安全
- 數據庫安全
- 上傳安全
- 其它安全建議
- xss過濾
- 擴展
- 函數
- 類庫
- 行為
- 驅動
- Composer包
- Time
- 數據庫遷移工具
- Workerman
- MongoDb
- htmlpurifier XSS過濾
- 新浪SAE
- oauth2.0
- 命令行及生成文件
- 系統現成命令
- 創建類庫文件
- 生成類庫映射文件
- 生成路由緩存
- 清除緩存文件
- 生成配置緩存文件
- 生成數據表字段緩存
- 自定義命令行
- 開始
- 調用命令
- 雜項
- 助手函數
- URL重寫
- 緩存
- 緩存總結
- Session
- Cookie
- 多語言
- 分頁
- 上傳
- 驗證碼
- 圖像處理
- 文件處理
- 單元測試
- 自定義表單令牌