letmein 0.0.4 → 0.0.5

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.4
1
+ 0.0.5
data/letmein.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{letmein}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
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"]
12
- s.date = %q{2011-03-26}
12
+ s.date = %q{2011-03-27}
13
13
  s.description = %q{minimalistic authentication}
14
14
  s.email = %q{oleg@khabarov.ca}
15
15
  s.extra_rdoc_files = [
data/lib/letmein.rb CHANGED
@@ -3,25 +3,41 @@ require 'bcrypt'
3
3
 
4
4
  module LetMeIn
5
5
 
6
+ mattr_accessor :model, :identifier, :password, :salt
7
+
8
+ def self.initialize(params = {})
9
+ @@model = params[:model] || 'User'
10
+ @@identifier = params[:identifier] || 'email'
11
+ @@password = params[:password] || 'password_hash'
12
+ @@salt = params[:salt] || 'password_salt'
13
+ @@model.constantize.send :include, LetMeIn::Model
14
+ end
15
+
6
16
  class Railtie < Rails::Railtie
7
17
  config.after_initialize do
8
- # Rails loads models on demand. Configuration doesn't set properly unless assosiated model
9
- # is already loaded. This will force it.
10
- Dir[Rails.root + 'app/models/**/*.rb'].each{|path| require path }
18
+ LetMeIn.initialize unless LetMeIn.model.present?
11
19
  end
12
20
  end
13
21
 
14
22
  class Error < StandardError
15
23
  end
16
24
 
17
- class Configuration
18
- attr_accessor :model, :identifier, :password, :salt
19
-
20
- def initialize
21
- @model = nil
22
- @identifier = 'email'
23
- @password = 'password_hash'
24
- @salt = 'password_salt'
25
+ module Model
26
+ def self.included(base)
27
+ base.instance_eval do
28
+ attr_accessor :password
29
+ before_save :encrypt_password
30
+ end
31
+ base.class_eval do
32
+ class_eval %Q^
33
+ def encrypt_password
34
+ if password.present?
35
+ self.send("#{LetMeIn.salt}=", BCrypt::Engine.generate_salt)
36
+ self.send("#{LetMeIn.password}=", BCrypt::Engine.hash_secret(password, self.send(LetMeIn.salt)))
37
+ end
38
+ end
39
+ ^
40
+ end
25
41
  end
26
42
  end
27
43
 
@@ -32,7 +48,7 @@ module LetMeIn
32
48
 
33
49
  def initialize(params = { })
34
50
  unless params.blank?
35
- self.identifier = params[:identifier] || params[LetMeIn.configuration.identifier.to_sym]
51
+ self.identifier = params[:identifier] || params[LetMeIn.identifier.to_sym]
36
52
  self.password = params[:password]
37
53
  end
38
54
  end
@@ -60,20 +76,16 @@ module LetMeIn
60
76
  # Mapping to the identifier and authenticated object accessor
61
77
  def method_missing(method_name, *args)
62
78
  case method_name.to_s
63
- when LetMeIn.configuration.identifier
64
- self.identifier
65
- when "#{LetMeIn.configuration.identifier}="
66
- self.identifier = args[0]
67
- when LetMeIn.configuration.model.underscore
68
- self.authenticated_object
69
- else
70
- super
79
+ when LetMeIn.identifier then self.identifier
80
+ when "#{LetMeIn.identifier}=" then self.identifier = args[0]
81
+ when LetMeIn.model.underscore then self.authenticated_object
82
+ else super
71
83
  end
72
84
  end
73
85
 
74
86
  def authenticate
75
- object = LetMeIn.configuration.model.constantize.send("find_by_#{LetMeIn.configuration.identifier}", self.identifier)
76
- self.authenticated_object = if object && object.send(LetMeIn.configuration.password) == BCrypt::Engine.hash_secret(self.password, object.send(LetMeIn.configuration.salt))
87
+ object = LetMeIn.model.constantize.send("find_by_#{LetMeIn.identifier}", self.identifier)
88
+ self.authenticated_object = if object && object.send(LetMeIn.password) == BCrypt::Engine.hash_secret(self.password, object.send(LetMeIn.salt))
77
89
  object
78
90
  else
79
91
  errors.add(:base, 'Failed to authenticate')
@@ -85,38 +97,4 @@ module LetMeIn
85
97
  nil
86
98
  end
87
99
  end
88
-
89
- module Model
90
- def self.included(base)
91
- base.extend ClassMethods
92
- end
93
-
94
- module ClassMethods
95
- def letmein(*args)
96
- LetMeIn.configuration.model = self.to_s
97
- LetMeIn.configuration.identifier = args[0].to_s if args[0]
98
- LetMeIn.configuration.password = args[1].to_s if args[1]
99
- LetMeIn.configuration.salt = args[2].to_s if args[2]
100
-
101
- attr_accessor :password
102
-
103
- before_save :encrypt_password
104
-
105
- class_eval %Q^
106
- def encrypt_password
107
- if password.present?
108
- self.send("#{LetMeIn.configuration.salt}=", BCrypt::Engine.generate_salt)
109
- self.send("#{LetMeIn.configuration.password}=", BCrypt::Engine.hash_secret(password, self.send(LetMeIn.configuration.salt)))
110
- end
111
- end
112
- ^
113
- end
114
- end
115
- end
116
-
117
- def self.configuration
118
- @configuration ||= LetMeIn::Configuration.new
119
- end
120
- end
121
-
122
- ActiveRecord::Base.send :include, LetMeIn::Model
100
+ end
data/test/letmein_test.rb CHANGED
@@ -11,7 +11,6 @@ class User < ActiveRecord::Base
11
11
  # example values for password info:
12
12
  # pass: $2a$10$0MeSaaE3I7.0FQ5ZDcKPJeD1.FzqkcOZfEKNZ/DNN.w8xOwuFdBCm
13
13
  # salt: $2a$10$0MeSaaE3I7.0FQ5ZDcKPJe
14
- letmein :username, :pass_crypt, :pass_salt
15
14
  end
16
15
 
17
16
  class LetMeInTest < Test::Unit::TestCase
@@ -19,11 +18,13 @@ class LetMeInTest < Test::Unit::TestCase
19
18
  ActiveRecord::Base.logger
20
19
  ActiveRecord::Schema.define(:version => 1) do
21
20
  create_table :users do |t|
22
- t.column :username, :string
23
- t.column :pass_crypt, :string
24
- t.column :pass_salt, :string
21
+ t.column :email, :string
22
+ t.column :password_hash, :string
23
+ t.column :password_salt, :string
25
24
  end
26
25
  end
26
+ LetMeIn.initialize
27
+ User.create!(:email => 'test@test.test', :password => 'test')
27
28
  end
28
29
 
29
30
  def teardown
@@ -32,56 +33,43 @@ class LetMeInTest < Test::Unit::TestCase
32
33
  end
33
34
  end
34
35
 
35
- def test_configuration_defaults
36
- assert config = LetMeIn::Configuration.new
37
- assert_equal nil, config.model
38
- assert_equal 'email', config.identifier
39
- assert_equal 'password_hash', config.password
40
- assert_equal 'password_salt', config.salt
41
- end
42
-
43
36
  def test_configuration_initialization
44
- conf = LetMeIn.configuration
45
- assert_equal 'User', conf.model
46
- assert_equal 'username', conf.identifier
47
- assert_equal 'pass_crypt', conf.password
48
- assert_equal 'pass_salt', conf.salt
37
+ assert_equal 'User', LetMeIn.model
38
+ assert_equal 'email', LetMeIn.identifier
39
+ assert_equal 'password_hash', LetMeIn.password
40
+ assert_equal 'password_salt', LetMeIn.salt
49
41
  end
50
42
 
51
43
  def test_model_password_saving
52
- user = User.new(:username => 'test', :password => 'test')
53
- user.save!
54
- user = User.find(user.id)
44
+ user = User.first
55
45
  assert_equal nil, user.password
56
- assert_match /.{60}/, user.pass_crypt
57
- assert_match /.{29}/, user.pass_salt
46
+ assert_match /.{60}/, user.password_hash
47
+ assert_match /.{29}/, user.password_salt
58
48
  end
59
49
 
60
50
  def test_session_initialization
61
- session = LetMeIn::Session.new(:username => 'test_user', :password => 'test_pass')
62
- assert_equal 'test_user', session.identifier
63
- assert_equal 'test_user', session.username
51
+ session = LetMeIn::Session.new(:email => 'test@test.test', :password => 'test_pass')
52
+ assert_equal 'test@test.test', session.identifier
53
+ assert_equal 'test@test.test', session.email
64
54
  assert_equal 'test_pass', session.password
65
55
 
66
- session.username = 'new_user'
67
- assert_equal 'new_user', session.identifier
68
- assert_equal 'new_user', session.username
56
+ session.email = 'new_user@test.test'
57
+ assert_equal 'new_user@test.test', session.identifier
58
+ assert_equal 'new_user@test.test', session.email
69
59
 
70
60
  assert_equal nil, session.authenticated_object
71
61
  assert_equal nil, session.user
72
62
  end
73
63
 
74
64
  def test_session_authentication
75
- user = User.create!(:username => 'test', :password => 'test')
76
- session = LetMeIn::Session.create(:username => 'test', :password => 'test')
65
+ session = LetMeIn::Session.create(:email => User.first.email, :password => 'test')
77
66
  assert session.errors.blank?
78
- assert_equal user, session.authenticated_object
79
- assert_equal user, session.user
67
+ assert_equal User.first, session.authenticated_object
68
+ assert_equal User.first, session.user
80
69
  end
81
70
 
82
71
  def test_session_authentication_failure
83
- user = User.create!(:username => 'test', :password => 'test')
84
- session = LetMeIn::Session.create(:username => 'test', :password => 'bad_pass')
72
+ session = LetMeIn::Session.create(:email => User.first.email, :password => 'bad_pass')
85
73
  assert session.errors.present?
86
74
  assert_equal 'Failed to authenticate', session.errors[:base].first
87
75
  assert_equal nil, session.authenticated_object
@@ -89,8 +77,7 @@ class LetMeInTest < Test::Unit::TestCase
89
77
  end
90
78
 
91
79
  def test_session_authentication_exception
92
- user = User.create!(:username => 'test', :password => 'test')
93
- session = LetMeIn::Session.new(:username => 'test', :password => 'bad_pass')
80
+ session = LetMeIn::Session.new(:email => User.first.email, :password => 'bad_pass')
94
81
  begin
95
82
  session.save!
96
83
  rescue LetMeIn::Error => e
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Oleg Khabarov
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-03-26 00:00:00 -04:00
17
+ date: 2011-03-27 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -108,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
108
  requirements:
109
109
  - - ">="
110
110
  - !ruby/object:Gem::Version
111
- hash: 4289054067406348260
111
+ hash: 2972491954571850709
112
112
  segments:
113
113
  - 0
114
114
  version: "0"