misarji / zend-oauth2
为 ZF2 优秀的 OAuth2 客户端。它提供了对 github、google、facebook 和 linkedin 的客户端支持,其他客户端即将推出。
1.0.0
2014-08-25 11:11 UTC
Requires
- php: >=5.3.3
- zendframework/zendframework: 2.*
This package is not auto-updated.
Last update: 2024-09-24 08:22:42 UTC
README
为 ZF2 优秀的 OAuth2 客户端。它提供了对 github、google、facebook 和 linkedin 的客户端支持,其他客户端即将推出。
该库尽可能简单,不提供路由或控制器。
使用 Composer 安装
- 在您的
composer.json
中添加此项目
"require": { "misarji/zend-auth2": "dev-master", }
- 使用 composer 检索仓库
$ php composer.phar update
- 在您的
config/application.config.php
文件中启用它
return array( 'modules' => array( // ... 'ZendOAuth2', ), // ... );
使用方法
像往常一样,将其添加到您的 application.config.php 中的 'ZendOAuth2'。
复制并重命名 config/zendoauth2.local.php.dist
到您的 autoload 文件夹,并填写所需的信息。
在您的控制器/操作中执行以下操作
public function callbackAction() { $me = $this->getServiceLocator()->get('ZendOAuth2\Google'); //$me = $this->getServiceLocator()->get('ZendOAuth2\Github'); //$me = $this->getServiceLocator()->get('ZendOAuth2\Facebook'); //$me = $this->getServiceLocator()->get('ZendOAuth2\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); }
操作名称取决于您的设置。getUrl() 将返回您应该重定向用户的 URL,没有自动重定向,请自行完成。
客户端配置
除了 module.config.php
和 reverseoath2.local.php
中的配置选项之外,您还可以在运行时更改客户端配置。
public function callbackAction() { $me = $this->getServiceLocator()->get('ZendOAuth2\Google'); //$me = $this->getServiceLocator()->get('ZendOAuth2\Github'); //$me = $this->getServiceLocator()->get('ZendOAuth2\Facebook'); //$me = $this->getServiceLocator()->get('ZendOAuth2\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/'); }
ZendOAuth2 身份验证适配器
该模块还提供了一个 zend\authentication\adapter。
public function authGithubAction() // controller action { $me = $this->getServiceLocator()->get('ZendOAuth2\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('ZendOAuth2\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; }
适配器还提供了一个名为 oauth2.success
的事件。在这里,您可以检查来自客户端的数据与您的用户注册信息。您将获得有关用户、令牌信息和提供者类型的信息。
在您的模块类中,您可以这样做
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('ZendOAuth2\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. }); }
待办事项
- 添加其他客户端
- 编写一些不错的文档。
- 演示模块即将推出。