andrewbrereton / oauth-bundle
Symfony2 的 OAuth 防火墙
dev-master
2012-12-27 23:09 UTC
Requires
- php: >=5.3.2
- kriswallsmith/buzz: *
- symfony/framework-bundle: 2.*
This package is not auto-updated.
Last update: 2024-09-22 03:57:15 UTC
README
此组件仍在开发中,可能会发生变化!
安装
使用 svn
$ git clone https://github.com/mazen/EtcpasswdOAuthBundle.git vendor/bundles/Etcpasswd/OAuthBundle
$ git clone https://github.com/kriswallsmith/Buzz.git vendor/buzz
在自动加载器中注册命名空间
# app/autoload.php
$loader->registerNamespaces(array(
'Etcpasswd' => __DIR__.'/../vendor/bundles',
'Buzz' => __DIR__.'/../vendor/buzz/lib',
// .. your other namespaces
));
在应用的 Kernel 中注册该组件
# app/AppKernel.php
$bundles = array(
new Etcpasswd\OAuthBundle\EtcpasswdOAuthBundle(),
// .. other bundles
);
配置安全防火墙
# app/config/security.yml
firewalls:
oauth:
anonymous: true
logout: true
pattern: ^/
oauth:
auth_provider: api provider
client_id: client id
client_secret: secret
uid: email
scope: requested scope
login_path: /login
check_path: /auth
failure_path: /
factories:
- "%kernel.root_dir%/../vendor/bundles/Etcpasswd/OAuthBundle/Resources/config/security_factories.xml"
请注意,您不需要为登录路径或检查路径构建任何控制器。它们仅用于内部识别何时需要登录。
另外请注意,您仍然需要提供一个用户提供者。此组件仅基于 OAuth 服务进行用户认证,但不会创建任何用户对象。
指定多个 OAuth2 提供者
此包还允许一次使用多个提供者进行登录。您只需将这些提供者添加到 security.yml 文件中即可。
示例
firewalls:
main:
anonymous: true
logout: true
pattern: ^/
oauth_github:
auth_provider: "github"
client_id: xxx
client_secret: xxx
scope: repo,user
login_path: /login/github
check_path: /auth/github
failure_path: /
oauth_facebook:
auth_provider: "facebook"
client_id: xxx
client_secret: xxx
scope: ""
login_path: /login/facebook
check_path: /auth/facebook
failure_path: /
oauth_google:
auth_provider: "google"
client_id: xxx
client_secret: xxx
scope: "https://www.googleapis.com/auth/plus.me"
login_path: /login/google
check_path: /auth/google
failure_path: /
调用 /login/github、/login/facebook 或 /login/google 将使用正确的 OAuth 提供者。
内置 OAuth 提供者
此组件包含以下内置提供者
- GitHub
关于 Google 的说明:您至少需要提供 https://www.googleapis.com/auth/plus.me
范围,才能获取用户名。
授权用户
此组件不提供任何用户授权和/或持久状态的方式。您可以查看 https://github.com/FriendsOfSymfony/FOSUserBundle 来实现这些功能。如果您仍然想使用这些用户,而无需将它们持久化到数据库中,您可以添加此组件提供的提供者到您的安全配置。这允许您通过安全上下文服务访问 accessToken,以查询给定提供者的其他 API 服务。
示例 security.yml
security:
firewalls:
main:
anonymous: true
logout: true
pattern: ^/
oauth_github:
auth_provider: "github"
client_id: xxx
client_secret: xxx
scope: repo,user
login_path: /login/github
check_path: /auth/github
failure_path: /
role_hierarchy:
ROLE_ADMIN: [ROLE_USER]
providers:
main:
id: etcpasswd_oauth.user.provider
access_control: ~
factories:
- "%kernel.root_dir%/../vendor/bundles/Etcpasswd/OAuthBundle/Resources/config/security_factories.xml"