authlogic 3.4.6 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/ISSUE_TEMPLATE.md +13 -0
- data/.github/triage.md +87 -0
- data/.gitignore +4 -0
- data/.rubocop.yml +127 -0
- data/.rubocop_todo.yml +65 -0
- data/.travis.yml +18 -10
- data/CHANGELOG.md +156 -6
- data/CONTRIBUTING.md +71 -3
- data/Gemfile +2 -2
- data/README.md +386 -0
- data/Rakefile +13 -7
- data/UPGRADING.md +22 -0
- data/authlogic.gemspec +33 -22
- data/lib/authlogic.rb +60 -52
- data/lib/authlogic/acts_as_authentic/base.rb +40 -26
- data/lib/authlogic/acts_as_authentic/email.rb +96 -32
- data/lib/authlogic/acts_as_authentic/logged_in_status.rb +36 -12
- data/lib/authlogic/acts_as_authentic/login.rb +114 -49
- data/lib/authlogic/acts_as_authentic/magic_columns.rb +17 -6
- data/lib/authlogic/acts_as_authentic/password.rb +296 -139
- data/lib/authlogic/acts_as_authentic/perishable_token.rb +34 -20
- data/lib/authlogic/acts_as_authentic/persistence_token.rb +20 -24
- data/lib/authlogic/acts_as_authentic/queries/find_with_case.rb +67 -0
- data/lib/authlogic/acts_as_authentic/restful_authentication.rb +68 -23
- data/lib/authlogic/acts_as_authentic/session_maintenance.rb +128 -85
- data/lib/authlogic/acts_as_authentic/single_access_token.rb +41 -25
- data/lib/authlogic/acts_as_authentic/validations_scope.rb +8 -8
- data/lib/authlogic/authenticates_many/association.rb +22 -14
- data/lib/authlogic/authenticates_many/base.rb +35 -16
- data/lib/authlogic/config.rb +10 -10
- data/lib/authlogic/controller_adapters/abstract_adapter.rb +40 -12
- data/lib/authlogic/controller_adapters/rack_adapter.rb +15 -8
- data/lib/authlogic/controller_adapters/rails_adapter.rb +42 -22
- data/lib/authlogic/controller_adapters/sinatra_adapter.rb +3 -3
- data/lib/authlogic/crypto_providers.rb +91 -0
- data/lib/authlogic/crypto_providers/aes256.rb +42 -14
- data/lib/authlogic/crypto_providers/bcrypt.rb +35 -20
- data/lib/authlogic/crypto_providers/md5.rb +11 -9
- data/lib/authlogic/crypto_providers/scrypt.rb +26 -13
- data/lib/authlogic/crypto_providers/sha1.rb +14 -8
- data/lib/authlogic/crypto_providers/sha256.rb +16 -12
- data/lib/authlogic/crypto_providers/sha512.rb +8 -24
- data/lib/authlogic/crypto_providers/wordpress.rb +44 -15
- data/lib/authlogic/i18n.rb +33 -20
- data/lib/authlogic/i18n/translator.rb +1 -1
- data/lib/authlogic/random.rb +12 -29
- data/lib/authlogic/regex.rb +59 -27
- data/lib/authlogic/session/activation.rb +36 -23
- data/lib/authlogic/session/active_record_trickery.rb +13 -10
- data/lib/authlogic/session/base.rb +20 -8
- data/lib/authlogic/session/brute_force_protection.rb +87 -56
- data/lib/authlogic/session/callbacks.rb +99 -49
- data/lib/authlogic/session/cookies.rb +128 -59
- data/lib/authlogic/session/existence.rb +29 -19
- data/lib/authlogic/session/foundation.rb +70 -16
- data/lib/authlogic/session/http_auth.rb +39 -31
- data/lib/authlogic/session/id.rb +27 -15
- data/lib/authlogic/session/klass.rb +17 -13
- data/lib/authlogic/session/magic_columns.rb +78 -59
- data/lib/authlogic/session/magic_states.rb +50 -27
- data/lib/authlogic/session/params.rb +79 -50
- data/lib/authlogic/session/password.rb +197 -118
- data/lib/authlogic/session/perishable_token.rb +12 -6
- data/lib/authlogic/session/persistence.rb +20 -14
- data/lib/authlogic/session/priority_record.rb +20 -16
- data/lib/authlogic/session/scopes.rb +63 -33
- data/lib/authlogic/session/session.rb +40 -25
- data/lib/authlogic/session/timeout.rb +51 -34
- data/lib/authlogic/session/unauthorized_record.rb +24 -18
- data/lib/authlogic/session/validation.rb +32 -21
- data/lib/authlogic/test_case.rb +123 -35
- data/lib/authlogic/test_case/mock_controller.rb +14 -13
- data/lib/authlogic/test_case/mock_cookie_jar.rb +14 -5
- data/lib/authlogic/test_case/mock_logger.rb +1 -1
- data/lib/authlogic/test_case/mock_request.rb +9 -4
- data/lib/authlogic/test_case/rails_request_adapter.rb +8 -7
- data/lib/authlogic/version.rb +21 -0
- data/test/acts_as_authentic_test/base_test.rb +1 -1
- data/test/acts_as_authentic_test/email_test.rb +80 -63
- data/test/acts_as_authentic_test/logged_in_status_test.rb +14 -8
- data/test/acts_as_authentic_test/login_test.rb +91 -49
- data/test/acts_as_authentic_test/magic_columns_test.rb +13 -13
- data/test/acts_as_authentic_test/password_test.rb +82 -60
- data/test/acts_as_authentic_test/perishable_token_test.rb +31 -25
- data/test/acts_as_authentic_test/persistence_token_test.rb +9 -5
- data/test/acts_as_authentic_test/restful_authentication_test.rb +18 -9
- data/test/acts_as_authentic_test/session_maintenance_test.rb +86 -22
- data/test/acts_as_authentic_test/single_access_test.rb +15 -15
- data/test/adapter_test.rb +21 -0
- data/test/authenticates_many_test.rb +26 -11
- data/test/config_test.rb +9 -9
- data/test/crypto_provider_test/aes256_test.rb +3 -3
- data/test/crypto_provider_test/bcrypt_test.rb +1 -1
- data/test/crypto_provider_test/scrypt_test.rb +2 -2
- data/test/crypto_provider_test/sha1_test.rb +4 -4
- data/test/crypto_provider_test/sha256_test.rb +2 -2
- data/test/crypto_provider_test/sha512_test.rb +3 -3
- data/test/crypto_provider_test/wordpress_test.rb +24 -0
- data/test/gemfiles/Gemfile.rails-4.2.x +2 -2
- data/test/gemfiles/Gemfile.rails-5.0.x +6 -0
- data/test/gemfiles/Gemfile.rails-5.1.x +6 -0
- data/test/gemfiles/Gemfile.rails-5.2.x +6 -0
- data/test/gemfiles/Gemfile.rails-master +6 -0
- data/test/i18n_test.rb +9 -9
- data/test/libs/affiliate.rb +2 -2
- data/test/libs/company.rb +4 -4
- data/test/libs/employee.rb +2 -2
- data/test/libs/employee_session.rb +1 -1
- data/test/libs/ldaper.rb +1 -1
- data/test/libs/project.rb +1 -1
- data/test/libs/user_session.rb +2 -2
- data/test/random_test.rb +9 -38
- data/test/session_test/activation_test.rb +7 -7
- data/test/session_test/active_record_trickery_test.rb +9 -6
- data/test/session_test/brute_force_protection_test.rb +26 -21
- data/test/session_test/callbacks_test.rb +10 -4
- data/test/session_test/cookies_test.rb +54 -20
- data/test/session_test/existence_test.rb +45 -23
- data/test/session_test/foundation_test.rb +17 -1
- data/test/session_test/http_auth_test.rb +11 -12
- data/test/session_test/id_test.rb +3 -3
- data/test/session_test/klass_test.rb +2 -2
- data/test/session_test/magic_columns_test.rb +15 -17
- data/test/session_test/magic_states_test.rb +17 -19
- data/test/session_test/params_test.rb +26 -20
- data/test/session_test/password_test.rb +11 -12
- data/test/session_test/perishability_test.rb +5 -5
- data/test/session_test/persistence_test.rb +4 -3
- data/test/session_test/scopes_test.rb +15 -9
- data/test/session_test/session_test.rb +7 -6
- data/test/session_test/timeout_test.rb +16 -14
- data/test/session_test/unauthorized_record_test.rb +3 -3
- data/test/session_test/validation_test.rb +5 -5
- data/test/test_helper.rb +115 -49
- metadata +107 -36
- data/README.rdoc +0 -232
- data/test/gemfiles/Gemfile.rails-3.2.x +0 -7
- data/test/gemfiles/Gemfile.rails-4.0.x +0 -7
- data/test/gemfiles/Gemfile.rails-4.1.x +0 -7
data/README.rdoc
DELETED
@@ -1,232 +0,0 @@
|
|
1
|
-
= Authlogic
|
2
|
-
|
3
|
-
** Authlogic supports both rails 3 and 4. For rails 2, see the rail2 branch
|
4
|
-
|
5
|
-
{<img src="https://travis-ci.org/binarylogic/authlogic.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/binarylogic/authlogic]
|
6
|
-
|
7
|
-
{<img src="https://codeclimate.com/github/binarylogic/authlogic.png" />}[https://codeclimate.com/github/binarylogic/authlogic]
|
8
|
-
|
9
|
-
Authlogic is a clean, simple, and unobtrusive ruby authentication solution.
|
10
|
-
|
11
|
-
A code example can replace a thousand words...
|
12
|
-
|
13
|
-
Authlogic introduces a new type of model. You can have as many as you want, and name them whatever you want, just like your other models. In this example, we want to authenticate with the User model, which is inferred by the name:
|
14
|
-
|
15
|
-
class UserSession < Authlogic::Session::Base
|
16
|
-
# specify configuration here, such as:
|
17
|
-
# logout_on_timeout true
|
18
|
-
# ...many more options in the documentation
|
19
|
-
end
|
20
|
-
|
21
|
-
Log in with any of the following. Create a UserSessionsController and use it just like your other models:
|
22
|
-
|
23
|
-
UserSession.create(:login => "bjohnson", :password => "my password", :remember_me => true)
|
24
|
-
session = UserSession.new(:login => "bjohnson", :password => "my password", :remember_me => true); session.save
|
25
|
-
UserSession.create(:openid_identifier => "identifier", :remember_me => true) # requires the authlogic-oid "add on" gem
|
26
|
-
UserSession.create(my_user_object, true) # skip authentication and log the user in directly, the true means "remember me"
|
27
|
-
|
28
|
-
The above handles the entire authentication process for you. It first authenticates, then it sets up the proper session values and cookies to persist the session. Just like you would if you rolled your own authentication solution.
|
29
|
-
|
30
|
-
You can also log out / destroy the session:
|
31
|
-
|
32
|
-
session.destroy
|
33
|
-
|
34
|
-
After a session has been created, you can persist it across requests. Thus keeping the user logged in:
|
35
|
-
|
36
|
-
session = UserSession.find
|
37
|
-
|
38
|
-
To get all of the nice authentication functionality in your model just do this:
|
39
|
-
|
40
|
-
class User < ActiveRecord::Base
|
41
|
-
acts_as_authentic do |c|
|
42
|
-
c.my_config_option = my_value
|
43
|
-
end # the configuration block is optional
|
44
|
-
end
|
45
|
-
|
46
|
-
This handles validations, etc. It is also "smart" in the sense that it if a login field is present it will use that to authenticate, if not it will look for an email field, etc. This is all configurable, but for 99% of cases that above is all you will need to do.
|
47
|
-
|
48
|
-
You may specify how passwords are cryptographically hashed (or encrypted) by setting the Authlogic::CryptoProvider option:
|
49
|
-
|
50
|
-
c.crypto_provider = Authlogic::CryptoProviders::BCrypt
|
51
|
-
|
52
|
-
You may validate international email addresses by enabling the provided alternate regex:
|
53
|
-
|
54
|
-
c.validates_format_of_email_field_options = {:with => Authlogic::Regex.email_nonascii}
|
55
|
-
|
56
|
-
Also, sessions are automatically maintained. You can switch this on and off with configuration, but the following will automatically log a user in after a successful registration:
|
57
|
-
|
58
|
-
User.create(params[:user])
|
59
|
-
|
60
|
-
This also updates the session when the user changes his/her password.
|
61
|
-
|
62
|
-
Authlogic is very flexible, it has a strong public API and a plethora of hooks to allow you to modify behavior and extend it. Check out the helpful links below to dig deeper.
|
63
|
-
|
64
|
-
== Upgrading to Authlogic 3.4.0
|
65
|
-
|
66
|
-
In version 3.4.0, the default crypto_provider was changed from *Sha512* to *SCrypt*.
|
67
|
-
|
68
|
-
If you never set a crypto_provider and are upgrading, your passwords will break unless you set the original:
|
69
|
-
|
70
|
-
c.crypto_provider = Authlogic::CryptoProviders::Sha512
|
71
|
-
|
72
|
-
And if you want to automatically upgrade from *Sha512* to *SCrypt* as users login:
|
73
|
-
|
74
|
-
c.transition_from_crypto_providers = [Authlogic::CryptoProviders::Sha512]
|
75
|
-
c.crypto_provider = Authlogic::CryptoProviders::SCrypt
|
76
|
-
|
77
|
-
== Helpful links
|
78
|
-
|
79
|
-
* <b>Documentation:</b> http://rdoc.info/projects/binarylogic/authlogic
|
80
|
-
* <b>Repository:</b> http://github.com/binarylogic/authlogic/tree/master
|
81
|
-
* <b>Railscasts Screencast:</b> http://railscasts.com/episodes/160-authlogic
|
82
|
-
* <b>Example repository with tutorial in README:</b> http://github.com/binarylogic/authlogic_example/tree/master
|
83
|
-
* <b>Tutorial: Reset passwords with Authlogic the RESTful way:</b> http://www.binarylogic.com/2008/11/16/tutorial-reset-passwords-with-authlogic
|
84
|
-
* <b>Issues:</b> http://github.com/binarylogic/authlogic/issues
|
85
|
-
|
86
|
-
== Authlogic "add ons"
|
87
|
-
|
88
|
-
* <b>Authlogic OpenID addon:</b> http://github.com/binarylogic/authlogic_openid
|
89
|
-
* <b>Authlogic LDAP addon:</b> http://github.com/binarylogic/authlogic_ldap
|
90
|
-
* <b>Authlogic Facebook Connect:</b> http://github.com/kalasjocke/authlogic_facebook_connect
|
91
|
-
* <b>Authlogic Facebook Connect (New JS API):</b> http://github.com/studybyte/authlogic_facebook_connect
|
92
|
-
* <b>Authlogic Facebook Shim</b> http://github.com/james2m/authlogic_facebook_shim
|
93
|
-
* <b>Authlogic OAuth (Twitter):</b> http://github.com/jrallison/authlogic_oauth
|
94
|
-
* <b>Authlogic Oauth and OpenID:</b> http://github.com/viatropos/authlogic-connect
|
95
|
-
* <b>Authlogic PAM:</b> http://github.com/nbudin/authlogic_pam
|
96
|
-
* <b>Authlogic x509:</b> http://github.com/auth-scc/authlogic_x509
|
97
|
-
|
98
|
-
If you create one of your own, please let me know about it so I can add it to this list. Or just fork the project, add your link, and send me a pull request.
|
99
|
-
|
100
|
-
== Documentation explanation
|
101
|
-
|
102
|
-
You can find anything you want about Authlogic in the {documentation}[http://rdoc.info/projects/binarylogic/authlogic], all that you need to do is understand the basic design behind it.
|
103
|
-
|
104
|
-
That being said, there are 2 models involved during authentication. Your Authlogic model and your ActiveRecord model:
|
105
|
-
|
106
|
-
1. <b>Authlogic::Session</b>, your session models that extend Authlogic::Session::Base.
|
107
|
-
2. <b>Authlogic::ActsAsAuthentic</b>, which adds in functionality to your ActiveRecord model when you call acts_as_authentic.
|
108
|
-
|
109
|
-
Each of the above has its various sub modules that contain common logic. The sub modules are responsible for including *everything* related to it: configuration, class methods, instance methods, etc.
|
110
|
-
|
111
|
-
For example, if you want to timeout users after a certain period of inactivity, you would look in <b>Authlogic::Session::Timeout</b>. To help you out, I listed the following publicly relevant modules with short descriptions. For the sake of brevity, there are more modules than listed here, the ones not listed are more for internal use, but you can easily read up on them in the {documentation}[http://rdoc.info/projects/binarylogic/authlogic].
|
112
|
-
|
113
|
-
== Example migration
|
114
|
-
|
115
|
-
If you want to enable all the features of Authlogic, a migration to create a
|
116
|
-
+User+ model, for example, might look like this:
|
117
|
-
|
118
|
-
class CreateUser < ActiveRecord::Migration
|
119
|
-
def change
|
120
|
-
create_table :users do |t|
|
121
|
-
# Authlogic::ActsAsAuthentic::Email
|
122
|
-
t.string :email
|
123
|
-
|
124
|
-
# Authlogic::ActsAsAuthentic::Password
|
125
|
-
t.string :crypted_password
|
126
|
-
t.string :password_salt
|
127
|
-
|
128
|
-
# Authlogic::ActsAsAuthentic::PersistenceToken
|
129
|
-
t.string :persistence_token
|
130
|
-
|
131
|
-
# Authlogic::ActsAsAuthentic::SingleAccessToken
|
132
|
-
t.string :single_access_token
|
133
|
-
|
134
|
-
# Authlogic::ActsAsAuthentic::PerishableToken
|
135
|
-
t.string :perishable_token
|
136
|
-
|
137
|
-
# Authlogic::Session::MagicColumns
|
138
|
-
t.integer :login_count, default: 0, null: false
|
139
|
-
t.integer :failed_login_count, default: 0, null: false
|
140
|
-
t.datetime :last_request_at
|
141
|
-
t.datetime :current_login_at
|
142
|
-
t.datetime :last_login_at
|
143
|
-
t.string :current_login_ip
|
144
|
-
t.string :last_login_ip
|
145
|
-
|
146
|
-
# Authlogic::Session::MagicStates
|
147
|
-
t.boolean :active, default: false
|
148
|
-
t.boolean :approved, default: false
|
149
|
-
t.boolean :confirmed, default: false
|
150
|
-
|
151
|
-
t.timestamps
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
== Quick Rails example
|
157
|
-
|
158
|
-
What if creating sessions worked like an ORM library on the surface...
|
159
|
-
|
160
|
-
UserSession.create(params[:user_session])
|
161
|
-
|
162
|
-
What if your user sessions controller could look just like your other controllers...
|
163
|
-
|
164
|
-
class UserSessionsController < ApplicationController
|
165
|
-
def new
|
166
|
-
@user_session = UserSession.new
|
167
|
-
end
|
168
|
-
|
169
|
-
def create
|
170
|
-
@user_session = UserSession.new(params[:user_session])
|
171
|
-
if @user_session.save
|
172
|
-
redirect_to account_url
|
173
|
-
else
|
174
|
-
render :action => :new
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
def destroy
|
179
|
-
current_user_session.destroy
|
180
|
-
redirect_to new_user_session_url
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
As you can see, this fits nicely into the RESTful development pattern. What about the view...
|
185
|
-
|
186
|
-
<%= form_for @user_session do |f| %>
|
187
|
-
<% if @user_session.errors.any? %>
|
188
|
-
<div id="error_explanation">
|
189
|
-
<h2><%= pluralize(@user_session.errors.count, "error") %> prohibited:</h2>
|
190
|
-
<ul>
|
191
|
-
<% @user_session.errors.full_messages.each do |msg| %>
|
192
|
-
<li><%= msg %></li>
|
193
|
-
<% end %>
|
194
|
-
</ul>
|
195
|
-
</div>
|
196
|
-
<% end %>
|
197
|
-
<%= f.label :login %><br />
|
198
|
-
<%= f.text_field :login %><br />
|
199
|
-
<br />
|
200
|
-
<%= f.label :password %><br />
|
201
|
-
<%= f.password_field :password %><br />
|
202
|
-
<br />
|
203
|
-
<%= f.submit "Login" %>
|
204
|
-
<% end %>
|
205
|
-
|
206
|
-
Or how about persisting the session...
|
207
|
-
|
208
|
-
class ApplicationController
|
209
|
-
helper_method :current_user_session, :current_user
|
210
|
-
|
211
|
-
private
|
212
|
-
def current_user_session
|
213
|
-
return @current_user_session if defined?(@current_user_session)
|
214
|
-
@current_user_session = UserSession.find
|
215
|
-
end
|
216
|
-
|
217
|
-
def current_user
|
218
|
-
return @current_user if defined?(@current_user)
|
219
|
-
@current_user = current_user_session && current_user_session.user
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
== Testing
|
224
|
-
|
225
|
-
See Authlogic::TestCase
|
226
|
-
|
227
|
-
== Tell me quickly how Authlogic works
|
228
|
-
|
229
|
-
Interested in how all of this all works? Think about an ActiveRecord model. A database connection must be established before you can use it. In the case of Authlogic, a controller connection must be established before you can use it. It uses that controller connection to modify cookies, the current session, login with HTTP basic, etc. It connects to the controller through a before filter that is automatically set in your controller which lets Authlogic know about the current controller object. Then Authlogic leverages that to do everything, it's a pretty simple design. Nothing crazy going on, Authlogic is just leveraging the tools your framework provides in the controller object.
|
230
|
-
|
231
|
-
|
232
|
-
Copyright (c) 2012 {Ben Johnson of Binary Logic}[http://www.binarylogic.com], released under the MIT license
|