承接 silvester/reverse-oauth2 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

silvester/reverse-oauth2

Composer 安装命令:

composer require silvester/reverse-oauth2

包简介

Oauth2 client for zf2. Facebook, github and google is supported.

README 文档

README

Build Status

Another OAuth2 client for ZF2. It provides clients for github, google, facebook and linkedin others soon to come.

The library is kept as simple as possible, it does not provide routes or controllers.

Demo

Minimum rights are used. If you feel intimidated revoke the rights. Click the login button.

Github: http://reverseform.modo.si/oauth-github

Google: http://reverseform.modo.si/oauth-google

Facebook: http://reverseform.modo.si/oauth-facebook

Installation with Composer

  1. Add this project in your composer.json:
    "require": {
        "silvester/reverse-oauth2": "dev-master",
    }
  1. Fetch the repository with composer:
$ php composer.phar update
  1. Enable it in your config/application.config.php file:
return array(
	'modules' => array(
		// ...
		'ReverseOAuth2',
	),
	// ...
);

Usage

As usual add it to your application.config.php 'ReverseOAuth2'.

Copy & rename the config/reverseoauth2.local.php.dist to your autoload folder and fill the information needed.

In your controller/action do:

public function callbackAction()
{

    $me = $this->getServiceLocator()->get('ReverseOAuth2\Google');
    //$me = $this->getServiceLocator()->get('ReverseOAuth2\Github');
    //$me = $this->getServiceLocator()->get('ReverseOAuth2\Facebook');
    //$me = $this->getServiceLocator()->get('ReverseOAuth2\LinkedIn');

    if (strlen($this->params()->fromQuery('code')) > 10) {
    	
    	if($me->getToken($this->request)) {
    		$token = $me->getSessionToken(); // token in session
    	} else {
    		$token = $me->getError(); // last returned error (array)
    	}
        
        $info = $me->getInfo();
        
    } else {
    
        $url = $me->getUrl();
        
    }

    return array('token' => $token, 'info' => $info, 'url' => $url);

}

The action name depends on your settings. getUrl() will return the url where you should redirect the user, there is no automatic redirection do it yourself.

Client Configuration

Beside the configuration options in module.config.php and reverseoath2.local.php you can change the client configuration on runtime.

public function callbackAction()
{

    $me = $this->getServiceLocator()->get('ReverseOAuth2\Google');
    //$me = $this->getServiceLocator()->get('ReverseOAuth2\Github');
    //$me = $this->getServiceLocator()->get('ReverseOAuth2\Facebook');
    //$me = $this->getServiceLocator()->get('ReverseOAuth2\LinkedIn');

	$me->getOptions()->setScope(array('email', 'user'));
	$me->getOptions()->setAuthUri('http://google.com/');
	$me->getOptions()->setTokenUri('http://google.com/');
	$me->getOptions()->setInfoUri('http://google.com/');
	$me->getOptions()->setClientId('my-id.com');
	$me->getOptions()->setClientSecret('my-secret');
	$me->getOptions()->setRedirectUri('http://my-server.com/');

}

The ReverseOAuth2 authentication adapter

The module provides also an zend\authentication\adapter.

public function authGithubAction() // controller action
{

    $me = $this->getServiceLocator()->get('ReverseOAuth2\Github');

    $auth = new AuthenticationService(); // zend
    
    if (strlen($this->params()->fromQuery('code')) > 10) {
         
        if($me->getToken($this->request)) { // if getToken is true, the user has authenticated successfully by the provider, not yet by us.
            $token = $me->getSessionToken(); // token in session
        } else {
            $token = $me->getError(); // last returned error (array)
        }
        
        $adapter = $this->getServiceLocator()->get('ReverseOAuth2\Auth\Adapter'); // added in module.config.php
        $adapter->setOAuth2Client($me); // $me is the oauth2 client
        $rs = $auth->authenticate($adapter); // provides an eventManager 'oauth2.success'
        
        if (!$rs->isValid()) {
            foreach ($rs->getMessages() as $message) {
                echo "$message\n";
            }
            echo 'no valid';
        } else {
            echo 'valid';
        }

    } else {
        $url = $me->getUrl();
    }

    $view = new ViewModel(array('token' => $token, 'info' => $info, 'url' => $url, 'error' => $me->getError()));
    
    return $view;

}

The adapter also provides an event called oauth2.success. Here you can check the data from the client against your user registry. You will be provided with information from the user, token info and provider type.

In your module class you could do:

public function onBootstrap(Event $e)
{
    /* Some bad code here, only for demo purposes. */
    $userTable = new UserTable($e->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter')); // my user table
    $e->getApplication()->getServiceManager()->get('ReverseOAuth2\Auth\Adapter')->getEventManager() // the the adapters eventmanager
        ->attach('oauth2.success', //attach to the event
            function($e) use ($userTable){
                
                $params = $e->getParams(); //print_r($params); so you see whats in if
                
                if($user = $userTable->getUserByRemote($params['provider'], $params['info']['id'])) { // check for user from facebook with id 1000
    
                    $user->token = $params['token']['access_token'];
                    $expire = (isset($params['token']['expires'])) ? $params['token']['expires'] : 3600;
                    $user->token_valid = new \Zend\Db\Sql\Expression('DATE_ADD(NOW(), INTERVAL '.$expire.' SECOND)');
                    $user->date_update = new \Zend\Db\Sql\Expression('NOW()');
                    
                    $userTable->saveUser($user);
                                    
                } else {
                    
                    $user = new User;
                    $user->token = $params['token']['access_token'];
                    $expire = (isset($params['token']['expires'])) ? $params['token']['expires'] : 3600;
                    $user->token_valid = new \Zend\Db\Sql\Expression('DATE_ADD(NOW(), INTERVAL '.$expire.' SECOND)');
                    $user->date_update = new \Zend\Db\Sql\Expression('NOW()');
                    $user->date_create = new \Zend\Db\Sql\Expression('NOW()');
                    $user->remote_source = $params['provider'];
                    $user->remote_id = $params['info']['id'];
                    $user->name = $params['info']['name'];
                    $user->info = \Zend\Json\Encoder::encode($params['info']);
                    
                    $userTable->saveUser($user);
                    
                }
                
                $user = $userTable->getUserByRemote($params['provider'], $params['info']['id']);
                $params['info'] = $user->getArrayCopy();
                $params['info']['info'] = false;
    
    			// here the params info is rewitten. The result object returned from the auth object will have the db row.
    			
    			$params['code'] = \Zend\Authentication\Result::FAILURE; // this would deny authentication. default is \Zend\Authentication\Result::SUCCESS.
    
            });

}

TODO

  • Add other clients
  • Write some decent documentation.
  • Demo module is on it's way.

silvester/reverse-oauth2 适用场景与选型建议

silvester/reverse-oauth2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.27k 次下载、GitHub Stars 达 35, 最近一次更新时间为 2012 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「oauth2」 「zf2」 「module」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 silvester/reverse-oauth2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 silvester/reverse-oauth2 我们能提供哪些服务?
定制开发 / 二次开发

基于 silvester/reverse-oauth2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7.27k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 35
  • 点击次数: 27
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 35
  • Watchers: 10
  • Forks: 22
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-11-07