sorcery 0.2.1 → 0.3.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 (79) hide show
  1. data/README.rdoc +129 -61
  2. data/VERSION +1 -1
  3. data/lib/generators/sorcery_migration/sorcery_migration_generator.rb +24 -0
  4. data/lib/generators/sorcery_migration/templates/activity_logging.rb +17 -0
  5. data/lib/generators/sorcery_migration/templates/brute_force_protection.rb +11 -0
  6. data/lib/generators/sorcery_migration/templates/core.rb +16 -0
  7. data/lib/generators/sorcery_migration/templates/external.rb +14 -0
  8. data/lib/generators/sorcery_migration/templates/remember_me.rb +15 -0
  9. data/lib/generators/sorcery_migration/templates/reset_password.rb +13 -0
  10. data/lib/generators/sorcery_migration/templates/user_activation.rb +17 -0
  11. data/lib/sorcery/controller/adapters/sinatra.rb +97 -0
  12. data/lib/sorcery/controller/submodules/email.rb +44 -0
  13. data/lib/sorcery/controller/submodules/external/protocols/oauth1.rb +35 -0
  14. data/lib/sorcery/controller/submodules/external/protocols/oauth2.rb +26 -0
  15. data/lib/sorcery/controller/submodules/{oauth → external}/providers/facebook.rb +24 -6
  16. data/lib/sorcery/controller/submodules/{oauth → external}/providers/twitter.rb +31 -6
  17. data/lib/sorcery/controller/submodules/external.rb +83 -0
  18. data/lib/sorcery/controller/submodules/http_basic_auth.rb +10 -6
  19. data/lib/sorcery/controller/submodules/oauth.rb +6 -3
  20. data/lib/sorcery/controller.rb +5 -3
  21. data/lib/sorcery/model/submodules/activity_logging.rb +1 -1
  22. data/lib/sorcery/model/submodules/brute_force_protection.rb +0 -4
  23. data/lib/sorcery/model/submodules/{oauth.rb → external.rb} +3 -3
  24. data/lib/sorcery/model.rb +9 -6
  25. data/lib/sorcery/sinatra.rb +14 -0
  26. data/lib/sorcery/test_helpers/rails.rb +57 -0
  27. data/lib/sorcery/test_helpers/sinatra.rb +131 -0
  28. data/lib/sorcery/test_helpers.rb +8 -52
  29. data/lib/sorcery.rb +17 -7
  30. data/sorcery.gemspec +86 -10
  31. data/spec/Gemfile +1 -1
  32. data/spec/Gemfile.lock +2 -2
  33. data/spec/rails3/app_root/Gemfile +2 -4
  34. data/spec/rails3/app_root/Gemfile.lock +2 -5
  35. data/spec/rails3/app_root/app/controllers/application_controller.rb +11 -11
  36. data/spec/rails3/app_root/spec/controller_oauth2_spec.rb +15 -13
  37. data/spec/rails3/app_root/spec/controller_oauth_spec.rb +18 -13
  38. data/spec/rails3/app_root/spec/controller_session_timeout_spec.rb +2 -2
  39. data/spec/rails3/app_root/spec/spec_helper.rb +1 -0
  40. data/spec/rails3/app_root/spec/user_oauth_spec.rb +4 -4
  41. data/spec/rails3/app_root/spec/user_spec.rb +2 -2
  42. data/spec/sinatra/Gemfile +12 -0
  43. data/spec/sinatra/Gemfile.lock +134 -0
  44. data/spec/sinatra/Rakefile +10 -0
  45. data/spec/sinatra/authentication.rb +3 -0
  46. data/spec/sinatra/db/migrate/activation/20101224223622_add_activation_to_users.rb +17 -0
  47. data/spec/sinatra/db/migrate/activity_logging/20101224223624_add_activity_logging_to_users.rb +17 -0
  48. data/spec/sinatra/db/migrate/brute_force_protection/20101224223626_add_brute_force_protection_to_users.rb +11 -0
  49. data/spec/sinatra/db/migrate/core/20101224223620_create_users.rb +16 -0
  50. data/spec/sinatra/db/migrate/external/20101224223628_create_authentications.rb +14 -0
  51. data/spec/sinatra/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
  52. data/spec/sinatra/db/migrate/reset_password/20101224223622_add_reset_password_to_users.rb +13 -0
  53. data/spec/sinatra/filters.rb +21 -0
  54. data/spec/sinatra/myapp.rb +133 -0
  55. data/spec/sinatra/sorcery_mailer.rb +25 -0
  56. data/spec/sinatra/spec/controller_activity_logging_spec.rb +85 -0
  57. data/spec/sinatra/spec/controller_brute_force_protection_spec.rb +69 -0
  58. data/spec/sinatra/spec/controller_http_basic_auth_spec.rb +53 -0
  59. data/spec/sinatra/spec/controller_oauth2_spec.rb +119 -0
  60. data/spec/sinatra/spec/controller_oauth_spec.rb +121 -0
  61. data/spec/sinatra/spec/controller_remember_me_spec.rb +64 -0
  62. data/spec/sinatra/spec/controller_session_timeout_spec.rb +52 -0
  63. data/spec/sinatra/spec/controller_spec.rb +120 -0
  64. data/spec/sinatra/spec/spec.opts +4 -0
  65. data/spec/sinatra/spec/spec_helper.rb +44 -0
  66. data/spec/sinatra/spec/user_activation_spec.rb +188 -0
  67. data/spec/sinatra/spec/user_activity_logging_spec.rb +36 -0
  68. data/spec/sinatra/spec/user_brute_force_protection_spec.rb +76 -0
  69. data/spec/sinatra/spec/user_oauth_spec.rb +39 -0
  70. data/spec/sinatra/spec/user_remember_me_spec.rb +66 -0
  71. data/spec/sinatra/spec/user_reset_password_spec.rb +178 -0
  72. data/spec/sinatra/spec/user_spec.rb +317 -0
  73. data/spec/sinatra/user.rb +6 -0
  74. data/spec/sinatra/views/test_login.erb +4 -0
  75. data/spec/untitled folder +18 -0
  76. metadata +85 -9
  77. data/lib/sorcery/controller/submodules/oauth/oauth1.rb +0 -26
  78. data/lib/sorcery/controller/submodules/oauth/oauth2.rb +0 -24
  79. /data/spec/rails3/app_root/db/migrate/{oauth → external}/20101224223628_create_authentications.rb +0 -0
