Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrating FOSUserBundle & HWIOAuthBundle #81

Closed
nickrouty opened this issue Jul 16, 2012 · 17 comments
Closed

Integrating FOSUserBundle & HWIOAuthBundle #81

nickrouty opened this issue Jul 16, 2012 · 17 comments

Comments

@nickrouty
Copy link
Contributor

I am now able to login to an account through Google, however I am having to manually insert the id that it is returning into the google_id field for an existing test user in order for that to work. As far as it actually automatically creating an account for the user, that is not happening, and I'm not sure how that should be done, from what I have read, it seems that the configuration that I now have should be doing that.

config.yml

hwi_oauth:
    resource_owners:
        google:
            type: google
            client_id: 123456789.apps.googleusercontent.com
            client_secret: SupercalifragaliZ
            scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
            user_response_class: 'Acme\UserBundle\OAuth\Response\GoogleUserResponse'
            paths:
                email: email
                profilepicture: picture
                firstname: given_name
                lastname: family_name
                gender: gender
                locale: locale
    firewall_name: secured_area
    fosub:
        username_iterations: 5
        properties:
            google: googleId
    connect: ~

security.yml

security:
    providers:
        fos_userbundle:
            id: fos_user.user_manager
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        secured_area:
            pattern:    ^/
            anonymous: true
            logout: true
            form_login:
                provider: fos_userbundle
                login_path: /login
                check_path: /login/login_check
                csrf_provider: form.csrf_provider
            oauth:
                resource_owners:
                    google: "/login/check-google"
                login_path: /connect
                failure_path: /connect
                oauth_user_provider:
                    service: hwi_oauth.user.provider.fosub_bridge

routing.yml

hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /connect

hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"

hwi_oauth_security:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix: /login

#fos_user_security:
#    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

google_login:
    pattern: /login/check-google

Acme/UserBundle/OAuth/Response/GoogleUserResponse.php

I would like to use this to add more data from google directly into the user's account. The idea is to allow a user to register with us through our own registration form, or to click on an OAuth provider, and we will then create their account for them. This response implementation would be the start of that, as we would be able to pull in the different fields that google provides after authorization.

<?php

namespace Acme\UserBundle\OAuth\Response;

use HWI\Bundle\OAuthBundle\OAuth\Response\AdvancedPathUserResponse;

/**
 * GoogleUserResponse
 *
 */
class GoogleUserResponse extends AdvancedPathUserResponse
{
    /**
     * {@inheritdoc}
     */
    public function getEmail()
    {
        return $this->getValueForPath('email', false);
    }

    /**
     * {@inheritdoc}
     */
    public function getProfilePicture()
    {
        return $this->getValueForPath('profilepicture', false);
    }

    /**
     * {@inheritdoc}
     */
    public function getFirstName()
    {
        return $this->getValueForPath('firstname', false);
    }

    /**
     * {@inheritdoc}
     */
    public function getLastName()
    {
        return $this->getValueForPath('lastname', false);
    }

    /**
     * {@inheritdoc}
     */
    public function getGender()
    {
        return $this->getValueForPath('gender', false);
    }

    /**
     * {@inheritdoc}
     */
    public function getLocale()
    {
        return $this->getValueForPath('locale', false);
    }
}
@nickrouty nickrouty mentioned this issue Jul 17, 2012
8 tasks
@mablae
Copy link
Contributor

mablae commented Jul 18, 2012

Hello,

I wanted the same thing: Creating users by Twitter Login.

But this is not possible atm. You have to be logged in to symfony2 already - than you can connect the accounts.

In Twitter Ouath you don't get the email address, so storing a "new" user is not possible.

@nickrouty
Copy link
Contributor Author

@mablae Ah - with Google you do. So in the case of Twitter, you should have any information filled in that IS retrievable populated into a form, and then the user just needs to fill in their email address. The twitter ID should be already set in the form in a hidden field. Was this the situation for you?

@mablae
Copy link
Contributor

mablae commented Jul 18, 2012

@nickrouty: Right, I had this registration-form once.

But now I got stuck with my config at all. So there is just the
message: "Could not connect Accounts" when using Twitter Button
without a logged in User.

Or sth else goes wrong, like the session before is still active i.e.
there is no logout done by HWIOauthBundle. I have to look deeper in
code, clear caches and so on.

I will do some more research this evening and then post again.

@quba
Copy link

quba commented Jul 19, 2012

Hi. What if I want to use other FormType for normal registration and other for OAuth registration? Tried to set:

connect:
       registration_form: my_form_type

But it looks like it needs Form and not FormType. Am I wrong?

@quba
Copy link

quba commented Jul 19, 2012

Solution how to define service returning Form:

my_user.oauth.registration.form:
        factory_method: createNamed
        factory_service: form.factory
        class: Symfony\Component\Form\Form
        arguments: [ %fos_user.registration.form.name%, @my_user.oauth.registration.form.type, null, { 'validation_groups': %fos_user.registration.form.validation_groups% } ] 

