sorcery 0.1.4 → 0.2.0

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.

Potentially problematic release.


This version of sorcery might be problematic. Click here for more details.

Files changed (65) hide show
  1. data/Gemfile +4 -2
  2. data/Gemfile.lock +16 -13
  3. data/README.rdoc +28 -27
  4. data/Rakefile +5 -0
  5. data/VERSION +1 -1
  6. data/lib/sorcery.rb +12 -0
  7. data/lib/sorcery/controller.rb +29 -17
  8. data/lib/sorcery/controller/submodules/activity_logging.rb +20 -7
  9. data/lib/sorcery/controller/submodules/brute_force_protection.rb +9 -2
  10. data/lib/sorcery/controller/submodules/http_basic_auth.rb +8 -3
  11. data/lib/sorcery/controller/submodules/oauth.rb +95 -0
  12. data/lib/sorcery/controller/submodules/oauth/oauth1.rb +25 -0
  13. data/lib/sorcery/controller/submodules/oauth/oauth2.rb +23 -0
  14. data/lib/sorcery/controller/submodules/oauth/providers/facebook.rb +64 -0
  15. data/lib/sorcery/controller/submodules/oauth/providers/twitter.rb +61 -0
  16. data/lib/sorcery/controller/submodules/remember_me.rb +14 -5
  17. data/lib/sorcery/controller/submodules/session_timeout.rb +6 -1
  18. data/lib/sorcery/engine.rb +9 -2
  19. data/lib/sorcery/model.rb +10 -3
  20. data/lib/sorcery/model/submodules/activity_logging.rb +12 -7
  21. data/lib/sorcery/model/submodules/brute_force_protection.rb +11 -4
  22. data/lib/sorcery/model/submodules/oauth.rb +53 -0
  23. data/lib/sorcery/model/submodules/remember_me.rb +5 -3
  24. data/lib/sorcery/model/submodules/reset_password.rb +16 -13
  25. data/lib/sorcery/model/submodules/user_activation.rb +38 -19
  26. data/lib/sorcery/model/temporary_token.rb +22 -0
  27. data/lib/sorcery/test_helpers.rb +84 -0
  28. data/sorcery.gemspec +69 -40
  29. data/spec/Gemfile +3 -2
  30. data/spec/Gemfile.lock +15 -2
  31. data/spec/rails3/app_root/.rspec +1 -0
  32. data/spec/rails3/{Gemfile → app_root/Gemfile} +5 -3
  33. data/spec/rails3/{Gemfile.lock → app_root/Gemfile.lock} +25 -2
  34. data/spec/rails3/{Rakefile → app_root/Rakefile} +0 -0
  35. data/spec/rails3/app_root/app/controllers/application_controller.rb +42 -1
  36. data/spec/rails3/app_root/app/models/authentication.rb +3 -0
  37. data/spec/rails3/app_root/app/models/user.rb +4 -1
  38. data/spec/rails3/app_root/config/application.rb +1 -3
  39. data/spec/rails3/app_root/config/routes.rb +1 -10
  40. data/spec/rails3/app_root/db/migrate/activation/20101224223622_add_activation_to_users.rb +6 -4
  41. data/spec/rails3/app_root/db/migrate/core/20101224223620_create_users.rb +4 -4
  42. data/spec/rails3/app_root/db/migrate/oauth/20101224223628_create_authentications.rb +14 -0
  43. data/spec/rails3/{controller_activity_logging_spec.rb → app_root/spec/controller_activity_logging_spec.rb} +13 -13
  44. data/spec/rails3/{controller_brute_force_protection_spec.rb → app_root/spec/controller_brute_force_protection_spec.rb} +16 -6
  45. data/spec/rails3/{controller_http_basic_auth_spec.rb → app_root/spec/controller_http_basic_auth_spec.rb} +3 -3
  46. data/spec/rails3/app_root/spec/controller_oauth2_spec.rb +117 -0
  47. data/spec/rails3/app_root/spec/controller_oauth_spec.rb +117 -0
  48. data/spec/rails3/{controller_remember_me_spec.rb → app_root/spec/controller_remember_me_spec.rb} +4 -4
  49. data/spec/rails3/{controller_session_timeout_spec.rb → app_root/spec/controller_session_timeout_spec.rb} +4 -4
  50. data/spec/rails3/{controller_spec.rb → app_root/spec/controller_spec.rb} +20 -13
  51. data/spec/rails3/app_root/spec/spec_helper.orig.rb +27 -0
  52. data/spec/rails3/app_root/spec/spec_helper.rb +61 -0
  53. data/spec/rails3/{user_activation_spec.rb → app_root/spec/user_activation_spec.rb} +60 -20
  54. data/spec/rails3/{user_activity_logging_spec.rb → app_root/spec/user_activity_logging_spec.rb} +4 -4
  55. data/spec/rails3/{user_brute_force_protection_spec.rb → app_root/spec/user_brute_force_protection_spec.rb} +7 -7
  56. data/spec/rails3/app_root/spec/user_oauth_spec.rb +39 -0
  57. data/spec/rails3/{user_remember_me_spec.rb → app_root/spec/user_remember_me_spec.rb} +4 -4
  58. data/spec/rails3/{user_reset_password_spec.rb → app_root/spec/user_reset_password_spec.rb} +21 -41
  59. data/spec/rails3/{user_spec.rb → app_root/spec/user_spec.rb} +68 -38
  60. metadata +127 -58
  61. data/spec/rails3/app_root/test/fixtures/users.yml +0 -9
  62. data/spec/rails3/app_root/test/performance/browsing_test.rb +0 -9
  63. data/spec/rails3/app_root/test/test_helper.rb +0 -13
  64. data/spec/rails3/app_root/test/unit/user_test.rb +0 -8
  65. data/spec/rails3/spec_helper.rb +0 -135
