suhaboncukcu/oauth2server

CakePHP 的 Oauth2Server 插件

安装: 8

依赖: 0

建议者: 0

安全: 0

星标: 5

关注者: 1

分支: 0

公开问题: 0

类型:cakephp-plugin

1.0.2 2018-02-28 20:31 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:21:02 UTC


README

Build Status

CakePHP 的 Oauth2Server 插件

此插件旨在提供一个简单的方法,使用 thephpleague/oauth2-server 来构建 Oauth2 服务器。

!!注意!! 此插件目前不支持刷新令牌存储库。访问令牌可以使用没有到期日期的令牌。 使用时请自行承担风险!

欢迎提交 PR

如何使用?

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

1. 使用 composer 安装

composer require suhaboncukcu/oauth2server

! 创建 openSSL 和加密密钥。我通常使用以下 composer 片段来处理这些任务:

...
   "create-keys": [
        "openssl genrsa -out private.key 2048",
        "openssl rsa -in private.key -pubout -out public.key"
    ],
    "create-encryption-key": [
        "./vendor/bin/generate-defuse-key"
    ],
...

2. 加载插件

Plugin::load('Oauth2Server', ['bootstrap' => true, 'routes' => false]);

3. 创建验证器

!!注意!!

您可以在 vendors\suhaboncukcu\Oauth2Server\src\OauthLogic\Validators 下找到示例验证器类。您应该将它们复制并粘贴到您想要的位置。

4. 创建并更新配置文件

vendors\suhaboncukcu\Oauth2Server\config\oauth2.php 复制并粘贴到您的配置文件夹中,并更新它。创建您的密钥后,应将它们的权限设置为 600660

5. 实现端点。

// in one of your controllers

    // Auth endpoint 
    public function authorize()
    {
        $this->autoRender = false;


        $this->loadComponent('Oauth2Server.Oauth2');

        $response = $this->Oauth2->authorize($this->request, $this->response);
        $response = $response->withHeader('Content-Type', 'application/json');

        return $response;
    }
    
    // callback endpoint
    public function code()
    {
        $this->autoRender = false;
        $response = $this->response
            ->withHeader('Content-Type', 'application/json')
            ->withStringBody(json_encode([
                'code' => urldecode($this->request->getQuery('code'))
            ]));

        return $response;
    }
    
    // access token endpoint
    public function accessToken()
    {
        $this->autoRender = false;

        $this->loadComponent('Oauth2Server.Oauth2');


        $response = $this->Oauth2->accessToken($this->request, $this->response);
        $response = $response->withHeader('Content-Type', 'application/json');

        return $response;
    }
    
    

6. 使用中间件来保护您的路由。

// assuming you have a plugin named Api 

//\Api\config\routes
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;

use League\OAuth2\Server\Middleware\ResourceServerMiddleware;
use Oauth2Server\OauthLogic\ServerUtility;

$serverUtility = new ServerUtility();
$server = $serverUtility->getPublicServer();


Router::plugin(
    'Api',
    ['path' => '/api'],
    function (RouteBuilder $routes) use ($server) {

        $routes->registerMiddleware('resourceServer', new ResourceServerMiddleware($server));
        $routes->middlewareGroup('Oauth2Stack', ['resourceServer']);

        $routes->applyMiddleware('Oauth2Stack');


        $routes->scope('/v1', function ($routes) {
            $routes->fallbacks(DashedRoute::class);
        });

    }
);

7. 如果验证器不足以在操作中获得完全控制,请使用属性。

$this->request->getAttributes()