Tonight I’m releasing my first Rails plugin. This is the result of about half the work I did with my team at Rubyred Labs at Yahoo Hack Day 2006. I wrote the guts, but generous amounts of Rails plugin goodness were provided by Scott Fleckenstein.
The Plugin:
It’s a drop-in addition to ActionController that allows you to define a consumer of Yahoo’s unified login scheme that was released last week, called Browser Based Authentication, or BBAuth. More info
It’s configurable so that you may define your own endpoint within the Rails app and a controller method is defined to allow you to easily retrieve the necessary credentials for authenticated API calls after the initial login. It’s up to you to handle the storage of the returned cookie and wssid.
Many of Yahoo’s APIs are currently open, and each of them is slightly different. I left this complexity out of the plugin to make it as simple to use and unobtrusive as possible. You can contact me at cameron at this domain if something is busted. I’d like this plugin to “just work” so if something is wrong, let me know.
You can see the plugin in action in a dummy application at http://hackday2006.rubyredlabs.com/
Instructions:
Unzip the archive and drop it in your vendor/plugins folder.
You’ll need to register your application with Yahoo here. Configuring the plugin is easy, after you have followed all the instructions on Yahoo’s developer website about authenticating your application’s domain.
The only necessary configuration is to place the following in config/yahoo.yml:
application_id: YOUR_APPLICATION_ID
secret: YOUR_SECRET
Download: Yahoo BBAuth plugin
Sample Controller
class UsersController < ApplicationController
authorizes_through_yahoo
def index
end
verify :params => [:token, :sig, :appid, :ts], :only => :get_credentials, :redirect_to => :index
def get_credentials
@credentials = request_yahoo_credentials(params[:token])
end
end
In this example, assuming the route ':controller/:action/:id' exists, the value to enter in “Web Application URL” when registering your app with Yahoo is http://your-domain.com/users/get_credentials
Update: Looks like the error messages weren’t being passed through to the exception. Code has been updated!
NEW:
raise YahooAuthorizationException.new("Yahoo BBAuth error: %s %s" %
[ doc.elements['//Error/ErrorCode'].text.strip,
doc.elements['//Error/ErrorDescription'].text.strip ]) unless doc.elements['//Success']
OLD:
raise YahooAuthorizationException.new("Yahoo BBAuth error: :code :message" %
{ :code => doc.elements['//Error/ErrorCode'].text.strip,
:message => doc.elements['//Error/ErrorDescription'].text.strip }) unless doc.elements['//Success']
Technorati Tags: yahoo hackday06 bbauth yahoobbauth authentication rubyredlabs