Strict Standards: Redefining already defined constructor for class Object in /home/dizz/cake_1.2.5/cake/libs/object.php on line 63

Strict Standards: Non-static method Configure::getInstance() should not be called statically in /home/dizz/cake_1.2.5/cake/bootstrap.php on line 47
Using CakePHP and RPX for Social Authentication | andrw.net

Using CakePHP and RPX for Social Authentication

Posted by Andrew on November 21st 2009, 10:24pm

I am working on a social network application where I want to allow users to import their information from another social network or service that provides an open authentication. I looked at Facebook connect and Twitter sign on but after doing research the easiest solution for me was using RPX's hosted solution. It took me about 10 minutes to implement and maybe another hour to fine tune to my needs.

To start you will need to register with RPX and then set up your widget. Some of the providers (such as Facebook and Twitter) need you to set up the application on their network first and then they will provide you with an API key that you can then plug into RPX.

After setting up the widget they will provide you with some code to copy and paste, there really isn't any modifications needed to the code except setting the token_url which is used as a redirect url once the user has been authenticated. You will probably want to set the token_url to a registration page where the user can enter a username and password for CakePHP's authentication component.

Once you have everything in place you will need to set up some code for the action that corresponds with the token_url. First you will need to receive the token from RPX used to retrieve the user's data, then you will need to request the user's info using their API. Here is the code that I am using in my action.

App::import('Core', 'HttpSocket');
$http = new HttpSocket();

$token = $this->params['form']['token'];
$results = $http->post(
'https://rpxnow.com/api/v2/auth_info',
array(
'token' => $token,
'apiKey' => 'YOUR-API-KEY-HERE'
)
);
$user = json_decode($results);

I am using CakePHP's built in HttpSocket class to request the user's information. The information sent from RPX is in JSON format so I used PHP 5's built in json_decode method to create an object out of the json. Now you would most likely create a new user record and save the information received from RPX and in the view display a form to collect the rest of the user's credentials such as username and password.

That's it, it's very simple to use. If you have any questions leave a comment and I will get back to you as soon as possible.

Bookmark and Share
Posted in CakePHP

4 Comments

Ryan R. said on November 22nd 2009, 7:20pm
Hi, Thanks a lot for this post. I was planning on using RPX in my next project this should save me some time. Great blog by the way as well.
Andrew said on November 22nd 2009, 7:23pm
Hi Ryan. Thanks, glad you found it useful.
Naoise said on June 6th 2010, 5:14pm
hi. thanks for the post! it really looks simple.
But I get an empty $results response.
Maybe I am not doing the url process correctly:

My login widget is at http://localhost/app/
In the login widget's settings, I set the token_url to http://localhost/app/users/rpx

then I put the code you provided in the users_controller
function rpx() {
}

When I debug $token, it appears correctly, but when I var_dump(json_decode($results, true)); it comes out NULL.

I'd appreciate any help, thanks a lot.
Matt Kaufman said on October 14th 2010, 3:56am
Thanks for the notes on using HttpSocket in Cake
Add New Comment