authlogic_facebook_koala 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 James McCarthy
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.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = authlogic_facebook_koala
2
+
3
+ This is a plugin for integrating facebook sessions into authlogic.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * 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.
11
+ * Commit, do not mess with rakefile, version, or history. (if you want to have
12
+ your own version, that is fine but bump version in a commit by itself I can
13
+ ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2010 James McCarthy. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'rake/rdoctask'
5
+ Rake::RDocTask.new do |rdoc|
6
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
7
+
8
+ rdoc.rdoc_dir = 'rdoc'
9
+ rdoc.title = "authlogic_facebook_koala #{version}"
10
+ rdoc.rdoc_files.include('README*')
11
+ rdoc.rdoc_files.include('lib/**/*.rb')
12
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,64 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{authlogic_facebook_koala}
3
+ s.version = "0.0.2"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["James McCarthy"]
7
+ s.date = %q{2010-05-27}
8
+ s.description = %q{Authlogic plugin to support Facebook Javascript OAuth2 Sessions, using the koala gem for facebook graph access.}
9
+ s.email = %q{james2mccarthy@gmail.com}
10
+ s.extra_rdoc_files = [
11
+ "LICENSE",
12
+ "README.rdoc"
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
+ ]
33
+ s.homepage = %q{http://github.com/james2m/authlogic_facebook_koala}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ 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
48
+
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
63
+ end
64
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init.rb"
@@ -0,0 +1,20 @@
1
+ module AuthlogicFacebookKoala
2
+ module ActsAsAuthentic
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ extend Config
6
+ add_acts_as_authentic_module(Methods, :prepend)
7
+ end
8
+ end
9
+
10
+ module Config
11
+ end
12
+
13
+ module Methods
14
+ def self.included(klass)
15
+ klass.class_eval do
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ require 'yaml'
2
+ require 'ostruct'
3
+
4
+ module AuthlogicFacebookKoala
5
+ CONFIG = OpenStruct.new( YAML.load_file("#{Rails.root}/config/facebook.yml")[Rails.env] )
6
+ end
@@ -0,0 +1,59 @@
1
+ require 'ostruct'
2
+ module AuthlogicFacebookKoala
3
+ module Controller
4
+
5
+ def self.included(controller)
6
+ controller.send(:helper_method, :facebook_user?, :facebook_user, :facebook_session?, :facebook_session)
7
+ end
8
+
9
+ # TODO update to use the new facebook_user stuff we are going to create
10
+ # can probably kill this now.
11
+ def set_current_account_session_from_facebook_session
12
+ unless current_account_session
13
+ @current_account_session = AccountSession.new
14
+ @current_account_session.account = Account.find_by_facebook_uid(facebook_session.user.uid)
15
+ end
16
+ end
17
+
18
+ def facebook_user?
19
+ !!facebook_user
20
+ end
21
+
22
+ def facebook_user
23
+ if @facebook_user
24
+ return @facebook_user
25
+ elsif facebook_session?
26
+ facebook_graph = Koala::Facebook::GraphAPI.new(facebook_session.access_token)
27
+ user = facebook_graph.get_object('me')
28
+ user[:uid] = user.delete('id')
29
+ return @facebook_user = OpenStruct.new( user )
30
+ end
31
+ end
32
+
33
+ def facebook_session?
34
+ !!facebook_session
35
+ end
36
+
37
+ protected
38
+
39
+ def facebook_session
40
+ @facebook_session
41
+ end
42
+
43
+ def set_facebook_session
44
+ if @facebook_session
45
+ return(@facebook_session)
46
+ elsif cookies.has_key?("fbs_#{facebook_params.app_id}")
47
+ oauth = Koala::Facebook::OAuth.new(facebook_params.app_id, facebook_params.secret_key)
48
+ @facebook_session = OpenStruct.new( oauth.get_user_from_cookie(cookies) )
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def facebook_params
55
+ AuthlogicFacebookKoala::CONFIG
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,4 @@
1
+ module AuthlogicFacebookKoala
2
+ module Helper
3
+ end
4
+ end
@@ -0,0 +1,134 @@
1
+ module AuthlogicFacebookKoala
2
+ module Session
3
+ def self.included(klass)
4
+ 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
76
+ end
77
+
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
92
+
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)
97
+
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
106
+
107
+ @logged_in_with_facebook = true
108
+ return self.attempted_record.save(false)
109
+ 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
+
120
+ def facebook_auto_register?
121
+ self.class.facebook_auto_register
122
+ end
123
+
124
+ def facebook_uid_field
125
+ self.class.facebook_uid_field
126
+ end
127
+
128
+ def facebook_finder
129
+ self.class.facebook_finder || "find_by_#{facebook_uid_field}"
130
+ end
131
+
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,11 @@
1
+ require 'authlogic_facebook_koala/config'
2
+ require 'authlogic_facebook_koala/acts_as_authentic'
3
+ require 'authlogic_facebook_koala/session'
4
+ require 'authlogic_facebook_koala/helper'
5
+ require 'authlogic_facebook_koala/controller'
6
+
7
+ 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)
11
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'authlogic_facebook_koala'
data/test/libs/user.rb ADDED
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ acts_as_authentic
3
+ end
@@ -0,0 +1,7 @@
1
+ class UserSession < Authlogic::Session::Base
2
+
3
+ facebook_app_id '233423200151'
4
+ facebook_api_key 'f4fdb590052b81c3d73c5d2aa5908308'
5
+ facebook_secret_key '8371c9fc95a7d0a3d9cdb6bbacfea306'
6
+
7
+ end
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class SessionTest < ActiveSupport::TestCase
4
+ def test_logged_in_with_facebook
5
+ session = UserSession.new
6
+ assert session.respond_to?(:logged_in_with_facebook?)
7
+ end
8
+
9
+ def test_validate_openid_error
10
+ session = UserSession.new
11
+ session.openid_identifier = "yes"
12
+ session.openid_identifier = "%"
13
+ assert_nil session.openid_identifier
14
+ assert !session.save
15
+ assert session.errors.on(:openid_identifier)
16
+ end
17
+
18
+ def test_validate_by_nil_openid_identifier
19
+ session = UserSession.new
20
+ assert !session.save
21
+ assert !redirecting_to_yahoo?
22
+ end
23
+
24
+ def test_validate_by_correct_openid_identifier
25
+ session = UserSession.new
26
+ session.openid_identifier = "https://me.yahoo.com/a/9W0FJjRj0o981TMSs0vqVxPdmMUVOQ--"
27
+ assert !session.save
28
+ assert redirecting_to_yahoo?
29
+ end
30
+ end
@@ -0,0 +1,82 @@
1
+ require "test/unit"
2
+ require "rubygems"
3
+ require "ruby-debug"
4
+ require "active_record"
5
+
6
+ ActiveRecord::Schema.verbose = false
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
8
+ ActiveRecord::Base.configurations = true
9
+ ActiveRecord::Schema.define(:version => 1) do
10
+ create_table :users do |t|
11
+ t.datetime :created_at
12
+ t.datetime :updated_at
13
+ t.integer :lock_version, :default => 0
14
+ t.string :login
15
+ t.string :crypted_password
16
+ t.string :password_salt
17
+ t.string :persistence_token
18
+ t.string :single_access_token
19
+ t.string :perishable_token
20
+ t.string :openid_identifier
21
+ t.string :email
22
+ t.string :first_name
23
+ t.string :last_name
24
+ t.integer :login_count, :default => 0, :null => false
25
+ t.integer :failed_login_count, :default => 0, :null => false
26
+ t.datetime :last_request_at
27
+ t.datetime :current_login_at
28
+ t.datetime :last_login_at
29
+ t.string :current_login_ip
30
+ t.string :last_login_ip
31
+ t.string :facebook_uid
32
+ t.string :facebook_session_key
33
+ t.string :facebook_access_token
34
+ t.string :facebook_expires
35
+ t.string :facebook_secret
36
+ t.string :facebook_sig
37
+
38
+ end
39
+ end
40
+
41
+ require "active_record/fixtures"
42
+ require 'action_controller'
43
+ Rails = true # to trick authlogic into loading the rails adapter
44
+ require "authlogic"
45
+ require "authlogic/test_case"
46
+ require File.dirname(__FILE__) + '/../lib/authlogic_facebook_koala' unless defined?(AuthlogicFacebookKoala)
47
+ require File.dirname(__FILE__) + '/libs/user'
48
+ require File.dirname(__FILE__) + '/libs/user_session'
49
+
50
+ class ActiveSupport::TestCase
51
+ include ActiveRecord::TestFixtures
52
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures"
53
+ self.use_transactional_fixtures = false
54
+ self.use_instantiated_fixtures = false
55
+ self.pre_loaded_fixtures = false
56
+ fixtures :all
57
+ setup :activate_authlogic
58
+
59
+ private
60
+ def activate_authlogic
61
+ Authlogic::Session::Base.controller = controller
62
+ end
63
+
64
+ def controller
65
+ @controller ||= Authlogic::ControllerAdapters::RailsAdapter.new(ActionController.new)
66
+ end
67
+ end
68
+
69
+ # --- Sample valid cookie hash generated with the code below
70
+ # cookie_hash = {'fbs_233423200151' => 'access_token=233423200151|6892d62675cd952ade8b3f9b-6184456410|xrNaOlTCUF0QFZrJCHmVWzTb5Mk.&expires=0&secret=339a00cdafe6959c3caa1b8004e5f8db&session_key=6892d62675cd952ade8b3f9b-6184456410&sig=19d3c9ccb5b5a55d680ed1cf18698f57&uid=6184456410'}
71
+
72
+ # --- Generates user params from cookie for this app_id & secret
73
+ # oauth = Koala::Facebook::OAuth.new('233423200151', '8371c9fc95a7d0a3d9cdb6bbacfea306')
74
+ # params = oauth.get_user_from_cookie(cookie_hash)
75
+
76
+ # --- Generates a signature for a cookie hash
77
+ # fb_cookie = cookie_hash["fbs_233423200151"]
78
+ # components = {}
79
+ # fb_cookie.split("&").map {|param| param = param.split("="); components[param[0]] = param[1]}
80
+ # auth_string = components.keys.sort.collect {|a| a == "sig" ? nil : "#{a}=#{components[a]}"}.reject {|a| a.nil?}.join("")
81
+ # sig = Digest::MD5.hexdigest(auth_string + '8371c9fc95a7d0a3d9cdb6bbacfea306')
82
+
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: authlogic_facebook_koala
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - James McCarthy
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-27 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: koala
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 0
32
+ - 7
33
+ - 1
34
+ version: 0.7.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: authlogic
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 13
46
+ segments:
47
+ - 2
48
+ - 1
49
+ - 3
50
+ version: 2.1.3
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: test-unit
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 2
64
+ - 0
65
+ - 6
66
+ version: 2.0.6
67
+ type: :development
68
+ version_requirements: *id003
69
+ description: Authlogic plugin to support Facebook Javascript OAuth2 Sessions, using the koala gem for facebook graph access.
70
+ email: james2mccarthy@gmail.com
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files:
76
+ - LICENSE
77
+ - README.rdoc
78
+ files:
79
+ - authlogic_facebook_koala.gemspec
80
+ - init.rb
81
+ - lib/authlogic_facebook_koala/acts_as_authentic.rb
82
+ - lib/authlogic_facebook_koala/config.rb
83
+ - lib/authlogic_facebook_koala/controller.rb
84
+ - lib/authlogic_facebook_koala/helper.rb
85
+ - lib/authlogic_facebook_koala/session.rb
86
+ - lib/authlogic_facebook_koala.rb
87
+ - LICENSE
88
+ - rails/init.rb
89
+ - Rakefile
90
+ - README.rdoc
91
+ - test/test_helper.rb
92
+ - test/session_test.rb
93
+ - test/libs/user.rb
94
+ - test/libs/user_session.rb
95
+ - VERSION
96
+ has_rdoc: true
97
+ homepage: http://github.com/james2m/authlogic_facebook_koala
98
+ licenses: []
99
+
100
+ post_install_message:
101
+ rdoc_options:
102
+ - --charset=UTF-8
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 3
120
+ segments:
121
+ - 0
122
+ version: "0"
123
+ requirements: []
124
+
125
+ rubyforge_project:
126
+ rubygems_version: 1.3.7
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Authlogic plugin to support Facebook OAuth2 javascript sessions using koala gem
130
+ test_files:
131
+ - test/libs/user.rb
132
+ - test/libs/user_session.rb
133
+ - test/test_helper.rb
134
+ - test/session_test.rb