authlogic_facebook_koala 0.0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/README.rdoc +60 -2
  2. data/Rakefile +28 -1
  3. data/VERSION +1 -1
  4. data/authlogic_facebook_koala.gemspec +12 -45
  5. data/lib/authlogic_facebook_koala.rb +10 -4
  6. data/lib/authlogic_facebook_koala/adapter.rb +35 -0
  7. data/lib/authlogic_facebook_koala/config.rb +78 -4
  8. data/lib/authlogic_facebook_koala/session.rb +50 -116
  9. data/test/rails_root/app/controllers/application_controller.rb +10 -0
  10. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  11. data/test/rails_root/app/models/user.rb +7 -0
  12. data/test/{libs → rails_root/app/models}/user_session.rb +0 -0
  13. data/test/rails_root/config/boot.rb +110 -0
  14. data/test/rails_root/config/database.yml +10 -0
  15. data/test/rails_root/config/environment.rb +31 -0
  16. data/test/rails_root/config/environments/development.rb +0 -0
  17. data/test/rails_root/config/environments/test.rb +0 -0
  18. data/test/rails_root/config/facebook.yml +7 -0
  19. data/test/rails_root/config/initializers/authlogic_facebook_koala.rb +5 -0
  20. data/test/rails_root/config/initializers/new_rails_defaults.rb +21 -0
  21. data/test/rails_root/config/initializers/session_store.rb +15 -0
  22. data/test/rails_root/config/locales/en.yml +5 -0
  23. data/test/rails_root/config/routes.rb +43 -0
  24. data/test/rails_root/db/migrate/20101217000008_create_users.rb +37 -0
  25. data/test/rails_root/db/seeds.rb +7 -0
  26. data/test/rails_root/script/console +3 -0
  27. data/test/rails_root/script/dbconsole +3 -0
  28. data/test/rails_root/script/generate +3 -0
  29. data/test/test_helper.rb +19 -58
  30. data/test/units/adapter_test.rb +182 -0
  31. data/test/units/config_test.rb +145 -0
  32. data/test/units/session_test.rb +221 -0
  33. metadata +139 -27
  34. data/lib/authlogic_facebook_koala/controller.rb +0 -59
  35. data/test/libs/user.rb +0 -3
  36. data/test/session_test.rb +0 -30
data/README.rdoc CHANGED
@@ -2,12 +2,70 @@
2
2
 
3
3
  This is a plugin for integrating facebook sessions into authlogic.
4
4
 
5
+ This requires a config/facebook.yml file that looks like this:
6
+
7
+ development:
8
+ app_id: appid
9
+ api_key: apikey
10
+ secret_key: secretkey
11
+
12
+ production:
13
+ app_id: appid
14
+ api_key: apikey
15
+ secret_key: secretkey
16
+
17
+ If you don't have different facebook credentials for different environments you can set these in UserSession
18
+
19
+ facebook_app_id 'appid'
20
+ facebook_api_key 'apikey'
21
+ facebook_secret_key 'secretkey'
22
+
23
+ In your controller you probably have something like this;
24
+
25
+ def current_user_session
26
+ @current_user_session ||= AccountSession.find
27
+ end
28
+
29
+ def current_user
30
+ @current_user ||= current_user_session.try(:user)
31
+ end
32
+
33
+ def logged_in?
34
+ current_user && !current_user_session.stale?
35
+ end
36
+
37
+ To get hold of the facebook particulars you will need to add something like this;
38
+
39
+ def facebook_user
40
+ current_user_session.try(:facebook_user)
41
+ end
42
+
43
+ def facebook_user?
44
+ !facebook_user.nil?
45
+ end
46
+
47
+ def facebook_session?
48
+ current_user_session.try(:facebook_session?)
49
+ end
50
+
51
+ If you have conventional signin and want to ignore facebook for it you will need to do this in your user_sessions_controller;
52
+
53
+ def create
54
+ @account_session = AccountSession.new(params[:account_session])
55
+ @account_session.skip_facebook_authentication = true
56
+ if @account_session.save
57
+ redirect_to member_home_path
58
+ else
59
+ render :action => :new
60
+ end
61
+ end
62
+
63
+
5
64
  == Note on Patches/Pull Requests
6
65
 
7
66
  * Fork the project.
8
67
  * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a future version
10
- unintentionally.
68
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
11
69
  * Commit, do not mess with rakefile, version, or history. (if you want to have
12
70
  your own version, that is fine but bump version in a commit by itself I can
13
71
  ignore when I pull)
