authlogic 4.4.2 → 5.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +5 -5
  2. data/lib/authlogic.rb +4 -28
  3. data/lib/authlogic/acts_as_authentic/base.rb +3 -18
  4. data/lib/authlogic/acts_as_authentic/email.rb +3 -170
  5. data/lib/authlogic/acts_as_authentic/logged_in_status.rb +3 -1
  6. data/lib/authlogic/acts_as_authentic/login.rb +7 -174
  7. data/lib/authlogic/acts_as_authentic/magic_columns.rb +7 -4
  8. data/lib/authlogic/acts_as_authentic/password.rb +54 -253
  9. data/lib/authlogic/acts_as_authentic/perishable_token.rb +8 -5
  10. data/lib/authlogic/acts_as_authentic/persistence_token.rb +10 -4
  11. data/lib/authlogic/acts_as_authentic/queries/case_sensitivity.rb +53 -0
  12. data/lib/authlogic/acts_as_authentic/queries/find_with_case.rb +36 -20
  13. data/lib/authlogic/acts_as_authentic/session_maintenance.rb +8 -6
  14. data/lib/authlogic/acts_as_authentic/single_access_token.rb +10 -8
  15. data/lib/authlogic/config.rb +9 -1
  16. data/lib/authlogic/controller_adapters/abstract_adapter.rb +7 -4
  17. data/lib/authlogic/controller_adapters/rack_adapter.rb +2 -0
  18. data/lib/authlogic/controller_adapters/rails_adapter.rb +19 -19
  19. data/lib/authlogic/controller_adapters/sinatra_adapter.rb +6 -0
  20. data/lib/authlogic/cookie_credentials.rb +63 -0
  21. data/lib/authlogic/crypto_providers.rb +5 -20
  22. data/lib/authlogic/crypto_providers/bcrypt.rb +3 -3
  23. data/lib/authlogic/crypto_providers/md5.rb +3 -6
  24. data/lib/authlogic/crypto_providers/scrypt.rb +2 -0
  25. data/lib/authlogic/crypto_providers/sha1.rb +4 -6
  26. data/lib/authlogic/crypto_providers/sha256.rb +2 -0
  27. data/lib/authlogic/crypto_providers/sha512.rb +6 -5
  28. data/lib/authlogic/i18n.rb +3 -1
  29. data/lib/authlogic/i18n/translator.rb +3 -0
  30. data/lib/authlogic/random.rb +2 -0
  31. data/lib/authlogic/session/base.rb +2087 -39
  32. data/lib/authlogic/session/magic_column/assigns_last_request_at.rb +46 -0
  33. data/lib/authlogic/test_case.rb +4 -0
  34. data/lib/authlogic/test_case/mock_controller.rb +2 -0
  35. data/lib/authlogic/test_case/mock_cookie_jar.rb +7 -0
  36. data/lib/authlogic/test_case/mock_logger.rb +2 -0
  37. data/lib/authlogic/test_case/mock_request.rb +2 -0
  38. data/lib/authlogic/test_case/rails_request_adapter.rb +2 -0
  39. data/lib/authlogic/version.rb +2 -1
  40. metadata +136 -182
  41. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -28
  42. data/.github/ISSUE_TEMPLATE/feature_proposal.md +0 -32
  43. data/.github/triage.md +0 -86
  44. data/.gitignore +0 -15
  45. data/.rubocop.yml +0 -133
  46. data/.rubocop_todo.yml +0 -74
  47. data/.travis.yml +0 -24
  48. data/CHANGELOG.md +0 -326
  49. data/CONTRIBUTING.md +0 -91
  50. data/Gemfile +0 -6
  51. data/LICENSE +0 -20
  52. data/README.md +0 -439
  53. data/Rakefile +0 -21
  54. data/UPGRADING.md +0 -22
  55. data/authlogic.gemspec +0 -40
  56. data/doc/use_normal_rails_validation.md +0 -82
  57. data/gemfiles/Gemfile.rails-4.2.x +0 -6
  58. data/gemfiles/Gemfile.rails-5.1.x +0 -6
  59. data/gemfiles/Gemfile.rails-5.2.x +0 -6
  60. data/lib/authlogic/acts_as_authentic/restful_authentication.rb +0 -106
  61. data/lib/authlogic/acts_as_authentic/validations_scope.rb +0 -35
  62. data/lib/authlogic/authenticates_many/association.rb +0 -50
  63. data/lib/authlogic/authenticates_many/base.rb +0 -81
  64. data/lib/authlogic/crypto_providers/aes256.rb +0 -71
  65. data/lib/authlogic/crypto_providers/wordpress.rb +0 -72
  66. data/lib/authlogic/regex.rb +0 -79
  67. data/lib/authlogic/session/activation.rb +0 -73
  68. data/lib/authlogic/session/active_record_trickery.rb +0 -65
  69. data/lib/authlogic/session/brute_force_protection.rb +0 -127
  70. data/lib/authlogic/session/callbacks.rb +0 -153
  71. data/lib/authlogic/session/cookies.rb +0 -296
  72. data/lib/authlogic/session/existence.rb +0 -103
  73. data/lib/authlogic/session/foundation.rb +0 -105
  74. data/lib/authlogic/session/http_auth.rb +0 -107
  75. data/lib/authlogic/session/id.rb +0 -53
  76. data/lib/authlogic/session/klass.rb +0 -73
  77. data/lib/authlogic/session/magic_columns.rb +0 -119
  78. data/lib/authlogic/session/magic_states.rb +0 -82
  79. data/lib/authlogic/session/params.rb +0 -130
  80. data/lib/authlogic/session/password.rb +0 -318
  81. data/lib/authlogic/session/perishable_token.rb +0 -24
  82. data/lib/authlogic/session/persistence.rb +0 -77
  83. data/lib/authlogic/session/priority_record.rb +0 -38
  84. data/lib/authlogic/session/scopes.rb +0 -138
  85. data/lib/authlogic/session/session.rb +0 -77
  86. data/lib/authlogic/session/timeout.rb +0 -103
  87. data/lib/authlogic/session/unauthorized_record.rb +0 -56
  88. data/lib/authlogic/session/validation.rb +0 -93
  89. data/test/acts_as_authentic_test/base_test.rb +0 -27
  90. data/test/acts_as_authentic_test/email_test.rb +0 -241
  91. data/test/acts_as_authentic_test/logged_in_status_test.rb +0 -64
  92. data/test/acts_as_authentic_test/login_test.rb +0 -153
  93. data/test/acts_as_authentic_test/magic_columns_test.rb +0 -29
  94. data/test/acts_as_authentic_test/password_test.rb +0 -263
  95. data/test/acts_as_authentic_test/perishable_token_test.rb +0 -98
  96. data/test/acts_as_authentic_test/persistence_token_test.rb +0 -62
  97. data/test/acts_as_authentic_test/restful_authentication_test.rb +0 -48
  98. data/test/acts_as_authentic_test/session_maintenance_test.rb +0 -150
  99. data/test/acts_as_authentic_test/single_access_test.rb +0 -46
  100. data/test/adapter_test.rb +0 -23
  101. data/test/authenticates_many_test.rb +0 -33
  102. data/test/config_test.rb +0 -38
  103. data/test/crypto_provider_test/aes256_test.rb +0 -16
  104. data/test/crypto_provider_test/bcrypt_test.rb +0 -16
  105. data/test/crypto_provider_test/scrypt_test.rb +0 -16
  106. data/test/crypto_provider_test/sha1_test.rb +0 -25
  107. data/test/crypto_provider_test/sha256_test.rb +0 -16
  108. data/test/crypto_provider_test/sha512_test.rb +0 -16
  109. data/test/crypto_provider_test/wordpress_test.rb +0 -26
  110. data/test/fixtures/companies.yml +0 -5
  111. data/test/fixtures/employees.yml +0 -17
  112. data/test/fixtures/projects.yml +0 -3
  113. data/test/fixtures/users.yml +0 -41
  114. data/test/i18n/lol.yml +0 -4
  115. data/test/i18n_test.rb +0 -35
  116. data/test/libs/affiliate.rb +0 -9
  117. data/test/libs/company.rb +0 -8
  118. data/test/libs/employee.rb +0 -9
  119. data/test/libs/employee_session.rb +0 -4
  120. data/test/libs/ldaper.rb +0 -5
  121. data/test/libs/project.rb +0 -5
  122. data/test/libs/user.rb +0 -9
  123. data/test/libs/user_session.rb +0 -27
  124. data/test/random_test.rb +0 -15
  125. data/test/session_test/activation_test.rb +0 -45
  126. data/test/session_test/active_record_trickery_test.rb +0 -78
  127. data/test/session_test/brute_force_protection_test.rb +0 -110
  128. data/test/session_test/callbacks_test.rb +0 -42
  129. data/test/session_test/cookies_test.rb +0 -226
  130. data/test/session_test/credentials_test.rb +0 -0
  131. data/test/session_test/existence_test.rb +0 -88
  132. data/test/session_test/foundation_test.rb +0 -24
  133. data/test/session_test/http_auth_test.rb +0 -60
  134. data/test/session_test/id_test.rb +0 -19
  135. data/test/session_test/klass_test.rb +0 -42
  136. data/test/session_test/magic_columns_test.rb +0 -62
  137. data/test/session_test/magic_states_test.rb +0 -60
  138. data/test/session_test/params_test.rb +0 -61
  139. data/test/session_test/password_test.rb +0 -107
  140. data/test/session_test/perishability_test.rb +0 -17
  141. data/test/session_test/persistence_test.rb +0 -35
  142. data/test/session_test/scopes_test.rb +0 -68
  143. data/test/session_test/session_test.rb +0 -80
  144. data/test/session_test/timeout_test.rb +0 -84
  145. data/test/session_test/unauthorized_record_test.rb +0 -15
  146. data/test/session_test/validation_test.rb +0 -25
  147. data/test/test_helper.rb +0 -272
