我昨天已经开通了一个关于symfony2中关于hwioauthbundle的问题的主题. Symfony2 HWIOauthBundle响应错误
感谢stloyd的回答,这有助于我弄清楚如何使用bundle来访问linkedin.
我现在正在尝试不仅获得来自linkedin的格式化名称,还获得其他信息,如教育,技能等
我不确定HwioAuth通常是否可以这样做?那是我的config.yml
hwi_oauth:
firewall_name: main
resource_owners:
linkedin:
type: linkedin
client_id: %linkedin_client_id%
client_secret: %linkedin_client_secret%
scope: r_fullprofile
infos_url: 'https://api.linkedin.com/v1/people/~:(id,educations,languages,skills)?format=json'
scope: 'r_fullprofile'
paths:
educations: educations
languages: languages
skills: skills
Run Code Online (Sandbox Code Playgroud)
那是我的security.yml:
providers:
....
my_custom_hwi_provider:
id: linkedin.oauth_user_provider
firewalls:
main:
...
oauth:
resource_owners:
linkedin: "/login/check-linkedin"
login_path: /login
failure_path: /login
oauth_user_provider:
service: linkedin.oauth_user_provider
Run Code Online (Sandbox Code Playgroud)
这是我的服务类提供者:
<?php
namespace XXXX\UtilBundle\Util\OAuthProvider;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
class Provider
{
protected $session, $doctrine;
public function __construct($session, $doctrine) {
$this->session = $session;
$this->doctrine = $doctrine;
}
public function loadUserByOAuthUserResponse(UserResponseInterface $response) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个Symfony2应用程序.我正在使用FOSUserBundle来处理身份验证,最近使用本教程将其与FOSUserBundle集成:https://gist.github.com/danvbe/4476697 .
问题是:我可以在localhost上使用google api登录,一切正常.
但是,当我尝试登录真实服务器时,我得到:
Error: invalid_request
device_id and device_name are required for private IP: http://<server_ip>/login/check-google
Run Code Online (Sandbox Code Playgroud)
请求详情:
response_type=code
scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
redirect_uri=http://<server_ip>/login/check-google
client_id=<my_id>
Run Code Online (Sandbox Code Playgroud)
Google文档没有提到这两个参数.我尝试手动发送请求,其中device_id是UUID,device_name设置为"notes".我这次得到的回应是:
Error: invalid_request
Device info can be set only for native apps.
Run Code Online (Sandbox Code Playgroud)
请求详情:
cookie_policy_enforce=false
response_type=code
device_name=notes
scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile
redirect_uri=http://<server_ip>/login/check-google
device_id=4b3403665fea6
client_id=<my_id>
Run Code Online (Sandbox Code Playgroud)
现在,我做错了什么?
编辑:请参阅下面的我自己的解决方案,在撰写本文时,它正在运作但不完美.我会喜欢一些批评和反馈,如果我把一些我觉得非常可靠的东西放在一起,那么我会为面临同样挑战的其他人制作一篇howto博客文章.
几天来我一直在努力奋斗,如果我走在正确的道路上,我希望有人能告诉我.
我有一个带有FOSRestBundle webservice的系统,我正在使用FOSUserBundle和HWIOAuthBundle来验证用户.
我想为webservice设置无状态api密钥身份验证.
我已经阅读了http://symfony.com/doc/current/cookbook/security/api_key_authentication.html,这看起来很简单,我已经安装了UecodeApiKeyBundle,这似乎只是本书页面的一个实现.
我的问题是n00b ...现在怎么样?书籍页面和包都涵盖了通过API密钥对用户进行身份验证,但不涉及日志用户流,生成API密钥,允许用户注册等.我真正想要的是用于登录的简单API端点,注册,以及我的应用开发者可以使用的注销.像/ api/v1/login等等.
我想我可以处理注册....虽然登录让我感到困惑.根据一些额外的阅读,在我看来,我需要做的登录是这样的:
在api/v1/login创建一个接受POST请求的控制器.请求看起来像{_username:foo,_ password:bar}或类似{facebook_access_token:foo.或者,facebook登录可能需要不同的操作,例如/ user/login/facebook,并且只需重定向到HWIOAuthBundle路径}.
如果请求包含_username和_password参数,那么我需要将请求转发到login-check(我不确定这个.我可以自己处理这个表单吗?或者,我应该手动检查用户名和密码数据库?)
添加一个登录事件监听器,如果用户认证成功,则为用户生成一个api密钥(当然,如果我自己没有检查它,这是必要的)
在POST请求的响应中返回API密钥(这会打破post-redirect-get策略,但是我没有看到任何问题)我认为这消除了上面列出的重定向登录检查选项.
你可能会看到我很困惑.这是我的第一个Symfony2项目,关于安全的书页很简单......但似乎掩盖了一些细节,这使我不确定要采取的方法.
提前致谢!
================================================== ===========
编辑:
我已经安装了API密钥身份验证,与相关的相关菜谱文章完全相同:http://symfony.com/doc/current/cookbook/security/api_key_authentication.html
为了处理用户的登录,我创建了一个自定义控制器方法.我怀疑这是完美的,我很乐意听到一些关于如何改进它的反馈,但我相信我正走在正确的道路上,因为我的流程正在发挥作用.这是代码(请注意,还在开发的早期......我还没有看过Facebook登录,只有简单的用户名/密码登录):
class SecurityController extends FOSRestController
{
/**
* Create a security token for the user
*/
public function tokenCreateAction()
{
$request = $this->getRequest();
$username = $request->get('username',NULL);
$password = $request->get('password',NULL);
if (!isset($username) || !isset($password)){
throw new BadRequestHttpException("You must pass username and password fields");
}
$um = $this->get('fos_user.user_manager');
$user = $um->findUserByUsernameOrEmail($username);
if (!$user instanceof \Acme\UserBundle\Entity\User) …Run Code Online (Sandbox Code Playgroud) 我有一个实体用户表单,其中包含电子邮件字段:
->add('email', EmailType::class, [
'constraints' => [
new NotBlank(),
new Email([
'checkMX' => true,
])
],
'required' => true
])
Run Code Online (Sandbox Code Playgroud)
当我正在编辑类似的电子邮件test@gmail.com1并提交表单时,它会显示错误"此值不是有效的电子邮件地址".没关系,但是在那之后symfony将错误的电子邮件填充到令牌中,当我要去任何其他页面或只是重新加载页面时,我得到了这个:
警告安全性在所选用户提供程序中找不到用户名.
我认为问题是:为什么symfony填充错误的电子邮件验证失败的令牌以及我如何防止它?
控制器:
public function meSettingsAction(Request $request)
{
$user = $this->getUser();
$userUnSubscribed = $this->getDoctrine()->getRepository('AppBundle:UserUnsubs')->findOneBy(
[
'email' => $user->getEmail(),
]
);
$form = $this->createForm(UserSettingsType::class, $user);
$form->get('subscribed')->setData(!(bool)$userUnSubscribed);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/**
* @var $user User
*/
$user = $form->getData();
/** @var UploadedFile $avatar */
$avatar = $request->files->get('user_settings')['photo'];
$em = $this->getDoctrine()->getManager();
if ($avatar) { …Run Code Online (Sandbox Code Playgroud) 我试图birthday从Google API获取,但检索到的数据HWIOAuthBundle不包含它.
我想知道google plus apiconfig.yml中的指定范围是否正确!
如果没有,请提供链接或更正的范围.
google:
type: google
client_id: %client_id%
client_secret: %secret_id%
scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
paths:
email: email
profilepicture: picture
Run Code Online (Sandbox Code Playgroud) HWIOAuthBundle是一个在Symfony2项目中启用社交登录的作曲家包:
https://github.com/hwi/HWIOAuthBundle
Symfony2文档提供了如何在功能测试中模拟身份验证的示例:
http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html
但是,我没有成功地让这两个人在我的测试中一起工作.Symfony2安全系统指示存在非匿名用户,但我的控制器中的getUser()返回null.
任何人都有这方面的成功或有一些关于如何最佳调试或实施的指针?
使用:
我怎么刷新令牌?我使用带有此令牌的Google api - 它可以工作但无法找到如何刷新它,在这个例子中我们不保存过期时间.我需要
`access_type: offline `
Run Code Online (Sandbox Code Playgroud)
然后
$client = new Google_Client();
//$client->setClientId($GoogleClientId);
$client->setApplicationName($GoogleAppName);
$client->setClientId($this->user->getGoogleId());
$client->setAccessType('offline');
Run Code Online (Sandbox Code Playgroud)
如果令牌有效,我可以工作但是什么时候过期我试试
$token = [
'access_token' => $this->user->getGoogleAccessToken(),
'expires_in' => (new \DateTime())->modify('-1 year')->getTimestamp(),
];
Run Code Online (Sandbox Code Playgroud)
我把它放在任何日期,因为在这个例子中我们不保存过期时间
https://gist.github.com/danvbe/4476697
$client->setAccessToken($token);
if($client->isAccessTokenExpired()){
$refreshedToken = $client->refreshToken($client->getAccessToken());
Run Code Online (Sandbox Code Playgroud)
我有错误
array:2 [?
"error" => "invalid_request"
"error_description" => "Could not determine client ID from request."
]
Run Code Online (Sandbox Code Playgroud)
有HwiAuthBundle方法来刷新令牌吗?为什么这不适用于Google_Client刷新?
我正在使用HWIOAuthBundle让用户使用Oauth登录,我创建了一个自定义用户提供程序,以便在用户不存在的情况下创建用户:
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
$attr = $response->getResponse();
switch($response->getResourceOwner()->getName()) {
case 'google':
if(!$user = $this->userRepository->findOneByGoogleId($attr['id'])) {
if(($user = $this->userRepository->findOneByEmail($attr['email'])) && $attr['verified_email']) {
$user->setGoogleId($attr['id']);
if(!$user->getFirstname()) {
$user->setFirstname($attr['given_name']);
}
if(!$user->getLastname()) {
$user->setLastname($attr['family_name']);
}
$user->setGoogleName($attr['name']);
}else{
$user = new User();
$user->setUsername($this->userRepository->createUsernameByEmail($attr['email']));
$user->setEmail($attr['email']);
$user->setFirstname($attr['given_name']);
$user->setLastname($attr['family_name']);
$user->setPassword('');
$user->setIsActive(true);
$user->setGoogleId($attr['id']);
$user->setGoogleName($attr['name']);
$user->addGroup($this->groupRepository->findOneByRole('ROLE_USER'));
$this->entityManager->persist($user);
}
}
break;
case 'facebook':
if(!$user = $this->userRepository->findOneByFacebookId($attr['id'])) {
if(($user = $this->userRepository->findOneByEmail($attr['email'])) && $attr['verified']) {
$user->setFacebookId($attr['id']);
if(!$user->getFirstname()) {
$user->setFirstname($attr['first_name']);
}
if(!$user->getLastname()) {
$user->setLastname($attr['last_name']);
}
$user->setFacebookUsername($attr['username']);
}else{
$user = new User();
$user->setUsername($this->userRepository->createUsernameByEmail($attr['email']));
$user->setEmail($attr['email']); …Run Code Online (Sandbox Code Playgroud) 我有一个网站(Symfony2)与HWIOauthBundle用于连接Facebook,一切正常.
现在,我正在尝试使用Cordova和Ionic框架(AngularJS)构建一个iOS应用程序,我想用Facebook验证我的用户:
使用$cordovaFacebook,我验证我的用户并获得有效的Facebook访问令牌,这没关系
我尝试使用此访问令牌在服务器端使用HWIOauthBundle对我的用户进行身份验证:
GET http://..../login/facebook?code=MY_FACEBOOK_ACCESS_TOKEN
Run Code Online (Sandbox Code Playgroud)Symfony用这个日志拒绝我的请求:
INFO - Matched route "facebook_login" (parameters: "_route": "facebook_login")
INFO - Authentication request failed: OAuth error: "Invalid verification code format."
Run Code Online (Sandbox Code Playgroud)所以我的问题是:如何通过Facebook连接在前端和后端验证我的用户?
谢谢 :)
将我的项目升级到symfony 2.8后,当我将代码推送到登台服务器时,出现以下错误.
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]"hwi_oauth"下无法识别的选项"firewall_name"当我删除
firewall_name选项时,我收到一个错误,指出该选项是必需的.
如果有人提供一些见解将非常感激.
hwioauthbundle ×10
symfony ×10
php ×4
google-api ×2
google-oauth ×2
oauth ×2
facebook ×1
google-plus ×1
symfony-2.1 ×1
symfony-3.3 ×1
testing ×1