data/Rakefile CHANGED
@@ -1,7 +1,10 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
-
3
+ require 'jeweler'
4
4
  require 'rake/rdoctask'
5
+
6
+ Jeweler::Tasks.new
7
+
5
8
  Rake::RDocTask.new do |rdoc|
6
9
  version = File.exist?('VERSION') ? File.read('VERSION') : ""
7
10
 
@@ -10,3 +13,27 @@ Rake::RDocTask.new do |rdoc|
10
13
  rdoc.rdoc_files.include('README*')
11
14
  rdoc.rdoc_files.include('lib/**/*.rb')
12
15
  end
16
+
17
+ require 'rake/testtask'
18
+ Rake::TestTask.new(:test) do |test|
19
+ test.libs << 'test'
20
+ test.pattern = 'test/**/*_test.rb'
21
+ test.verbose = true
22
+ end
23
+
24
+ begin
25
+ require 'rcov/rcovtask'
26
+ Rcov::RcovTask.new do |test|
27
+ test.libs << 'test'
28
+ test.pattern = 'test/**/*_test.rb'
29
+ test.verbose = true
30
+ end
31
+ rescue LoadError
32
+ task :rcov do
33
+ abort "RCov is not available. In order to run rcov, you must: gem install rcov"
34
+ end
35
+ end
36
+
37
+ task :test => :check_dependencies
38
+
39
+ task :default => :test
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.3.0
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{authlogic_facebook_koala}
3
- s.version = "0.0.2"
3
+ s.version = "0.3.0"
4
4
 
5
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
5
+ s.required_rubygems_version = Gem::Requirement.new(">=1.2.0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["James McCarthy"]
7
7
  s.date = %q{2010-05-27}
8
8
  s.description = %q{Authlogic plugin to support Facebook Javascript OAuth2 Sessions, using the koala gem for facebook graph access.}
@@ -11,54 +11,21 @@ Gem::Specification.new do |s|
11
11
  "LICENSE",
12
12
  "README.rdoc"
13
13
  ]
