gigablah / silex-oauth
Silex OAuth 认证提供者
Requires
- lusitanian/oauth: >=0.2.3
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.
功能
- 支持大多数流行的提供商,如 Facebook、Twitter、Google 和 GitHub
- 通过事件钩子可扩展,因此您可以将自己的监听器和用户提供者插入
- 支持可选的 CSRF 保护机制
示例
查看 示例应用程序 以获取代码示例。
安装
使用 Composer 将 gigablah/silex-oauth 库添加到您的 composer.json
中以安装。
应使用与您的 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 文件。