碎片整理器 / silex-oauth
Silex OAuth 认证提供者
Requires
- lusitanian/oauth: >=0.2.3
README
OAuthServiceProvider 将 lusitanian/oauth 库与 Security 组件集成,为 Silex 微型框架提供社交登录。
此库仅提供认证系统。您需要提供自己的用户提供者,或者可以使用内存提供者进行测试。
功能
- 支持大多数流行的提供者,如Facebook、Twitter、Google和GitHub
- 通过事件钩子可扩展,以便您可以插入自己的监听器和用户提供者
- 支持可选的CSRF保护机制
示例
查看演示应用程序以获取代码示例。
安装
使用Composer通过将其添加到您的composer.json
中安装gigablah/silex-oauth库。
应使用与您的Silex安装兼容的版本。
请注意,以下示例中指定的symfony/security
(或任何其他Symfony组件)的版本基于与Silex的兼容性。OAuthServiceProvider自身应从2.3.0版本开始与组件版本兼容(如果不符合此条件,请提交一个问题)。
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" } }
用法
首先,您需要注册服务提供者,并使用要支持的每个OAuth提供者的应用程序密钥、密钥、作用域和用户API端点对其进行配置。以下是一些示例
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' ) ) ));
接下来,在您的防火墙中注册oauth
认证提供者。
// 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') ) ));
请注意,此库假定默认的登录、回调和检查路径以/auth
为前缀,因此需要保护此路径。您可以取消注释路径选项并更改默认值。
您需要为每个OAuth提供者配置正确的绝对callback_path
。例如,Facebook的默认回调将是http://your.domain/auth/facebook/callback
。
最后,您可以提供登录/注销界面。此示例假定使用Twig模板引擎
// 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');
模板本身
<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>
自定义事件处理器
默认情况下已注册两个默认事件监听器
UserInfoListener
在OAuth访问令牌成功生成后立即执行。然后,安全令牌使用配置的API端点的用户配置文件信息进行填充。UserProviderListener
在认证提供者从用户提供者查询用户对象时执行。
根据您的应用程序,您可能希望自动注册尚未拥有现有用户帐户的OAuth用户。这可以通过覆盖UserProviderListener
并将注册代码放在监听器函数中实现,或者通过简单地注册链中的单独监听器来实现。
自定义服务
您可以通过手动指定要实例化的类来注册自己的服务或覆盖现有服务
$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) { ... } ), // ... ) ));
许可证
在MIT许可证下发布。有关详细信息,请参阅LICENSE文件。