@@ -1,6 +1,9 @@
1
1
  module Sorcery
2
2
  module Controller
3
3
  module Submodules
4
+ # This submodule integrates HTTP Basic authentication into sorcery.
5
+ # You are provided with a before filter, require_login_from_http_basic, which requests the browser for authentication.
6
+ # Then the rest of the submodule takes care of logging the user in into the session, so that the next requests will keep him logged in.
4
7
  module HttpBasicAuth
5
8
  def self.included(base)
6
9
  base.send(:include, InstanceMethods)
@@ -31,17 +34,19 @@ module Sorcery
31
34
  def require_login_from_http_basic
32
35
  (request_http_basic_authentication(realm_name_by_controller) and (session[:http_authentication_used] = true) and return) if (request.authorization.nil? || session[:http_authentication_used].nil?)
33
36
  require_login
37
+ session[:http_authentication_used] = nil unless logged_in?
34
38
  end
35
39
 
36
40
  # given to main controller module as a login source callback
37
41
  def login_from_basic_auth
38
42
  authenticate_with_http_basic do |username, password|
39
- @logged_in_user = (Config.user_class.authenticate(username, password) if session[:http_authentication_used]) || false
40
- login_user(@logged_in_user) if @logged_in_user
41
- @logged_in_user
43
+ @current_user = (Config.user_class.authenticate(username, password) if session[:http_authentication_used]) || false
44
+ login_user(@current_user) if @current_user
45
+ @current_user
42
46
  end
43
47
  end
44
48
 
49
+ # Sets the realm name by searching the controller name in the hash given at configuration time.
45
50
  def realm_name_by_controller
46
51
  current_controller = self.class
47
52
  while current_controller != ActionController::Base
@@ -0,0 +1,95 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ # This submodule helps you login users from OAuth providers such as Twitter.
5
+ # This is the controller part which handles the http requests and tokens passed between the app and the provider.
6
+ # For more configuration options see Sorcery::Model::Oauth.
7
+ module Oauth
8
+ def self.included(base)
9
+ base.send(:include, InstanceMethods)
10
+ Config.module_eval do
11
+ class << self
12
+ attr_reader :oauth_providers # oauth providers like twitter.
13
+
14
+ attr_accessor :authentications_class
15
+
16
+ def merge_oauth_defaults!
17
+ @defaults.merge!(:@oauth_providers => [],
18
+ :@authentications_class => nil)
19
+ end
20
+
21
+ def oauth_providers=(providers)
22
+ providers.each do |provider|
23
+ include Providers.const_get(provider.to_s.split("_").map {|p| p.capitalize}.join(""))
24
+ end
25
+ end
26
+ end
27
+ merge_oauth_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 auth_at_provider(provider)
37
+ @provider = Config.send(provider)
38
+ if @provider.respond_to?(:get_request_token)
39
+ args = {:request_token => @provider.get_request_token}
40
+ session[:request_token] = args[:request_token]
41
+ end
42
+ redirect_to @provider.authorize_url(args)
43
+ end
44
+
45
+ # tries to login the user from access token
46
+ def login_from_access_token(provider)
47
+ @provider = Config.send(provider)
48
+ args = {}
49
+ args.merge!({:oauth_verifier => params[:oauth_verifier], :request_token => session[:request_token]}) if @provider.respond_to?(:get_request_token)
50
+ args.merge!({:code => params[:code]}) if params[:code]
51
+ @access_token = @provider.get_access_token(args)
52
+ @user_hash = @provider.get_user_hash(@access_token)
53
+ if user = Config.user_class.load_from_provider(provider,@user_hash[:uid])
54
+ reset_session
55
+ login_user(user)
56
+ user
57
+ end
58
+ end
59
+
60
+ def get_user_hash(provider)
61
+ @provider = Config.send(provider)
62
+ @provider.get_user_hash(@access_token)
63
+ end
64
+
65
+ # this method automatically creates a new user from the data in the external user hash.
66
+ # The mappings from user hash fields to user db fields are set at controller config.
67
+ # If the hash field you would like to map is nested, use slashes. For example, Given a hash like:
68
+ #
69
+ # "user" => {"name"=>"moishe"}
70
+ #
71
+ # You will set the mapping:
72
+ #
73
+ # {:username => "user/name"}
74
+ #
75
+ # And this will cause 'moishe' to be set as the value of :username field.
76
+ def create_from_provider!(provider)
77
+ provider = provider.to_sym
78
+ @provider = Config.send(provider)
79
+ @user_hash = get_user_hash(provider)
80
+ config = Config.user_class.sorcery_config
81
+ attrs = {}
82
+ @provider.user_info_mapping.each do |k,v|
83
+ (varr = v.split("/")).size > 1 ? attrs.merge!(k => varr.inject(@user_hash[:user_info]) {|hsh,v| hsh[v] }) : attrs.merge!(k => @user_hash[:user_info][v])
84
+ end
85
+ Config.user_class.transaction do
86
+ @user = Config.user_class.create!(attrs)
87
+ Config.authentications_class.create!({config.authentications_user_id_attribute_name => @user.id, config.provider_attribute_name => provider, config.provider_uid_attribute_name => @user_hash[:uid]})
88
+ end
89
+ @user
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,25 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module Oauth
5
+ module Oauth1
6
+ def oauth_version
7
+ "1.0"
8
+ end
9
+
10
+ def get_request_token
11
+ ::OAuth::Consumer.new(@key, @secret, :site => @site).get_request_token(:oauth_callback => @callback_url)
12
+ end
13
+
14
+ def authorize_url(args)
15
+ args[:request_token].authorize_url(:oauth_callback => @callback_url)
16
+ end
17
+
18
+ def get_access_token(args)
19
+ args[:request_token].get_access_token(:oauth_verifier => args[:oauth_verifier])
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module Oauth
5
+ module Oauth2
6
+ def oauth_version
7
+ "2.0"
8
+ end
9
+
10
+ def authorize_url(args)
11
+ client = ::OAuth2::Client.new(@key, @secret, :site => @site)
12
+ client.web_server.authorize_url(:redirect_uri => @callback_url, :scope => @scope)
13
+ end
14
+
15
+ def get_access_token(args)
16
+ client = ::OAuth2::Client.new(@key, @secret, :site => @site)
17
+ client.web_server.get_access_token(args[:code], :redirect_uri => @callback_url)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,64 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module Oauth
5
+ module Providers
6
+ # This module adds support for OAuth with facebook.com.
7
+ # When included in the 'config.providers' option, it adds a new option, 'config.facebook'.
8
+ # Via this new option you can configure Facebook specific settings like your app's key and secret.
9
+ #
10
+ # config.facebook.key = <key>
11
+ # config.facebook.secret = <secret>
12
+ # ...
13
+ #
14
+ module Facebook
15
+ def self.included(base)
16
+ base.module_eval do
17
+ class << self
18
+ attr_reader :facebook # access to facebook_client.
19
+
20
+ def merge_facebook_defaults!
21
+ @defaults.merge!(:@facebook => FacebookClient)
22
+ end
23
+ end
24
+ merge_facebook_defaults!
25
+ update!
26
+ end
27
+ end
28
+
29
+ module FacebookClient
30
+ class << self
31
+ attr_accessor :key,
32
+ :secret,
33
+ :callback_url,
34
+ :site,
35
+ :user_info_path,
36
+ :scope,
37
+ :user_info_mapping
38
+
39
+ include Oauth2
40
+
41
+ def init
42
+ @site = "https://graph.facebook.com"
43
+ @user_info_path = "/me"
44
+ @scope = "email,offline_access"
45
+ @user_info_mapping = {}
46
+ end
47
+
48
+ def get_user_hash(access_token)
49
+ user_hash = {}
50
+ response = access_token.get(@user_info_path)
51
+ user_hash[:user_info] = JSON.parse(response)
52
+ user_hash[:uid] = user_hash[:user_info]['id'].to_i
53
+ user_hash
54
+ end
55
+ end
56
+ init
57
+ end
58
+
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,61 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module Oauth
5
+ module Providers
6
+ # This module adds support for OAuth with Twitter.com.
7
+ # When included in the 'config.providers' option, it adds a new option, 'config.twitter'.
8
+ # Via this new option you can configure Twitter specific settings like your app's key and secret.
9
+ #
10
+ # config.twitter.key = <key>
11
+ # config.twitter.secret = <secret>
12
+ # ...
13
+ #
14
+ module Twitter
15
+ def self.included(base)
16
+ base.module_eval do
17
+ class << self
18
+ attr_reader :twitter # access to twitter_client.
19
+
20
+ def merge_twitter_defaults!
21
+ @defaults.merge!(:@twitter => TwitterClient)
22
+ end
23
+ end
24
+ merge_twitter_defaults!
25
+ update!
26
+ end
27
+ end
28
+
29
+ module TwitterClient
30
+ class << self
31
+ attr_accessor :key,
32
+ :secret,
33
+ :callback_url,
34
+ :site,
35
+ :user_info_path,
36
+ :user_info_mapping
37
+
38
+ include Oauth1
39
+
40
+ def init
41
+ @site = "https://api.twitter.com"
42
+ @user_info_path = "/1/account/verify_credentials.json"
43
+ @user_info_mapping = {}
44
+ end
45
+
46
+ def get_user_hash(access_token)
47
+ user_hash = {}
48
+ response = access_token.get(@user_info_path)
49
+ user_hash[:user_info] = JSON.parse(response.body)
50
+ user_hash[:uid] = user_hash[:user_info]['id']
51
+ user_hash
52
+ end
53
+ end
54
+ init
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,6 +1,9 @@
1
1
  module Sorcery
2
2
  module Controller
3
3
  module Submodules
4
+ # The Remember Me submodule takes care of setting the user's cookie so that he will be automatically logged in to the site on every visit,
5
+ # until the cookie expires.
6
+ # See Sorcery::Model::Submodules::RememberMe for configuration options.
4
7
  module RememberMe
5
8
  def self.included(base)
6
9
  base.send(:include, InstanceMethods)
@@ -10,29 +13,35 @@ module Sorcery
10
13
  end
11
14
 
12
15
  module InstanceMethods
16
+ # This method sets the cookie and calls the user to save the token and the expiration to db.
13
17
  def remember_me!
14
- logged_in_user.remember_me!
15
- cookies[:remember_me_token] = { :value => logged_in_user.remember_me_token, :expires => logged_in_user.remember_me_token_expires_at }
18
+ current_user.remember_me!
19
+ cookies[:remember_me_token] = { :value => current_user.remember_me_token, :expires => current_user.remember_me_token_expires_at }
16
20
  end
17
21
 
22
+ # Clears the cookie and clears the token from the db.
18
23
  def forget_me!
19
- logged_in_user.forget_me!
24
+ current_user.forget_me!
20
25
  cookies[:remember_me_token] = nil
21
26
  end
22
27
 
23
28
  protected
24
29
 
30
+ # calls remember_me! if a third credential was passed to the login method.
31
+ # Runs as a hook after login.
25
32
  def remember_me_if_asked_to(user, credentials)
26
33
  remember_me! if credentials.size == 3 && credentials[2]
27
34
  end
28
35
 
36
+ # Checks the cookie for a remember me token, tried to find a user with that token and logs the user in if found.
37
+ # Runs as a login source. See 'current_user' method for how it is used.
29
38
  def login_from_cookie
30
39
  user = cookies[:remember_me_token] && Config.user_class.find_by_remember_me_token(cookies[:remember_me_token])
31
40
  if user && user.remember_me_token?
