Skip to content

Instantly share code, notes, and snippets.

@simianhacker
Created June 8, 2009 05:42
Show Gist options
  • Save simianhacker/125649 to your computer and use it in GitHub Desktop.
Save simianhacker/125649 to your computer and use it in GitHub Desktop.
describe "Facebook Connect Authentication" do
it "should create a new user in the system when the Facebook Connect cookies are set" do
response = request(url(:home), :cookie=>facebook_cookies)
user = User.first(:facebook_id=>facebook_params[:user])
user.should_not be_nil
user.should be_valid
end
end
# Some helper methods to reduce a bunch of redundancy
# probably could use some refactoring but hey...
def facebook_params
{
:user => 123456,
:session_key => Digest::MD5.hexdigest('this is a fake session key'),
:ss => Digest::MD5.hexdigest('this is a fake session secert'),
:expires => (Time.new+3600).to_i
}
end # facebook_params
def facebook_key
Merb.config.fetch(:facebook_key, nil)
end # facebook_key
def facebook_secert
Merb.config.fetch(:facebook_secret, nil)
end # facebook_secert
# Generate the Facebook signature according to the Facebook Connect documentation
def generate_signature
Digest::MD5.hexdigest(facebook_params.to_mash.sort.map{|k,v| "#{k}=#{v}"}.join)
end # generate_signature
def facebook_cookies
[
"#{facebook_key}_user=#{facebook_params[:user]}",
"#{facebook_key}_session_key=#{facebook_params[:session_key]}",
"#{facebook_key}_expires=#{facebook_params[:expires]}",
"#{facebook_key}_ss=#{facebook_params[:ss]}",
"#{facebook_key}=#{generate_signature}",
]
end #facebook_cookies
class FacebookConnectStrategy < Merb::Authentication::Strategy
# Some convenience functions
include FacebookConnect::Mixins
def run!
Merb::logger.debug("Authenticating Facebook: #{facebook_id}")
unless user = User.first(:facebook_id => facebook_id)
# create a new user record for the facebook user.
user = User.new
user.facebook_id = facebook_id
user.first_name = "Facebook"
user.last_name = "User"
user.password = user.password_confirmation = Digest::MD5.hexdigest(facebook_id.to_s + Merb::Config.fetch(:facebook_secret, Time.now.to_i))
user.login = facebook_id.to_s
user.save
end # unless user
valid_facebook_user? ? user : nil
end # run!
end # FacebookConnectStrategy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment