sorcery 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (101) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +19 -0
  4. data/Gemfile.lock +129 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +139 -0
  7. data/Rakefile +61 -0
  8. data/VERSION +1 -0
  9. data/features/support/env.rb +13 -0
  10. data/lib/sorcery.rb +28 -0
  11. data/lib/sorcery/controller.rb +156 -0
  12. data/lib/sorcery/controller/submodules/brute_force_protection.rb +89 -0
  13. data/lib/sorcery/controller/submodules/remember_me.rb +43 -0
  14. data/lib/sorcery/controller/submodules/session_timeout.rb +42 -0
  15. data/lib/sorcery/crypto_providers/aes256.rb +44 -0
  16. data/lib/sorcery/crypto_providers/bcrypt.rb +96 -0
  17. data/lib/sorcery/crypto_providers/md5.rb +39 -0
  18. data/lib/sorcery/crypto_providers/sha1.rb +40 -0
  19. data/lib/sorcery/crypto_providers/sha256.rb +55 -0
  20. data/lib/sorcery/crypto_providers/sha512.rb +55 -0
  21. data/lib/sorcery/engine.rb +20 -0
  22. data/lib/sorcery/model.rb +175 -0
  23. data/lib/sorcery/model/submodules/password_reset.rb +64 -0
  24. data/lib/sorcery/model/submodules/remember_me.rb +42 -0
  25. data/lib/sorcery/model/submodules/user_activation.rb +84 -0
  26. data/spec/Gemfile +11 -0
  27. data/spec/Gemfile.lock +108 -0
  28. data/spec/Rakefile +11 -0
  29. data/spec/rails3/.rspec +1 -0
  30. data/spec/rails3/Gemfile +12 -0
  31. data/spec/rails3/Gemfile.lock +114 -0
  32. data/spec/rails3/Rakefile +10 -0
  33. data/spec/rails3/app_root/.gitignore +4 -0
  34. data/spec/rails3/app_root/README +256 -0
  35. data/spec/rails3/app_root/Rakefile.unused +7 -0
  36. data/spec/rails3/app_root/app/controllers/application_controller.rb +61 -0
  37. data/spec/rails3/app_root/app/helpers/application_helper.rb +2 -0
  38. data/spec/rails3/app_root/app/mailers/sorcery_mailer.rb +25 -0
  39. data/spec/rails3/app_root/app/models/user.rb +3 -0
  40. data/spec/rails3/app_root/app/views/layouts/application.html.erb +14 -0
  41. data/spec/rails3/app_root/app/views/sorcery_mailer/activation_email.html.erb +17 -0
  42. data/spec/rails3/app_root/app/views/sorcery_mailer/activation_email.text.erb +9 -0
  43. data/spec/rails3/app_root/app/views/sorcery_mailer/activation_success_email.html.erb +17 -0
  44. data/spec/rails3/app_root/app/views/sorcery_mailer/activation_success_email.text.erb +9 -0
  45. data/spec/rails3/app_root/app/views/sorcery_mailer/reset_password_email.html.erb +16 -0
  46. data/spec/rails3/app_root/app/views/sorcery_mailer/reset_password_email.text.erb +8 -0
  47. data/spec/rails3/app_root/config.ru +4 -0
  48. data/spec/rails3/app_root/config/application.rb +48 -0
  49. data/spec/rails3/app_root/config/boot.rb +13 -0
  50. data/spec/rails3/app_root/config/database.yml +27 -0
  51. data/spec/rails3/app_root/config/environment.rb +5 -0
  52. data/spec/rails3/app_root/config/environments/development.rb +26 -0
  53. data/spec/rails3/app_root/config/environments/in_memory.rb +0 -0
  54. data/spec/rails3/app_root/config/environments/production.rb +49 -0
  55. data/spec/rails3/app_root/config/environments/test.rb +35 -0
  56. data/spec/rails3/app_root/config/initializers/backtrace_silencers.rb +7 -0
  57. data/spec/rails3/app_root/config/initializers/inflections.rb +10 -0
  58. data/spec/rails3/app_root/config/initializers/mime_types.rb +5 -0
  59. data/spec/rails3/app_root/config/initializers/secret_token.rb +7 -0
  60. data/spec/rails3/app_root/config/initializers/session_store.rb +8 -0
  61. data/spec/rails3/app_root/config/locales/en.yml +5 -0
  62. data/spec/rails3/app_root/config/routes.rb +67 -0
  63. data/spec/rails3/app_root/db/migrate/activation/20101224223622_add_activation_to_users.rb +15 -0
  64. data/spec/rails3/app_root/db/migrate/core/20101224223620_create_users.rb +16 -0
  65. data/spec/rails3/app_root/db/migrate/password_reset/20101224223622_add_password_reset_to_users.rb +9 -0
  66. data/spec/rails3/app_root/db/migrate/remember_me/20101224223623_add_remember_me_token_to_users.rb +15 -0
  67. data/spec/rails3/app_root/db/schema.rb +23 -0
  68. data/spec/rails3/app_root/db/seeds.rb +7 -0
  69. data/spec/rails3/app_root/lib/tasks/.gitkeep +0 -0
  70. data/spec/rails3/app_root/public/404.html +26 -0
  71. data/spec/rails3/app_root/public/422.html +26 -0
  72. data/spec/rails3/app_root/public/500.html +26 -0
  73. data/spec/rails3/app_root/public/favicon.ico +0 -0
  74. data/spec/rails3/app_root/public/images/rails.png +0 -0
  75. data/spec/rails3/app_root/public/index.html +239 -0
  76. data/spec/rails3/app_root/public/javascripts/application.js +2 -0
  77. data/spec/rails3/app_root/public/javascripts/controls.js +965 -0
  78. data/spec/rails3/app_root/public/javascripts/dragdrop.js +974 -0
  79. data/spec/rails3/app_root/public/javascripts/effects.js +1123 -0
  80. data/spec/rails3/app_root/public/javascripts/prototype.js +6001 -0
  81. data/spec/rails3/app_root/public/javascripts/rails.js +175 -0
  82. data/spec/rails3/app_root/public/robots.txt +5 -0
  83. data/spec/rails3/app_root/public/stylesheets/.gitkeep +0 -0
  84. data/spec/rails3/app_root/script/rails +6 -0
  85. data/spec/rails3/app_root/test/fixtures/users.yml +9 -0
  86. data/spec/rails3/app_root/test/performance/browsing_test.rb +9 -0
  87. data/spec/rails3/app_root/test/test_helper.rb +13 -0
  88. data/spec/rails3/app_root/test/unit/user_test.rb +8 -0
  89. data/spec/rails3/app_root/vendor/plugins/.gitkeep +0 -0
  90. data/spec/rails3/controller_brute_force_protection_spec.rb +72 -0
  91. data/spec/rails3/controller_remember_me_spec.rb +65 -0
  92. data/spec/rails3/controller_session_timeout_spec.rb +49 -0
  93. data/spec/rails3/controller_spec.rb +115 -0
  94. data/spec/rails3/spec_helper.rb +115 -0
  95. data/spec/rails3/user_activation_spec.rb +148 -0
  96. data/spec/rails3/user_password_reset_spec.rb +76 -0
  97. data/spec/rails3/user_remember_me_spec.rb +66 -0
  98. data/spec/rails3/user_spec.rb +283 -0
  99. data/spec/sorcery_crypto_providers_spec.rb +182 -0
  100. data/spec/spec_helper.rb +18 -0
  101. metadata +341 -0
