letmein 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.1.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{letmein}
8
- s.version = "0.0.8"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oleg Khabarov"]
@@ -7,25 +7,50 @@ module LetMeIn
7
7
 
8
8
  class Railtie < Rails::Railtie
9
9
  config.to_prepare do
10
- LetMeIn.initialize unless LetMeIn.models.present?
10
+ LetMeIn.initialize
11
11
  end
12
12
  end
13
13
 
14
- mattr_accessor :models, :identifiers, :passwords, :salts
15
- def self.initialize(params = {})
16
- @@models = [params[:model] || 'User' ].flatten
17
- @@identifiers = [params[:identifier] || 'email' ].flatten
18
- @@passwords = [params[:password] || 'password_hash' ].flatten
19
- @@salts = [params[:salt] || 'password_salt' ].flatten
14
+ # Configuration class with some defaults. Can be changed like this:
15
+ # LetMeIn.configure do |conf|
16
+ # conf.model = 'Account'
17
+ # conf.identifier = 'username'
18
+ # end
19
+ class Config
20
+ ACCESSORS = %w(models identifiers passwords salts)
21
+ attr_accessor *ACCESSORS
22
+ def initialize
23
+ @models = ['User']
24
+ @identifiers = ['email']
25
+ @passwords = ['password_hash']
26
+ @salts = ['password_salt']
27
+ end
28
+ ACCESSORS.each do |a|
29
+ define_method("#{a.singularize}=") do |val|
30
+ send("#{a}=", [val].flatten)
31
+ end
32
+ end
33
+ end
34
+
35
+ def self.config
36
+ @config ||= Config.new
37
+ end
38
+
39
+ def self.configure
40
+ yield config
41
+ end
42
+
43
+ def self.initialize
20
44
 
21
45
  def self.accessor(name, index = 0)
22
46
  name = name.to_s.pluralize
23
- self.send(name)[index] || self.send(name)[0]
47
+ self.config.send(name)[index] || self.config.send(name)[0]
24
48
  end
25
49
 
26
- @@models.each do |model|
50
+ self.config.models.each do |model|
51
+ klass = model.constantize rescue next
27
52
 
28
- model.constantize.send :include, LetMeIn::Model
53
+ klass.send :include, LetMeIn::Model
29
54
 
30
55
  Object.const_set("#{model.to_s.camelize}Session", Class.new do
31
56
  include ActiveModel::Validations
@@ -34,7 +59,7 @@ module LetMeIn
34
59
 
35
60
  def initialize(params = { })
36
61
  unless params.blank?
37
- i = LetMeIn.accessor(:identifier, LetMeIn.models.index(self.class.to_s.gsub('Session','')))
62
+ i = LetMeIn.accessor(:identifier, LetMeIn.config.models.index(self.class.to_s.gsub('Session','')))
38
63
  self.identifier = params[:identifier] || params[i.to_sym]
39
64
  self.password = params[:password]
40
65
  end
@@ -58,7 +83,7 @@ module LetMeIn
58
83
 
59
84
  def method_missing(method_name, *args)
60
85
  m = self.class.to_s.gsub('Session','')
61
- i = LetMeIn.accessor(:identifier, LetMeIn.models.index(m))
86
+ i = LetMeIn.accessor(:identifier, LetMeIn.config.models.index(m))
62
87
  case method_name.to_s
63
88
  when i then self.identifier
64
89
  when "#{i}=" then self.identifier = args[0]
@@ -69,9 +94,9 @@ module LetMeIn
69
94
 
70
95
  def authenticate
71
96
  m = self.class.to_s.gsub('Session','')
72
- i = LetMeIn.accessor(:identifier, LetMeIn.models.index(m))
73
- p = LetMeIn.accessor(:password, LetMeIn.models.index(m))
74
- s = LetMeIn.accessor(:password, LetMeIn.models.index(m))
97
+ i = LetMeIn.accessor(:identifier, LetMeIn.config.models.index(m))
98
+ p = LetMeIn.accessor(:password, LetMeIn.config.models.index(m))
99
+ s = LetMeIn.accessor(:password, LetMeIn.config.models.index(m))
75
100
  object = m.constantize.send("find_by_#{i}", self.identifier)
76
101
  self.authenticated_object = if object && object.send(p) == BCrypt::Engine.hash_secret(self.password, object.send(s))
77
102
  object
@@ -96,8 +121,8 @@ module LetMeIn
96
121
 
97
122
  define_method :encrypt_password do
98
123
  if password.present?
99
- p = LetMeIn.accessor(:password, LetMeIn.models.index(self.class.to_s))
100
- s = LetMeIn.accessor(:salt, LetMeIn.models.index(self.class.to_s))
124
+ p = LetMeIn.accessor(:password, LetMeIn.config.models.index(self.class.to_s))
125
+ s = LetMeIn.accessor(:salt, LetMeIn.config.models.index(self.class.to_s))
101
126
  self.send("#{s}=", BCrypt::Engine.generate_salt)
102
127
  self.send("#{p}=", BCrypt::Engine.hash_secret(password, self.send(s)))
103
128
  end
@@ -25,50 +25,85 @@ class LetMeInTest < Test::Unit::TestCase
25
25
  t.column :pass_salt, :string
26
26
  end
27
27
  end
28
- LetMeIn.initialize(
29
- :model => ['User', 'Admin'],
30
- :identifier => ['email', 'username'],
31
- :password => ['password_hash', 'pass_hash'],
32
- :salt => ['password_salt', 'pass_salt']
33
- )
34
- User.create!(:email => 'test@test.test', :password => 'test')
35
- Admin.create!(:username => 'admin', :password => 'test')
28
+ init_default_configuration
29
+ end
30
+
31
+ def init_default_configuration
32
+ remove_session_classes
33
+ LetMeIn.configure do |c|
34
+ c.models = ['User']
35
+ c.identifiers = ['email']
36
+ c.passwords = ['password_hash']
37
+ c.salts = ['password_salt']
38
+ end
39
+ LetMeIn.initialize
40
+ end
41
+
42
+ def init_custom_configuration
43
+ remove_session_classes
44
+ LetMeIn.configure do |c|
45
+ c.models = ['User', 'Admin']
46
+ c.identifiers = ['email', 'username']
47
+ c.passwords = ['password_hash', 'pass_hash']
48
+ c.salts = ['password_salt', 'pass_salt']
49
+ end
50
+ LetMeIn.initialize
51
+ end
52
+
53
+ def remove_session_classes
54
+ Object.send(:remove_const, :UserSession) rescue nil
55
+ Object.send(:remove_const, :AdminSession) rescue nil
36
56
  end
37
57
 
38
58
  def teardown
39
59
  ActiveRecord::Base.connection.tables.each do |table|
40
60
  ActiveRecord::Base.connection.drop_table(table)
41
61
  end
42
- Object.send(:remove_const, :UserSession)
43
- Object.send(:remove_const, :AdminSession)
62
+ remove_session_classes
63
+ end
64
+
65
+ # -- Tests ----------------------------------------------------------------
66
+ def test_default_configuration_initialization
67
+ assert_equal ['User'], LetMeIn.config.models
68
+ assert_equal ['email'], LetMeIn.config.identifiers
69
+ assert_equal ['password_hash'], LetMeIn.config.passwords
70
+ assert_equal ['password_salt'], LetMeIn.config.salts
44
71
  end
45
72
 
46
- def test_configuration_initialization
47
- assert_equal ['User', 'Admin'], LetMeIn.models
48
- assert_equal ['email', 'username'], LetMeIn.identifiers
49
- assert_equal ['password_hash', 'pass_hash'], LetMeIn.passwords
50
- assert_equal ['password_salt', 'pass_salt'], LetMeIn.salts
73
+ def test_custom_configuration_initialization
74
+ LetMeIn.configure do |c|
75
+ c.model = 'Account'
76
+ c.identifier = 'username'
77
+ c.password = 'encrypted_pass'
78
+ c.salt = 'salt'
79
+ end
80
+ assert_equal ['Account'], LetMeIn.config.models
81
+ assert_equal ['username'], LetMeIn.config.identifiers
82
+ assert_equal ['encrypted_pass'], LetMeIn.config.passwords
83
+ assert_equal ['salt'], LetMeIn.config.salts
51
84
  end
52
85
 
53
- def test_model_password_saving
54
- user = User.first
55
- assert_equal nil, user.password
86
+ def test_model_integration
87
+ assert User.new.respond_to?(:password)
88
+ user = User.create!(:email => 'test@test.test', :password => 'pass')
56
89
  assert_match /.{60}/, user.password_hash
57
90
  assert_match /.{29}/, user.password_salt
58
91
  end
59
92
 
60
- def test_model_password_saving_secondary
61
- user = Admin.first
62
- assert_equal nil, user.password
93
+ def test_model_integration_custom
94
+ init_custom_configuration
95
+ assert Admin.new.respond_to?(:password)
96
+ user = Admin.create!(:username => 'test', :password => 'pass')
63
97
  assert_match /.{60}/, user.pass_hash
64
98
  assert_match /.{29}/, user.pass_salt
65
99
  end
66
100
 
67
101
  def test_session_initialization
68
- session = UserSession.new(:email => 'test@test.test', :password => 'test_pass')
102
+ assert defined?(UserSession)
103
+ session = UserSession.new(:email => 'test@test.test', :password => 'pass')
69
104
  assert_equal 'test@test.test', session.identifier
70
105
  assert_equal 'test@test.test', session.email
71
- assert_equal 'test_pass', session.password
106
+ assert_equal 'pass', session.password
72
107
 
73
108
  session.email = 'new_user@test.test'
74
109
  assert_equal 'new_user@test.test', session.identifier
@@ -79,6 +114,8 @@ class LetMeInTest < Test::Unit::TestCase
79
114
  end
80
115
 
81
116
  def test_session_initialization_secondary
117
+ init_custom_configuration
118
+ assert defined?(AdminSession)
82
119
  session = AdminSession.new(:username => 'admin', :password => 'test_pass')
83
120
  assert_equal 'admin', session.identifier
84
121
  assert_equal 'admin', session.username
@@ -93,21 +130,25 @@ class LetMeInTest < Test::Unit::TestCase
93
130
  end
94
131
 
95
132
  def test_session_authentication
96
- session = UserSession.create(:email => User.first.email, :password => 'test')
133
+ user = User.create!(:email => 'test@test.test', :password => 'pass')
134
+ session = UserSession.create(:email => user.email, :password => 'pass')
97
135
  assert session.errors.blank?
98
- assert_equal User.first, session.authenticated_object
99
- assert_equal User.first, session.user
136
+ assert_equal user, session.authenticated_object
137
+ assert_equal user, session.user
100
138
  end
101
139
 
102
- def test_session_authentication_secondary
103
- session = AdminSession.create(:username => Admin.first.username, :password => 'test')
140
+ def test_session_authentication_custom
141
+ init_custom_configuration
142
+ admin = Admin.create!(:username => 'admin', :password => 'pass')
143
+ session = AdminSession.create(:username => admin.username, :password => 'pass')
104
144
  assert session.errors.blank?
105
- assert_equal Admin.first, session.authenticated_object
106
- assert_equal Admin.first, session.admin
145
+ assert_equal admin, session.authenticated_object
146
+ assert_equal admin, session.admin
107
147
  end
108
148
 
109
149
  def test_session_authentication_failure
110
- session = UserSession.create(:email => User.first.email, :password => 'bad_pass')
150
+ user = User.create!(:email => 'test@test.test', :password => 'pass')
151
+ session = UserSession.create(:email => user.email, :password => 'bad_pass')
111
152
  assert session.errors.present?
112
153
  assert_equal 'Failed to authenticate', session.errors[:base].first
113
154
  assert_equal nil, session.authenticated_object
@@ -115,7 +156,8 @@ class LetMeInTest < Test::Unit::TestCase
115
156
  end
116
157
 
117
158
  def test_session_authentication_exception
118
- session = UserSession.new(:email => User.first.email, :password => 'bad_pass')
159
+ user = User.create!(:email => 'test@test.test', :password => 'pass')
160
+ session = UserSession.new(:email => user.email, :password => 'bad_pass')
119
161
  begin
120
162
  session.save!
121
163
  rescue LetMeIn::Error => e
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: letmein
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.8
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Oleg Khabarov
@@ -88,7 +88,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
88
  requirements:
89
89
  - - ">="
90
90
  - !ruby/object:Gem::Version
91
- hash: -2714324773765342920
91
+ hash: -3376518755329452587
92
92
  segments:
93
93
  - 0
94
94
  version: "0"