lorenzoferrarajr/lfj-opauth

LfjOpauth 是一个基于 Zend Framework 2 的模块,通过 Opauth 框架支持多种身份验证提供者。

v0.2.0 2015-03-13 18:37 UTC

This package is not auto-updated.

Last update: 2024-09-28 13:19:51 UTC


README

由 Lorenzo Ferrara Junior 创建

简介

LfjOpauth 是一个基于 Zend Framework 2 的模块,通过 Opauth 框架支持多种身份验证提供者。

安装

要使用此模块,您需要

  • 使用 composer 安装 LfjOpauth 模块
  • 安装至少一种 Opauth 策略
  • 启用 LfjOpauth 模块

要安装 LfjOpauth 模块,您需要将 "lorenzoferrarajr/lfj-opauth": "dev-master" 添加到项目 composer.json 文件中的 require 列表。

要安装 Opauth 策略,您需要在 PackagistGitHub 上找到所需的包,并将其添加到项目 composer.json 文件中的 require 列表。

以下是一个修改后的 composer.json 示例,其中包含 LfjOpauth 模块和 Facebook 策略

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": ">2.2.0rc1",
        "lorenzoferrarajr/lfj-opauth": "dev-master",
        "opauth/facebook": "0.2.1"
   }
}

示例包括 Facebook Opauth 策略的安装。有关 Opauth 策略的更多信息,请参阅 GitHub

使用 composer 安装 LfjOpauth 时,Opauth 依赖项会自动解决,但您仍必须提供至少一个策略。

接下来,您需要将 LfjOpauth 添加到您的 Zend Framework 2 项目的 config/application.config.php 文件中的 modules 列表。

以下是一个示例

<?php
return array(
    'modules' => array(
        'LfjOpauth',
        'Application',
    ),
    // more code
);

配置

一旦安装了 LfjOpauth,您必须在 config/autoload 目录中创建一个名为 lfjopauth.global.php 的文件。这是您指定 LfjOpauth 选项的配置文件。

lfjopauth.global.php 文件的示例

$settings = array(
    'security_salt' => 'Some random text',
    'Strategy' => array(
        'Facebook' => array(
            'app_id' => 'facebook application id',
            'app_secret' => 'facebook application secret',
            'scope' => 'email,user_relationships',
        ),
        'second_facebook_app' => array(
            'app_id' => 'another facebook application id',
            'app_secret' => 'another facebook application secret',
            'scope' => 'email,user_relationships',
            'strategy_class' => 'Facebook',
            'strategy_url_name' => 'second_facebook_app'
        )
    ),
    'check_controller_enabled' => false
);

return array('lfjopauth' => $settings);

配置几乎与 Opauth 配置 相同,只是没有处理 pathcallback_url 选项,这些选项由模块处理。

check_controller_enabled 标志启用或禁用对 CheckController 的访问。

或者,可以使用 Service Manager 设置选项提供者来替换当前的配置加载器。为此,您只需

  • 注册一个名为 lfjopauth_module_options 的服务
  • 实现 LfjOpauth\Provider\OptionsProviderInterface 接口
  • 并返回一个与上述相同的配置 array

如果您想从数据库加载配置或为复杂情况生成配置,这可能很有用。

登录和回调 URL

基于上述配置(以及相应的 Facebook 应用程序),您将能够使用以下方式登录

并且可以注销

对于示例配置中描述的两个演示 Facebook 应用程序,您应该使用

作为“具有Facebook登录的网站,网站URL”选项的值。

事件

在回调处理完成后,LfjOpauth\Service\OpauthService 触发了 LfjOpauth\LfjOpauthEvent::EVENT_LOGIN_CALLBACK 事件。请注意,即使登录过程失败,LfjOpauth\LfjOpauthEvent::EVENT_LOGIN_CALLBACK 事件也会被触发。使用可用的事件参数来实现结果检查。

该事件包含三个参数

  • authenticationService:一个 Zend\Authentication\Result 实例
  • authenticationResult:一个 Zend\Authentication\AuthenticationService 实例
  • provider:尝试登录时使用的提供者(例如:facebook,google)

其目标是 LfjOpauth\Service\OpauthService 的一个实例。

有关如何附加到事件的示例,请参考以下代码。

namespace Application;

