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 +1 -1
- data/letmein.gemspec +1 -1
- data/lib/letmein.rb +42 -17
- data/test/letmein_test.rb +74 -32
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/letmein.gemspec
CHANGED
data/lib/letmein.rb
CHANGED
@@ -7,25 +7,50 @@ module LetMeIn
|
|
7
7
|
|
8
8
|
class Railtie < Rails::Railtie
|
9
9
|
config.to_prepare do
|
10
|
-
LetMeIn.initialize
|
10
|
+
LetMeIn.initialize
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
50
|
+
self.config.models.each do |model|
|
51
|
+
klass = model.constantize rescue next
|
27
52
|
|
28
|
-
|
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
|
data/test/letmein_test.rb
CHANGED
@@ -25,50 +25,85 @@ class LetMeInTest < Test::Unit::TestCase
|
|
25
25
|
t.column :pass_salt, :string
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
43
|
-
|
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
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
54
|
-
|
55
|
-
|
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
|
61
|
-
|
62
|
-
|
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
|
-
|
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 '
|
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
|
-
|
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
|
99
|
-
assert_equal
|
136
|
+
assert_equal user, session.authenticated_object
|
137
|
+
assert_equal user, session.user
|
100
138
|
end
|
101
139
|
|
102
|
-
def
|
103
|
-
|
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
|
106
|
-
assert_equal
|
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
|
-
|
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
|
-
|
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
|
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: -
|
91
|
+
hash: -3376518755329452587
|
92
92
|
segments:
|
93
93
|
- 0
|
94
94
|
version: "0"
|