32
41
  cookies[:remember_me_token] = { :value => user.remember_me_token, :expires => user.remember_me_token_expires_at }
33
- @logged_in_user = user
42
+ @current_user = user
34
43
  else
35
- @logged_in_user = false
44
+ @current_user = false
36
45
  end
37
46
  end
38
47
  end
@@ -1,6 +1,8 @@
1
1
  module Sorcery
2
2
  module Controller
3
3
  module Submodules
4
+ # This submodule helps you set a timeout to all user sessions.
5
+ # The timeout can be configured and also you can choose to reset it on every user action.
4
6
  module SessionTimeout
5
7
  def self.included(base)
6
8
  base.send(:include, InstanceMethods)
@@ -23,16 +25,19 @@ module Sorcery
23
25
  module InstanceMethods
24
26
  protected
25
27
 
28
+ # Registers last login to be used as the timeout starting point.
29
+ # Runs as a hook after a successful login.
26
30
  def register_login_time(user, credentials)
27
31
  session[:login_time] = session[:last_action_time] = Time.now.utc
28
32
  end
29
33
 
34
+ # Checks if session timeout was reached and expires the current session if so.
30
35
  # To be used as a before_filter, before require_login
31
36
  def validate_session
32
37
  session_to_use = Config.session_timeout_from_last_action ? session[:last_action_time] : session[:login_time]
33
38
  if session_to_use && (Time.now.utc - session_to_use > Config.session_timeout)
34
39
  reset_session
35
- @logged_in_user = false
40
+ @current_user = false
36
41
  else
37
42
  session[:last_action_time] = Time.now.utc
38
43
  end
@@ -13,8 +13,15 @@ module Sorcery
13
13
 
14
14
  initializer "extend Controller with sorcery" do |app|
15
15
  ActionController::Base.send(:include, Sorcery::Controller)
16
- ActionController::Base.helper_method :logged_in_user
16
+ ActionController::Base.helper_method :current_user
17
17
  end
18
-
18
+
19
+ initializer "attempt to preload user model" do |app|
20
+ begin
21
+ require Rails.root + "app/models/user.rb"
22
+ rescue LoadError
23
+ end
24
+ end
25
+
19
26
  end
20
27
  end
data/lib/sorcery/model.rb CHANGED
@@ -55,7 +55,8 @@ module Sorcery
55
55
  _salt = user.send(@sorcery_config.salt_attribute_name) if user && !@sorcery_config.salt_attribute_name.nil? && !@sorcery_config.encryption_provider.nil?
56
56
  user if user && @sorcery_config.before_authenticate.all? {|c| user.send(c)} && credentials_match?(user.send(@sorcery_config.crypted_password_attribute_name),credentials[1],_salt)
57
57
  end
58
-
58
+
59
+ # Calls the configured encryption provider to compare the supplied password with the encrypted one.
59
60
  def credentials_match?(crypted, *tokens)
60
61
  return crypted == tokens.join if @sorcery_config.encryption_provider.nil?
61
62
  @sorcery_config.encryption_provider.matches?(crypted, *tokens)
@@ -78,13 +79,19 @@ module Sorcery
78
79
  self.class.sorcery_config
79
80
  end
80
81
 
82
+ # identifies whether this user is regular, i.e. we hold his credentials in our db,
83
+ # or that he is external, and his credentials are saved elsewhere (twitter/facebook etc.).
84
+ def external?
85
+ send(sorcery_config.crypted_password_attribute_name).nil?
86
+ end
87
+
81
88
  protected
82
89
 
83
90
  # creates new salt and saves it.
84
91
  # encrypts password with salt and save it.
85
92
  def encrypt_password
86
93
  config = sorcery_config
87
- new_salt = self.send(:"#{config.salt_attribute_name}=", generate_random_code) if !config.salt_attribute_name.nil?
94
+ new_salt = self.send(:"#{config.salt_attribute_name}=", generate_random_token) if !config.salt_attribute_name.nil?
88
95
  self.send(:"#{config.crypted_password_attribute_name}=", self.class.encrypt(self.send(config.password_attribute_name),new_salt))
89
96
  end
90
97
 
@@ -104,7 +111,7 @@ module Sorcery
104
111
  end
105
112
 
106
113
  # Random code, used for salt and temporary tokens.
107
- def generate_random_code
114
+ def generate_random_token
108
115
  return Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
109
116
  end
110
117
  end