upstream-simple_facebook_connect 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. data/.gitignore +1 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +90 -0
  4. data/Rakefile +27 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/simple_facebook_connect/connect_controller.rb +59 -0
  7. data/app/views/shared/_facebook_connect_button.html.erb +15 -0
  8. data/config/simple_facebook_connect_routes.rb +4 -0
  9. data/generators/simple_facebook_connect_features/simple_facebook_connect_features_generator.rb +14 -0
  10. data/generators/simple_facebook_connect_features/templates/facebook_auth_getsession.xml +9 -0
  11. data/generators/simple_facebook_connect_features/templates/facebook_connect.feature +27 -0
  12. data/generators/simple_facebook_connect_features/templates/facebook_connect_steps.rb +19 -0
  13. data/generators/simple_facebook_connect_features/templates/facebook_connect_stubs.rb +44 -0
  14. data/generators/simple_facebook_connect_features/templates/facebook_users_getinfo.xml +119 -0
  15. data/generators/simple_facebook_connect_migration/simple_facebook_connect_migration_generator.rb +13 -0
  16. data/generators/simple_facebook_connect_migration/templates/migration.rb +17 -0
  17. data/init.rb +35 -0
  18. data/lib/simple_facebook_connect.rb +14 -0
  19. data/lib/simple_facebook_connect/controller_extension.rb +11 -0
  20. data/lib/simple_facebook_connect/extensions/routes.rb +13 -0
  21. data/lib/simple_facebook_connect/parser.rb +161 -0
  22. data/lib/simple_facebook_connect/service.rb +34 -0
  23. data/lib/simple_facebook_connect/session.rb +73 -0
  24. data/lib/simple_facebook_connect/user.rb +26 -0
  25. data/lib/simple_facebook_connect/user_extension.rb +21 -0
  26. data/rails/init.rb +1 -0
  27. data/simple_facebook_connect.gemspec +71 -0
  28. data/spec/fixtures/facebook.auth.getSession/default.xml +9 -0
  29. data/spec/fixtures/facebook.users.getInfo/default.xml +119 -0
  30. data/spec/lib/simple_facebook_connect/parser_spec.rb +12 -0
  31. data/spec/lib/simple_facebook_connect/user_extension_spec.rb +16 -0
  32. data/spec/lib/simple_facebook_connect/user_spec.rb +30 -0
  33. data/spec/spec_helper.rb +31 -0
  34. metadata +89 -0
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.sqlite3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ ## Simple Facebook Connect
2
+
3
+ This plugin adds the ability to sign in/sign up using facebook connect to your Rails application.
4
+
5
+ ## Disclaimer
6
+
7
+ Most of the code here is a direct rip-off of the facebooker gem. We wanted something much simpler than facebooker so we took the code we needed, refactored and changed bits and pieces and here we are.
8
+
9
+ ## Basic process
10
+
11
+ With this plugin you will add the well know facebook connect button to your site. When a visitor clicks on it:
12
+
13
+ * she will be redirected to facebook
14
+ * after signing in at facebook be directed back to your signup page
15
+ * there she will sign up as usual but without being asked to enter a password
16
+ * after signing up she will be able to sign into your site by clicking the facebook connect button
17
+
18
+ When an existing user of your site clicks the button he will also be able to log in via facebook, given the email address he signed up with at your site is also known to facebook.
19
+
20
+ ## Installation
21
+
22
+ This plugin assumes you have a `User` model with an `email` attribute. It will add some extensions to your `User` model and controllers in order to provide the necessary hooks and calls for facebook to authenticate your users.
23
+
24
+ Install the gem:
25
+
26
+ sudo gem install upstream-simple_facebook_connect
27
+
28
+ Add it as a dependency to your Rails application:
29
+
30
+ # environment.rb
31
+ gem 'upstream-simple_facebook_connect', :source => 'http://gems.github.com'
32
+
33
+ Run the migration generator and migrate:
34
+
35
+ script/generate simple_facebook_connect_migration
36
+ rake db:migrate
37
+ rake db:test:clone
38
+
39
+ Add the facebook connect button to your sign up and/or sign in pages. We have prepared a partial for you:
40
+
41
+ <%= render :partial => 'shared/facebook_connect_button' %>
42
+
43
+ Add this to your signup action (`users/create` in most cases):
44
+
45
+ class UsersController < ApplicationController
46
+ def create
47
+ @user = # initialize user
48
+ ....
49
+ @user.fb_uid = facebook_user.uid if facebook_user
50
+ @user.save
51
+ end
52
+ end
53
+
54
+ Change your signup form to not show any password fields when signing up via facebook:
55
+
56
+ <%- unless facebook_user -%>
57
+ <%= f.password_field :password -%>
58
+ ...
59
+ <%- end -%>
60
+
61
+ Provide a `log_in(user)` method, either in your `ApplicationController` or by subclassing `SimpleFacebookConnect::ConnectController`:
62
+
63
+ class AppicationController < ActionController::Base
64
+
65
+ ...
66
+
67
+ private
68
+
69
+ def log_in(user)
70
+ session[:user_id] = user.id # or whatever your app does
71
+ redirect_to account_path
72
+ end
73
+ end
74
+
75
+ Optional step (well, you really should do this): Generate Cucumber Features:
76
+
77
+ script/generate simple_facebook_connect_features
78
+
79
+ This will generate a feature, step definitions and fixtures to test the facebook connect integration. You will probably have to adjust those files to fit your app, for example we are using _Machinist_ to generate `User` instances.
80
+
81
+ Finally: run the features and by now everything should be green. Congrats.
82
+
83
+ ## Links
84
+
85
+ * facebooker: http://facebooker.rubyforge.org/
86
+ * contact: http://upstream-berlin.com
87
+ * email: alex [at] upstream-berlin dot com
88
+
89
+
90
+ Copyright (c) 2009 Alexander Lang, Frank Prößdorf, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+ require 'spec/rake/spectask'
6
+
7
+ begin
8
+ require 'jeweler'
9
+ Jeweler::Tasks.new do |gem|
10
+ gem.name = "simple_facebook_connect"
11
+ gem.summary = %Q{This plugin adds the ability to sign in/sign up using facebook connect to your Rails application.}
12
+ gem.email = "alex@upstream-berlin.com"
13
+ gem.homepage = "http://github.com/upstream/simple_facebook_connect"
14
+ gem.authors = ["Alexander Lang", 'Frank Prößdorf']
15
+ end
16
+
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ desc 'Default: run specs.'
22
+ task :default => :spec
23
+
24
+ desc "Run all specs"
25
+ Spec::Rake::SpecTask.new(:spec) do |t|
26
+ t.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,59 @@
1
+ module SimpleFacebookConnect
2
+ class ConnectController < ApplicationController
3
+
4
+ def authenticate
5
+ redirect_to new_facebook_session.login_url
6
+ end
7
+
8
+ def connect
9
+ begin
10
+ new_facebook_session(params['auth_token'])
11
+ if facebook_user
12
+ unless log_in_via_fb_id(facebook_user) || log_in_via_email_hash(facebook_user)
13
+ redirect_to(signup_path)
14
+ end
15
+ else
16
+ render(:nothing => true)
17
+ end
18
+ rescue StandardError => e
19
+ Rails.logger.warn e.message
20
+ redirect_to fb_authenticate_path
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def signup_path
27
+ new_user_path(:fb_user => 1)
28
+ end
29
+
30
+ def new_facebook_session(token = nil)
31
+ facebook_session = Session.new(SimpleFacebookConnect.api_key, SimpleFacebookConnect.secret_key)
32
+ if token
33
+ facebook_session.auth_token = token
34
+ facebook_session.secure!
35
+ session[:facebook_session] = facebook_session
36
+ end
37
+ facebook_session
38
+ end
39
+
40
+ def log_in_via_fb_id(facebook_user)
41
+ if user = ::User.find_by_fb_uid(facebook_user.uid)
42
+ log_in(user)
43
+ true
44
+ end
45
+ end
46
+
47
+ def log_in_via_email_hash(facebook_user)
48
+ facebook_user.email_hashes.each do |hash|
49
+ if user = ::User.find_by_email_hash(hash)
50
+ user.update_attribute(:fb_uid, facebook_user.uid)
51
+ log_in(user)
52
+ return true
53
+ end
54
+ end
55
+ false
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,15 @@
1
+ <fb:login-button onclick="redirect_to_authenticate()"></fb:login-button>
2
+ <%= javascript_include_tag 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php' %>
3
+ <script type="text/javascript" charset="utf-8">
4
+ function redirect_to_authenticate() {
5
+ FB.Facebook.get_sessionState().waitUntilReady(function() {
6
+ window.location = window.facebook_authenticate_location;
7
+ });
8
+ };
9
+
10
+ window.api_key = "<%= SimpleFacebookConnect.api_key %>";
11
+ window.facebook_authenticate_location = '<%= fb_authenticate_path %>';
12
+ window.xd_receiver = '/xd_receiver.html';
13
+
14
+ FB.init(window.api_key, window.xd_receiver);
15
+ </script>
@@ -0,0 +1,4 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.fb_connect '/fb/connect', :controller => 'simple_facebook_connect/connect', action: 'connect'
3
+ map.fb_authenticate '/fb/authenticate', :controller => 'simple_facebook_connect/connect', action: 'authenticate'
4
+ end
@@ -0,0 +1,14 @@
1
+ class SimpleFacebookConnectFeaturesGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |r|
4
+ r.file 'facebook_connect.feature', "features/facebook_connect.feature"
5
+ r.file 'facebook_connect_steps.rb', "features/step_definitions/facebook_connect_steps.rb"
6
+ r.file 'facebook_connect_stubs.rb', "features/support/facebook_connect_stubs.rb"
7
+
8
+ r.directory 'features/fixtures/facebook.auth.getSession'
9
+ r.file 'facebook_auth_getsession.xml', "features/fixtures/facebook.auth.getSession/default.xml"
10
+ r.directory 'features/fixtures/facebook.users.getInfo'
11
+ r.file 'facebook_users_getinfo.xml', "features/fixtures/facebook.users.getInfo/default.xml"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <auth_getSession_response
3
+ xmlns="http://api.facebook.com/1.0/"
4
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
+ xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd">
6
+ <session_key>5f34e11bfb97c762e439e6a5-8055</session_key>
7
+ <uid>8055</uid>
8
+ <expires>1173309298</expires>
9
+ </auth_getSession_response>
@@ -0,0 +1,27 @@
1
+ Feature: facebook connect
2
+ In order to get more users
3
+ As a site owner
4
+ I want users to easily sign in and sign up via facebook
5
+
6
+ Scenario: sign in via facebook when being connected
7
+ Given a user who is connected via facebook
8
+ When I go to the start page
9
+ And I sign in via facebook
10
+ Then I should see "Welcome"
11
+
12
+ Scenario: sign in via facebook when not being connected
13
+ Given a user with the email "email@person.com"
14
+ When I go to the start page
15
+ And I sign in via facebook
16
+ Then I should see "Welcome"
17
+
18
+ Scenario: sign up via facebook
19
+ When I go to the start page
20
+ And I sign up via facebook
21
+ Then I should not see "Password"
22
+ When I fill in "Email" with "email@person.com"
23
+ And I press "Sign Up"
24
+
25
+ Then I should see "instructions for confirming"
26
+ And a confirmation message should be sent to "email@person.com"
27
+ And my facebook id should be set
@@ -0,0 +1,19 @@
1
+ When /I sign up via facebook/ do
2
+ get fb_authenticate_path
3
+ response.redirected_to.should include('facebook.com')
4
+ # go to facebook and come back with auth token
5
+ post fb_connect_path, auth_token: '3e4a22bb2f5ed75114b0fc9995ea85f1'
6
+ visit response.redirected_to
7
+ end
8
+
9
+ When /I sign in via facebook/ do
10
+ When 'I sign up via facebook'
11
+ end
12
+
13
+ Then 'my facebook id should be set' do
14
+ User.last.fb_uid.should == 8055 # taken from xml fixture
15
+ end
16
+
17
+ Given /a user who is connected via facebook/ do
18
+ User.make fb_uid: 8055 # make from machinist
19
+ end
@@ -0,0 +1,44 @@
1
+ module SimpleFacebookConnect
2
+ module ServiceStub
3
+ def read_fixture(method)
4
+ if File.exists?(fixture_path(method, 'default'))
5
+ File.read fixture_path(method, 'default')
6
+ else
7
+ e.message << "\nFacebook API Reference: http://wiki.developers.facebook.com/index.php/#{method.sub(/^facebook\./, '')}#Example_Return_XML"
8
+ raise e
9
+ end
10
+ end
11
+
12
+ def post(params)
13
+ method = params.delete(:method)
14
+ params.delete_if {|k,_| [:v, :api_key, :call_id, :sig].include?(k) }
15
+ Parser.parse(method, read_fixture(method))
16
+ end
17
+
18
+ private
19
+
20
+ def fixture_path(method, filename)
21
+ File.join(Rails.root + 'features/fixtures', method, "#{filename}.xml")
22
+ end
23
+ end
24
+
25
+ module SessionStub
26
+ def service
27
+ unless @service
28
+ service = Service.new(Session::API_SERVER_BASE_URL, Session::API_PATH_REST, @api_key)
29
+ service.extend(ServiceStub)
30
+ @service = service
31
+ else
32
+ @service
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ SimpleFacebookConnect::Session.class_eval do
39
+ def self.new(*args)
40
+ session = super
41
+ session.extend SimpleFacebookConnect::SessionStub
42
+ session
43
+ end
44
+ end
@@ -0,0 +1,119 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <users_getInfo_response xmlns="http://api.facebook.com/1.0/"
3
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+ xsi:schemaLocation="http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd" list="true">
5
+ <user>
6
+ <uid>8055</uid>
7
+ <about_me>This field perpetuates the glorification of the ego. Also, it has a character limit.</about_me>
8
+ <activities>Here: facebook, etc. There: Glee Club, a capella, teaching.</activities>
9
+ <affiliations list="true">
10
+ <affiliation>
11
+ <nid>50453093</nid>
12
+ <name>Facebook Developers</name>
13
+ <type>work</type>
14
+ <status/>
15
+ <year/>
16
+ </affiliation>
17
+ </affiliations>
18
+ <birthday>November 3</birthday>
19
+ <books>The Brothers K, GEB, Ken Wilber, Zen and the Art, Fitzgerald, The Emporer's New Mind, The Wonderful Story of Henry Sugar</books>
20
+ <current_location>
21
+ <city>Palo Alto</city>
22
+ <state>California</state>
23
+ <country>United States</country>
24
+ <zip>94303</zip>
25
+ </current_location>
26
+ <education_history list="true">
27
+ <education_info>
28
+ <name>Harvard</name>
29
+ <year>2003</year>
30
+ <concentrations list="true">
31
+ <concentration>Applied Mathematics</concentration>
32
+ <concentration>Computer Science</concentration>
33
+ </concentrations>
34
+ </education_info>
35
+ </education_history>
36
+ <email_hashes list="true">
37
+ <email_hash_elt>2781152470_9f9c29692798573d8c76eaaf053a1911</email_hash_elt>
38
+ </email_hashes>
39
+ <family list="true">
40
+ <family_elt list="true">
41
+ <family_elt_elt>mother</family_elt_elt>
42
+ <family_elt_elt>1394244902</family_elt_elt>
43
+ </family_elt>
44
+ <family_elt list="true">
45
+ <family_elt_elt>sister</family_elt_elt>
46
+ <family_elt_elt>48703107</family_elt_elt>
47
+ </family_elt>
48
+ <family_elt list="true">
49
+ <family_elt_elt>brother</family_elt_elt>
50
+ <family_elt_elt>1078767258</family_elt_elt>
51
+ </family_elt>
52
+ <family_elt list="true">
53
+ <family_elt_elt>brother</family_elt_elt>
54
+ <family_elt_elt>John Doe</family_elt_elt>
55
+ <family_elt_elt/>
56
+ </family_elt>
57
+ </family>
58
+ <first_name>Dave</first_name>
59
+ <hometown_location>
60
+ <city>York</city>
61
+ <state>Pennsylvania</state>
62
+ <country>United States</country>
63
+ </hometown_location>
64
+ <hs_info>
65
+ <hs1_name>Central York High School</hs1_name>
66
+ <hs2_name/>
67
+ <grad_year>1999</grad_year>
68
+ <hs1_id>21846</hs1_id>
69
+ <hs2_id>0</hs2_id>
70
+ </hs_info>
71
+ <is_app_user>1</is_app_user>
72
+ <has_added_app>1</has_added_app>
73
+ <interests>coffee, computers, the funny, architecture, code breaking,snowboarding, philosophy, soccer, talking to strangers</interests>
74
+ <last_name>Fetterman</last_name>
75
+ <locale>en_US</locale>
76
+ <meeting_for list="true">
77
+ <seeking>Friendship</seeking>
78
+ </meeting_for>
79
+ <meeting_sex list="true">
80
+ <sex>female</sex>
81
+ </meeting_sex>
82
+ <movies>Tommy Boy, Billy Madison, Fight Club, Dirty Work, Meet the Parents, My Blue Heaven, Office Space </movies>
83
+ <music>New Found Glory, Daft Punk, Weezer, The Crystal Method, Rage, the KLF, Green Day, Live, Coldplay, Panic at the Disco, Family Force 5</music>
84
+ <name>Dave Fetterman</name>
85
+ <notes_count>0</notes_count>
86
+ <pic>http://photos-055.facebook.com/ip007/profile3/1271/65/s8055_39735.jpg</pic>
87
+ <pic_big>http://photos-055.facebook.com/ip007/profile3/1271/65/n8055_39735.jpg</pic_big>
88
+ <pic_small>http://photos-055.facebook.com/ip007/profile3/1271/65/t8055_39735.jpg</pic_small>
89
+ <pic_square>http://photos-055.facebook.com/ip007/profile3/1271/65/q8055_39735.jpg</pic_square>
90
+ <political>Moderate</political>
91
+ <profile_update_time>1170414620</profile_update_time>
92
+ <quotes/>
93
+ <relationship_status>In a Relationship</relationship_status>
94
+ <religion/>
95
+ <sex>male</sex>
96
+ <significant_other_id xsi:nil="true"/>
97
+ <status>
98
+ <message>Fast Company, November issue, page 84</message>
99
+ <time>1193075616</time>
100
+ </status>
101
+ <timezone>-8</timezone>
102
+ <tv>cf. Bob Trahan</tv>
103
+ <wall_count>121</wall_count>
104
+ <work_history list="true">
105
+ <work_info>
106
+ <location>
107
+ <city>Palo Alto</city>
108
+ <state>CA</state>
109
+ <country>United States</country>
110
+ </location>
111
+ <company_name>Facebook</company_name>
112
+ <position>Software Engineer</position>
113
+ <description>Tech Lead, Facebook Platform</description>
114
+ <start_date>2006-01</start_date>
115
+ <end_date/>
116
+ </work_info>
117
+ </work_history>
118
+ </user>
119
+ </users_getInfo_response>