entrance 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/entrance.gemspec +1 -1
  2. data/examples/rails-app/.gitignore +16 -0
  3. data/examples/rails-app/Gemfile +6 -0
  4. data/examples/rails-app/Gemfile.lock +96 -0
  5. data/examples/rails-app/README.rdoc +28 -0
  6. data/examples/rails-app/Rakefile +6 -0
  7. data/examples/rails-app/app/assets/images/.keep +0 -0
  8. data/examples/rails-app/app/assets/javascripts/application.js +16 -0
  9. data/examples/rails-app/app/assets/stylesheets/application.css +13 -0
  10. data/examples/rails-app/app/controllers/application_controller.rb +6 -0
  11. data/examples/rails-app/app/controllers/concerns/.keep +0 -0
  12. data/examples/rails-app/app/controllers/sessions_controller.rb +25 -0
  13. data/examples/rails-app/app/controllers/users_controller.rb +24 -0
  14. data/examples/rails-app/app/controllers/welcome_controller.rb +7 -0
  15. data/examples/rails-app/app/helpers/application_helper.rb +2 -0
  16. data/examples/rails-app/app/mailers/.keep +0 -0
  17. data/examples/rails-app/app/models/.keep +0 -0
  18. data/examples/rails-app/app/models/concerns/.keep +0 -0
  19. data/examples/rails-app/app/models/user.rb +3 -0
  20. data/examples/rails-app/app/views/layouts/application.html.erb +19 -0
  21. data/examples/rails-app/app/views/sessions/new.html.erb +29 -0
  22. data/examples/rails-app/app/views/users/new.html.erb +32 -0
  23. data/examples/rails-app/app/views/welcome/index.html.erb +3 -0
  24. data/examples/rails-app/bin/bundle +3 -0
  25. data/examples/rails-app/bin/rails +4 -0
  26. data/examples/rails-app/bin/rake +4 -0
  27. data/examples/rails-app/config/application.rb +24 -0
  28. data/examples/rails-app/config/boot.rb +4 -0
  29. data/examples/rails-app/config/database.yml +25 -0
  30. data/examples/rails-app/config/environment.rb +5 -0
  31. data/examples/rails-app/config/environments/development.rb +29 -0
  32. data/examples/rails-app/config/environments/production.rb +80 -0
  33. data/examples/rails-app/config/environments/test.rb +36 -0
  34. data/examples/rails-app/config/initializers/backtrace_silencers.rb +7 -0
  35. data/examples/rails-app/config/initializers/entrance.rb +9 -0
  36. data/examples/rails-app/config/initializers/filter_parameter_logging.rb +4 -0
  37. data/examples/rails-app/config/initializers/inflections.rb +16 -0
  38. data/examples/rails-app/config/initializers/mime_types.rb +5 -0
  39. data/examples/rails-app/config/initializers/secret_token.rb +12 -0
  40. data/examples/rails-app/config/initializers/session_store.rb +3 -0
  41. data/examples/rails-app/config/initializers/wrap_parameters.rb +14 -0
  42. data/examples/rails-app/config/locales/en.yml +23 -0
  43. data/examples/rails-app/config/routes.rb +12 -0
  44. data/examples/rails-app/config.ru +4 -0
  45. data/examples/rails-app/db/migrate/20150107032724_create_users.rb +21 -0
  46. data/examples/rails-app/db/schema.rb +28 -0
  47. data/examples/rails-app/db/seeds.rb +7 -0
  48. data/examples/rails-app/lib/assets/.keep +0 -0
  49. data/examples/rails-app/lib/tasks/.keep +0 -0
  50. data/examples/rails-app/log/.keep +0 -0
  51. data/examples/rails-app/public/404.html +58 -0
  52. data/examples/rails-app/public/422.html +58 -0
  53. data/examples/rails-app/public/500.html +57 -0
  54. data/examples/rails-app/public/favicon.ico +0 -0
  55. data/examples/rails-app/public/robots.txt +5 -0
  56. data/examples/rails-app/test/controllers/.keep +0 -0
  57. data/examples/rails-app/test/fixtures/.keep +0 -0
  58. data/examples/rails-app/test/fixtures/users.yml +11 -0
  59. data/examples/rails-app/test/helpers/.keep +0 -0
  60. data/examples/rails-app/test/integration/.keep +0 -0
  61. data/examples/rails-app/test/mailers/.keep +0 -0
  62. data/examples/rails-app/test/models/.keep +0 -0
  63. data/examples/rails-app/test/models/user_test.rb +7 -0
  64. data/examples/rails-app/test/test_helper.rb +15 -0
  65. data/examples/rails-app/vendor/assets/javascripts/.keep +0 -0
  66. data/examples/rails-app/vendor/assets/stylesheets/.keep +0 -0
  67. data/lib/entrance/ciphers.rb +7 -5
  68. data/lib/entrance/config.rb +21 -2
  69. data/lib/entrance/controller.rb +67 -27
  70. data/lib/entrance/model.rb +122 -75
  71. data/lib/entrance/version.rb +2 -2
  72. data/lib/entrance.rb +6 -0
  73. metadata +128 -46
  74. checksums.yaml +0 -7
