muffin/oauth2
Composer 安装命令:
composer require muffin/oauth2
包简介
CakePHP 3 authentication using the league/oauth2-client family
README 文档
README
CakePHP 3 authentication using the league/oauth2-client.
Install
Using Composer:
composer require muffin/oauth2
You then need to load the plugin.
bin/cake plugin load Muffin/OAuth2
Wait, you're not done yet. This plugin will NOT require any of the clients. You will have to do it yourself. For e.g. if you want to use Github client do:
composer require "league/oauth2-github"
Usage
First, start by defining the providers:
// either in `config/bootstrap.php` Configure::write('Muffin/OAuth2', [ 'providers' => [ 'github' => [ 'className' => 'League\OAuth2\Client\Provider\Github', // all options defined here are passed to the provider's constructor 'options' => [ 'clientId' => 'foo', 'clientSecret' => 'bar', ], 'mapFields' => [ 'username' => 'login', // maps the app's username to github's login ], // ... add here the usual AuthComponent configuration if needed like fields, etc. ], ], ]); // or in `src/Controller/AppController.php` $this->loadComponent('Auth', [ 'authenticate' => [ // ... 'Muffin/OAuth2.OAuth' => [ 'providers' => [ // the array from example above ], ], ], ]);
Upon successful authorization, and if the user has no local instance, an event (Muffin/OAuth2.newUser)
is triggered. Use it to create a user like so:
// bootstrap.php use Cake\Event\Event; use Cake\ORM\TableRegistry; EventManager::instance()->on( 'Muffin/OAuth2.newUser', [TableRegistry::get('Users'), 'createNewUser'] ); // UsersTable.php use Cake\Event\Event; use League\OAuth2\Client\Provider\AbstractProvider; public function createNewUser(Event $event, AbstractProvider $provider, array $data) { $entity = $this->newEntity($data); $this->save($entity); return $entity->toArray(); // user data to be used in session }
Finally, once token is received, the Muffin/OAuth2.afterIdentify event is triggered. Use this to update your local
tokens for example:
// bootstrap.php use Cake\Event\Event; use Cake\ORM\TableRegistry; EventManager::instance()->on( 'Muffin/OAuth2.afterIdentify', [TableRegistry::get('Tokens'), 'createOrUpdate'] ); // TokensTable.php use Cake\Event\Event; use League\OAuth2\Client\Provider\AbstractProvider; public function createOrUpdate(Event $event, AbstractProvider $provider, array $data) { // ... return; // void }
Next up, you need to create a route that will be used by all providers:
// config/routes.php Router::connect( '/oauth/:provider', ['controller' => 'users', 'action' => 'login'], ['provider' => implode('|', array_keys(Configure::read('Muffin/OAuth2.providers')))] );
Now, if you have already read the book's AuthComponent documentation, you should be familiar with how to
add the new authentication object to it:
// src/Controller/AppController.php $this->loadComponent('Auth', [ 'authenticate' => [ 'Form', 'Muffin/OAuth2.OAuth', ] ]);
Patches & Features
- Fork
- Mod, fix
- Test - this is important, so it's not unintentionally broken
- Commit - do not mess with license, todo, version, etc. (if you do change any, bump them into commits of their own that I can ignore when I pull)
- Pull request - bonus point for topic branches
To ensure your PRs are considered for upstream, you MUST follow the CakePHP coding standards.
Bugs & Feedback
http://github.com/usemuffin/oauth2/issues
License
Copyright (c) 2018, Use Muffin and licensed under The MIT License.
muffin/oauth2 适用场景与选型建议
muffin/oauth2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 110.71k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2015 年 09 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Authentication」 「oauth2」 「cakephp」 「auth」 「league」 「muffin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 muffin/oauth2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 muffin/oauth2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 muffin/oauth2 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 110.71k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-13