@@ -0,0 +1,89 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module BruteForceProtection
5
+ def self.included(base)
6
+ base.send(:include, InstanceMethods)
7
+ Config.module_eval do
8
+ class << self
9
+ attr_accessor :login_retries_amount_allowed, # how many failed logins allowed.
10
+ :login_retries_time_period, # the time after which the failed logins counter is reset.
11
+ :login_ban_time_period, # how long the user should be banned. in seconds.
12
+ :banned_action # what controller action should be called when a banned user tries.
13
+
14
+ def merge_brute_force_protection_defaults!
15
+ @defaults.merge!(:@login_retries_amount_allowed => 50,
16
+ :@login_retries_time_period => 30,
17
+ :@login_ban_time_period => 3600,
18
+ :@banned_action => :default_banned_action)
19
+ end
20
+ end
21
+ merge_brute_force_protection_defaults!
22
+ end
23
+ Config.after_failed_login << :check_failed_logins_limit_reached
24
+ base.prepend_before_filter :deny_banned_user
25
+ end
26
+
27
+ module InstanceMethods
28
+ def check_failed_logins_limit_reached(user, credentials)
29
+ now = Time.now.utc
30
+
31
+ # not banned
32
+ if session[:first_failed_login_time]
33
+ reset_failed_logins_if_time_passed(now)
34
+ else
35
+ session[:first_failed_login_time] = now
36
+ end
37
+ increment_failed_logins
38
+ # ban
39
+ ban_if_above_limit(now)
40
+ end
41
+
42
+ protected
43
+
44
+ def release_ban_if_time_passed(now)
45
+ if now - session[:ban_start_time] > Config.login_ban_time_period
46
+ session[:banned] = nil
47
+ session[:failed_logins] = 0
48
+ return true
49
+ end
50
+ false
51
+ end
52
+
53
+ def increment_failed_logins
54
+ session[:failed_logins] ||= 0
55
+ session[:failed_logins] += 1
56
+ end
57
+
58
+ def reset_failed_logins_if_time_passed(now)
59
+ if now - session[:first_failed_login_time] > Config.login_retries_time_period
60
+ session[:failed_logins] = 0
61
+ session[:first_failed_login_time] = now
62
+ end
63
+ end
64
+
65
+ def ban_if_above_limit(now)
66
+ if session[:failed_logins] > Config.login_retries_amount_allowed
67
+ session[:banned] = true
68
+ session[:ban_start_time] = now
69
+ end
70
+ end
71
+
72
+ def deny_banned_user
73
+ if session[:banned]
74
+ now = Time.now.utc
75
+ release_ban_if_time_passed(now)
76
+ end
77
+
78
+ # if still banned
79
+ send(Config.banned_action) if session[:banned]
80
+ end
81
+
82
+ def default_banned_action
83
+ render :nothing => true
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,43 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module RememberMe
5
+ def self.included(base)
6
+ base.send(:include, InstanceMethods)
7
+ Config.login_sources << :login_from_cookie
8
+ Config.after_login << :remember_me_if_asked_to
9
+ Config.after_logout << :forget_me!
10
+ end
11
+
12
+ module InstanceMethods
13
+ 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 }
16
+ end
17
+
18
+ def forget_me!
19
+ logged_in_user.forget_me!
20
+ cookies[:remember_me_token] = nil
21
+ end
22
+
23
+ protected
24
+
25
+ def remember_me_if_asked_to(user, credentials)
26
+ remember_me! if credentials.size == 3 && credentials[2]
27
+ end
28
+
29
+ def login_from_cookie
30
+ user = cookies[:remember_me_token] && Config.user_class.find_by_remember_me_token(cookies[:remember_me_token])
31
+ if user && user.remember_me_token?
32
+ cookies[:remember_me_token] = { :value => user.remember_me_token, :expires => user.remember_me_token_expires_at }
33
+ @logged_in_user = user
34
+ else
35
+ @logged_in_user = false
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ module Sorcery
2
+ module Controller
3
+ module Submodules
4
+ module SessionTimeout
5
+ def self.included(base)
6
+ base.send(:include, InstanceMethods)
7
+ Config.module_eval do
8
+ class << self
9
+ attr_accessor :session_timeout, # how long in seconds to keep the session alive.
10
+ :session_timeout_from_last_action # use the last action as the beginning of session timeout.
11
+
12
+ def merge_session_timeout_defaults!
13
+ @defaults.merge!(:@session_timeout => 3600, # 1.hour
14
+ :@session_timeout_from_last_action => false)
15
+ end
16
+ end
17
+ merge_session_timeout_defaults!
18
+ end
19
+ Config.after_login << :register_login_time
20
+ base.prepend_before_filter :validate_session
21
+ end
22
+
23
+ module InstanceMethods
24
+ def register_login_time(user, credentials)
25
+ session[:login_time] = session[:last_action_time] = Time.now.utc
26
+ end
27
+
28
+ # To be used as a before_filter, before authenticate
29
+ def validate_session
30
+ session_to_use = Config.session_timeout_from_last_action ? session[:last_action_time] : session[:login_time]
31
+ if session_to_use && (Time.now.utc - session_to_use > Config.session_timeout)
32
+ reset_session
33
+ @logged_in_user = false
34
+ else
35
+ session[:last_action_time] = Time.now.utc
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ require "openssl"
2
+
3
+ module Sorcery
4
+ module CryptoProviders
5
+ # This encryption method is reversible if you have the supplied key. So in order to use this encryption method you must supply it with a key first.
6
+ # In an initializer, or before your application initializes, you should do the following:
7
+ #
8
+ # Sorcery::Model::ConfigAES256.key = "my 32 bytes long key"
9
+ #
10
+ # My final comment is that this is a strong encryption method, but its main weakness is that its reversible. If you do not need to reverse the hash
11
+ # then you should consider Sha512 or BCrypt instead.
12
+ #
13
+ # Keep your key in a safe place, some even say the key should be stored on a separate server.
14
+ # This won't hurt performance because the only time it will try and access the key on the separate server is during initialization, which only
15
+ # happens once. The reasoning behind this is if someone does compromise your server they won't have the key also. Basically, you don't want to
16
+ # store the key with the lock.
17
+ class AES256
18
+ class << self
19
+ attr_writer :key
20
+
21
+ def encrypt(*tokens)
22
+ aes.encrypt
23
+ aes.key = @key
24
+ [aes.update(tokens.join) + aes.final].pack("m").chomp
25
+ end
26
+
27
+ def matches?(crypted, *tokens)
28
+ aes.decrypt
29
+ aes.key = @key
30
+ (aes.update(crypted.unpack("m").first) + aes.final) == tokens.join
31
+ rescue OpenSSL::CipherError
32
+ false
33
+ end
34
+
35
+ private
36
+
37
+ def aes
38
+ raise ArgumentError.new("#{name} expects a 32 bytes long key. Please use Sorcery::Model::Config.encryption_key to set it.") if ( @key.nil? || @key == "" )
39
+ @aes ||= OpenSSL::Cipher::Cipher.new("AES-256-ECB")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,96 @@
1
+ begin
2
+ require "bcrypt"
3
+ rescue LoadError
4
+ "sudo gem install bcrypt-ruby"
5
+ end
6
+
7
+ module Sorcery
8
+ module CryptoProviders
9
+ # For most apps Sha512 is plenty secure, but if you are building an app that stores nuclear launch codes you might want to consier BCrypt. This is an extremely
10
+ # secure hashing algorithm, mainly because it is slow. A brute force attack on a BCrypt encrypted password would take much longer than a brute force attack on a
11
+ # password encrypted with a Sha algorithm. Keep in mind you are sacrificing performance by using this, generating a password takes exponentially longer than any
12
+ # of the Sha algorithms. I did some benchmarking to save you some time with your decision:
13
+ #
14
+ # require "bcrypt"
15
+ # require "digest"
16
+ # require "benchmark"
17
+ #
18
+ # Benchmark.bm(18) do |x|
19
+ # x.report("BCrypt (cost = 10:") { 100.times { BCrypt::Password.create("mypass", :cost => 10) } }
20
+ # x.report("BCrypt (cost = 2:") { 100.times { BCrypt::Password.create("mypass", :cost => 2) } }
21
+ # x.report("Sha512:") { 100.times { Digest::SHA512.hexdigest("mypass") } }
22
+ # x.report("Sha1:") { 100.times { Digest::SHA1.hexdigest("mypass") } }
23
+ # end
24
+ #
25
+ # user system total real
26
+ # BCrypt (cost = 10): 10.780000 0.060000 10.840000 ( 11.100289)
27
+ # BCrypt (cost = 2): 0.180000 0.000000 0.180000 ( 0.181914)
28
+ # Sha512: 0.000000 0.000000 0.000000 ( 0.000829)
29
+ # Sha1: 0.000000 0.000000 0.000000 ( 0.000395)
30
+ #
31
+ # You can play around with the cost to get that perfect balance between performance and security.
32
+ #
33
+ # Decided BCrypt is for you? Just insall the bcrypt gem:
34
+ #
35
+ # gem install bcrypt-ruby
36
+ #
37
+ # Tell activate_sorcery! to use it:
38
+ #
39
+ # activate_sorcery! do |c|
40
+ # c.encryption_algorithm = :bcrypt
41
+ # end
42
+ #
43
+ # You are good to go!
44
+ class BCrypt
45
+ class << self
46
+ # This is the :cost option for the BCrpyt library. The higher the cost the more secure it is and the longer is take the generate a hash. By default this is 10.
47
+ # Set this to whatever you want, play around with it to get that perfect balance between security and performance.
48
+ def cost
49
+ @cost ||= 10
50
+ end
51
+ attr_writer :cost
52
+ alias :stretches= :cost=
53
+
54
+ # Creates a BCrypt hash for the password passed.
55
+ def encrypt(*tokens)
56
+ ::BCrypt::Password.create(join_tokens(tokens), :cost => cost)
57
+ end
58
+
59
+ # Does the hash match the tokens? Uses the same tokens that were used to encrypt.
60
+ def matches?(hash, *tokens)
61
+ hash = new_from_hash(hash)
62
+ return false if hash.nil? || hash == {}
63
+ hash == join_tokens(tokens)
64
+ end
65
+
66
+ # This method is used as a flag to tell Sorcery to "resave" the password upon a successful login, using the new cost
67
+ def cost_matches?(hash)
68
+ hash = new_from_hash(hash)
69
+ if hash.nil? || hash == {}
70
+ false
71
+ else
72
+ hash.cost == cost
73
+ end
74
+ end
75
+
76
+ def reset!
77
+ @cost = 10
78
+ end
79
+
80
+ private
81
+
82
+ def join_tokens(tokens)
83
+ tokens.flatten.join
84
+ end
85
+
86
+ def new_from_hash(hash)
87
+ begin
88
+ ::BCrypt::Password.new(hash)
89
+ rescue ::BCrypt::Errors::InvalidHash
90
+ return nil
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,39 @@
1
+ require "digest/md5"
2
+
3
+ module Sorcery
4
+ module CryptoProviders
5
+ # This class was made for the users transitioning from md5 based systems.
6
+ # I highly discourage using this crypto provider as it superbly inferior
7
+ # to your other options.
8
+ #
9
+ # Please use any other provider offered by Sorcery.
10
+ class MD5
11
+ class << self
12
+ attr_accessor :join_token
13
+
14
+ # The number of times to loop through the encryption.
15
+ def stretches
16
+ @stretches ||= 1
17
+ end
18
+ attr_writer :stretches
19
+
20
+ # Turns your raw password into a MD5 hash.
21
+ def encrypt(*tokens)
22
+ digest = tokens.flatten.join(join_token)
23
+ stretches.times { digest = Digest::MD5.hexdigest(digest) }
24
+ digest
25
+ end
26
+
27
+ # Does the crypted password match the tokens? Uses the same tokens that were used to encrypt.
28
+ def matches?(crypted, *tokens)
29
+ encrypt(*tokens) == crypted
30
+ end
31
+
32
+ def reset!
33
+ @stretches = 1
34
+ @join_token = nil
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require "digest/sha1"
2
+
3
+ module Sorcery
4
+ module CryptoProviders
5
+ # This class was made for the users transitioning from restful_authentication. I highly discourage using this
6
+ # crypto provider as it inferior to your other options. Please use any other provider offered by Sorcery.
7
+ class SHA1
8
+ class << self
9
+ def join_token
10
+ @join_token ||= "--"
11
+ end
12
+ attr_writer :join_token
13
+
14
+ # The number of times to loop through the encryption. This is ten because that is what restful_authentication defaults to.
15
+ def stretches
16
+ @stretches ||= 10
17
+ end
18
+ attr_writer :stretches
19
+
20
+ # Turns your raw password into a Sha1 hash.
21
+ def encrypt(*tokens)
22
+ tokens = tokens.flatten
23
+ digest = tokens.shift
24
+ stretches.times { digest = Digest::SHA1.hexdigest([digest, *tokens].join(join_token)) }
25
+ digest
26
+ end
27
+
28
+ # Does the crypted password match the tokens? Uses the same tokens that were used to encrypt.
29
+ def matches?(crypted, *tokens)
30
+ encrypt(*tokens) == crypted
31
+ end
32
+
33
+ def reset!
34
+ @stretches = 10
35
+ @join_token = nil
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,55 @@
1
+ require "digest/sha2"
2
+
3
+ module Sorcery
4
+ # The activate_sorcery method has a custom_crypto_provider configuration option. This allows you to use any type of encryption you like.
5
+ # Just create a class with a class level encrypt and matches? method. See example below.
6
+ #
7
+ # === Example
8
+ #
9
+ # class MyAwesomeEncryptionMethod
10
+ # def self.encrypt(*tokens)
11
+ # # the tokens passed will be an array of objects, what type of object is irrelevant,
12
+ # # just do what you need to do with them and return a single encrypted string.
13
+ # # for example, you will most likely join all of the objects into a single string and then encrypt that string
14
+ # end
15
+ #
16
+ # def self.matches?(crypted, *tokens)
17
+ # # return true if the crypted string matches the tokens.
18
+ # # depending on your algorithm you might decrypt the string then compare it to the token, or you might
19
+ # # encrypt the tokens and make sure it matches the crypted string, its up to you
20
+ # end
21
+ # end
22
+ module CryptoProviders
23
+ # = Sha256
24
+ #
25
+ # Uses the Sha256 hash algorithm to encrypt passwords.
26
+ class SHA256
27
+ class << self
28
+ attr_accessor :join_token
29
+
30
+ # The number of times to loop through the encryption.
31
+ def stretches
32
+ @stretches ||= 20
33
+ end
34
+ attr_writer :stretches
35
+
36
+ # Turns your raw password into a Sha256 hash.
37
+ def encrypt(*tokens)
38
+ digest = tokens.flatten.join(join_token)
39
+ stretches.times { digest = Digest::SHA256.hexdigest(digest) }
40
+ digest
41
+ end
42
+
43
+ # Does the crypted password match the tokens? Uses the same tokens that were used to encrypt.
44
+ def matches?(crypted, *tokens)
45
+ encrypt(*tokens) == crypted
46
+ end
47
+
48
+ def reset!
49
+ @stretches = 20
50
+ @join_token = nil
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end