data/README.rdoc CHANGED
@@ -1,34 +1,137 @@
1
1
  = sorcery
2
- Magical Authentication for Rails 3.
2
+ Magical Authentication for Rails 3 and Sinatra.
3
3
 
4
4
  Inspired by restful_authentication, Authlogic and Devise.
5
5
  Crypto code taken almost unchanged from Authlogic.
6
- OAuth code inspired by OmniAuth.
6
+ OAuth code inspired by OmniAuth and Ryan Bates's railscasts about it.
7
7
 
8
- == Summary
9
8
 
10
- Sorcery aims to make your life easier by giving you an easy API to write your own user authentication flow with.
11
- It does this with a few goals in mind:
9
+ == Philosophy
12
10
 
13
- * Less is more - less than 20 simple methods to remember for the entire feature-set make the lib easy to 'get'.
14
- * No built-in or generated code - use the API inside *your own* MVC structures, and don't fight to fix someone else's.
11
+
12
+ Sorcery is a stripped-down, bare-bones authentication library, with which you can write your own authentication flow.
13
+ It was built with a few goals in mind:
14
+
15
+ * Less is more - less than 20 public methods to remember for the entire feature-set make the lib easy to 'get'.
16
+ * No built-in or generated code - use the library's methods inside *your own* MVC structures, and don't fight to fix someone else's.
15
17
  * Magic yes, Voodoo no - the lib should be easy to hack for most developers.
16
18
  * Configuration over Confusion - Simple & short configuration as possible, not drowning in syntactic sugar.
17
19
  * Keep MVC cleanly separated - DB is for models, sessions are for controllers. Models stay unaware of sessions.
