Skip to content

Instantly share code, notes, and snippets.

@uzyn
Created June 25, 2012 04:45
Show Gist options
  • Save uzyn/2986576 to your computer and use it in GitHub Desktop.
Save uzyn/2986576 to your computer and use it in GitHub Desktop.
Tutorial for Opauth CakePHP plugin

Implement Opauth, a simple PHP multi-provider authentication framework, 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 (long and thorough)
  • A quick sample app (pre-configured CakePHP app, with screenshots)

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:
Demo homepage

After authentication, this is what you should be getting:
Demo callback

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

See also

Issues & questions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment