Search code examples
authenticationsymfonyoauth-2.0symfony-2.1hwioauthbundle

Symfony2.1 - HWIOAuthBundle and google oauth authentication


I am trying to setup Symfony2.1 with HWIOAuthBundle to handle google oauth authentication.

I have followed manual instructions in bundle description, but still something is missing. When I visit an action which requires authenticated user (or just type url /connect/google) I am redirected to google authentication page - this part is working. But after redirection back (after logging to Google and agree for sharing my data) new user is not created in DB and I am redirected into connect/fail action. Google authentication should be only one way to authenticate user. Any idea what could be wrong?

This is my routing:

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

google_login:
    pattern: /login/check-google

security.yml

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

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

and config

hwi_oauth:
    firewall_name: secured_area
    resource_owners:
            google:
                type:                google
                client_id:           ....
                client_secret:       ......
                scope:               "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
    fosub:
        username_iterations: 5
        properties:
            google: googleId

google redirectURL

http://xxxx/login/check-google

my User entity:

class User extends BaseUser
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var integer $id
     *
     * @ORM\Column(name="google_id", type="integer")
     */ 
    protected $googleId;

//edit- update

I just found in Symfony logs that I had one request from google redirect: GET http://xxxxx.dk/login/check-google?code=4%2F8tiDAhI45D2g7BvrdtwoF7wJ.Eu7i2fmkYOl05ti8ZT3YDrXXXXXfakeXXXE4hcwI just before redirect to /connect-file
Unfortunately it looks that request only tried to retrieve user from my db based on google_id SELECT * FROM app_user t0 WHERE t0.googleId = ? LIMIT 1 : ['123456789'] but that user does not exist there.

Should I create user befor I tryied to authenticate him/her?


Solution

  • Sadly, HWIOAuthBundle doesn't handle automatic registration by default. If you want to login now you'll have to create the user manually.