18
20
 
19
21
  Hopefully, I've achieved this. If not, let me know.
20
22
 
23
+
21
24
  == Useful Links:
22
25
 
23
- Example app using sorcery: https://github.com/NoamB/sorcery-example-app
24
26
 
25
- Documentation: http://rubydoc.info/gems/sorcery/0.2.0/frames
27
+ Example Rails 3 app using sorcery: https://github.com/NoamB/sorcery-example-app
28
+
29
+ Example Sinatra app using sorcery: https://github.com/NoamB/sorcery-example-app-sinatra
30
+
31
+ Documentation: http://rubydoc.info/gems/sorcery/0.3.1/frames
26
32
 
27
33
  Check out the tutorials in the github wiki!
28
34
 
35
+
36
+ == Installation:
37
+
38
+
39
+ If using bundler, first add 'sorcery' to your Gemfile:
40
+
41
+ gem "sorcery"
42
+
43
+ And run
44
+
45
+ bundle install
46
+
47
+ Otherwise simply
48
+
49
+ gem install sorcery
50
+
51
+
52
+ == Configuration:
53
+
54
+
55
+ 1. config/application.rb
56
+
57
+ config.sorcery.submodules = [:user_activation, :remember_me] # add the submodules you want to use
58
+ # You can also configure here any controller and any controller-submodule option here. For example:
59
+ config.sorcery.session_timeout = 10.minutes
60
+
61
+ 2. app/models/user.rb (or another model of your choice, but a User class is assumed by default)
62
+
63
+ activate_sorcery! do |config|
64
+ config.user_activation_mailer = MyMailer
65
+ config.username_attribute_name = :email
66
+ end
67
+
68
+ 3. app/controllers/application_controller.rb (OPTIONAL: this is actually needed only in some cases)
69
+
70
+ activate_sorcery! do |config|
71
+ config.session_timeout = 10.minutes
72
+ end
73
+
74
+ The configuration options vary with the submodules you've chosen to use, so check the documentation or the wiki tutorials regarding the specific submodule.
75
+
76
+ For your convenience, Sorcery includes a migrations generator for Rails, which can be used like so:
77
+
78
+ rails g sorcery_migration [list of submodules]
79
+
80
+ For example, for only the core functionality use:
81
+
82
+ rails g sorcery_migration core
83
+
84
+ To generate migrations for both the core AND 'remember_me' submodule:
85
+
86
+ rails g sorcery_migration core remember_me
87
+
88
+ These migrations use the default fields. You can choose to use these migrations or make your own tables and fields. Sorcery tries not to impose a database structure and naming scheme on your application.
89
+
90
+
91
+ == API Summary
92
+
93
+ Below is a summary of the library methods. Most method names are self explaining and the rest are commented:
94
+
95
+ # core
96
+ require_login # this is a before filter
97
+ login(username,password,remember_me = false)
98
+ logout
99
+ logged_in?
100
+ current_user
101
+ redirect_back_or_to # used when a user tries to access a page while logged out, is asked to login, and we want to return him back to the page he originally wanted.
102
+ @user.external? # external users, such as facebook/twitter etc.
103
+
104
+ # activity logging
105
+ current_users
106
+
107
+ # http basic auth
108
+ require_login_from_http_basic # this is a before filter
109
+
110
+ # external
111
+ login_at(provider) # sends the user to an external service (twitter etc.) to authenticate.
112
+ login_from(provider) # tries to login from the external provider's callback.
113
+ create_from(provider) # create the user in the local app db.
114
+
115
+ # remember me
116
+ remember_me!
117
+ forget_me!
118
+
119
+ # reset password
120
+ User.load_from_reset_password_token(token)
121
+ @user.deliver_reset_password_instructions!
122
+ @user.reset_password!(params)
123
+
124
+ # user activation
125
+ User.load_from_activation_token(token)
126
+ @user.activate!
127
+
128
+ Please see the tutorials in the github wiki for detailed usage information.
129
+
130
+
29
131
  == Full Features List by module:
30
132
 
31
- Core (see lib/sorcery/model/model.rb and lib/sorcery/controller/controller.rb):
133
+
134
+ Core (see lib/sorcery/model.rb and lib/sorcery/controller.rb):
32
135
  * login/logout, optional return user to requested url on login, configurable redirect for non-logged-in users.
33
136
  * password encryption, algorithms: bcrypt(default), md5, sha1, sha256, sha512, aes256, custom(yours!), none. Configurable stretches and salt.
34
137
  * configurable attribute names for username, password and email.
@@ -68,88 +171,53 @@ Activity Logging (see lib/sorcery/model/submodules/activity_logging.rb):
68
171
  * an easy method of collecting the list of currently logged in users.
69
172
  * configurable timeout by which to decide whether to include a user in the list of logged in users.
70
173
 
71
- Oauth (see lib/sorcery/controller/submodules/oauth.rb):
174
+ External (see lib/sorcery/controller/submodules/external.rb):
72
175
  * OAuth1 and OAuth2 support (currently twitter & facebook)
73
176
  * configurable db field names and authentications table.
74
177
 
178
+
75
179
  == Next Planned Features:
76
180
 
181
+
77
182
  I've got many plans which include (by priority):
78
- * Sinatra support
183
+ * Scoping logins (to a subdomain or another arbitrary field)
184
+ * Simple auth (no user)
185
+ * Switching authentication mode at runtime (Maintenance mode)
79
186
  * Mongoid support
80
- * Configurable Auto login on registration/activation
81
187
  * Other reset password strategies (security questions?)
82
188
  * Other brute force protection strategies (captcha)
83
189
  * Have an idea? Let me know, and it might get into the gem!
84
190
 
85
- == Installation:
86
-
87
- You can either git clone and then 'rake install' to live on the edge (unstable),
88
-
89
- Or simply (stable):
90
-
91
- gem install sorcery
92
-
93
- == Configuration:
94
-
95
- First add 'sorcery' to your Gemfile:
96
-
97
- gem "sorcery"
98
-
99
- And run
100
-
101
- bundle install
102
-
103
- There are 2 required places to configure the plugin, and an optional one:
104
- 1. config/application.rb
105
-
106
- config.sorcery.submodules = [:user_activation, :remember_me] # add the modules you want to use
107
-
108
- You can also configure here any controller and any controller-submodule option here.
109
- For example:
110
-
111
- config.sorcery.session_timeout = 10.minutes
112
-
113
-
114
- 2. app/models/user.rb (or another model of your choice)
115
-
116
- activate_sorcery! do |config|
117
- config.user_activation_mailer = MyMailer
118
- config.username_attribute_name = :email
119
- end
120
-
121
- 3. app/controllers/application_controller.rb (OPTIONAL: this is actually needed only in some cases)
122
-
123
- activate_sorcery! do |config|
124
- config.session_timeout = 10.minutes
125
- end
126
-
127
- Also check the migrations in the example app to see what database fields are expected.
128
-
129
- The configuration options vary with the modules you've chosen to use.
191
+ Other stuff:
192
+ * Improve documentation
193
+ * Tty to reduce the number of library methods, and find better names to some
130
194
 
131
195
 
132
196
  == Contributing to sorcery
133
197
 
198
+
134
199
  Your feedback is very welcome and will make this gem much much better for you, me and everyone else.
135
200
  Besides feedback on code, features, suggestions and bug reports, you may want to actually make an impact on the code.
136
201
  For this:
137
202
 
138
203
  * Fork the project.
139
204
  * Make your feature addition or bug fix.
140
- * Add tests for it. This is important so I don’t break it in a future version unintentionally.
205
+ * Add tests for it. I've used RSpec so far, please remain consistent with it.
141
206
  * Commit, do not mess with Rakefiles, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
142
207
  * Send me a pull request. Bonus points for topic branches.
143
208
 
144
209
  If you feel my work has made your life easier, and you would like to thank me through a donation, my paypal email is in the contact details.
145
210
 
211
+
146
212
  == Contact
147
213
 
