Authenticating to Twitter with Zend_Auth
UPDATE: I've refactored this project to be a generic OAuth adapter as opposed to an exclusively twitter adapter. The new github repo is available and code samples have been updated below.
I recently had the need to use Twitter as an authentication source for a Zend Framework application I was working on. The idea would be to allow my users to authenticate with their Twitter credentials as opposed to having them create a new account and password on my web app.
I already had a Zend_Auth implementation in place for my authentication needs, so I decided to create a Zend_Auth_Adapter that uses Twitter's OAuth service as an authentication source. I'm not going to go into details about how Twitter's OAuth service works as Twitter can do a much better job. I can show you how to use the adapter though, so here goes.
Step 1: Download the Adapter
The code for the adapter is available on GitHub at http://github.com/jfaustin/Zend_Auth_Adapter_Oauth. You can download that code and place the "Ja" folder (in /library/Ja) somewhere in your include path.
Step 2: Register your application with Twitter
To use Twitter's OAuth service, you have to register your application with them so they can create the required keys needed for authentication. You can do this by signing into Twitter and going to http://twitter.com/apps/new. Once you register your application, you will be presented with a consumer key and consumer secret. You will need to configure your adapter with these to actually do the authentication.
Step 3: Configure your adapter
You will need to configure your adapter to use the consumer key, consumer secret, and callback URL that are specific to your application. The callback URL will be the same URL which your users are directed to authenticate at, so if your authentication directs to http://yoururl.com/login, that will also be your callback URL.
<?php
require_once 'Zend/Auth.php';
require_once 'Ja/Auth/Adapter/Oauth/Twitter.php';
$consumerKey = 'replace with your consumer key';
$consumerSecret = 'replace with your consumer secret';
$callbackUrl = 'replace with your callback URL';
$adapter = new Ja_Auth_Adapter_Oauth_Twitter();
$adapter->setConsumerKey($consumerKey)
->setConsumerSecret($consumerSecret)
->setCallbackUrl($callbackUrl);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
?>
Replace the values for $consumerKey, $consumerSecret, and $callbackUrl with your application's specific values and you should be good to go. All you need to do now is direct your users to this page and the adapter will handle the rest. For a complete working example, see the code you downloaded from GitHub in the /example folder.
Developer Notes
To maintain state in the web application between acquiring the request token, sending the user to Twitter to authenticate, then getting them back and acquiring the access token, I am using Zend_Session to store the request token in the "Ja_Auth_Adapter_Oauth" session namespace. You can change this namespace if you want to by setting the "sessionNamespace" option in an option array.
$adapter = new Ja_Auth_Adapter_Oauth_Twitter();
$options = array(
'sessionNamespace' => 'myCustomNamespace'
);
$adapter->setOptions($options);
This adapter also expects to have the "oauth_token" parameter set in the URL query string when it is redirected back from Twitter. I'm still considering the ramifications of this, as it may be better to allow this to be configured when the adapter is instantiated, but I haven't decided yet. Some feedback on that would be good.
UPDATE: I've changed the way this functions, as now you can call the setQueryData($_GET) method on the adapter to populate the oauth_token parameter.
On another note, I have submitted a proposal for this code to be included with Zend Framework. If you are interested, the proposal is at http://framework.zend.com/wiki/display/ZFPROP/Zend_Auth_Adapter_Twitter+-+Jason+Austin. I'd love to hear your feedback, either on the ZF contributors wiki or here.
TriPUG Slides and Code on Zend Framework
Here are the slides from my talk at TriPUG on Zend Framework components.
If you are interested in the code, it is hosted on GitHub for download and distribution. Special thanks for everyone coming out tonight to hear my talk. If you guys have questions, please let me know.
Presenting at September’s TriPUG meetup
On Tuesday September 21st I will be presenting at TriPUG, the Triangle's PHP User Group. I'll be giving a quick overview of Zend Framework, then doing several "mini-presentations" about ways to use different aspects of Zend Framework. My mini presentations will be about:
- Validators
- Filtering Input
- Zend_Form
- RSS Feeds
- Delpoying an API with ZF
- Zend_Date
- Zend_Config
- Zend_Log
- Zend_Mail
- Zend_Registry
This is also the first (of hopefully many) PHP meetups at NC State. The group typically meets in Carrboro which is a good ways from where I live, so this is part of an effort to get more meetups in Raleigh. If you are a PHP person in the Triangle, I'd like to encourage you to get involved in the community and come to the meetup. You can RSVP from meetup.com.
I'm looking forward to being able to present, especially about something that I really love. Let's hope we have a big crowd that continues to come back and contribute to the group.
tekx Day 2 Sessions
Day 2 of tekx was actually the first real conference day full of sessions. The day started out with a bang from Josh Holmes who delivered a keynote that rang true in so many ways. His talk was about the importance of keeping our programming and our problems simple. I am as guilty as any developer of over-engineering a problem and it has slowed me down quite a few times. An excellent example that sticks out to me was when he talked about how the initial idea of twitter was so simple, most of us developers would have been like "it's not worth us working on it because that is a way too simple problem to solve". He's right too. When I first saw twitter, I was like "I could do that". But the point is, I didn't because I was too concerned with the complex problems. Anyway, it was a great keynote. I even tweeted during that keynote that I was going to make all my developers watch it.
I went to a few other interesting talks as well.
- I started with Rob Allen's talk on Zend_Form which really was a refresher course to me. I did catch a few new hints, mainly using a translator to do custom error messages in Zend_Form.
- I followed that up with a talk about Graph theory which was incredibly relevant to the state of social media. It was also a good bit of stuff I hadn't heard about since I was in college, so it was good to get back into that.
- I spent a dual session hanging out with Keith Casey and writing some Flex. I used Flex a few years ago but never had a use for it, but it seems to have grown up a bit since I used it last. The talk got me thinking about ways I can apply it to some of my projects.
- Next up was Eli White's talk on code and release management. I had one of those moments that you have at these conferences like "thank god I'm doing this right". It was good to hear that we hadn't screwed anything up too bad...yet.
- The last talk of the day was Matthew Weier O'Phinney about NoSQL. Matthew always does a good talk that is really informative, and this was no different. I'm definitely going to look into Mongo DB, which is a NoSQL database, when we get back.
The day ended with a few of us going out to Gino's Pizza for dinner. It was pretty good, but Giordano's was better IMO. When we got back, I was so exhausted that I went to bed early. Long day of learning.