@vpassapera
Copy link

We really need to add proper documentation for integration with the FOSUserBundle.

I'm about to install this on my App, so whatever I find that is not straight forward, I'll try to help out with.

@justin-johnn
Copy link

Hello,

Shall I need to add new FOS\UserBundle\FOSUserBundle(), in AppKernel registerBundles function, in addition to new HWI\Bundle\OAuthBundle\HWIOAuthBundle() ?

Is it possible to load class FOS\UserBundle\FOSUserBundle() from composer instead adding in AppKernel?

@stof
Copy link
Contributor

stof commented Aug 24, 2012

@justin-amt I don't understand what you mean. Composer is about installing the packages (and autoloading them). The code in the registerBundles method creates an instance of the bundle class and give it to the kernel.

@asm89
Copy link
Contributor

asm89 commented Aug 27, 2012

Closing this. #67 keep track of the (lack) of documentation. ;)

@asm89 asm89 closed this as completed Aug 27, 2012
@justin-johnn
Copy link

@stof : What I mean is when I installing the HWIOAuthBundle, FOSUserBundle will also installed as added dependency written in hwioauthbundle composer.json file like

"require": {
       "friendsofsymfony/user-bundle": "*",
  },

After installing, I need to create an HWIOAuthBundle and FOSUserBundle instance in AppKernel.

Can I add HWIOAuthBundle instance only in Appkernel and create FOSUserBundle instance inside HWIOAuthBundle files?

Dependent Bundle instance should be avoided from AppKernel and it should be created inside HWIOAuthBundle files. Is it possible?

@stof
Copy link
Contributor

stof commented Sep 6, 2012

@justin-john no you cannot.

Thus, FOSUserBundle is not a required dependency of HWIOAuthBundle, only an optional one

@knallgelb
Copy link

hi, please complete the FOSUserBundle integration. or maybe you can send me your files, so that i can find out why it's not working in my bundle.

btw i get a bad curl_setopt error:
CRITICAL - ErrorException: Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /www/htdocs/.../Symfony/vendor/kriswallsmith/buzz/lib/Buzz/Client/AbstractCurl.php line 173 (uncaught exception) at /www/htdocs/.../Symfony/vendor/kriswallsmith/buzz/lib/Buzz/Client/AbstractCurl.php line 173

safemode is off (phpinfo();)

if you can help me - maybe you've got a amazon wishlist or something (donate) like that?

@Mopster
Copy link

Mopster commented Oct 9, 2012

Has anyone been able to fully integrate these 2 bundles ? I've had no luck so far. I'm trying with GitHub as resource-owner, I get the popup to authorize the request. I then get redirected back to the login page, but nothing happened.

I'm sure I'm missing some crucial steps, like how do I link my existing users to their GitHub accounts ?

I want to use the OAuth bundle to allow my existing (FOS)users to login to my CMS. I would love to see some more documentation on how to use this bundle.

@tobiassjosten
Copy link

I have the same problem but I see the error User '721814015' not found. where 721814015 is my Facebook ID.

The documentation isn't really clear and I have seen people both say that you can't have HWIOAuthBundle/FOSUserBundle automatically register new users as they connet AND people who say that it's workin fine for them.

If someone could shed some light on this I'd be immensely grateful.

@dagrinchi
Copy link

Hey tobiassjosten check the column type in the model for facebookId or googleId, string works for me.

@tobiassjosten
Copy link

I took the advice in #116 and that worked a charm.

@danvbe
Copy link

danvbe commented Jan 7, 2013

I have managed to install this… and make it work. I will present my solution. All the code is available in this Gist. Mostly it is as described in #116, with a bit of more code presented. The key aspects that lack in the #116 presentation (IMO) are:

  • the registration as service of your custom FOSUBUserProvider (with the necessary parameters)
  • set the service for oauth_user_provider in the security.yml with your custom created service

Here are the steps:

  1. Routing. In routing.yml I have added all the routes for both bundles.
  2. Configuration. I have set the config.yml mostly as it is presented in the HWIOAuthBundle.
  3. Security. I have set the security.yml mostly as it is presented in the HWIOAuthBundle (though my routes are using /login pattern, not /connect). Also, the oauth_user_provider is set for my custom service.
  4. User. My own User entity, extended from FosUser.
  5. UserProvider. My user provider, registered as service, extended from FOSUBUserProvider. This is the one that actually does the User registration in YOUR database with data from PROVIDERS (Facebook, Google, etc.)
  6. Custom service. My user provider is registered as service.

Using this code, when:

  1. No user is authenticated on my site: by accessing http://my_app_web_root/login/facebook or http://my_app_web_root/login/google, a user is created in my database (with data as it is saved in the custom FOSUBUserProvider) and it is automatically login-ed to my site.
  2. A user is authenticated on my site: by accessing http://my_app_web_root/login/facebook or http://my_app_web_root/login/google, the current user is updated with data from the provider (account linking).

I think this is the behavior everybody was expecting :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests