simple_auth 1.5.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -1
  3. data/.travis.yml +11 -0
  4. data/CHANGELOG.md +5 -0
  5. data/Gemfile +0 -2
  6. data/Gemfile.lock +102 -79
  7. data/README.md +243 -0
  8. data/Rakefile +15 -0
  9. data/gemfiles/rails_3_1.gemfile +5 -0
  10. data/gemfiles/rails_3_1.gemfile.lock +151 -0
  11. data/gemfiles/rails_3_2.gemfile +5 -0
  12. data/gemfiles/rails_3_2.gemfile.lock +149 -0
  13. data/gemfiles/rails_4_0.gemfile +4 -0
  14. data/gemfiles/rails_4_0.gemfile.lock +140 -0
  15. data/gemfiles/rails_4_1.gemfile +4 -0
  16. data/gemfiles/rails_4_1.gemfile.lock +146 -0
  17. data/lib/simple_auth.rb +1 -6
  18. data/lib/simple_auth/action_controller.rb +14 -10
  19. data/lib/simple_auth/active_record.rb +86 -0
  20. data/lib/simple_auth/compat.rb +2 -0
  21. data/lib/simple_auth/compat/active_record.rb +31 -0
  22. data/lib/simple_auth/compat/config.rb +17 -0
  23. data/lib/simple_auth/config.rb +0 -20
  24. data/lib/simple_auth/exceptions.rb +0 -1
  25. data/lib/simple_auth/railtie.rb +1 -1
  26. data/lib/simple_auth/rspec.rb +2 -2
  27. data/lib/simple_auth/session.rb +1 -1
  28. data/lib/simple_auth/version.rb +2 -2
  29. data/simple_auth.gemspec +4 -4
  30. data/spec/controllers/redirect_logged_user_spec.rb +16 -16
  31. data/spec/controllers/require_logged_user_spec.rb +34 -34
  32. data/spec/schema.rb +5 -1
  33. data/spec/simple_auth/active_record_spec.rb +104 -2
  34. data/spec/simple_auth/compat_spec.rb +31 -0
  35. data/spec/simple_auth/config_spec.rb +8 -27
  36. data/spec/simple_auth/helper_spec.rb +7 -7
  37. data/spec/simple_auth/session_spec.rb +76 -76
  38. data/spec/spec_helper.rb +2 -168
  39. data/spec/support/app/models/customer.rb +3 -0
  40. data/templates/initializer.rb +0 -8
  41. metadata +62 -33
  42. data/README.markdown +0 -202
  43. data/lib/simple_auth/orm/active_record.rb +0 -80
  44. data/lib/simple_auth/orm/base.rb +0 -89
  45. data/lib/simple_auth/orm/mongo_mapper.rb +0 -62
  46. data/spec/simple_auth/mongo_mapper_spec.rb +0 -10
  47. data/spec/support/app/models/account.rb +0 -6
@@ -1,89 +0,0 @@
1
- module SimpleAuth
2
- module Orm
3
- module Base
4
- module InstanceMethods
5
- def require_password!
6
- @require_password = true
7
- end
8
-
9
- def require_password?
10
- @require_password
11
- end
12
-
13
- def password=(password)
14
- @password_changed = true
15
- @password = password
16
- end
17
-
18
- def password_changed?
19
- @password_changed == true
20
- end
21
-
22
- def authorized?
23
- true
24
- end
25
-
26
- private
27
- def encrypt_password
28
- self.password_salt = SimpleAuth::Config.salt.call(self)
29
- self.password_hash = SimpleAuth::Config.crypter.call(password, password_salt)
30
- end
31
-
32
- def erase_password
33
- self.password = nil
34
- self.password_confirmation = nil
35
-
36
- # Mark password as unchanged after erasing passwords,
37
- # or it will be marked as changed anyway
38
- @password_changed = false
39
-
40
- # Also erase require password
41
- @require_password = false
42
- end
43
-
44
- def validate_password?
45
- new_record? || password_changed? || require_password?
46
- end
47
- end
48
-
49
- module ClassMethods
50
- # Find user by its credential.
51
- #
52
- # User.find_by_credential "john@doe.com" # using e-mail
53
- # User.find_by_credential "john" # using username
54
- #
55
- def find_by_credential(credential)
56
- raise SimpleAuth::AbstractMethodError
57
- end
58
-
59
- # Find user by its credential. If no user is found, raise
60
- # SimpleAuth::RecordNotFound exception.
61
- #
62
- # User.find_by_credential! "john@doe.com"
63
- #
64
- def find_by_credential!(credential)
65
- raise SimpleAuth::AbstractMethodError
66
- end
67
-
68
- # Receive a credential and a password and try to authenticate the specified user.
69
- # If the credential is valid, then an user is returned; otherwise nil is returned.
70
- #
71
- # User.authenticate "johndoe", "test"
72
- # User.authenticate "john@doe.com", "test"
73
- #
74
- def authenticate(credential, password)
75
- record = find_by_credential(credential.to_s)
76
-
77
- # If no record has been found
78
- return nil unless record
79
-
80
- # Compare password
81
- return nil unless record.password_hash == SimpleAuth::Config.crypter.call(password.to_s, record.password_salt)
82
-
83
- # Yay! Everything matched so return record.
84
- record
85
- end
86
- end
87
- end
88
- end
89
- end
@@ -1,62 +0,0 @@
1
- module SimpleAuth
2
- module Orm
3
- module MongoMapper
4
- def self.included(base)
5
- base.class_eval { extend Macro }
6
- end
7
-
8
- module Macro
9
- def authentication(&block)
10
- SimpleAuth.setup(&block) if block_given?
11
- SimpleAuth::Config.model ||= name.underscore.to_sym
12
-
13
- return if respond_to?(:authenticate)
14
-
15
- include SimpleAuth::Orm::Base::InstanceMethods
16
- extend SimpleAuth::Orm::Base::ClassMethods
17
- extend SimpleAuth::Orm::MongoMapper::ClassMethods
18
-
19
- attr_reader :password
20
- attr_accessor :password_confirmation
21
-
22
- before_save :encrypt_password, :if => :validate_password?
23
- after_save :erase_password
24
-
25
- validates_presence_of :password, :if => :validate_password?
26
- validates_length_of :password, :if => :validate_password?, :minimum => 4, :allow_blank => true
27
- validates_presence_of :password_confirmation, :if => :validate_password?
28
- validates_confirmation_of :password, :if => :validate_password?
29
-
30
- key :password_salt, String
31
- key :password_hash, String
32
- end
33
- end
34
-
35
- module ClassMethods
36
- # Find user by its credential.
37
- #
38
- # User.find_by_credential "john@doe.com" # using e-mail
39
- # User.find_by_credential "john" # using username
40
- #
41
- def find_by_credential(credential)
42
- conditions = SimpleAuth::Config.credentials.collect do |attr_name|
43
- {attr_name => credential}
44
- end
45
-
46
- SimpleAuth::Config.model_class.where("$or" => conditions).first
47
- end
48
-
49
- # Find user by its credential. If no user is found, raise
50
- # SimpleAuth::RecordNotFound exception.
51
- #
52
- # User.find_by_credential! "john@doe.com"
53
- #
54
- def find_by_credential!(credential)
55
- record = find_by_credential(credential)
56
- raise SimpleAuth::RecordNotFound, "couldn't find #{SimpleAuth::Config.model} using #{credential.inspect} as credential" unless record
57
- record
58
- end
59
- end
60
- end
61
- end
62
- end
@@ -1,10 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe SimpleAuth::Orm::MongoMapper do
4
- let(:model) { Account }
5
- let(:model_name) { :account }
6
- subject { model.new }
7
- before { model.delete_all }
8
-
9
- it_should_behave_like "orm"
10
- end
@@ -1,6 +0,0 @@
1
- class Account
2
- include MongoMapper::Document
3
- include SimpleAuth::Orm::MongoMapper
4
-
5
- authentication
6
- end