14
- s.files = [
15
- "authlogic_facebook_koala.gemspec",
16
- "init.rb",
17
- "lib/authlogic_facebook_koala/acts_as_authentic.rb",
18
- "lib/authlogic_facebook_koala/config.rb",
19
- "lib/authlogic_facebook_koala/controller.rb",
20
- "lib/authlogic_facebook_koala/helper.rb",
21
- "lib/authlogic_facebook_koala/session.rb",
22
- "lib/authlogic_facebook_koala.rb",
23
- "LICENSE",
24
- "rails/init.rb",
25
- "Rakefile",
26
- "README.rdoc",
27
- "test/test_helper.rb",
28
- "test/session_test.rb",
29
- 'test/libs/user.rb',
30
- 'test/libs/user_session.rb',
31
- "VERSION"
32
- ]
14
+ s.files = Dir.glob('**/*') - Dir.glob('authlogic_facebook_koala*.gem')
33
15
  s.homepage = %q{http://github.com/james2m/authlogic_facebook_koala}
34
16
  s.rdoc_options = ["--charset=UTF-8"]
35
17
  s.require_paths = ["lib"]
36
18
  s.rubygems_version = %q{1.3.5}
37
19
  s.summary = %q{Authlogic plugin to support Facebook OAuth2 javascript sessions using koala gem}
38
- s.test_files = [
39
- 'test/libs/user.rb',
40
- 'test/libs/user_session.rb',
41
- "test/test_helper.rb",
42
- "test/session_test.rb"
43
- ]
44
-
45
- if s.respond_to? :specification_version then
46
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
- s.specification_version = 3
20
+ s.test_files = Dir.glob('test/**/*')
48
21
 
49
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
- s.add_runtime_dependency(%q<koala>, [">= 0.7.1"])
51
- s.add_runtime_dependency(%q<authlogic>, [">= 2.1.3"])
52
- s.add_development_dependency(%q<test-unit>, [">= 2.0.6"])
53
- else
54
- s.add_dependency(%q<koala>, [">= 0.7.1"])
55
- s.add_dependency(%q<authlogic>, [">= 2.1.3"])
56
- s.add_dependency(%q<test-unit>, [">= 2.0.6"])
57
- end
58
- else
59
- s.add_dependency(%q<mini_fb>, [">= 0.2.2"])
60
- s.add_dependency(%q<authlogic>, [">= 2.1.3"])
61
- s.add_dependency(%q<test-unit>, [">= 2.0.6"])
62
- end
22
+ s.add_runtime_dependency('authlogic', ">= 2.1.3")
23
+ s.add_runtime_dependency('koala', ">= 0.7.1")
24
+ s.add_development_dependency('rails', '=2.3.5')
25
+ s.add_development_dependency('flexmock')
26
+ s.add_development_dependency('jeweler')
27
+ s.add_development_dependency('shoulda')
28
+ s.add_development_dependency('sqlite3-ruby')
29
+ s.add_development_dependency('test-unit')
63
30
  end
64
31
 
@@ -1,11 +1,17 @@
1
+ require 'yaml'
2
+ require 'ostruct'
3
+ require 'singleton'
4
+
1
5
  require 'authlogic_facebook_koala/config'
6
+ require 'authlogic_facebook_koala/adapter'
2
7
  require 'authlogic_facebook_koala/acts_as_authentic'
3
8
  require 'authlogic_facebook_koala/session'
4
9
  require 'authlogic_facebook_koala/helper'
5
- require 'authlogic_facebook_koala/controller'
6
10
 
7
11
  if ActiveRecord::Base.respond_to?(:add_acts_as_authentic_module)
8
- ActiveRecord::Base.send(:include, AuthlogicFacebookKoala::ActsAsAuthentic)
9
- Authlogic::Session::Base.send(:include, AuthlogicFacebookKoala::Session)
10
- ActionController::Base.send(:include, AuthlogicFacebookKoala::Controller)
12
+ ActiveRecord::Base.send :include, AuthlogicFacebookKoala::ActsAsAuthentic
13
+
14
+ Authlogic::Session::Base.send :extend, AuthlogicFacebookKoala::Config
15
+ Authlogic::Session::Base.send :include, AuthlogicFacebookKoala::Session
16
+ Authlogic::Session::Base.send :include, AuthlogicFacebookKoala::Adapter
11
17
  end
@@ -0,0 +1,35 @@
1
+ module AuthlogicFacebookKoala
2
+
3
+ module Adapter
4
+
5
+ def facebook_session
6
+ @facebook_session ||= begin
7
+ if controller.cookies.has_key?("fbs_#{facebook_app_id}")
8
+ oauth = Koala::Facebook::OAuth.new(facebook_app_id, facebook_secret_key)
9
+ if oauth.respond_to?(:get_user_info_from_cookie)
10
+ user_info = oauth.get_user_info_from_cookie(controller.cookies)
11
+ else
12
+ user_info = oauth.get_user_from_cookie(controller.cookies)
13
+ end
14
+ OpenStruct.new( user_info )
15
+ end
16
+ end
17
+ end
18
+
19
+ def facebook_session?
20
+ !facebook_session.nil?
21
+ end
22
+
23
+ def facebook_user
24
+ @facebook_user ||= begin
25
+ facebook_graph = Koala::Facebook::GraphAPI.new(facebook_session.access_token)
26
+ user = facebook_graph.get_object('me')
27
+ user[:uid] = user.delete('id')
28
+ OpenStruct.new( user )
29
+ end if facebook_session?
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
@@ -1,6 +1,80 @@
1
- require 'yaml'
2
- require 'ostruct'
3
-
4
1
  module AuthlogicFacebookKoala
5
- CONFIG = OpenStruct.new( YAML.load_file("#{Rails.root}/config/facebook.yml")[Rails.env] )
2
+
3
+ module Config
4
+
5
+ def self.extended(klass)
6
+ (class << klass; self end).send(:define_method, :default_config) do
7
+ @default_config ||= begin
8
+ config_file = File.join(Rails.root, 'config', facebook_config_file)
9
+ OpenStruct.new(File.exist?(config_file) ? YAML.load_file(config_file)[Rails.env] : {})
10
+ end
11
+ end
12
+ end
13
+
14
+ # Specify your config file if using one. If not then you need to specify
15
+ # facebook_app_id, facebook_secret_key & facebook_api_key
16
+ #
17
+ # * <tt>Default:</tt> facebook.yml
18
+ # * <tt>Accepts:</tt> String
19
+ def facebook_config_file(value=nil)
20
+ rw_config(:facebook_config_file, value, 'facebook.yml')
21
+ end
22
+ alias_method :facebook_config_file=, :facebook_config_file
23
+
24
+ # Specify your app_id.
25
+ #
26
+ # * <tt>Default:</tt> app_id in config/facebook.yml
27
+ # * <tt>Accepts:</tt> String
28
+ def facebook_app_id(value=nil)
29
+ rw_config(:facebook_app_id, value, default_config.app_id)
30
+ end
31
+ alias_method :facebook_app_id=, :facebook_app_id
32
+
33
+ # Specify your secret_key.
34
+ #
35
+ # * <tt>Default:</tt> secret_key in config/facebook.yml
36
+ # * <tt>Accepts:</tt> String
37
+ def facebook_secret_key(value=nil)
38
+ rw_config(:facebook_secret_key, value, default_config.secret_key)
39
+ end
40
+ alias_method :facebook_secret_key=, :facebook_secret_key
41
+
42
+ # Specify your api_key.
43
+ #
44
+ # * <tt>Default:</tt> api_key in config/facebook.yml
45
+ # * <tt>Accepts:</tt> String
46
+ def facebook_api_key(value=nil)
47
+ rw_config(:facebook_api_key, value, default_config.api_key)
48
+ end
49
+ alias_method :facebook_api_key=, :facebook_api_key
50
+
51
+ # What user field should be used for the facebook UID?
52
+ #
53
+ # * <tt>Default:</tt> :facebook_uid
54
+ # * <tt>Accepts:</tt> Symbol
55
+ def facebook_uid_field(value=nil)
56
+ rw_config(:facebook_uid_field, value, :facebook_uid)
57
+ end
58
+ alias_method :facebook_uid_field=, :facebook_uid_field
59
+
60
+ # What method should be used to find the facebook account?
61
+ #
62
+ # * <tt>Default:</tt> :find_by_#{facebook_uid_field}
63
+ # * <tt>Accepts:</tt> Symbol or String
64
+ def facebook_finder(value=nil)
65
+ rw_config(:facebook_finder, value, nil)
66
+ end
67
+ alias_method :facebook_finder=, :facebook_finder
68
+
69
+ # Should a new user be automatically created if there is no user with
70
+ # given facebook uid?
71
+ #
72
+ # * <tt>Default:</tt> false
73
+ # * <tt>Accepts:</tt> Boolean
74
+ def facebook_auto_register(value=nil)
75
+ rw_config(:facebook_auto_register, value, false)
76
+ end
77
+ alias_method :facebook_auto_register=, :facebook_auto_register
78
+ end
79
+
6
80
  end
@@ -1,134 +1,68 @@
1
1
  module AuthlogicFacebookKoala
2
2
  module Session
3
+
3
4
  def self.included(klass)
5
+
4
6
  klass.class_eval do
5
- extend Config
6
- include Methods
7
- end
8
- end
9
-
10
- module Config
11
- # REQUIRED
12
- #
13
- # Specify your app_id.
14
- #
15
- # * <tt>Default:</tt> nil
16
- # * <tt>Accepts:</tt> String
17
- def facebook_app_id(value=nil)
18
- rw_config(:facebook_app_id, value, nil)
19
- end
20
- alias_method :facebook_app_id=, :facebook_app_id
21
-
22
- # REQUIRED
23
- #
24
- # Specify your secret_key.
25
- #
26
- # * <tt>Default:</tt> nil
27
- # * <tt>Accepts:</tt> String
28
- def facebook_secret_key(value=nil)
29
- rw_config(:facebook_secret_key, value, nil)
30
- end
31
- alias_method :facebook_secret_key=, :facebook_secret_key
32
-
33
- # Specify your api_key.
34
- #
35
- # * <tt>Default:</tt> nil
36
- # * <tt>Accepts:</tt> String
37
- def facebook_api_key(value=nil)
38
- rw_config(:facebook_api_key, value, nil)
39
- end
40
- alias_method :facebook_api_key=, :facebook_api_key
41
-
42
- # What user field should be used for the facebook UID?
43
- #
44
- # * <tt>Default:</tt> :facebook_uid
45
- # * <tt>Accepts:</tt> Symbol
46
- def facebook_uid_field(value=nil)
47
- rw_config(:facebook_uid_field, value, :facebook_uid)
48
- end
49
- alias_method :facebook_uid_field=, :facebook_uid_field
50
-
51
- # What method should be used to find the facebook account?
52
- #
53
- # * <tt>Default:</tt> :find_by_#{facebook_uid_field}
54
- # * <tt>Accepts:</tt> Symbol or String
55
- def facebook_finder(value=nil)
56
- rw_config(:facebook_finder, value, nil)
57
- end
58
- alias_method :facebook_finder=, :facebook_finder
59
-
60
- # Should a new user be automatically created if there is no user with
61
- # given facebook uid?
62
- #
63
- # * <tt>Default:</tt> false
64
- # * <tt>Accepts:</tt> Boolean
65
- def facebook_auto_register(value=nil)
66
- rw_config(:facebook_auto_register, value, false)
67
- end
68
- alias_method :facebook_auto_register=, :facebook_auto_register
69
- end
70
-
71
- module Methods
72
- def self.included(klass)
73
- klass.class_eval do
74
- validate :validate_by_facebook, :if => :authenticating_with_facebook?
75
- end
7
+ attr_accessor :skip_facebook_authentication
8
+ validate :validate_by_facebook, :if => :authenticating_with_facebook?
76
9
  end
77
10
 
78
- def logged_in_with_facebook?
79
- @logged_in_with_facebook
80
- end
81
-
82
- protected
83
- # Override this if you want only some requests to use facebook
84
- def authenticating_with_facebook?
85
- if controller.respond_to?(:controller) && controller.controller.respond_to?(:set_facebook_session)
86
- controller.set_facebook_session
87
- !authenticating_with_unauthorized_record? && controller.facebook_session?
88
- end
89
- end
90
-
91
- private
11
+ end
92
12
 
93
- def validate_by_facebook
94
- puts "validating with facebook"
95
- facebook_uid = facebook_session.uid
96
- self.attempted_record = klass.send(facebook_finder, facebook_uid)
13
+ def logged_in_with_facebook?
14
+ @logged_in_with_facebook
15
+ end
97
16
 
98
- if self.attempted_record || !facebook_auto_register?
99
- return @logged_in_with_facebook = !!self.attempted_record
100
- else
101
- self.attempted_record = klass.new
102
- self.attempted_record.send(:"#{facebook_uid_field}=", facebook_uid)
103
- if self.attempted_record.respond_to?(:before_connect)
104
- self.attempted_record.send(:before_connect, facebook_session)
105
- end
17
+ protected
18
+ # Override this if you want only some requests to use facebook
19
+ def authenticating_with_facebook?
20
+ !skip_facebook_authentication && !authenticating_with_unauthorized_record? && facebook_session?
21
+ end
106
22
 
107
- @logged_in_with_facebook = true
108
- return self.attempted_record.save(false)
23
+ private
24
+
25
+ def validate_by_facebook
26
+ facebook_uid = facebook_session.uid
27
+ self.attempted_record = klass.send(facebook_finder, facebook_uid)
28
+
29
+ if self.attempted_record || !facebook_auto_register?
30
+ return @logged_in_with_facebook = !!self.attempted_record
31
+ else
32
+ self.attempted_record = klass.new
33
+ self.attempted_record.send(:"#{facebook_uid_field}=", facebook_uid)
34
+ if self.attempted_record.respond_to?(:before_connect)
35
+ self.attempted_record.send(:before_connect, facebook_session)
109
36
  end
110
- end
111
-
112
- def facebook_user
113
- controller.facebook_user
114
- end
115
-
116
- def facebook_session
117
- controller.facebook_session
118
- end
119
37
 
120
- def facebook_auto_register?
121
- self.class.facebook_auto_register
38
+ @logged_in_with_facebook = true
39
+ return self.attempted_record.save(false)
122
40
  end
41
+ end
42
+
43
+ def facebook_app_id
44
+ self.class.facebook_app_id
45
+ end
46
+
47
+ def facebook_api_key
48
+ self.class.facebook_api_key
49
+ end
50
+
51
+ def facebook_secret_key
52
+ self.class.facebook_secret_key
53
+ end
123
54
 
124
- def facebook_uid_field
125
- self.class.facebook_uid_field
126
- end
55
+ def facebook_auto_register?
56
+ self.class.facebook_auto_register
57
+ end
127
58
 
128
- def facebook_finder
129
- self.class.facebook_finder || "find_by_#{facebook_uid_field}"
130
- end
59
+ def facebook_uid_field
60
+ self.class.facebook_uid_field
61
+ end
131
62
 
63
+ def facebook_finder
64
+ self.class.facebook_finder || "find_by_#{facebook_uid_field}"
132
65
  end
66
+
133
67
  end
134
68
  end