NinjAuth: The Social Integration Package PHP has been dying for

Posted: 2011-09-17
Category: FuelPHP

In the past I have never needed to implement oAuth into a PHP project. I have done it in Rails and boy it was easy thanks to OmniAuth. OmniAuth abstracts away so much of the grunt work that it takes about 5 minutes to add a new social network to your site, and 4 of those minutes are spent signing up for the API keys. What options do we have in the world of PHP? A bunch of screwy hacks or provider specific classes like TwitterOAuth. I don't want to hunt down 20 libraries with different methods, I want to get a key, bang it in and go to the pub. Well, now I can!

NinjAuth - named after my company HappyNinjas - is a package which provides abstraction layer over various strategies such as oAuth and oAuth2 and sits on top of the built in Auth driver for handling users and registration. Multiple "authentications" can be made meaning you have a single user who has Twitter, Facebook, Instagram, etc all attached to his account so you can start doing fun things with their data.

fuel-oauth - A basic package which contains the providers for the oAuth strategy. This was a port of the great work done by shadowhand with his Kohana oAuth package. This is an open source world and I feel time can be better spent improving on existing code than re-inventing the wheel more than you need to. This code has been tweaked and improved upon and will get better over time.

fuel-oauth2 - Another simplistic package which has providers in the same format as fuel-oauth. Some of this code was provided by Calvin Froedge who supplied the codeigniter-oauth spark.

Using abstraction of strategies and providers to this level means we can easily add in OpenID or XAuth (pull request anyone?) and support new systems without needing to totally rewrite our apps. How many of you have had to recode the f**k out of your sites because of the upcoming Facebook oAuth2 Migration? Well if we had this system we would have just moved the Facebook class from one to the other, tweaked it and your application would have seamlessly switched over as you updated the package.

How do I use NinjAuth?!

The most important thing about this is that it is insanely easy.

  1. Install the package via Zip, Git or use Oil, whatever.
  2. Enable ninjauth, oauth, oauth2, orm and auth in config.php
  3. Create an authentication controller. I use "auth.php":
    class Controller_Auth extends \NinjAuth\Controller {}
  4. Add keys and secrets for all the providers you wish to use in config/ninjauth.php by either modifying the file in the package or copy it to the app/config folder:
    'providers' => array(
    	
    	'facebook' => array(
    		'id' => '',
    		'secret' => '',
    		'scope' => '',
    	),
    	
    	'github' => array(
    		'id' => '',
    		'secret' => '',
    		'scope' => 'user,public_repo',
    	),
    	
    	'twitter' => array(
    		'key' => '',
    		'secret' => '',
    	),
  5. Go to http://example.com/auth/session/facebook
  6. Laugh about how easy it all was.

Currently NinjAuth supports:

  • Dropbox
  • Twitter
  • Flickr
  • Google
  • GitHub
  • LinkedIn
  • Facebook
  • YouTube

More can be added pretty easily by adding them to the correct oauth package then modifying ninjauth/classes/strategy.php and adding to the array which lists which provider uses which strategy. Then add it to the bootstrap.php and send me a pull request.

Whats next?

The oauth2 package has been ported to CodeIgniter and Calvin is working on making the codeigniter-oauth Spark more awesome. Porting NinjAuth for CodeIgniter would be remarkably simple, but without a user package it's pretty pointless. I'll probably just write a blog about how to make your own in CI and use whatever user system you like. PyroCMS can expect to pick up a few sweet features from this code, potentially for v1.4 or v1.5.

These packages were the fruit of some freelance work done in London for the last few weeks (keeping up my Nomadic Web Development lifestyle of hostel living with hot French girls) and they will all become crucial parts of the next few applications I build.

What I want from people reading this is for them to download it, give it a go and provide feedback, build support for new providers (try Foursquare it's pretty easy but I refuse to sign up for an account) and help me make it even better. I firmly believe OmniAuth is one of the best projects in Rails-world and there is no reason PHP users shouldn't have something so damned useful.

Update: As Adam Fairholm quite rightly pointed out this is only for v1.1 which is still in development, but I have been using it for every client project in Fuel and it seems fine to me.

Comments

Gravatar
Stu Green

2011-09-17

Boom. Nice strapline ;-)

Gravatar
Albert Alemany

2011-09-17

Good Job! ;)

Gravatar
Ossama Khayat

2011-09-17

Been waiting for this and requesting it since Fuel started.
Thank you very much for making it happen :-)

Gravatar
Antti Peisa

2011-09-17

Looks great! Will definitely try this when I dive into fuel ;)

What are the biggest differences between this and ie. http://hybridauth.sourceforge.net/ ?

Gravatar

2011-09-17

Their code examples are scary chunks of weird looking camel-case. The main point of this was that implementation should be as simple as possible and with this package you're just dropping in the package, configuring and making an empty Controller which does everything for you.

Flexibility is obviously a little different but by extending the controller and adding a few config options we can make this simple, powerful AND flexible.

