damianociarla / opauth-bundle
DCSOpauthBundle 是一个 Symfony2 扩展包,通过 Opauth 框架支持多个身份验证提供者。
v1.0.3
2014-11-12 10:11 UTC
Requires
- php: >=5.3
- opauth/opauth: 0.4.4
- symfony/symfony: ~2.3
This package is not auto-updated.
Last update: 2024-09-14 16:39:47 UTC
README
DCSOpauthBundle 是一个 Symfony2 扩展包,通过 Opauth 框架支持多个身份验证提供者。
安装
a) 下载并安装 DCSOpauthBundle
要安装 DCSOpauthBundle,请运行以下命令
bash $ php composer.phar require damianociarla/opauth-bundle
b) 安装 Opauth 策略
要安装 Opauth 策略,您需要在 Packagist 或 GitHub 上找到所需的包,并将其添加到项目 composer.json 文件的要求列表中。
以下是一个 Facebook 策略的示例
bash $ php composer.phar require opauth/facebook
c) 启用扩展包
要启用它,请将扩展包实例添加到内核中
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new DCS\OpauthBundle\DCSOpauthBundle(),
);
}
d) 配置扩展包
现在您已经正确启用了 DCSOpauthBundle,下一步是配置扩展包以满足您应用程序的特定需求。
根据您希望在应用程序中使用哪些策略,将以下配置添加到您的 config.yml 文件中。
# app/config/config.yml
dcs_opauth:
strategies:
# The name of the strategy is personal. es: google, facebook, google_app_2 etc...
google:
id: YOUR_CLIENT_ID
secret: YOUR_CLIENT_SECRET
strategy_class: Google
# Facebook example with option scope
facebook:
id: YOUR_APP_ID
secret: YOUR_APP_SECRET
strategy_class: Facebook
options:
scope: "email"
有关策略配置的列表,请参阅 Opauth 库的原始配置。
e) 导入 DCSOpauthBundle 路由文件
现在您已经启用并配置了扩展包,剩下要做的就是导入 DCSOpauthBundle 路由文件。
# app/config/routing.yml
dcs_opauth_connect:
resource: "@DCSOpauthBundle/Resources/config/routing/connect.xml"
prefix: /connect
dcs_opauth_response:
resource: "@DCSOpauthBundle/Resources/config/routing/response.xml"
prefix: /connect
它是如何工作的?
DCSOpauthBundle 在选定的提供者上登录,并为用户的会话提供一个令牌。
如何登录提供者?
您必须在路由 dcs_opauth_connect
上执行重定向,并传递参数 {provider}
# twig example
<a href="{{ url('dcs_opauth_connect', {'provider' : 'google'}) }}">Login with Google</a>
完整配置
dcs_opauth:
debug_mode: false # (boolean) Whether debug messages are to be displayed
callback_transport: session # ('session','post','get') HTTP transport type, for sending of Auth response
security_iteration: 300 # (int) The number of times hashing is done to sign auth response
security_timeout: 2 minutes # (string) Time limit allowed for an auth response to be considered valid
authentication_roles: [ROLE_USER] # (array) Roles to associate with the token
redirect_after_response: ~ # (string) Path or view_name to redirect user after login
error_view: DCSOpauthBundle:Response:error.html.twig # (string) View name to display the errors if the login fails
logic:
strategy_parser: dcs_opauth.configuration.strategy_parser.default # Service name to retrieve the config for a specific strategy
response_parser: dcs_opauth.response.response_parser.default # Service name to parse the Opauth's response
authenticator: dcs_opauth.security.authenticator.default # Service name that autenticates user
strategies:
NAME:
id: SECRET_ID
secret: SECRET_KEY
strategy_class: STRATEGY_CLASS
options: STRATEGY OPTIONS