2017 © Pedro Peláez
 

symfony-bundle o-auth-bundle

Integrating FOSUserBundle with OAuth

image

marcobuschini/o-auth-bundle

Integrating FOSUserBundle with OAuth

  • Monday, May 23, 2016
  • by marcobuschini
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 5 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Purpose of this bundle

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., (*1)

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:, (*2)


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

<, (*3)

p>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:, (*4)


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):, (*5)


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):, (*6)


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., (*7)


// 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., (*8)


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., (*9)

The Versions

23/05 2016

dev-master

9999999-dev

Integrating FOSUserBundle with OAuth

  Sources   Download

LGPL-3.0

The Requires

 

by Marco Buschini

fosub oauth