Gravatar
Dejan

2011-09-17

Great work Phil, thanks a lot.
Anyway, I would like to read a blog about hot French girls.
Please :D

Gravatar
Adam Fairholm

2011-09-18

Hey Phil,

This is fantasticly awesome. Just one thing - I think step 3 should be:

class Controller_Auth extends NinjAuth\Controller {}

Adam

Gravatar
Adam Fairholm

2011-09-18

Aha, I see what's happening. Hopefully this works:

class Controller_Auth extends NinjAuth\\Controller {}

Gravatar

2011-09-18

Backslashes got removed, but yes that is exactly right.

Gravatar
Adam Fairholm

2011-09-18

Also, for anyone getting errors of undefined methods, it looks like you need to be using the 1.1 development branch of Fuel core. NinjAuth relies on some new functions like Arr:get().

Gravatar
Coldclimate

2011-09-18

Great work, much needed.

Gravatar
Nevvermind

2011-09-19

Gee, give us a link, will ya?

Gravatar
Mike Purcell

2011-09-19

Awesome stuff. Looks like you cleaned up FB's mess with regards to their PHP SDK. Was hard to nail down the nuances when upgrading from 2.x to 3.x where they changed req_params to scope, and weak docs on how to upgrade to oauth2.

Gravatar
Amit Singh

2011-09-19

Awesome Phil, thanks for creating it.

Gravatar
Andreas Warnaar

2011-09-19

thanks and nice Coding

Gravatar
Taufan Aditya

2011-09-20

Now anyone no longer need to be late watching Dog Whisperer. In addition, will be nice if you wrote a little review about integrates this package with (...potentially) existing ACL, which may contain any possibilities, capabilities or/and built-in functions of your package, if any.
Nice one!

Gravatar

2011-09-20

Taufan: Do ACL however you normally would do in Fuel, it uses the built in driver and the built in ACL logic. :)

Gravatar
Jaap Rood

2011-09-20

Awesome package! This makes our lives so much easier, thanks Phil!

Gravatar
Wijnand

2011-10-09

Hi there Phil,

I just wanted to let you know how deeply thankfull I am for your efforts on this library. I've slightly rewritten it to make it work with my own framework en support php 5.2. Keep up the great work!

Gravatar
Matteo

2011-10-17

It need fuel to be used?

Gravatar
Tim

2011-11-24

I'm having a hard time getting twitter to work. I keep getting this error

OAuth\Exception [ Error ]: Error fetching remote https://api.twitter.com/oauth/request_token [ status 0 ] SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

I've set up virtual host on my dev machine so that shouldn't be a issue.

facebook will connect and auth but then i get this error

ErrorException [ Error ]: Class 'NinjAuth\Strategy_Array' not found

Gravatar
Harold

2011-12-06

Hi,

I get the same error that Tim gets,..

Fatal error: Class 'NinjAuth\Strategy_Array' not found in /projects/shopifyadmin/fuel/packages/ninjauth/classes/strategy.php on line 55

Anyone has a clue how to fix this?

Gravatar
Johan André

2011-12-14

Hi!

Great work!
I get the same error as Harold.

Something to do with using the develop-branch of FuelPHP?

Gravatar
Zarunor

2011-12-15

The problem you guys are having is due to the provider not getting set in the callback URL, so the wrong class name gets looked for. If I remember correctly, <a href="https://github.com/happyninjas/fuel-ninjauth/pull/10/files">unapplied pull request</a> fixes it.

There is at least one more bug in ninjauth and several in oauth2 that are breaking ninjauth. Several misnamed variables. I'm trying to get them sorted out.


Gravatar
Zarunor

2011-12-15

Actually, looks like they fixed the issues I was thinking of in some recent commits. I filed another couple issues on fuel-ninjauth and fuel-omniauth2 on github.

Gravatar
Paul

2012-01-17

thanks for this phil, once again amazing stuff....

Gravatar
Stuman

2012-02-16

Hi, does this work for CI 2.0?

Gravatar
Phil Sturgeon

2012-02-17

Stuman: This is FuelPHP specific code but I have ported the OAuth 1.0 and 2.0 packages to CodeIgniter:

https://github.com/philsturgeon/codeigniter-oauth
https://github.com/philsturgeon/codeigniter-oauth2

The only bit missing is the controller that handles it all, and that is going to be different for most people. There are examples on the README's of both projects.

Gravatar
Corinne

2012-02-21

Thanks for your work on this!

I have a question. Does the controller : class Controller_Auth extends NinjAuth\Controller &#123;&#125; need to be empty for NinjAuth to work? When I have functions in my auth controller, I get this error :

Fatal error: Class 'Auth_Login_NinjAuth' not found in packages/auth/classes/auth/login/driver.php on line 45

I'm hoping to use both NinjAuth and SimpleAuth for SSO and local registration, respectively.

Thanks for your help!

Gravatar
Shamier Coffie

2012-05-24

Nice post thnx!

Posting comments after three months has been disabled.