OpenId 2.0 and Ruby on Rails 2.0.2
I just converted a website from the openid_consumer plugin to DHH’s open_id_authentication plugin and patched it for OpenId 2.0.
I found face’s port of Dr. Nic’s sample open_id_authentication app to be a useful starting point. I ended up chopping down the open_id_helpers class to this:
module OpenIdsHelper
# Pass optional :required and :optional keys to specify what sreg fields you want.
# Be sure to yield registration, a third argument in the #authenticate_with_open_id block.
# REMEMBER: a "required" field is not guaranteed to be returned by OpenID provider
def open_id_authentication
authenticate_with_open_id( params[:openid_url],
:required => [ :nickname, :email ],
:optional => [ :fullname ] ) do |result, identity_url, registration|
if result.successful?
successful_openid_login(identity_url, registration)
else
failed_openid_login(result.message || "Sorry could not log in with identity URL: #{identity_url}")
end
end
end
private
def successful_openid_login(identity_url, registration = {})
throw “Implement me in this controller!”
end
def failed_openid_login(message)
throw “Implement me in this controller!”
end
end
implement the pass/fail methods in the controller that includes OpenIdsHelper and call open_id_authentication and you are off to the races.
I hit a couple hiccups here and there and submitted some patches to the patch ticket on Rails Trac. It is important to note that authentication a Yahoo OpenId will fail unless you are authenticating from a server with a real hostname. Face made a post about this recently too which quotes yahoo’s security policy.
I also modified Jainrain’s ruby-openid gem to allow symbols as well as strings to be passed into sreg. Their trac instance is down right now but when it comes back up I will submit a patch and link to it.