gigablah/silex-oauth
Composer 安装命令:
composer require gigablah/silex-oauth
包简介
Silex OAuth Authentication Provider
README 文档
README
The OAuthServiceProvider integrates the lusitanian/oauth library with the Security component to provide social logins for the Silex microframework.
This library only provides the authentication system. You would have to supply your own user provider, or you can make use of the in-memory provider for testing.
Features
- Supports most popular providers such as Facebook, Twitter, Google and GitHub
- Extensible via event hooks so you can plug in your own listeners and user providers
- Supports optional CSRF protection mechanism
Example
Check out the demo application for a code example.
Installation
Use Composer to install the gigablah/silex-oauth library by adding it to your composer.json.
You should use a version that is compatible with your Silex installation.
Take note that the versions specified for symfony/security (or any other Symfony component) in the examples below are based on compatibility with Silex. OAuthServiceProvider itself should be compatible with component versions from 2.3.0 onwards (please open an issue if this is not the case).
Silex 2.0
{
"require": {
"silex/silex": "~2.0@dev",
"symfony/security": "~2.7,<3.0",
"gigablah/silex-oauth": "~2.0@dev"
}
}
Silex 1.3
{
"require": {
"silex/silex": "~1.3,<2.0",
"symfony/security": "~2.4,<3.0",
"gigablah/silex-oauth": "~1.3"
}
}
Silex 1.0
{
"require": {
"silex/silex": ">=1.0 <1.3",
"symfony/security": "~2.3.0",
"gigablah/silex-oauth": "~1.0.0"
}
}
Usage
First, you need to register the service provider and configure it with the application keys, secrets, scopes and user API endpoints for each OAuth provider you wish to support. Some examples are shown below:
ini_set('display_errors', 1); error_reporting(-1); require_once __DIR__.'/vendor/autoload.php'; define('FACEBOOK_API_KEY', ''); define('FACEBOOK_API_SECRET', ''); define('TWITTER_API_KEY', ''); define('TWITTER_API_SECRET', ''); define('GOOGLE_API_KEY', ''); define('GOOGLE_API_SECRET', ''); define('GITHUB_API_KEY', ''); define('GITHUB_API_SECRET', ''); $app = new Silex\Application(); $app['debug'] = true; $app->register(new Gigablah\Silex\OAuth\OAuthServiceProvider(), array( 'oauth.services' => array( 'Facebook' => array( 'key' => FACEBOOK_API_KEY, 'secret' => FACEBOOK_API_SECRET, 'scope' => array('email'), 'user_endpoint' => 'https://graph.facebook.com/me' ), 'Twitter' => array( 'key' => TWITTER_API_KEY, 'secret' => TWITTER_API_SECRET, 'scope' => array(), // Note: permission needs to be obtained from Twitter to use the include_email parameter 'user_endpoint' => 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true', 'user_callback' => function ($token, $userInfo, $service) { $token->setUser($userInfo['name']); $token->setEmail($userInfo['email']); $token->setUid($userInfo['id']); } ), 'Google' => array( 'key' => GOOGLE_API_KEY, 'secret' => GOOGLE_API_SECRET, 'scope' => array( 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile' ), 'user_endpoint' => 'https://www.googleapis.com/oauth2/v1/userinfo' ), 'GitHub' => array( 'key' => GITHUB_API_KEY, 'secret' => GITHUB_API_SECRET, 'scope' => array('user:email'), 'user_endpoint' => 'https://api.github.com/user' ) ) ));
Next, register the oauth authentication provider in your firewall.
// Provides CSRF token generation // You will have to include symfony/form in your composer.json $app->register(new Silex\Provider\FormServiceProvider()); // Provides session storage $app->register(new Silex\Provider\SessionServiceProvider(), array( 'session.storage.save_path' => '/tmp' )); $app->register(new Silex\Provider\SecurityServiceProvider(), array( 'security.firewalls' => array( 'default' => array( 'pattern' => '^/', 'anonymous' => true, 'oauth' => array( //'login_path' => '/auth/{service}', //'callback_path' => '/auth/{service}/callback', //'check_path' => '/auth/{service}/check', 'failure_path' => '/login', 'with_csrf' => true ), 'logout' => array( 'logout_path' => '/logout', 'with_csrf' => true ), // OAuthInMemoryUserProvider returns a StubUser and is intended only for testing. // Replace this with your own UserProvider and User class. 'users' => new Gigablah\Silex\OAuth\Security\User\Provider\OAuthInMemoryUserProvider() ) ), 'security.access_rules' => array( array('^/auth', 'ROLE_USER') ) ));
Note that the library assumes the default login, callback and check paths to be prefixed with /auth, so this path needs to be secured. You can uncomment the path options and change the defaults.
You will need to configure each of your OAuth providers with the correct absolute callback_path. For example, the default callback for Facebook would be http://your.domain/auth/facebook/callback.
Finally, you can provide a login/logout interface. This example assumes usage of the Twig templating engine:
// Provides Twig template engine $app->register(new Silex\Provider\TwigServiceProvider(), array( 'twig.path' => __DIR__.'/views' )); $app->before(function (Symfony\Component\HttpFoundation\Request $request) use ($app) { if (isset($app['security.token_storage'])) { $token = $app['security.token_storage']->getToken(); } else { $token = $app['security']->getToken(); } $app['user'] = null; if ($token && !$app['security.trust_resolver']->isAnonymous($token)) { $app['user'] = $token->getUser(); } }); $app->get('/login', function (Symfony\Component\HttpFoundation\Request $request) use ($app) { $services = array_keys($app['oauth.services']); return $app['twig']->render('index.twig', array( 'login_paths' => $app['oauth.login_paths'], 'logout_path' => $app['url_generator']->generate('logout', array( '_csrf_token' => $app['oauth.csrf_token']('logout') )), 'error' => $app['security.last_error']($request) )); }); $app->match('/logout', function () {})->bind('logout');
The template itself:
<div>
{% if error %}
<p>{{ error }}</p>
{% endif %}
{% if app.user %}
<p>Hello {{ app.user.username }}! Your email is {{ app.user.email }}</p>
<a href="{{ logout_path }}">Logout</a>
{% else %}
<ul>
<li><a href="{{ login_paths.facebook }}">Login with Facebook</a></li>
<li><a href="{{ login_paths.twitter }}">Login with Twitter</a></li>
<li><a href="{{ login_paths.google }}">Login with Google</a></li>
<li><a href="{{ login_paths.github }}">Login with GitHub</a></li>
</ul>
{% endif %}
</div>
Custom Event Handlers
Two default event listeners are registered by default:
UserInfoListenerexecutes right after an OAuth access token is successfully generated. The security token is then populated with user profile information from the configured API endpoint.UserProviderListenerexecutes at the point where the authentication provider queries for a user object from the user provider.
Depending on your application, you might want to automatically register OAuth users who do not already have an existing user account. This can be done by overriding UserProviderListener and placing your registration code in the listener function, or by simply registering a separate listener in the chain.
Custom Services
You can register your own services or override existing ones by manually specifying the class to instantiate:
$app->register(new Gigablah\Silex\OAuth\OAuthServiceProvider(), array( 'oauth.services' => array( 'my_service' => array( 'class' => 'My\\Custom\\Namespace\\MyOAuthService', 'key' => MY_API_KEY, 'secret' => MY_API_SECRET, 'scope' => array(), 'user_endpoint' => 'https://my.domain/userinfo', 'user_callback' => function ($token, $userInfo, $service) { ... } ), // ... ) ));
License
Released under the MIT license. See the LICENSE file for details.
gigablah/silex-oauth 适用场景与选型建议
gigablah/silex-oauth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46.27k 次下载、GitHub Stars 达 108, 最近一次更新时间为 2013 年 08 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「silex」 「oauth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gigablah/silex-oauth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gigablah/silex-oauth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gigablah/silex-oauth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
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.
Object-oriented CRUD for Doctrine DBAL
Command bus implementation: Commands and domain events
统计信息
- 总下载量: 46.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 110
- 点击次数: 11
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-08-10