xavifr/oauth-server

CakePHP 3 的 OAuth 服务器,使用 PHP League 的 OAuth2 Server

维护者

详细信息

github.com/xavifr/oauth-server

源代码

安装次数: 3

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 50

类型:cakephp-plugin

v0.2.0 2016-02-10 13:43 UTC

This package is auto-updated.

Last update: 2024-09-29 06:08:35 UTC


README

Software License Build Status

一个用于在 CakePHP 3 中实现 OAuth2 服务器的插件。它建立在 PHP League 的 OAuth2 Server 之上。目前我们支持的授权类型有:AuthCode、RefreshToken、ClientCredentials。

安装

使用 composer 进行安装。运行

$ composer require uafrica/oauth-server

一旦 composer 安装了包,需要通过运行以下命令激活插件:

$ bin/cake plugin load -r OAuthServer

最后需要运行数据库迁移。

$ bin/cake migrations migrate --plugin OAuthServer

配置

假设您已经使用内置的 CakePHP 3 身份验证组件实现了基于表单的身份验证。如果没有,请阅读 身份验证章节

将 OAuthServer 设置为身份验证适配器。

在您的 AppController::beforeFilter() 方法中,添加(或修改)以下代码:

$this->Auth->config('authenticate', [
    'Form',
    'OAuthServer.OAuth'
]);

将登录方法修改如下:

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            $redirectUri = $this->Auth->redirectUrl();
            if ($this->request->query['redir'] === 'oauth') {
                $redirectUri = [
                    'plugin' => 'OAuthServer',
                    'controller' => 'OAuth',
                    'action' => 'authorize',
                    '?' => $this->request->query
                ];
            }
            return $this->redirect($redirectUri);
        } else {
            $this->Flash->error(
                __('Username or password is incorrect'),
                'default',
                [],
                'auth'
            );
        }
    }
}

或者,如果您正在使用 Friends Of Cake CRUD 插件,添加以下代码到您的 CRUD 操作配置中:

'login' => [
    'className' => 'OAuthServer.Login'
]

用法

OAuth2 的基本路径是 example.com/oauth

为了添加客户端和 OAuth 范围,您需要创建一个 ClientsController 和一个 ScopesController(这些不属于此插件)

最简单的方法是使用 Friends Of Cake CRUD-View 插件

通过运行以下命令进行安装:

$ composer require friendsofcake/bootstrap-ui:dev-master
$ composer require friendsofcake/crud:dev-master
$ composer require friendsofcake/crud-view:dev-master

然后创建一个如下所示的 ClientsController

<?php
namespace App\Controller;

use Crud\Controller\ControllerTrait;

/**
 * OauthClients Controller
 *
 * @property \OAuthServer\Model\Table\ClientsTable $Clients
 */
class ClientsController extends AppController
{

    use ControllerTrait;

    public $modelClass = 'OAuthServer.Clients';

    /**
     * @return void
     */
    public function initialize()
    {
        parent::initialize();
        $this->viewClass = 'CrudView\View\CrudView';
        $tables = [
            'Clients',
            'Scopes'
        ];
        $this->loadComponent('Crud.Crud', [
            'actions' => [
                'index' => [
                    'className' => 'Crud.Index',
                    'scaffold' => [
                        'tables' => $tables
                    ]
                ],
                'view' => [
                    'className' => 'Crud.View',
                    'scaffold' => [
                        'tables' => $tables
                    ]
                ],
                'edit' => [
                    'className' => 'Crud.Edit',
                    'scaffold' => [
                        'tables' => $tables,
                        'fields' => [
                            'name',
                            'redirect_uri',
                            'parent_model',
                            'parent_id' => [
                                'label' => 'Parent ID',
                                'type' => 'text'
                            ]
                        ]
                    ]
                ],
                'add' => [
                    'className' => 'Crud.Add',
                    'scaffold' => [
                        'tables' => $tables,
                        'fields' => [
                            'name',
                            'redirect_uri',
                            'parent_model',
                            'parent_id' => [
                                'label' => 'Parent ID',
                                'type' => 'text'
                            ]
                        ]
                    ]
                ],
                'delete' => [
                    'className' => 'Crud.Delete',
                    'scaffold' => [
                        'tables' => $tables
                    ]
                ],
            ],
            'listeners' => [
                'CrudView.View',
                'Crud.RelatedModels',
                'Crud.Redirect',
                'Crud.Api'
            ],
        ]);
    }
}

以及一个如下所示的 ScopesController

<?php
namespace App\Controller;

use Crud\Controller\ControllerTrait;

/**
 * Scopes Controller
 *
 * @property \OAuthServer\Model\Table\ScopesTable $Scopes
 */
class ScopesController extends AppController
{

    use ControllerTrait;

    public $modelClass = 'OAuthServer.Scopes';

    /**
     * @return void
     */
    public function initialize()
    {
        parent::initialize();
        $this->viewClass = 'CrudView\View\CrudView';
        $tables = [
            'Clients',
            'Scopes'
        ];
        $this->loadComponent('Crud.Crud', [
            'actions' => [
                'index' => [
                    'className' => 'Crud.Index',
                    'scaffold' => [
                        'tables' => $tables
                    ]
                ],
                'view' => [
                    'className' => 'Crud.View',
                    'scaffold' => [
                        'tables' => $tables
                    ]
                ],
                'edit' => [
                    'className' => 'Crud.Edit',
                    'scaffold' => [
                        'tables' => $tables,
                        'fields' => [
                            'id' => [
                                'label' => 'ID',
                                'type' => 'text'
                            ],
                            'description',
                        ]
                    ]
                ],
                'add' => [
                    'className' => 'Crud.Add',
                    'scaffold' => [
                        'tables' => $tables,
                        'fields' => [
                            'id' => [
                                'label' => 'ID',
                                'type' => 'text'
                            ],
                            'description',
                        ]
                    ]
                ],
                'delete' => [
                    'className' => 'Crud.Delete',
                    'scaffold' => [
                        'tables' => $tables
                    ]
                ],
            ],
            'listeners' => [
                'CrudView.View',
                'Crud.RelatedModels',
                'Crud.Redirect',
            ],
        ]);
    }
}

自定义

OAuth2 服务器可以自定义,通过在 Template/Plugin/OAuthServer/OAuth 中创建模板来更改各个页面的外观

服务器还触发了一些事件,可以用于在过程中注入值。当前触发的事件有:

  • OAuthServer.beforeAuthorize - 在渲染用户授权页面时。
  • OAuthServer.afterAuthorize - 用户授权客户端时。
  • OAuthServer.afterDeny - 用户拒绝客户端时。
  • OAuthServer.getUser - 在加载用于身份验证请求的用户详细信息时。

您可以通过创建覆盖模板文件 src/Template/Plugin/OAuthServer/OAuth/authorize.ctp 来自定义 OAuth 授权页面。