@@ -1,107 +1,154 @@
1
1
  require 'active_support/concern'
2
2
 
3
- module Model
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- # verify that username/password attributes are present
8
- attrs = Entrance.config.model.constantize.columns.collect(&:name)
9
- %w(username_attr password_attr).each do |key|
10
- attr = Entrance.config.send(key)
11
- raise "Couldn't find '#{attr}' in #{Entrance.config.model} model." unless attrs.include?(attr)
12
- end
3
+ module Entrance
4
+ module Model
5
+ extend ActiveSupport::Concern
13
6
 
14
- validates :password, :presence => true, :length => 6..32, :if => :password_required?
15
- validates :password, :confirmation => true, :if => :password_required?
16
- validates :password_confirmation, :presence => true, :if => :password_required?
17
- end
7
+ included do
8
+
9
+ # if the target model class does not have a Model.where() method,
10
+ # then login_by_session wont work, nor the ClassMethods below.
11
+ # won't work so we cannot continue.
12
+ unless respond_to?(:where)
13
+ raise "#{Entrance.config.model} does not have a class .where() method. Cannot continue."
14
+ end
15
+
16
+ fields = if self.respond_to?(:columns) # ActiveRecord::Base
17
+ self.columns.collect(&:name)
18
+ elsif self.respond_to?(:keys) # MongoMapper::Document
19
+ self.keys.keys
20
+ else # just get setters in the class
21
+ self.instance_methods(false).select { |m| m[/\=$/] }.map { |s| s.sub('=', '') }
22
+ end.map { |el| el.to_sym }
23
+
24
+ %w(username_attr password_attr).each do |key|
25
+ field = Entrance.config.send(key)
26
+ unless fields.include?(field.to_sym)
27
+ raise "Couldn't find '#{field}' in #{Entrance.config.model} model."
28
+ end
29
+ end
30
+
31
+ %w(remember reset).each do |what|
32
+ if field = Entrance.config.send("#{what}_token_attr")
18
33
 
19
- module ClassMethods
34
+ unless fields.include?(field.to_sym)
35
+ raise "No #{Entrance.config.send("#{what}_token_attr")} field found. \
36
+ Set the config.#{what}_token_attr option to nil to disable the #{what} option."
37
+ end
20
38
 
21
- def authenticate(username, password)
22
- return if username.blank? or password.blank?
39
+ Entrance.config.can?(what, true)
40
+ include what.to_sym == :remember ? RememberMethods : ResetMethods
41
+ end
42
+ end
23
43
 
24
- query = {}
25
- query[Entrance.config.username_attr] = username.downcase.strip
26
- if u = where(query).first
27
- return u.authenticated?(password) ? u : nil
44
+ if respond_to?(:validates)
45
+ validates :password, :presence => true, :length => 6..32, :if => :password_required?
46
+ validates :password, :confirmation => true, :if => :password_required?
47
+ validates :password_confirmation, :presence => true, :if => :password_required?
28
48
  end
49
+
29
50
  end
30
51
 
31
- def with_password_reset_token(token)
32
- return if token.blank?
52
+ module ClassMethods
53
+
54
+ def authenticate(username, password)
55
+ return if username.blank? or password.blank?
33
56
 
34
- query = {}
35
- query[Entrance.config.reset_token_attr] = token.strip
36
- if u = where(query).first \
37
- and (!Doorman.config.reset_until_attr || u.send(Doorman.config.reset_until_attr) > Time.now)
38
- return u
57
+ query = {}
58
+ query[Entrance.config.username_attr] = username.to_s.downcase.strip
59
+ if u = where(query).first
60
+ return u.authenticated?(password) ? u : nil
61
+ end
39
62
  end