data/CONTRIBUTING.md DELETED
@@ -1,91 +0,0 @@
1
- # Contributing to Authlogic
2
-
3
- ## Issues
4
-
5
- ### Security Issues
6
-
7
- **Do not disclose security issues in public.** Instead, please email:
8
-
9
- ```
10
- Ben Johnson <bjohnson@binarylogic.com>,
11
- Tieg Zaharia <tieg.zaharia@gmail.com>
12
- Jared Beck <jared@jaredbeck.com>
13
- ```
14
-
15
- We will review security issues promptly.
16
-
17
- ### Non-Security Issues
18
-
19
- Please use github issues only for bug reports and feature suggestions.
20
-
21
- ### Usage Questions
22
-
23
- Please ask usage questions on
24
- [stackoverflow](http://stackoverflow.com/questions/tagged/authlogic).
25
-
26
- ## Development
27
-
28
- Most local development should be done using the oldest supported version of
29
- ruby. See `required_ruby_version` in the gemspec.
30
-
31
- ### Testing
32
-
33
- Tests can be run against different versions of Rails like so:
34
-
35
- ```
36
- BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.2.x bundle install
37
- BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.2.x bundle exec rake
38
- ```
39
-
40
- To run a single test:
41
-
42
- ```
43
- BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.2.x \
44
- bundle exec ruby -I test path/to/test.rb
45
- ```
46
-
47
- Bundler can be omitted, and the latest installed version of a gem dependency
48
- will be used. This is only suitable for certain unit tests.
49
-
50
- ```
51
- ruby –I test path/to/test.rb
52
- ```
53
-
54
- ### Linting
55
-
56
- Running `rake` also runs a linter, rubocop. Contributions must pass both
57
- the linter and the tests. The linter can be run on its own.
58
-
59
- ```
60
- BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.2.x bundle exec rubocop
61
- ```
62
-
63
- To run the tests without linting, use `rake test`.
64
-
65
- ```
66
- BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.2.x bundle exec rake test
67
- ```
68
-
69
- ### Version Control Branches
70
-
71
- We've been trying to follow the rails way, stable branches, but have been
72
- inconsistent. We should have one branche for each minor version, named like
73
- `4-3-stable`. Releases should be done on those branches, not in master. So,
74
- the "stable" branches should be the only branches with release tags.
75
-
76
- ### A normal release (no backport)
77
-
78
- 1. git checkout 4-3-stable # the latest "stable" branch (see above)
79
- 1. Update version number in lib/authlogic/version.rb
80
- 1. In the changelog,
81
- - Add release date to entry
82
- - Add a new "Unreleased" section at top
83
- 1. In the readme,
84
- - Update version number in the docs table at the top
85
- - For non-patch versions, update the compatibility table
86
- 1. Commit with message like "Release 4.3.0"
87
- 1. git tag -a -m "v4.3.0" "v4.3.0"
88
- 1. git push --tags origin 4-3-stable # or whatever branch (see above)
89
- 1. CI should pass
90
- 1. gem build authlogic.gemspec
91
- 1. gem push authlogic-4.3.0
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source "https://rubygems.org"
4
- gemspec
5
-
6
- gem "sqlite3", platforms: :ruby
data/LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2011 Ben Johnson of Binary Logic
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md DELETED
@@ -1,439 +0,0 @@
1
- # Authlogic
2
-
3
- A clean, simple, and unobtrusive ruby authentication solution.
4
-
5
- [![Gem Version][5]][6] [![Build Status][1]][2] [![Code Climate][7]][8] [![Dependency Status][3]][4]
6
-
7
- ## Sponsors
8
-
9
- [![Timber Logging](http://res.cloudinary.com/timber/image/upload/v1490556810/pricing/sponsorship.png)](https://timber.io?utm_source=github&utm_medium=authlogic)
10
-
11
- [Tail Authlogic users](https://timber.io/docs/app/console/tail-a-user) in your logs!
12
-
13
- ## Documentation
14
-
15
- | Version | Documentation |
16
- | ----------- | ------------- |
17
- | Unreleased | https://github.com/binarylogic/authlogic/blob/master/README.md |
18
- | 4.4.2 | https://github.com/binarylogic/authlogic/blob/v4.4.2/README.md |
19
- | 3.7.0 | https://github.com/binarylogic/authlogic/blob/v3.7.0/README.md |
20
- | 2.1.11 | https://github.com/binarylogic/authlogic/blob/v2.1.11/README.rdoc |
21
- | 1.4.3 | https://github.com/binarylogic/authlogic/blob/v1.4.3/README.rdoc |
22
-
23
- ## Table of Contents
24
-
25
- - [1. Introduction](#1-introduction)
26
- - [1.a. Compatibility](#1a-compatibility)
27
- - [1.b. Overview](#1b-overview)
28
- - [1.c. Reference Documentation](#1c-reference-documentation)
29
- - [2. Rails](#2-rails)
30
- - [2.a. The users table](#2a-the-users-table)
31
- - [2.b. Controller](#2b-controller)
32
- - [2.c. View](#2c-view)
33
- - [2.d. CSRF Protection](#2d-csrf-protection)
34
- - [3. Testing](#3-testing)
35
- - [4. Helpful links](#4-helpful-links)
36
- - [5. Add-ons](#5-add-ons)
37
- - [6. Internals](#6-internals)
38
-
39
- ## 1. Introduction
40
-
41
- ### 1.a. Compatibility
42
-
43
- | Version | branch | ruby | activerecord |
44
- | ------- | ------------ | -------- | ------------- |
45
- | 4.4 | 4-4-stable | >= 2.3.0 | >= 4.2, < 5.3 |
46
- | 4.3 | 4-3-stable | >= 2.3.0 | >= 4.2, < 5.3 |
47
- | 4.2 | 4-2-stable | >= 2.2.0 | >= 4.2, < 5.3 |
48
- | 3 | 3-stable | >= 1.9.3 | >= 3.2, < 5.2 |
49
- | 2 | rails2 | >= 1.9.3 | ~> 2.3.0 |
50
- | 1 | ? | ? | ? |
51
-
52
- Under SemVer, [changes to dependencies][10] do not require a major release.
53
-
54
- ### 1.b. Overview
55
-
56
- Authlogic introduces a new type of model. You can have as many as you want, and
57
- name them whatever you want, just like your other models. In this example, we
58
- want to authenticate with our `User` model, which is inferred from the name:
59
-
60
- ```ruby
61
- class UserSession < Authlogic::Session::Base
62
- # specify configuration here, such as:
63
- # logout_on_timeout true
64
- # ...many more options in the documentation
65
- end
66
- ```
67
-
68
- In a `UserSessionsController`, login the user by using it just like your other models:
69
-
70
- ```ruby
71
- UserSession.create(:login => "bjohnson", :password => "my password", :remember_me => true)
72
-
73
- session = UserSession.new(:login => "bjohnson", :password => "my password", :remember_me => true)
74
- session.save
75
-
76
- # requires the authlogic-oid "add on" gem
77
- UserSession.create(:openid_identifier => "identifier", :remember_me => true)
78
-
79
- # skip authentication and log the user in directly, the true means "remember me"
80
- UserSession.create(my_user_object, true)
81
- ```
82
-
83
- The above handles the entire authentication process for you by:
84
-
85
- 1. authenticating (i.e. **validating** the record)
86
- 2. sets up the proper session values and cookies to persist the session (i.e. **saving** the record).
87
-
88
- You can also log out (i.e. **destroying** the session):
89
-
90
- ``` ruby
91
- session.destroy
92
- ```
93
-
94
- After a session has been created, you can persist it (i.e. **finding** the
95
- record) across requests. Thus keeping the user logged in:
96
-
97
- ``` ruby
98
- session = UserSession.find
99
- ```
100
-
101
- To get all of the nice authentication functionality in your model just do this:
102
-
103
- ```ruby
104
- class User < ApplicationRecord
105
- acts_as_authentic do |c|
106
- c.my_config_option = my_value
107
- end # the configuration block is optional
108
- end
109
- ```
110
-
111
- This handles validations, etc. It is also "smart" in the sense that it if a
112
- login field is present it will use that to authenticate, if not it will look for
113
- an email field, etc. This is all configurable, but for 99% of cases that above
114
- is all you will need to do.
115
-
116
- You may specify how passwords are cryptographically hashed (or encrypted) by
117
- setting the Authlogic::CryptoProvider option:
118
-
119
- ``` ruby
120
- c.crypto_provider = Authlogic::CryptoProviders::BCrypt
121
- ```
122
-
123
- You may validate international email addresses by enabling the provided alternate regex:
124
-
125
- ``` ruby
126
- c.validates_format_of_email_field_options = {:with => Authlogic::Regex.email_nonascii}
127
- ```
128
-
129
- Also, sessions are automatically maintained. You can switch this on and off with
130
- configuration, but the following will automatically log a user in after a
131
- successful registration:
132
-
133
- ``` ruby
134
- User.create(params[:user])
135
- ```
136
-
137
- You can switch this on and off with the following configuration:
138
-
139
- ```ruby
140
- class User < ApplicationRecord
141
- acts_as_authentic do |c|
142
- c.log_in_after_create = false
143
- end # the configuration block is optional
144
- end
145
- ```
146
-
147
- Authlogic also updates the session when the user changes his/her password. You can also switch this on and off with the following configuration:
148
-
149
- ```ruby
150
- class User < ApplicationRecord
151
- acts_as_authentic do |c|
152
- c.log_in_after_password_change = false
153
- end # the configuration block is optional
154
- end
155
- ```
156
-
157
- Authlogic is very flexible, it has a strong public API and a plethora of hooks
158
- to allow you to modify behavior and extend it. Check out the helpful links below
159
- to dig deeper.
160
-
161
- ### 1.c. Reference Documentation
162
-
163
- This README is just an introduction, but we also have [reference
164
- documentation](http://www.rubydoc.info/github/binarylogic/authlogic).
165
-
166
- **To use the reference documentation, you must understand how Authlogic's
167
- code is organized.** There are 2 models, your Authlogic model and your
168
- ActiveRecord model:
169
-
170
- 1. **Authlogic::Session**, your session models that
171
- extend `Authlogic::Session::Base`.
172
- 2. **Authlogic::ActsAsAuthentic**, which adds in functionality to your
173
- ActiveRecord model when you call `acts_as_authentic`.
174
-
175
- Each of the above has various modules that are organized by topic: passwords,
176
- cookies, etc. For example, if you want to timeout users after a certain period
177
- of inactivity, you would look in `Authlogic::Session::Timeout`.
178
-
179
- ## 2. Rails
180
-
181
- Let's walk through a typical rails setup.
182
-
183
- ### 2.a. The users table
184
-
185
- If you want to enable all the features of Authlogic, a migration to create a
186
- `User` model might look like this:
187
-
188
- ``` ruby
189
- class CreateUser < ActiveRecord::Migration
190
- def change
191
- create_table :users do |t|
192
- # Authlogic::ActsAsAuthentic::Email
193
- t.string :email
194
-
195
- # Authlogic::ActsAsAuthentic::Password
196
- t.string :crypted_password
197
- t.string :password_salt
198
-
199
- # Authlogic::ActsAsAuthentic::PersistenceToken
200
- t.string :persistence_token
201
- t.index :persistence_token, unique: true
202
-
203
- # Authlogic::ActsAsAuthentic::SingleAccessToken
204
- t.string :single_access_token
205
- t.index :single_access_token, unique: true
206
-
207
- # Authlogic::ActsAsAuthentic::PerishableToken
208
- t.string :perishable_token
209
- t.index :perishable_token, unique: true
210
-
211
- # Authlogic::Session::MagicColumns
212
- t.integer :login_count, default: 0, null: false
213
- t.integer :failed_login_count, default: 0, null: false
214
- t.datetime :last_request_at
215
- t.datetime :current_login_at
216
- t.datetime :last_login_at
217
- t.string :current_login_ip
218
- t.string :last_login_ip
219
-
220
- # Authlogic::Session::MagicStates
221
- t.boolean :active, default: false
222
- t.boolean :approved, default: false
223
- t.boolean :confirmed, default: false
224
-
225
- t.timestamps
226
- end
227
- end
228
- end
229
- ```
230
-
231
- In the `User` model,
232
-
233
- ```ruby
234
- class User < ApplicationRecord
235
- acts_as_authentic
236
-
237
- # Validate email, login, and password as you see fit.
238
- #
239
- # Authlogic < 5 added these validation for you, making them a little awkward
240
- # to change. In 4.4.0, those automatic validations were deprecated. See
241
- # https://github.com/binarylogic/authlogic/blob/master/doc/use_normal_rails_validation.md
242
- validates :email,
243
- format: {
244
- with: ::Authlogic::Regex::EMAIL,
245
- message: "should look like an email address."
246
- },
247
- length: { maximum: 100 },
248
- uniqueness: {
249
- case_sensitive: false,
250
- if: :email_changed?
251
- }
252
-
253
- validates :login,
254
- format: {
255
- with: ::Authlogic::Regex::LOGIN,
256
- message: "should use only letters, numbers, spaces, and .-_@+ please."
257
- },
258
- length: { within: 3..100 },
259
- uniqueness: {
260
- case_sensitive: false,
261
- if: :login_changed?
262
- }
263
-
264
- validates :password,
265
- confirmation: { if: :require_password? },
266
- length: {
267
- minimum: 8,
268
- if: :require_password?
269
- }
270
- validates :password_confirmation,
271
- length: {
272
- minimum: 8,
273
- if: :require_password?
274
- }
275
- end
276
- ```
277
-
278
- ### 2.b. Controller
279
-
280
- Your sessions controller will look just like your other controllers.
281
-
282
- ```ruby
283
- class UserSessionsController < ApplicationController
284
- def new
285
- @user_session = UserSession.new
286
- end
287
-
288
- def create
289
- @user_session = UserSession.new(user_session_params)
290
- if @user_session.save
291
- redirect_to account_url
292
- else
293
- render :action => :new
294
- end
295
- end
296
-
297
- def destroy
298
- current_user_session.destroy
299
- redirect_to new_user_session_url
300
- end
301
-
302
- private
303
-
304
- def user_session_params
305
- params.require(:user_session).permit(:email, :password, :remember_me)
306
- end
307
- end
308
- ```
309
-
310
- As you can see, this fits nicely into the [conventional controller methods][9].
311
-
312
- #### 2.b.1. Helper Methods
313
-
314
- ```ruby
315
- class ApplicationController
316
- helper_method :current_user_session, :current_user
317
-
318
- private
319
- def current_user_session
320
- return @current_user_session if defined?(@current_user_session)
321
- @current_user_session = UserSession.find
322
- end
323
-
324
- def current_user
325
- return @current_user if defined?(@current_user)
326
- @current_user = current_user_session && current_user_session.user
327
- end
328
- end
329
- ```
330
-
331
- ### 2.c. View
332
-
333
- ```erb
334
- <%= form_for @user_session do |f| %>
335
- <% if @user_session.errors.any? %>
336
- <div id="error_explanation">
337
- <h2><%= pluralize(@user_session.errors.count, "error") %> prohibited:</h2>
338
- <ul>
339
- <% @user_session.errors.full_messages.each do |msg| %>
340
- <li><%= msg %></li>
341
- <% end %>
342
- </ul>
343
- </div>
344
- <% end %>
345
- <%= f.label :login %><br />
346
- <%= f.text_field :login %><br />
347
- <br />
348
- <%= f.label :password %><br />
349
- <%= f.password_field :password %><br />
350
- <br />
351
- <%= f.submit "Login" %>
352
- <% end %>
353
- ```
354
-
355
- ### 2.d. CSRF Protection
356
-
357
- Because Authlogic introduces its own methods for storing user sessions, the CSRF
358
- (Cross Site Request Forgery) protection that is built into Rails will not work
359
- out of the box.
360
-
361
- No generally applicable mitigation by the authlogic library is possible, because
362
- the instance variable you use to store a reference to the user session in `def
363
- current_user_session` will not be known to authlogic.
364
-
365
- You will need to override `ActionController::Base#handle_unverified_request` to
366
- do something appropriate to how your app handles user sessions, e.g.:
367
-
368
- ```ruby
369
- class ApplicationController < ActionController::Base
370
- ...
371
- protected
372
-
373
- def handle_unverified_request
374
- # raise an exception
375
- fail ActionController::InvalidAuthenticityToken
376
- # or destroy session, redirect
377
- if current_user_session
378
- current_user_session.destroy
379
- end
380
- redirect_to root_url
381
- end
382
- end
383
- ```
384
-
385
- ## 3. Testing
386
-
387
- See [Authlogic::TestCase](https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/test_case.rb)
388
-
389
- ## 4. Helpful links
390
-
391
- * <b>API Reference:</b> http://www.rubydoc.info/github/binarylogic/authlogic
392
- * <b>Repository:</b> https://github.com/binarylogic/authlogic/tree/master
393
- * <b>Railscasts Screencast:</b> http://railscasts.com/episodes/160-authlogic
394
- * <b>Example repository with tutorial in README:</b> https://github.com/binarylogic/authlogic_example/tree/master
395
- * <b>Tutorial</b>: Rails Authentication with Authlogic https://www.sitepoint.com/rails-authentication-with-authlogic
396
- * <b>Issues:</b> https://github.com/binarylogic/authlogic/issues
397
- * <b>Chrome is not logging out on browser close</b> https://productforums.google.com/forum/#!topic/chrome/9l-gKYIUg50/discussion
398
-
399
- ## 5. Add-ons
400
-
401
- * <b>Authlogic OpenID addon:</b> https://github.com/binarylogic/authlogic_openid
402
- * <b>Authlogic LDAP addon:</b> https://github.com/binarylogic/authlogic_ldap
403
- * <b>Authlogic Facebook Connect:</b> https://github.com/kalasjocke/authlogic-facebook-connect
404
- * <b>Authlogic Facebook Connect (New JS API):</b> https://github.com/studybyte/authlogic_facebook_connect
405
- * <b>Authlogic Facebook Shim</b> https://github.com/james2m/authlogic_facebook_shim
406
- * <b>Authlogic OAuth (Twitter):</b> https://github.com/jrallison/authlogic_oauth
407
- * <b>Authlogic Oauth and OpenID:</b> https://github.com/lancejpollard/authlogic-connect
408
- * <b>Authlogic PAM:</b> https://github.com/nbudin/authlogic_pam
409
- * <b>Authlogic x509:</b> https://github.com/auth-scc/authlogic_x509
410
-
411
- If you create one of your own, please let us know about it so we can add it to
412
- this list. Or just fork the project, add your link, and send us a pull request.
413
-
414
- ## 6. Internals
415
-
416
- Interested in how all of this all works? Think about an ActiveRecord model. A
417
- database connection must be established before you can use it. In the case of
418
- Authlogic, a controller connection must be established before you can use it. It
419
- uses that controller connection to modify cookies, the current session, login
420
- with HTTP basic, etc. It connects to the controller through a before filter that
421
- is automatically set in your controller which lets Authlogic know about the
422
- current controller object. Then Authlogic leverages that to do everything, it's
423
- a pretty simple design. Nothing crazy going on, Authlogic is just leveraging the
424
- tools your framework provides in the controller object.
425
-
426
- ## Intellectual Property
427
-
428
- Copyright (c) 2012 Ben Johnson of Binary Logic, released under the MIT license
429
-
430
- [1]: https://api.travis-ci.org/binarylogic/authlogic.svg?branch=master
431
- [2]: https://travis-ci.org/binarylogic/authlogic
432
- [3]: https://gemnasium.com/badges/github.com/binarylogic/authlogic.svg
433
- [4]: https://gemnasium.com/binarylogic/authlogic
434
- [5]: https://badge.fury.io/rb/authlogic.png
435
- [6]: http://badge.fury.io/rb/authlogic
436
- [7]: https://codeclimate.com/github/binarylogic/authlogic.png
437
- [8]: https://codeclimate.com/github/binarylogic/authlogic
438
- [9]: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default
439
- [10]: https://semver.org/spec/v2.0.0.html#what-should-i-do-if-i-update-my-own-dependencies-without-changing-the-public-api