ciricihq / cake-oauth2-client
该包的最新版本(dev-master)没有可用的许可证信息。
CakePHP 的 OAuth2Client 插件
dev-master
2015-11-20 12:24 UTC
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-08 07:41:34 UTC
README
此插件为您的 cake3 项目提供 OAuth2 密码授权场景。
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
composer require ciricihq/cake-oauth2-client
配置
此插件需要以下两个配置参数块来满足您的系统要求
Configure::write('App.OAuth2Client.routes', [ 'base_uri' => 'http://your-api.url', // The OAuth2 API endpoint 'access_token_path' => '/oauth/v2/token', // The access token url relative to base_uri 'refresh_token_path' => '/oauth/v2/token', // The refresh token url relative to base_uri ]);
Configure::write('App.OAuth2Client.keys', [ 'client_id' => 'your-client-id', // The client id needed to request the access token 'client_secret' => 'your-client-secret', // The client secret needed to request the access token ]);
您可以在 App 数组中添加到您的 app.php 配置文件中。
'App' => [ ... ... 'OAuth2Client' => [ 'routes' => [ 'base_uri' => 'http://your-awesome-oauth-api-endpoint.url' 'access_token_path' => '/oauth/v2/token', 'refresh_token_path' => '/oauth/v2/token', ], 'keys' => [ 'client_id' => 'you_client_provided_id', 'client_secret' => 'you_client_provided_secret' ] ] ],
要启用插件,请在您的 bootstrap.php 中添加以下行
Plugin::load('OAuth2Client', ['bootstrap' => true, 'routes' => true]);
在您的 AppController.php 中,您应该有以下代码以访问您生成的 AccessToken。
public function initialize() { parent::initialize(); $this->loadComponent('Flash'); $this->loadComponent('Auth', [ 'loginAction' => [ 'controller' => 'AuthController', 'action' => 'login' ], 'loginRedirect' => [ 'plugin' => 'OAuth2Client', 'controller' => 'Auth', 'action' => 'login' ], 'authenticate' => [ 'OAuth2Client.OAuth2' ], 'authorize' => ['Controller'] ]); } public function beforeFilter(Event $event) { $authUser = $this->Auth->user(); // Here you are setting AccessToken, RefresToken and ExpiresIn to all the controllers and views in order to handle later // you can set cookies or whatever you need $this->set('authUser', $authUser); $this->set('access_token', $authUser['access_token']); $this->set('refresh_token', $authUser['refresh_token']); $this->set('token_expires', $authUser['expires_in']); } public function isAuthorized() { $user = $this->Auth->user(); if (isset($user['access_token'])) { return true; } return false; }