63
+
64
+ def with_password_reset_token(token)
65
+ Entrance.config.permit!(:reset)
66
+ return if token.blank?
67
+
68
+ query = {}
69
+ query[Entrance.config.reset_token_attr] = token.to_s.strip
70
+ if u = where(query).first \
71
+ and (!Doorman.config.reset_until_attr || u.send(Doorman.config.reset_until_attr) > Time.now)
72
+ return u
73
+ end
74
+ end
75
+
40
76
  end
41
77
 
42
- end
78
+ module ResetMethods
79
+
80
+ def request_password_reset!
81
+ send(Entrance.config.reset_token_attr + '=', Entrance.generate_token)
82
+ if Doorman.config.reset_until_attr
83
+ update_attribute(Entrance.config.reset_until_attr, Entrance.config.reset_password_window.from_now)
84
+ end
85
+ if save(:validate => false)
86
+ method = Entrance.config.reset_password_method
87
+ Entrance.config.reset_password_mailer.constantize.send(method, self).deliver
88
+ end
89
+ end
43
90
 
44
- def authenticated?(string)
45
- password === encrypt_password(string)
46
- end
91
+ end
47
92
 
48
- def remember_me!(until_date = nil)
49
- update_attribute(Entrance.config.remember_token_attr, Entrance.generate_token)
50
- update_remember_token_expiration!(until_date)
51
- end
93
+ module RememberMethods
52
94
 
53
- def update_remember_token_expiration!(until_date = nil)
54
- timestamp = until_date || Entrance.config.remember_for
55
- update_attribute(Entrance.config.remember_until_attr, timestamp.from_now)
56
- end
95
+ def remember_me!(until_date = nil)
96
+ update_attribute(Entrance.config.remember_token_attr, Entrance.generate_token)
97
+ update_remember_token_expiration!(until_date)
98
+ end
57
99
 
58
- def forget_me!
59
- update_attribute(Entrance.config.remember_token_attr, nil)
60
- update_attribute(Entrance.config.remember_until_attr, nil)
61
- end
100
+ def update_remember_token_expiration!(until_date = nil)
101
+ timestamp = until_date || Entrance.config.remember_for
102
+ update_attribute(Entrance.config.remember_until_attr, timestamp.from_now)
103
+ end
62
104
 
63
- def password
64
- @password || Entrance.config.cipher.read(send(Entrance.config.password_attr))
65
- end
105
+ def forget_me!
106
+ update_attribute(Entrance.config.remember_token_attr, nil)
107
+ update_attribute(Entrance.config.remember_until_attr, nil)
108
+ end
66
109
 
67
- def password=(new_password)
68
- return if new_password.blank?
110
+ end
69
111
 
70
- @password = new_password # for validation
71
- @password_changed = true
112
+ def authenticated?(string)
113
+ Entrance.config.cipher.match?(read_password, string, get_salt)
114
+ end
72
115
 
73
- # if we're using salt and it is empty, generate one
74
- if Entrance.config.salt_attr \
75
- and send(Entrance.config.salt_attr).blank?
76
- self.send(Entrance.config.salt_attr + '=', Entrance.generate_token)
116
+ def password
117
+ @password || read_password
77
118
  end
78
119
 
79
- self.send(Entrance.config.password_attr + '=', encrypt_password(new_password))
80
- end
120
+ def password=(new_password)
121
+ return if new_password.blank?
122
+
123
+ @password = new_password # for validation
124
+ @password_changed = true
125
+
126
+ # if we're using salt and it is empty, generate one
127
+ if Entrance.config.salt_attr \
128
+ and send(Entrance.config.salt_attr).blank?
129
+ self.send(Entrance.config.salt_attr + '=', Entrance.generate_token)
130
+ end
81
131
 
82
- def request_password_reset!
83
- send(Entrance.config.reset_token_attr + '=', Entrance.generate_token)
84
- if Doorman.config.reset_until_attr
85
- update_attribute(Entrance.config.reset_until_attr, Entrance.config.reset_password_window.from_now)
132
+ self.send(Entrance.config.password_attr + '=', encrypt_password(new_password))
86
133
  end
87
- if save(:validate => false)
88
- method = Entrance.config.reset_password_method
89
- Entrance.config.reset_password_mailer.constantize.send(method, self).deliver
134
+
135
+ private
136
+
137
+ def read_password
138
+ send(Entrance.config.password_attr)
90
139
  end
