Created
June 8, 2009 05:42
-
-
Save simianhacker/125649 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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