use Zend\EventManager\EventInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $sharedEventManager = $eventManager->getSharedManager();

        $sharedEventManager->attach('LfjOpauth\Service\OpauthService', \LfjOpauth\LfjOpauthEvent::EVENT_LOGIN_CALLBACK, function(EventInterface $e) {

            /** @var \Zend\Authentication\Result $result */
            $authenticationResult = $e->getParam('authenticationResult');

            /** @var \Zend\Authentication\AuthenticationService $authenticationService */
            $authenticationService = $e->getParam('authenticationService');

            /** @var \LfjOpauth\Service\OpauthService $target */
            $target = $e->getTarget();

            $provider = $e->getParam('provider');

            /*
            var_dump(get_class($e->getTarget()));
            var_dump($e->getParam('provider'));
            var_dump('$authenticationResult->isValid()', $authenticationResult->isValid());
            var_dump('$authenticationService->hasIdentity()', $authenticationService->hasIdentity());
            var_dump('$authenticationService->getIdentity()', $authenticationService->getIdentity());
            var_dump('$authenticationResult->getCode()', $authenticationResult->getCode());
            var_dump('$authenticationResult->getIdentity()', $authenticationResult->getIdentity());
            var_dump('$authenticationResult->getMessages()', $authenticationResult->getMessages());
            */

        }, 100);
    }

自定义回调URL

如果您需要自定义登录和/或回调URL(例如包含更多参数),您可以编写自定义路由和控制器。

这是定义 custom_lfjopauth_logincustom_lfjopauth_callback 路由的代码(custom-auth 是控制器别名)

return array(
    'router' => array(
        'routes' => array(
            'custom_lfjopauth_login' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/custom/login/[:provider[/:oauth_callback]]',
                    'constraints' => array(
                        'provider'       => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'oauth_callback' => '[a-zA-Z][a-zA-Z0-9_-]*'
                    ),
                    'defaults' => array(
                        'controller'    => 'custom-auth',
                        'action'        => 'redirectAndReturn'
                    )
                )
            ),
            'custom_lfjopauth_callback' => array(
                'type'    => 'Segment',
                'options' => array(
                    'route'    => '/custom/callback/[:provider]',
                    'constraints' => array(
                        'provider'  => '[a-zA-Z][a-zA-Z0-9_-]*'
                    ),
                    'defaults' => array(
                        'controller'    => 'custom-auth',
                        'action'        => 'callback'
                    )
                )
            ),
            // more code
        )
    )
);

这是管理登录和回调操作的假设控制器代码

// [...]
class AuthController extends AbstractActionController
{
    public function redirectAndReturnAction()
    {
        // if user is not logged in
        if (!$this->auth()->hasIdentity())
        {
       	    $provider = $this->params()->fromRoute('provider');
       	    $oauth_callback = $this->params()->fromRoute('oauth_callback');
            $opauth_service = $this->getServiceLocator()->get('opauth_service');

            // set custom login and callback routes
            $opauth_service->setLoginUrlName('custom_lfjopauth_login');
            $opauth_service->setCallbackUrlName('custom_lfjopauth_callback');

            return $opauth_service->redirect($provider, $oauth_callback);
        }

        return $this->redirect()->toRoute('somewhere_over_the_rainbow');
    }

    public function callbackAction()
    {
        // if user is not logged in
        if (!$this->auth()->hasIdentity())
        {
       	    $provider = $this->params()->fromRoute('provider');
       	    $opauth_service = $this->getServiceLocator()->get('opauth_service');

            // set custom login and callback routes
            $opauth_service->setLoginUrlName('custom_lfjopauth_login');
            $opauth_service->setCallbackUrlName('custom_lfjopauth_callback');

            $opauth_service->callback($provider);
        }
	
        return $this->redirect()->toRoute('somewhere_else_over_the_rainbow');
    }
}

检查登录状态

如果启用 check_controller_enabled 标志,您可以在以下URL打印当前会话信息

check_controller_enabled 的默认值为 false

其他信息

LfjOpauth 使用 Zend\Authentication\AuthenticationService(别名 lfjopauth_auth_service)来管理身份验证。

LfjOpauth\Service\OpauthService(别名:opauth_service)类公开了可以在任何控制器中使用的 redirectcallback 方法。一个示例可以在 LfjOpauth\Controller\LoginController 类中找到。

许可证

此存档中的文件在MIT许可证下发布。您可以在 LICENSE.txt 中找到此许可证的副本。