91
- end
92
140
 
93
- private
141
+ def encrypt_password(string)
142
+ Entrance.config.cipher.encrypt(string, get_salt)
143
+ end
94
144
 
95
- def get_salt
96
- Entrance.config.salt_attr && send(Entrance.config.salt_attr)
97
- end
145
+ def get_salt
146
+ Entrance.config.salt_attr && send(Entrance.config.salt_attr)
147
+ end
98
148
 
99
- def encrypt_password(string)
100
- Entrance.config.cipher.encrypt(string, get_salt)
101
- end
149
+ def password_required?
150
+ password.blank? || @password_changed
151
+ end
102
152
 
103
- def password_required?
104
- password.blank? or @password_changed
105
153
  end
106
-
107
154
  end
@@ -1,7 +1,7 @@
1
1
  module Entrance
2
2
  MAJOR = 0
3
- MINOR = 1
4
- PATCH = 1
3
+ MINOR = 2
4
+ PATCH = 0
5
5
 
6
6
  VERSION = [MAJOR, MINOR, PATCH].join('.')
7
7
  end
data/lib/entrance.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'entrance/controller'
2
2
  require 'entrance/model'
3
3
  require 'entrance/ciphers'
4
+ require 'entrance/config'
4
5
 
5
6
  require 'active_support/core_ext/numeric/time'
6
7
 
@@ -12,6 +13,11 @@ module Entrance
12
13
 
13
14
  def self.configure
14
15
  yield config
16
+ config.validate!
17
+ end
18
+
19
+ def self.model
20
+ @model ||= config.model.constantize
15
21
  end
16
22
 
17
23
  def self.generate_token(length = 40)
metadata CHANGED
@@ -1,82 +1,164 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: entrance
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
5
10
  platform: ruby
6
- authors:
7
- - Tomás Pollak
11
+ authors:
12
+ - "Tom\xC3\xA1s Pollak"
8
13
  autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
- date: 2014-09-24 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
16
+
17
+ date: 2015-01-07 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
14
21
  name: bcrypt
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '3.0'
20
- type: :runtime
21
22
  prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '3.0'
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '3.0'
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ version: "3.0"
34
31
  type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
35
  prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '3.0'
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 3
42
+ - 0
43
+ version: "3.0"
44
+ type: :runtime
45
+ version_requirements: *id002
41
46
  description: Doesn't fiddle with your controllers and routes.
42
- email:
47
+ email:
43
48
  - tomas@forkhq.com
44
49
  executables: []
50
+
45
51
  extensions: []
52
+
46
53
  extra_rdoc_files: []
47
- files:
48
- - ".gitignore"
54
+
55
+ files:
56
+ - .gitignore
49
57
  - README.md
50
58
  - Rakefile
51
59
  - entrance.gemspec
