前奏
系统:Ubuntu
语言:PHP7
框架:YAF
OAuth2.0:bshaffer/oauth2-server-php
OAuth2.0 有很多开源代码库
Github 排名前两位
thephpleague/oauth2-server
bshaffer/oauth2-server-php
本文使用的是第二个:bshaffer。原因:使用简单,可以很快上手,文档齐全,功能完善。
引入 OAuth2.0 的 Server 端源代码
编辑 Composer.json 文件
{
"require": {
"bshaffer/oauth2-server-php" : "v1.10.0"
}
}
yaf框架结构
├── application
│ └── modules
│ └── User
│ ├── controllers
│ │ └── Oauth.php
│ └── views
│ └── oauth
│ ├── authorize.php
│ ├── auth.php
│ ├── index.php
│ └── resource.php
├── Bootstrap.php
├── cli
├── composer.json
├── composer.lock
├── composer.phar
├── conf
├── docs
├── public
└── vendor
Yaf 框架中,在 Bootstrap.php 文件中自动加载 OAuth2.0
public function _initLoader(Yaf_Dispatcher $dispatcher)
{
include(APP_PATH . '/vendor/autoload.php');
}
新建一个 Controller 文件:Oauth.php,在里面建立几个 Action
AuthorizeAction() 服务端:提供授权
TokenAction() 服务端:提供Token
ResourceAction() 服务端:提供资源
IndexAction() 客户端:模拟第三方接入
_server() 服务端:初始化服务器相关,如:存储,这里采用mysql
命名空间
use OAuth2\Server;
use OAuth2\Storage\Pdo;
use OAuth2\GrantType\AuthorizationCode;
use OAuth2\GrantType\ClientCredentials;
use OAuth2\GrantType\UserCredentials;
use OAuth2\Request;
use OAuth2\Response;
_server() 函数代码
private function _server()
{
$dbParams = array(
'dsn' => 'mysql:host=127.0.0.1;port=