214
+ Feel free to ask questions using these contact details:
215
+
148
216
  email: nbenari@gmail.com ( also for paypal )
149
217
  twitter: @nbenari
150
218
 
219
+
151
220
  == Copyright
152
221
 
153
- Copyright (c) 2010 Noam Ben Ari (nbenari@gmail.com). See LICENSE.txt for further details.
154
- Released with permission from Kontera (http://www.kontera.com), where I work.
155
222
 
223
+ Copyright (c) 2010 Noam Ben Ari (nbenari@gmail.com). See LICENSE.txt for further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.1
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class SorceryMigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ source_root File.join(File.dirname(__FILE__), 'templates')
8
+ argument :submodules, :type => :array, :required => true
9
+
10
+ def self.next_migration_number(dirname)
11
+ if ActiveRecord::Base.timestamped_migrations
12
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
13
+ else
14
+ "%.3d" % (current_migration_number(dirname) + 1)
15
+ end
16
+ end
17
+
18
+ def create_migration_file
19
+ self.submodules.each do |submodule|
20
+ migration_template "#{submodule}.rb", "db/migrate/sorcery_#{submodule}.rb"
21
+ sleep 1 # for the timestamp to change
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ class SorceryActivityLogging < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :last_login_at, :datetime, :default => nil
4
+ add_column :users, :last_logout_at, :datetime, :default => nil
5
+ add_column :users, :last_activity_at, :datetime, :default => nil
6
+
7
+ add_index :users, [:last_logout_at, :last_activity_at]
8
+ end
9
+
10
+ def self.down
11
+ remove_index :users, [:last_logout_at, :last_activity_at]
12
+
13
+ remove_column :users, :last_activity_at
14
+ remove_column :users, :last_logout_at
15
+ remove_column :users, :last_login_at
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ class SorceryBruteForceProtection < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :failed_logins_count, :integer, :default => 0
4
+ add_column :users, :lock_expires_at, :datetime, :default => nil
5
+ end
6
+
7
+ def self.down
8
+ remove_column :users, :lock_expires_at
9
+ remove_column :users, :failed_logins_count
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ class SorceryCore < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :users do |t|
4
+ t.string :username, :null => false
5
+ t.string :email, :default => nil
6
+ t.string :crypted_password, :default => nil
7
+ t.string :salt, :default => nil
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :users
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ class SorceryExternal < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :authentications do |t|
4
+ t.integer :user_id, :null => false
5
+ t.string :provider, :uid, :null => false
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :authentications
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ class SorceryRememberMe < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :remember_me_token, :string, :default => nil
4
+ add_column :users, :remember_me_token_expires_at, :datetime, :default => nil
5
+
6
+ add_index :users, :remember_me_token
7
+ end
8
+
9
+ def self.down
10
+ remove_index :users, :remember_me_token
11
+
12
+ remove_column :users, :remember_me_token_expires_at
13
+ remove_column :users, :remember_me_token
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ class SorceryResetPassword < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :reset_password_token, :string, :default => nil
4
+ add_column :users, :reset_password_token_expires_at, :datetime, :default => nil
5
+ add_column :users, :reset_password_email_sent_at, :datetime, :default => nil
6
+ end
7
+
8
+ def self.down
9
+ remove_column :users, :reset_password_email_sent_at
10
+ remove_column :users, :reset_password_token_expires_at
11
+ remove_column :users, :reset_password_token
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ class SorceryUserActivation < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :users, :activation_state, :string, :default => nil
4
+ add_column :users, :activation_token, :string, :default => nil
5
+ add_column :users, :activation_token_expires_at, :datetime, :default => nil
6
+
7
+ add_index :users, :activation_token
8
+ end
9
+
10
+ def self.down
11
+ remove_index :users, :activation_token
12
+
13
+ remove_column :users, :activation_token_expires_at
14
+ remove_column :users, :activation_token
15
+ remove_column :users, :activation_state
16
+ end
17
+ end
@@ -0,0 +1,97 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Adapters
4
+ # This module does the magic of translating Rails commands to Sinatra.
5
+ # This way the Rails code doesn't change, but it actually now calls Sinatra calls.
6
+ module Sinatra
7
+ def self.included(base)
8
+ base.class_eval do
9
+ class << self
10
+ # prepend a filter
11
+ def prepend_filter(type, path = nil, options = {}, &block)
12
+ return filters[type].unshift block unless path
13
+ path, options = //, path if path.respond_to?(:each_pair)
14
+ block, *arguments = compile!(type, path, block, options)
15
+ prepend_filter(type) do
16
+ process_route(*arguments) { instance_eval(&block) }
17
+ end
18
+ end
19
+
20
+ def after_filter(filter)
21
+ after do
22
+ send(filter)
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ ::Sinatra::Request.class_eval do
29
+ def authorization
30
+ env['HTTP_AUTHORIZATION'] ||
31
+ env['X-HTTP_AUTHORIZATION'] ||
32
+ env['X_HTTP_AUTHORIZATION'] ||
33
+ env['REDIRECT_X_HTTP_AUTHORIZATION'] || nil
34
+ end
35
+ end
36
+
37
+ base.send(:include, InstanceMethods)
38
+ base.extend(ClassMethods)
39
+ end
40
+
41
+ module InstanceMethods
42
+ def reset_session
43
+ session.clear
44
+ end
45
+
46
+ def redirect_to(*args)
47
+ args.pop if args.last.is_a?(Hash)
48
+ redirect(*args)
49
+ end
50
+
51
+ def root_path
52
+ '/'
53
+ end
54
+
55
+ helpers do
56
+ def request_http_basic_authentication(realm)
57
+ response.header['WWW-Authenticate'] = %(Basic realm="#{realm}")
58
+ response.status = 401
59
+ end
60
+
61
+ def authenticate_with_http_basic(&blk)
62
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
63
+ yield @auth.credentials if ( @auth.provided? && @auth.basic? && @auth.credentials )
64
+ end
65
+ end
66
+
67
+ def cookies
68
+ @cookie_proxy ||= CookieProxy.new(request,response)
69
+ end
70
+ end
71
+
72
+ class CookieProxy
73
+ def initialize(request,response)
74
+ @request = request
75
+ @response = response
76
+ end
77
+
78
+ def [](key)
79
+ @request.cookies[key.to_s]
80
+ end
81
+
82
+ def []=(key, value)
83
+ @response.set_cookie(key, value)
84
+ end
85
+ end
86
+
87
+ module ClassMethods
88
+ def prepend_before_filter(filter)
89
+ prepend_filter(:before) do
90
+ send(filter)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,44 @@
1
+ # module Sorcery
2
+ # module Controller
3
+ # module Submodules
4
+ # # This module allows you to authenticate to the site using a mail server.
5
+ # # Let's say your organization is called blup, and everyone has someklutz@blup.com,
6
+ # # Instead of registering a new username and password for an internal web application,
7
+ # # why not simply reuse your email credentials?
8
+ # # Of course this means that if the mail server is down, nobody can authenticate to your app.
9
+ # # I might add a fallback for that in the future.
10
+ # module Email
11
+ # def self.included(base)
12
+ # base.send(:include, InstanceMethods)
13
+ # Config.module_eval do
14
+ # class << self
15
+ # attr_reader :email_protocols # email protocols like pop3, IMAP etc.
16
+ #
17
+ # def merge_mail_defaults!
18
+ # @defaults.merge!(:@email_protocols => [])
19
+ # end
20
+ #
21
+ # def email_protocols=(protocols)
22
+ # protocols.each do |protocol|
23
+ # include Protocols.const_get(protocol.to_s.split("_").map {|p| p.capitalize}.join(""))
24
+ # end
25
+ # end
26
+ # end
27
+ # merge_mail_defaults!
28
+ # end
29
+ # end
30
+ #
31
+ # module InstanceMethods
32
+ # protected
33
+ #
34
+ # # sends user to authenticate at the provider's website.
35
+ # # after authentication the user is redirected to the callback defined in the provider config
36
+ # def login_at(protocol)
37
+ #
38
+ # end
39
+ #
40
+ # end
41
+ # end
42
+ # end
43
+ # end
44
+ # end
@@ -0,0 +1,35 @@
1
+ require 'oauth'
2
+ module Sorcery
3
+ module Controller
4
+ module Submodules
5
+ module External
6
+ module Protocols
7
+ module Oauth1
8
+ def oauth_version
9
+ "1.0"
10
+ end
11
+
12
+ def get_request_token(token=nil,secret=nil)
13
+ return ::OAuth::RequestToken.new(get_consumer,token,secret) if token && secret
14
+ get_consumer.get_request_token(:oauth_callback => @callback_url)
15
+ end
16
+
17
+ def authorize_url(args)
18
+ get_request_token(args[:request_token],args[:request_token_secret]).authorize_url(:oauth_callback => @callback_url)
19
+ end
20
+
21
+ def get_access_token(args)
22
+ get_request_token(args[:request_token],args[:request_token_secret]).get_access_token(:oauth_verifier => args[:oauth_verifier])
23
+ end
24
+
25
+ protected
26
+
27
+ def get_consumer
28
+ ::OAuth::Consumer.new(@key, @secret, :site => @site)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'oauth2'
2
+ module Sorcery
3
+ module Controller
4
+ module Submodules
5
+ module External
6
+ module Protocols
7
+ module Oauth2
8
+ def oauth_version
9
+ "2.0"
10
+ end
11
+
12
+ def authorize_url(*args)
13
+ client = ::OAuth2::Client.new(@key, @secret, :site => @site)
14
+ client.web_server.authorize_url(:redirect_uri => @callback_url, :scope => @scope)
15
+ end
16
+
17
+ def get_access_token(args)
18
+ client = ::OAuth2::Client.new(@key, @secret, :site => @site)
19
+ client.web_server.get_access_token(args[:code], :redirect_uri => @callback_url)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,7 +1,7 @@
1
1
  module Sorcery
2
2
  module Controller
3
3
  module Submodules
4
- module Oauth
4
+ module External
5
5
  module Providers
6
6
  # This module adds support for OAuth with facebook.com.
7
7
  # When included in the 'config.providers' option, it adds a new option, 'config.facebook'.
@@ -16,7 +16,7 @@ module Sorcery
16
16
  base.module_eval do
17
17
  class << self
18
18
  attr_reader :facebook # access to facebook_client.
19
-
19
+
20
20
  def merge_facebook_defaults!
21
21
  @defaults.merge!(:@facebook => FacebookClient)
22
22
  end
@@ -36,7 +36,7 @@ module Sorcery
36
36
  :scope,
37
37
  :user_info_mapping
38
38
 
39
- include Oauth2
39
+ include Protocols::Oauth2
40
40
 
41
41
  def init
42
42
  @site = "https://graph.facebook.com"
@@ -45,13 +45,31 @@ module Sorcery
45
45
  @user_info_mapping = {}
46
46
  end
47
47
 
48
- def get_user_hash(access_token)
48
+ def get_user_hash
49
49
  user_hash = {}
50
- response = access_token.get(@user_info_path)
50
+ response = @access_token.get(@user_info_path)
51
51
  user_hash[:user_info] = JSON.parse(response)
52
- user_hash[:uid] = user_hash[:user_info]['id'].to_i
52
+ user_hash[:uid] = user_hash[:user_info]['id']
53
53
  user_hash
54
54
  end
55
+
56
+ def has_callback?
57
+ true
58
+ end
59
+
60
+ # calculates and returns the url to which the user should be redirected,
61
+ # to get authenticated at the external provider's site.
62
+ def login_url(params,session)
63
+ self.authorize_url
64
+ end
65
+
66
+ # tries to login the user from access token
67
+ def process_callback(params,session)
68
+ args = {}
69
+ args.merge!({:code => params[:code]}) if params[:code]
70
+ @access_token = self.get_access_token(args)
71
+ end
72
+
55
73
  end
56
74
  init
57
75
  end