Simple 3rd-party provider authentication with Opauth plugin

by uzyn
Opauth is a multi-provider authentication framework for PHP, inspired by OmniAuth for Ruby. Opauth enables PHP applications to perform user authentication across different providers with much ease & simplicity.

Implement Opauth easily on CakePHP

What is Opauth?

Opauth is a multi-provider authentication framework for PHP, inspired by OmniAuth for Ruby.

Opauth enables PHP applications to perform user authentication across different providers with much ease & simplicity.

Opauth interfaces between authentication providers' API and your PHP applications through strategies. Strategies available for Opauth include Facebook, Google, Twitter, OpenID, and more.

Visit http://opauth.org for a quick demo.

Opauth on GitHub: uzyn/opauth
Opauth as a Composer package: opauth/opauth

Opauth on CakePHP

Opauth is made even easier to be implemented on CakePHP applications through Opauth CakePHP plugin (download).

Quick table of contents:

How to use this plugin

  1. Download Opauth CakePHP plugin and place it at your CakePHP Plugin directory.

    Or via Git:
    Assuming APP is the directory where your CakePHP app resides, it's usually app/ from the base of CakePHP.

    cd APP/Plugin
    git clone git://github.com/uzyn/cakephp-opauth.git Opauth
    
    cd Opauth
    git submodule init
    git submodule update
    
  2. Add this line to the bottom of your app's Config/bootstrap.php :

    <?php
    CakePlugin::load('Opauth', array('routes' => true, 'bootstrap' => true));
    

    Overwrite any Opauth configurations you want after the above line.

  3. Load strategies onto Strategy/ directory.

    Append configuration for strategies at your app's Config/bootstrap.php as follows:

    <?php
    CakePlugin::load('Opauth', array('routes' => true, 'bootstrap' => true));
    
    // Using Facebook strategy as an example
    Configure::write('Opauth.Strategy.Facebook', array(
       'app_id' => 'YOUR FACEBOOK APP ID',
       'app_secret' => 'YOUR FACEBOOK APP SECRET'
    ));
    
  4. Go to http://path_to_your_cake_app/auth/facebook to authenticate with Facebook, and similarly for other strategies that you have loaded.

  5. After validation, user will be redirected to Router::url('/opauth-complete') with validated auth response data retrievable available at $this->data .

    To route a controller to handle the response, at your app's Config/routes.php , add a connector, for example:

    <?php
    Router::connect(
       '/opauth-complete/*', 
       array('controller' => 'users', 'action' => 'opauth_complete')
    );
    

    You can then work with the authentication data at, say APP/Controller/UsersController.php as follows:

    <?php // APP/Controller/UsersController.php:
    class UsersController extends AppController {
       public function opauth_complete() {
           debug($this->data);
       }
    }
    

    Note that this CakePHP Opauth plugin already does auth response validation for you with its results available as a boolean value at $this->data['validated'] .

How about a sample?

Sure. Simply download this CakePHP app and set it up with CakePHP v2.x library.

Once it is set up, you should see: (Homepage screenshot)

After authentication, this is what you should be getting: (Callback screenshot)

More instructions on the sample app: https://github.com/uzyn/cakephp-opauth/tree/sample

See also

Issues & questions

Report

More on Tutorials

Advertising

Comments

  • hrshtsarma posted on 08/13/12 09:03:23 PM
    Fatal error: Class 'CakePlugin' not found in /var/www/cakephp/app/config/bootstrap.php on line 43

    class cakeplugin is present in /var/www/cakephp/lib/Cake/Core

    I am trying to use google oauth plugin for cakephp app , after following steps as given on :

    https://github.com/uzyn/opauth-google/blob/master/README.md
    But when I run my app I get above mentioned fatal error,

    Do I need to change the links or copy the plugin file ? Or something wrong is ther ein my approach?
  • uzyn posted on 07/02/12 03:18:54 AM
    @thehanx

    I'm guessing your CakePHP app is not at DocumentRoot (eg. http://localhost) but at a directory (eg. http://localhost/cakeapp).

    If that's true, please add this line to your app's Bootstrap.php:

    Configure::write('Opauth.path', '/cakeapp/auth/');

    Thanks.
  • uzyn posted on 07/02/12 03:17:20 AM
    @thehanx

    I'm guessing your CakePHP app is not at DocumentRoot (eg. http://localhost) but at a directory (eg. http://localhost/cakeapp).

    If that's true, please add this line to your app's Bootstrap.php:

    Configure::write('Opauth.path', '/cakeapp/auth/');

    Thanks.
  • thehanx posted on 06/26/12 05:41:09 PM
    I was following your tutorial here.. but not working in my app. when i access http://myapp-path/auth/facebook I got error missing controller, Error: OpauthController could not be found.

    and when I install your sample application, it said:
    Notice (8): Undefined index: strategy [APP\Plugin\Opauth\Vendor\Opauth\lib\Opauth\Opauth.php, line 120]
    Fatal Error (256): Unsupported or undefined Opauth strategy - [APP\Plugin\Opauth\Vendor\Opauth\lib\Opauth\Opauth.php, line 120]
    why this happen? whats the solution?

    nb: sorry about my english..
login to post a comment.