devise_koala_connectable 0.1.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.
data/Manifest ADDED
@@ -0,0 +1,13 @@
1
+ README.rdoc
2
+ Rakefile
3
+ devise_koala_connectable.gemspec
4
+ lib/devise_koala_connectable.rb
5
+ lib/devise_koala_connectable/locales/de.yml
6
+ lib/devise_koala_connectable/locales/en.yml
7
+ lib/devise_koala_connectable/model.rb
8
+ lib/devise_koala_connectable/schema.rb
9
+ lib/devise_koala_connectable/strategy.rb
10
+ lib/devise_koala_connectable/version.rb
11
+ lib/devise_koala_connectable/view_helpers.rb
12
+ rails/init.rb
13
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,31 @@
1
+ = devise_koala_connectable
2
+
3
+ Rails gem for adding Facebook authentification capabillity to devise using koala
4
+
5
+ = Install
6
+
7
+ The gem is hosted on https://rubygems.org/gems/devise_koala_connectable and can be installed using
8
+
9
+ - gem install devise_koala_connectable
10
+
11
+ = Licence
12
+
13
+ Copyright (C) 2011 Mathias Karstädt
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to deal
17
+ in the Software without restriction, including without limitation the rights
18
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in all
23
+ copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "echoe"
4
+
5
+ Echoe.new("devise_koala_connectable", "0.1.1") do |p|
6
+ p.description = "Facebook authentification with koala for devise"
7
+ p.url = "http://github.com/webmatze/devise_koala_connectable"
8
+ p.author = "Mathias Karstädt"
9
+ p.email = "mathias.karstaedt@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.runtime_dependencies = ["devise <=1.0.9", "koala"]
12
+ p.development_dependencies = ["devise <=1.0.9", "koala"]
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,42 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{devise_koala_connectable}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Mathias Karst\303\244dt"]
9
+ s.date = %q{2011-02-17}
10
+ s.description = %q{Facebook authentification with koala for devise}
11
+ s.email = %q{mathias.karstaedt@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/devise_koala_connectable.rb", "lib/devise_koala_connectable/locales/de.yml", "lib/devise_koala_connectable/locales/en.yml", "lib/devise_koala_connectable/model.rb", "lib/devise_koala_connectable/schema.rb", "lib/devise_koala_connectable/strategy.rb", "lib/devise_koala_connectable/version.rb", "lib/devise_koala_connectable/view_helpers.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "devise_koala_connectable.gemspec", "lib/devise_koala_connectable.rb", "lib/devise_koala_connectable/locales/de.yml", "lib/devise_koala_connectable/locales/en.yml", "lib/devise_koala_connectable/model.rb", "lib/devise_koala_connectable/schema.rb", "lib/devise_koala_connectable/strategy.rb", "lib/devise_koala_connectable/version.rb", "lib/devise_koala_connectable/view_helpers.rb", "rails/init.rb", "Manifest"]
14
+ s.homepage = %q{http://github.com/webmatze/devise_koala_connectable}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Devise_koala_connectable", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{devise_koala_connectable}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Facebook authentification with koala for devise}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<devise>, ["<= 1.0.9"])
27
+ s.add_runtime_dependency(%q<koala>, [">= 0"])
28
+ s.add_development_dependency(%q<devise>, ["<= 1.0.9"])
29
+ s.add_development_dependency(%q<koala>, [">= 0"])
30
+ else
31
+ s.add_dependency(%q<devise>, ["<= 1.0.9"])
32
+ s.add_dependency(%q<koala>, [">= 0"])
33
+ s.add_dependency(%q<devise>, ["<= 1.0.9"])
34
+ s.add_dependency(%q<koala>, [">= 0"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<devise>, ["<= 1.0.9"])
38
+ s.add_dependency(%q<koala>, [">= 0"])
39
+ s.add_dependency(%q<devise>, ["<= 1.0.9"])
40
+ s.add_dependency(%q<koala>, [">= 0"])
41
+ end
42
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ en:
2
+ devise:
3
+ sessions:
4
+ koala_invalid: "Could not login. Invalid account."
@@ -0,0 +1,159 @@
1
+ # encoding: utf-8
2
+
3
+ module Devise #:nodoc:
4
+ # module KoalaConnectable #:nodoc:
5
+ module Models #:nodoc:
6
+
7
+ # Koala Connectable Module, responsible for validating authenticity of a
8
+ # user and storing credentials while signing in.
9
+ #
10
+ # == Configuration:
11
+ #
12
+ # You can overwrite configuration values by setting in globally in Devise (+Devise.setup+),
13
+ # using devise method, or overwriting the respective instance method.
14
+ #
15
+ # +koala_identifier_field+ - Defines the name of the Koala identifier database attribute/column.
16
+ #
17
+ # +koala_auto_create_account+ - Speifies if account should automatically be created upon connect
18
+ # if not already exists.
19
+ #
20
+ # == Examples:
21
+ #
22
+ # User.authenticate_with_koala(:identifier => 'john@doe.com') # returns authenticated user or nil
23
+ # User.find(1).koala_connected? # returns true/false
24
+ #
25
+ module KoalaConnectable
26
+
27
+ def self.included(base) #:nodoc:
28
+ base.class_eval do
29
+ extend ClassMethods
30
+ end
31
+ end
32
+
33
+ # Store Koala account/session credentials.
34
+ #
35
+ def store_koala_credentials!(attributes = {})
36
+ self.send(:"#{self.class.koala_identifier_field}=", attributes[:identifier])
37
+
38
+ # Confirm without e-mail - if confirmable module is loaded.
39
+ self.skip_confirmation! if self.respond_to?(:skip_confirmation!)
40
+
41
+ # Only populate +email+ field if it's available (e.g. if +authenticable+ module is used).
42
+ self.email = attributes[:email] || '' if self.respond_to?(:email)
43
+
44
+ # Lazy hack: These database fields are required if +authenticable+/+confirmable+
45
+ # module(s) is used. Could be avoided with :null => true for authenticatable
46
+ # migration, but keeping this to avoid unnecessary problems.
47
+ self.password_salt = '' if self.respond_to?(:password_salt)
48
+ self.encrypted_password = '' if self.respond_to?(:encrypted_password)
49
+ end
50
+
51
+ # Checks if Koala connected.
52
+ #
53
+ def koala_connected?
54
+ self.send(:"#{self.class.koala_identifier_field}").present?
55
+ end
56
+ alias :is_koala_connected? :koala_connected?
57
+
58
+ # Hook that gets called before a successful connection (each time).
59
+ # Useful for fetching additional user info (etc.) from Facebook.
60
+ #
61
+ # Default: Do nothing.
62
+ #
63
+ # == Example:
64
+ #
65
+ # # Overridden in Koala connectable model, e.g. "User".
66
+ # #
67
+ # def before_koala_success(koala_user)
68
+ #
69
+ # # Get email (if the provider supports it)
70
+ # email = koala_user["email"]
71
+ # # etc...
72
+ #
73
+ # end
74
+ #
75
+ def on_before_koala_success(koala_user)
76
+ self.send(:before_koala_success, koala_user) if self.respond_to?(:before_koala_success)
77
+ end
78
+
79
+ # Hook that gets called before the auto creation of the user.
80
+ # Therefore, this hook is only called when koala_auto_create_account config option is enabled.
81
+ # Useful for fetching additional user info (etc.) from Facebook.
82
+ #
83
+ # Default: Do nothing.
84
+ #
85
+ # == Example:
86
+ #
87
+ # # Overridden in Koala connectable model, e.g. "User".
88
+ # #
89
+ # def before_koala_auto_create(koala_user)
90
+ #
91
+ # # Get email (if the provider supports it)
92
+ # email = koala_user["email"]
93
+ # # etc...
94
+ #
95
+ # end
96
+ #
97
+ def on_before_koala_auto_create(koala_user)
98
+ self.send(:before_koala_auto_create, koala_user) if self.respond_to?(:before_koala_auto_create)
99
+ end
100
+
101
+ module ClassMethods
102
+
103
+ # Configuration params accessible within +Devise.setup+ procedure (in initalizer).
104
+ #
105
+ # == Example:
106
+ #
107
+ # Devise.setup do |config|
108
+ # config.koala_identifier_field = :koala_identifier
109
+ # config.koala_auto_create_account = true
110
+ # config.koala_app_id = nil
111
+ # config.koala_secret_key = nil
112
+ # config.koala_callback_url = nil
113
+ # end
114
+ #
115
+ ::Devise::Models.config(self,
116
+ :koala_identifier_field,
117
+ :koala_auto_create_account,
118
+ :koala_app_id,
119
+ :koala_secret_key,
120
+ :koala_callback_url
121
+ )
122
+
123
+ # Alias don't work for some reason, so...a more Ruby-ish alias
124
+ # for +koala_auto_create_account+.
125
+ #
126
+ def koala_auto_create_account?
127
+ self.koala_auto_create_account
128
+ end
129
+
130
+ # Authenticate a user based on Facebook Identifier.
131
+ #
132
+ def authenticate_with_koala(attributes = {})
133
+ if attributes[:identifier].present?
134
+ self.find_for_koala(attributes[:identifier])
135
+ end
136
+ end
137
+
138
+ protected
139
+
140
+ # Find first record based on conditions given (Facebook identifier).
141
+ # Overwrite to add customized conditions, create a join, or maybe use a
142
+ # namedscope to filter records while authenticating.
143
+ #
144
+ def find_for_koala(identifier)
145
+ self.first(:conditions => { koala_identifier_field => identifier })
146
+ end
147
+
148
+ # Contains the logic used in authentication. Overwritten by other devise modules.
149
+ # In the Koala connect case; nothing fancy required.
150
+ #
151
+ def valid_for_koala(resource, attributes)
152
+ true
153
+ end
154
+
155
+ end
156
+
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require 'devise/schema'
3
+
4
+ module Devise #:nodoc:
5
+ module KoalaConnectable #:nodoc:
6
+
7
+ module Schema
8
+
9
+ # Database migration schema for koala.
10
+ #
11
+ def koala_connectable
12
+ apply_devise_schema ::Devise.koala_identifier_field, Integer
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+
19
+ Devise::Schema.module_eval do
20
+ include ::Devise::KoalaConnectable::Schema
21
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: utf-8
2
+ require 'devise/strategies/base'
3
+
4
+ module Devise #:nodoc:
5
+
6
+ module Strategies #:nodoc:
7
+
8
+ # Default strategy for signing in a user using Koala.
9
+ # Redirects to sign_in page if it's not authenticated
10
+ #
11
+ class KoalaConnectable < Base
12
+
13
+ def valid?
14
+ valid_controller? && valid_params? && mapping.to.respond_to?('authenticate_with_koala')
15
+ end
16
+
17
+ # Authenticate user with Koala.
18
+ #
19
+ def authenticate!
20
+
21
+ klass = mapping.to
22
+
23
+ raise StandardError, "No api_key or secret_key defined, please see the documentation of Koala gem to setup it." unless klass.koala_app_id.present? and klass.koala_secret_key.present?
24
+ begin
25
+ oauth = Koala::Facebook::OAuth.new(klass.koala_app_id, klass.koala_secret_key, klass.koala_callback_url)
26
+
27
+ user_info = oauth.get_user_info_from_cookies(request.cookies)
28
+ Rails.logger.info user_info.to_yaml
29
+
30
+ graph = Koala::Facebook::GraphAPI.new(user_info["access_token"])
31
+ koala_user = graph.get_object(user_info["uid"])
32
+
33
+ Rails.logger.info koala_user.to_yaml
34
+
35
+ fail!(:koala_invalid) and return unless koala_user
36
+
37
+ if user = klass.authenticate_with_koala(koala_user)
38
+ user.on_before_koala_success(koala_user)
39
+ success!(user)
40
+ return
41
+ end
42
+
43
+ fail!(:koala_invalid) and return unless klass.koala_auto_create_account?
44
+
45
+ user = klass.new
46
+ user.store_koala_credentials!(koala_user)
47
+ user.on_before_koala_auto_create(koala_user)
48
+
49
+ user.save(:validate => false)
50
+ user.on_before_koala_success(koala_user)
51
+ success!(user)
52
+
53
+ rescue Exception => e
54
+ Rails.logger.error e.to_yaml
55
+ fail!(:koala_invalid)
56
+ end
57
+ end
58
+
59
+ protected
60
+ def valid_controller?
61
+ params[:controller].to_s =~ /sessions/
62
+ end
63
+
64
+ def valid_params?
65
+ params[:code].present?
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ Warden::Strategies.add(:koala_connectable, Devise::Strategies::KoalaConnectable)
@@ -0,0 +1,5 @@
1
+ module Devise
2
+ module KoalaConnectable
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ module Devise #:nodoc:
4
+ module KoalaConnectable #:nodoc:
5
+
6
+ # Koala view helpers to easily add the link to the Facebook connection popup and also the necessary JS code.
7
+ #
8
+ module Helpers
9
+
10
+ # Creates the link to the Facebook connection popup.
11
+ # If you create a link without putting the JS code, the popup will load in a new page.
12
+ # The second parameter is the return URL, it must be absolute (***_url).
13
+ #
14
+ # For example :
15
+ # <%= link_to_koala "Signin using Facebook!", user_session_url %>
16
+ #
17
+ def link_to_koala(link_text, link_url, options={})
18
+ options = { :unobtrusive => true }.merge(options)
19
+ oauth = Koala::Facebook::OAuth.new(Devise::koala_app_id, Devise::koala_secret_key, Devise::koala_callback_url)
20
+ link_to link_text, oauth.url_for_oauth_code(:callback => link_url), options
21
+ end
22
+
23
+ # Returns the necessary JS code for the Facebook popup.
24
+ # It is recommended to put this code just before the </body> tag of your layout.
25
+ #
26
+ # For example :
27
+ # ...
28
+ # <%= javascript_include_koala %>
29
+ # </body>
30
+ # </html>
31
+ #
32
+ def javascript_include_koala
33
+ "<div id=\"fb-root\"></div>
34
+ <script src=\"http://connect.facebook.net/en_US/all.js\"></script>
35
+ <script>
36
+ FB.init({
37
+ appId:'#{Devise::koala_app_id}', cookie:true,
38
+ status:true, xfbml:true
39
+ });
40
+ </script>"
41
+ end
42
+
43
+
44
+ # Returns the Login To Facebook button.
45
+ #
46
+ # For example :
47
+ # ...
48
+ # <%= koala_login_button("Login with Facebook") %>
49
+ #
50
+ def koala_login_button(button_text = "Login with Facebook")
51
+ "<fb:login-button>#{button_text}</fb:login-button>"
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+
58
+ ::ActionView::Base.send :include, Devise::KoalaConnectable::Helpers
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ unless defined?(Devise)
3
+ require 'devise'
4
+ end
5
+ unless defined?(Koala)
6
+ require 'koala'
7
+ end
8
+
9
+ require 'devise_koala_connectable/model'
10
+ require 'devise_koala_connectable/strategy'
11
+ require 'devise_koala_connectable/schema'
12
+ require 'devise_koala_connectable/view_helpers'
13
+
14
+ module Devise
15
+ mattr_accessor :koala_identifier_field
16
+ @@koala_identifier_field = :koala_identifier
17
+
18
+ mattr_accessor :koala_auto_create_account
19
+ @@koala_auto_create_account = true
20
+
21
+ mattr_accessor :koala_app_id
22
+ @@koala_app_id = nil
23
+
24
+ mattr_accessor :koala_secret_key
25
+ @@koala_secret_key = nil
26
+
27
+ mattr_accessor :koala_callback_url
28
+ @@koala_callback_url = nil
29
+ end
30
+
31
+ I18n.load_path.unshift File.join(File.dirname(__FILE__), *%w[devise_koala_connectable locales en.yml])
32
+
33
+ Devise.add_module(:koala_connectable,
34
+ :strategy => true,
35
+ :controller => :sessions,
36
+ :model => 'devise_koala_connectable/model')
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require 'devise_koala_connectable'
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_koala_connectable
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - "Mathias Karst\xC3\xA4dt"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-17 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: devise
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - <=
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 9
34
+ version: 1.0.9
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: koala
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: devise
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - <=
58
+ - !ruby/object:Gem::Version
59
+ hash: 5
60
+ segments:
61
+ - 1
62
+ - 0
63
+ - 9
64
+ version: 1.0.9
65
+ type: :development
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: koala
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ description: Facebook authentification with koala for devise
82
+ email: mathias.karstaedt@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - README.rdoc
89
+ - lib/devise_koala_connectable.rb
90
+ - lib/devise_koala_connectable/locales/de.yml
91
+ - lib/devise_koala_connectable/locales/en.yml
92
+ - lib/devise_koala_connectable/model.rb
93
+ - lib/devise_koala_connectable/schema.rb
94
+ - lib/devise_koala_connectable/strategy.rb
95
+ - lib/devise_koala_connectable/version.rb
96
+ - lib/devise_koala_connectable/view_helpers.rb
97
+ files:
98
+ - README.rdoc
99
+ - Rakefile
100
+ - devise_koala_connectable.gemspec
101
+ - lib/devise_koala_connectable.rb
102
+ - lib/devise_koala_connectable/locales/de.yml
103
+ - lib/devise_koala_connectable/locales/en.yml
104
+ - lib/devise_koala_connectable/model.rb
105
+ - lib/devise_koala_connectable/schema.rb
106
+ - lib/devise_koala_connectable/strategy.rb
107
+ - lib/devise_koala_connectable/version.rb
108
+ - lib/devise_koala_connectable/view_helpers.rb
109
+ - rails/init.rb
110
+ - Manifest
111
+ has_rdoc: true
112
+ homepage: http://github.com/webmatze/devise_koala_connectable
113
+ licenses: []
114
+
115
+ post_install_message:
116
+ rdoc_options:
117
+ - --line-numbers
118
+ - --inline-source
119
+ - --title
120
+ - Devise_koala_connectable
121
+ - --main
122
+ - README.rdoc
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 3
131
+ segments:
132
+ - 0
133
+ version: "0"
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 11
140
+ segments:
141
+ - 1
142
+ - 2
143
+ version: "1.2"
144
+ requirements: []
145
+
146
+ rubyforge_project: devise_koala_connectable
147
+ rubygems_version: 1.3.7
148
+ signing_key:
149
+ specification_version: 3
150
+ summary: Facebook authentification with koala for devise
151
+ test_files: []
152
+