60
+ - examples/rails-app/.gitignore
61
+ - examples/rails-app/Gemfile
62
+ - examples/rails-app/Gemfile.lock
63
+ - examples/rails-app/README.rdoc
64
+ - examples/rails-app/Rakefile
65
+ - examples/rails-app/app/assets/images/.keep
66
+ - examples/rails-app/app/assets/javascripts/application.js
67
+ - examples/rails-app/app/assets/stylesheets/application.css
68
+ - examples/rails-app/app/controllers/application_controller.rb
69
+ - examples/rails-app/app/controllers/concerns/.keep
70
+ - examples/rails-app/app/controllers/sessions_controller.rb
71
+ - examples/rails-app/app/controllers/users_controller.rb
72
+ - examples/rails-app/app/controllers/welcome_controller.rb
73
+ - examples/rails-app/app/helpers/application_helper.rb
74
+ - examples/rails-app/app/mailers/.keep
75
+ - examples/rails-app/app/models/.keep
76
+ - examples/rails-app/app/models/concerns/.keep
77
+ - examples/rails-app/app/models/user.rb
78
+ - examples/rails-app/app/views/layouts/application.html.erb
79
+ - examples/rails-app/app/views/sessions/new.html.erb
80
+ - examples/rails-app/app/views/users/new.html.erb
81
+ - examples/rails-app/app/views/welcome/index.html.erb
82
+ - examples/rails-app/bin/bundle
83
+ - examples/rails-app/bin/rails
84
+ - examples/rails-app/bin/rake
85
+ - examples/rails-app/config.ru
86
+ - examples/rails-app/config/application.rb
87
+ - examples/rails-app/config/boot.rb
88
+ - examples/rails-app/config/database.yml
89
+ - examples/rails-app/config/environment.rb
90
+ - examples/rails-app/config/environments/development.rb
91
+ - examples/rails-app/config/environments/production.rb
92
+ - examples/rails-app/config/environments/test.rb
93
+ - examples/rails-app/config/initializers/backtrace_silencers.rb
94
+ - examples/rails-app/config/initializers/entrance.rb
95
+ - examples/rails-app/config/initializers/filter_parameter_logging.rb
96
+ - examples/rails-app/config/initializers/inflections.rb
97
+ - examples/rails-app/config/initializers/mime_types.rb
98
+ - examples/rails-app/config/initializers/secret_token.rb
99
+ - examples/rails-app/config/initializers/session_store.rb
100
+ - examples/rails-app/config/initializers/wrap_parameters.rb
101
+ - examples/rails-app/config/locales/en.yml
102
+ - examples/rails-app/config/routes.rb
103
+ - examples/rails-app/db/migrate/20150107032724_create_users.rb
104
+ - examples/rails-app/db/schema.rb
105
+ - examples/rails-app/db/seeds.rb
106
+ - examples/rails-app/lib/assets/.keep
107
+ - examples/rails-app/lib/tasks/.keep
108
+ - examples/rails-app/log/.keep
109
+ - examples/rails-app/public/404.html
110
+ - examples/rails-app/public/422.html
111
+ - examples/rails-app/public/500.html
112
+ - examples/rails-app/public/favicon.ico
113
+ - examples/rails-app/public/robots.txt
114
+ - examples/rails-app/test/controllers/.keep
115
+ - examples/rails-app/test/fixtures/.keep
116
+ - examples/rails-app/test/fixtures/users.yml
117
+ - examples/rails-app/test/helpers/.keep
118
+ - examples/rails-app/test/integration/.keep
119
+ - examples/rails-app/test/mailers/.keep
120
+ - examples/rails-app/test/models/.keep
121
+ - examples/rails-app/test/models/user_test.rb
122
+ - examples/rails-app/test/test_helper.rb
123
+ - examples/rails-app/vendor/assets/javascripts/.keep
124
+ - examples/rails-app/vendor/assets/stylesheets/.keep
52
125
  - lib/entrance.rb
53
126
  - lib/entrance/ciphers.rb
54
127
  - lib/entrance/config.rb
55
128
  - lib/entrance/controller.rb
56
129
  - lib/entrance/model.rb
57
130
  - lib/entrance/version.rb
131
+ has_rdoc: true
58
132
  homepage: https://github.com/tomas/entrance
59
133
  licenses: []
60
- metadata: {}
134
+
61
135
  post_install_message:
62
136
  rdoc_options: []
63
- require_paths:
137
+
138
+ require_paths:
64
139
  - lib
65
- required_ruby_version: !ruby/object:Gem::Requirement
66
- requirements:
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
67
142
  - - ">="
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- required_rubygems_version: !ruby/object:Gem::Requirement
71
- requirements:
143
+ - !ruby/object:Gem::Version
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
72
149
  - - ">="
73
- - !ruby/object:Gem::Version
150
+ - !ruby/object:Gem::Version
151
+ segments:
152
+ - 1
153
+ - 3
154
+ - 6
74
155
  version: 1.3.6
75
156
  requirements: []
157
+
76
158
  rubyforge_project: entrance
77
- rubygems_version: 2.2.0
159
+ rubygems_version: 1.3.6
78
160
  signing_key:
79
- specification_version: 4
161
+ specification_version: 3
80
162
  summary: Lean authentication alternative for Rails and Sinatra.
81
163
  test_files: []
82
- has_rdoc:
164
+
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: f661a4633af896da4a51983077f91da1bb209252
4
- data.tar.gz: cc99cf9bd7d25ebb0bceb68094e4ca58aaad7501
5
- SHA512:
6
- metadata.gz: 0e8c8342e325fe52064b63805519a3ba4d860a5e9f9f2939da4f78cc9cb2f4b48b2248c4397dcd7dc377780063c094c7acf3c6e118e8c7a5b68d0415424687a5
7
- data.tar.gz: a9b6bf64afe93cb6a757c1f06421fdd37948a9eddde63fc122ba8d8e219f89223e6a1a1d8c3b35528385890e1b3ecd703c68ad0ef36bd7962e82642dfdc1f52f