marcobuschini / o-auth-bundle
集成 FOSUserBundle 与 OAuth
Requires
This package is not auto-updated.
Last update: 2024-09-26 01:45:40 UTC
README
最近我对HWIOAuthBundle感到非常失望,因为它的文档似乎总是缺少一些关键步骤来进行配置(至少对我来说,我的主要OAuth提供商是谷歌)。我只想有一个能够以非常简单的方式登录和注册用户的包。于是这个包就诞生了。
安装包
目前,安装仅支持通过git克隆。在vendor目录下创建一个名为MLB/OAuthBundle的路径,并在其中运行以下命令
$ git clone git://github.com/marcobuschini/MLBOAuthBundle.git .
完成此操作后,您需要在app/AppKernel.php文件中激活该包。只需在$bundles的末尾添加以下行即可
new MLB\OAuthBundle\MLBOAuthBundle()
配置包
首先,您必须正确安装和配置FOSUserBundle。为此,请获取他们的文档。所需的最小配置如下(在app/config/config.yml中设置)
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: Acme\DemoBundle\Entity\User
然后,我们必须配置Google OAuth的OAuth参数。从您的开发者控制台获取/设置这些参数(在app/config/config.yml中设置)
mlbo_auth:
google:
client_id: client id
client_secret: client secret
redirect_uri: the url we are waiting the server to respond to our request
scope: https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
添加表字段
以下是一个示例用户实体定义,其中包含运行此包所需的所有字段。在这种情况下,我们只需要Google使用的google_id和google_access_token。因为它扩展了基础FOSUserBundle类,所以相当简单。
// src/Acme/DemoBundle/Entity/User.php
namespace Acme\DemoBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $google_id;
/**
* @ORM\Column(type="string")
*/
protected $google_access_token;
public function __construct()
{
parent::__construct();
// your own logic
}
public function getGoogleId()
{
return $this->google_id;
}
public function setGoogleId($google_id)
{
$this->google_id = $google_id;
}
public function getGoogleAccessToken()
{
return $this->google_access_token;
}
public function setGoogleAccessToken($google_access_token)
{
$this->google_access_token = $google_access_token;
}
}
连接路由
以下是我们必须添加到app/config/routing.yml中的路由,以便应用程序能够通过此包登录和注册新用户。google_login路由用于登录用户(即:它是用户的入口点)。google_connect路由由谷歌的OAuth服务器调用以确认谷歌识别用户。google_after_login路由在用户成功登录时调用。
google_login:
path: /google/login
defaults: { _controller: MLBOAuthBundle:Google:login }
google_connect:
path: /google/connect
defaults: { _controller: MLBOAuthBundle:Google:connect }
google_after_login:
path: /welcome
defaults: { _controller: AcmeDemoBundle:Welcome:index }
注意事项
这是一项非常初步的工作。它缺少许多功能,可能也存在一些错误。最突出的功能缺失是将现有用户连接到谷歌用户。此外,必须添加其他OAuth提供商。