marcobuschini/o-auth-bundle

Integrating FOSUserBundle with OAuth

dev-master 2016-05-23 09:34 UTC

This package is not auto-updated.

Last update: 2024-05-22 23:46:46 UTC


README

I have been very disappointed by the HWIOAuthBundle lately, as it seems its documentation always lacks some essential step to be configured (at leas with my main OAuth provider, which is Google). I just wanted a bundle capable of logging a user in, and registering her, in a very simple way. And this bundle was born.

Install the bundle

At the present moment installation is supported only via git cloning. Create a path under the vendor directory named MLB/OAuthBundle, and run the following command inside it:


$ git clone git://github.com/marcobuschini/MLBOAuthBundle.git .

That done, you will have to activate the bundle in the app/AppKernel.php file. Simply add the followin line at the end of the $bundles:


new MLB\OAuthBundle\MLBOAuthBundle()

Configure the bundle

First, and foremost you have to properly install, and configure FOSUserBundle. Get their documentation for that. The minimal configuration required is as follows (goes to 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

Then we have to configure the OAuth parameters for Google OAuth. Get/set these from your developer console (goes to: 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

Add the table fields

What follows is an example user entity definition with all the fields required for running this bundle. In this case we only need the google_id, and the google_access_token that Google uses. It's quite simple as it extends the base FOSUserBundle class.


// 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;
    }
}

Wire the routing

Here are the routes that we have to add to app/config/routing.yml to have the application be able to login, and register a new user via this bundle. The google_login route is used to log a user in (i.e.: it is the entry point for the user). The google_connect route is invoked by the Google's OAuth servers to confirm that Google reconizes the user. The google_after_login route is invoked when the user has logged in successfully.


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 }

Gotchas

This is a very preliminary work. It suffers from many missing features, and probably some bugs, too. The most prominent feature missing is the connection of an already existant user to a Google user. Also, other OAuth providers must be added.